Fossil SCM

Update the built-in SQLite to the latest 3.24.0 alpha.

drh 2018-05-05 11:30 trunk
Commit 58a6410206b27216e44ab7befbf9645aa49c5825400e3207cdff4a587a1ddc21
3 files changed +64 -43 +639 -463 +53 -2
+64 -43
--- src/shell.c
+++ src/shell.c
@@ -7617,13 +7617,13 @@
76177617
idxHashClear(&hIdx);
76187618
rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
76197619
"EXPLAIN QUERY PLAN %s", pStmt->zSql
76207620
);
76217621
while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7622
- int iSelectid = sqlite3_column_int(pExplain, 0);
7623
- int iOrder = sqlite3_column_int(pExplain, 1);
7624
- int iFrom = sqlite3_column_int(pExplain, 2);
7622
+ /* int iId = sqlite3_column_int(pExplain, 0); */
7623
+ /* int iParent = sqlite3_column_int(pExplain, 1); */
7624
+ /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
76257625
const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
76267626
int nDetail = STRLEN(zDetail);
76277627
int i;
76287628
76297629
for(i=0; i<nDetail; i++){
@@ -7646,13 +7646,11 @@
76467646
}
76477647
break;
76487648
}
76497649
}
76507650
7651
- pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%d|%d|%d|%s\n",
7652
- iSelectid, iOrder, iFrom, zDetail
7653
- );
7651
+ pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
76547652
}
76557653
76567654
for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
76577655
pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
76587656
}
@@ -8481,11 +8479,12 @@
84818479
};
84828480
84838481
/* A single line in the EQP output */
84848482
typedef struct EQPGraphRow EQPGraphRow;
84858483
struct EQPGraphRow {
8486
- int iSelectId; /* The SelectID for this row */
8484
+ int iEqpId; /* ID for this row */
8485
+ int iParentId; /* ID of the parent row */
84878486
EQPGraphRow *pNext; /* Next row in sequence */
84888487
char zText[1]; /* Text to display for this row */
84898488
};
84908489
84918490
/* All EQP output is collected into an instance of the following */
@@ -8503,10 +8502,11 @@
85038502
typedef struct ShellState ShellState;
85048503
struct ShellState {
85058504
sqlite3 *db; /* The database */
85068505
u8 autoExplain; /* Automatically turn on .explain mode */
85078506
u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8507
+ u8 autoEQPtest; /* autoEQP is in test mode */
85088508
u8 statsOn; /* True to display memory stats before each finalize */
85098509
u8 scanstatsOn; /* True to display scan stats before each finalize */
85108510
u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
85118511
u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
85128512
u8 nEqpLevel; /* Depth of the EQP output graph */
@@ -8553,14 +8553,14 @@
85538553
};
85548554
85558555
85568556
/* Allowed values for ShellState.autoEQP
85578557
*/
8558
-#define AUTOEQP_off 0
8559
-#define AUTOEQP_on 1
8560
-#define AUTOEQP_trigger 2
8561
-#define AUTOEQP_full 3
8558
+#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
8559
+#define AUTOEQP_on 1 /* Automatic EQP is on */
8560
+#define AUTOEQP_trigger 2 /* On and also show plans for triggers */
8561
+#define AUTOEQP_full 3 /* Show full EXPLAIN */
85628562
85638563
/* Allowed values for ShellState.openMode
85648564
*/
85658565
#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
85668566
#define SHELL_OPEN_NORMAL 1 /* Normal database file */
@@ -9167,16 +9167,20 @@
91679167
}
91689168
91699169
/*
91709170
** Add a new entry to the EXPLAIN QUERY PLAN data
91719171
*/
9172
-static void eqp_append(ShellState *p, int iSelectId, const char *zText){
9172
+static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
91739173
EQPGraphRow *pNew;
91749174
int nText = strlen30(zText);
9175
+ if( p->autoEQPtest ){
9176
+ utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9177
+ }
91759178
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
91769179
if( pNew==0 ) shell_out_of_memory();
9177
- pNew->iSelectId = iSelectId;
9180
+ pNew->iEqpId = iEqpId;
9181
+ pNew->iParentId = p2;
91789182
memcpy(pNew->zText, zText, nText+1);
91799183
pNew->pNext = 0;
91809184
if( p->sGraph.pLast ){
91819185
p->sGraph.pLast->pNext = pNew;
91829186
}else{
@@ -9196,50 +9200,33 @@
91969200
sqlite3_free(pRow);
91979201
}
91989202
memset(&p->sGraph, 0, sizeof(p->sGraph));
91999203
}
92009204
9201
-/* Return the next EXPLAIN QUERY PLAN line with iSelectId that occurs after
9205
+/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
92029206
** pOld, or return the first such line if pOld is NULL
92039207
*/
9204
-static EQPGraphRow *eqp_next_row(ShellState *p, int iSelectId, EQPGraphRow *pOld){
9208
+static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
92059209
EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9206
- while( pRow && pRow->iSelectId!=iSelectId ) pRow = pRow->pNext;
9210
+ while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
92079211
return pRow;
92089212
}
92099213
9210
-/* Render a single level of the graph shell having iSelectId. Called
9214
+/* Render a single level of the graph that has iEqpId as its parent. Called
92119215
** recursively to render sublevels.
92129216
*/
9213
-static void eqp_render_level(ShellState *p, int iSelectId){
9217
+static void eqp_render_level(ShellState *p, int iEqpId){
92149218
EQPGraphRow *pRow, *pNext;
9215
- int i;
92169219
int n = strlen30(p->sGraph.zPrefix);
92179220
char *z;
9218
- for(pRow = eqp_next_row(p, iSelectId, 0); pRow; pRow = pNext){
9219
- pNext = eqp_next_row(p, iSelectId, pRow);
9221
+ for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9222
+ pNext = eqp_next_row(p, iEqpId, pRow);
92209223
z = pRow->zText;
92219224
utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9222
- if( n<sizeof(p->sGraph.zPrefix)-7 && (z = strstr(z, " SUBQUER"))!=0 ){
9225
+ if( n<sizeof(p->sGraph.zPrefix)-7 ){
92239226
memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
9224
- if( strncmp(z, " SUBQUERY ", 9)==0 && (i = atoi(z+10))>iSelectId ){
9225
- eqp_render_level(p, i);
9226
- }else if( strncmp(z, " SUBQUERIES ", 12)==0 ){
9227
- i = atoi(z+12);
9228
- if( i>iSelectId ){
9229
- utf8_printf(p->out, "%s|--SUBQUERY %d\n", p->sGraph.zPrefix, i);
9230
- memcpy(&p->sGraph.zPrefix[n+3],"| ",4);
9231
- eqp_render_level(p, i);
9232
- }
9233
- z = strstr(z, " AND ");
9234
- if( z && (i = atoi(z+5))>iSelectId ){
9235
- p->sGraph.zPrefix[n+3] = 0;
9236
- utf8_printf(p->out, "%s`--SUBQUERY %d\n", p->sGraph.zPrefix, i);
9237
- memcpy(&p->sGraph.zPrefix[n+3]," ",4);
9238
- eqp_render_level(p, i);
9239
- }
9240
- }
9227
+ eqp_render_level(p, pRow->iEqpId);
92419228
p->sGraph.zPrefix[n] = 0;
92429229
}
92439230
}
92449231
}
92459232
@@ -9614,11 +9601,11 @@
96149601
}
96159602
utf8_printf(p->out, "%s", p->rowSeparator);
96169603
break;
96179604
}
96189605
case MODE_EQP: {
9619
- eqp_append(p, atoi(azArg[0]), azArg[3]);
9606
+ eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
96209607
break;
96219608
}
96229609
}
96239610
return 0;
96249611
}
@@ -10456,13 +10443,14 @@
1045610443
zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
1045710444
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1045810445
if( rc==SQLITE_OK ){
1045910446
while( sqlite3_step(pExplain)==SQLITE_ROW ){
1046010447
const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10461
- int iSelectId = sqlite3_column_int(pExplain, 0);
10448
+ int iEqpId = sqlite3_column_int(pExplain, 0);
10449
+ int iParentId = sqlite3_column_int(pExplain, 1);
1046210450
if( zEQPLine[0]=='-' ) eqp_render(pArg);
10463
- eqp_append(pArg, iSelectId, zEQPLine);
10451
+ eqp_append(pArg, iEqpId, iParentId, zEQPLine);
1046410452
}
1046510453
eqp_render(pArg);
1046610454
}
1046710455
sqlite3_finalize(pExplain);
1046810456
sqlite3_free(zEQP);
@@ -10840,10 +10828,11 @@
1084010828
".cd DIRECTORY Change the working directory to DIRECTORY\n"
1084110829
".changes on|off Show number of rows changed by SQL\n"
1084210830
".check GLOB Fail if output since .testcase does not match\n"
1084310831
".clone NEWDB Clone data into NEWDB from the existing database\n"
1084410832
".databases List names and files of attached databases\n"
10833
+ ".dbconfig ?op? ?val? List or change sqlite3_db_config() options\n"
1084510834
".dbinfo ?DB? Show status information about the database\n"
1084610835
".dump ?TABLE? ... Dump the database in an SQL text format\n"
1084710836
" If TABLE specified, only dump tables matching\n"
1084810837
" LIKE pattern TABLE.\n"
1084910838
".echo on|off Turn command echo on or off\n"
@@ -13282,11 +13271,39 @@
1328213271
sqlite3_free(zErrMsg);
1328313272
rc = 1;
1328413273
}
1328513274
}else
1328613275
13287
- if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
13276
+ if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13277
+ static const struct DbConfigChoices {const char *zName; int op;} aDbConfig[] = {
13278
+ { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
13279
+ { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
13280
+ { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
13281
+ { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
13282
+ { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
13283
+ { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
13284
+ { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
13285
+ { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
13286
+ };
13287
+ int ii, v;
13288
+ open_db(p, 0);
13289
+ for(ii=0; ii<ArraySize(aDbConfig); ii++){
13290
+ if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13291
+ if( nArg>=3 ){
13292
+ sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13293
+ }
13294
+ sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13295
+ utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13296
+ if( nArg>1 ) break;
13297
+ }
13298
+ if( nArg>1 && ii==ArraySize(aDbConfig) ){
13299
+ utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13300
+ utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13301
+ }
13302
+ }else
13303
+
13304
+ if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
1328813305
rc = shell_dbinfo_command(p, nArg, azArg);
1328913306
}else
1329013307
1329113308
if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
1329213309
const char *zLike = 0;
@@ -13385,14 +13402,18 @@
1338513402
}
1338613403
}else
1338713404
1338813405
if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
1338913406
if( nArg==2 ){
13407
+ p->autoEQPtest = 0;
1339013408
if( strcmp(azArg[1],"full")==0 ){
1339113409
p->autoEQP = AUTOEQP_full;
1339213410
}else if( strcmp(azArg[1],"trigger")==0 ){
1339313411
p->autoEQP = AUTOEQP_trigger;
13412
+ }else if( strcmp(azArg[1],"test")==0 ){
13413
+ p->autoEQP = AUTOEQP_on;
13414
+ p->autoEQPtest = 1;
1339413415
}else{
1339513416
p->autoEQP = (u8)booleanValue(azArg[1]);
1339613417
}
1339713418
}else{
1339813419
raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
1339913420
--- src/shell.c
+++ src/shell.c
@@ -7617,13 +7617,13 @@
7617 idxHashClear(&hIdx);
7618 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7619 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7620 );
7621 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7622 int iSelectid = sqlite3_column_int(pExplain, 0);
7623 int iOrder = sqlite3_column_int(pExplain, 1);
7624 int iFrom = sqlite3_column_int(pExplain, 2);
7625 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7626 int nDetail = STRLEN(zDetail);
7627 int i;
7628
7629 for(i=0; i<nDetail; i++){
@@ -7646,13 +7646,11 @@
7646 }
7647 break;
7648 }
7649 }
7650
7651 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%d|%d|%d|%s\n",
7652 iSelectid, iOrder, iFrom, zDetail
7653 );
7654 }
7655
7656 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7657 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7658 }
@@ -8481,11 +8479,12 @@
8481 };
8482
8483 /* A single line in the EQP output */
8484 typedef struct EQPGraphRow EQPGraphRow;
8485 struct EQPGraphRow {
8486 int iSelectId; /* The SelectID for this row */
 
8487 EQPGraphRow *pNext; /* Next row in sequence */
8488 char zText[1]; /* Text to display for this row */
8489 };
8490
8491 /* All EQP output is collected into an instance of the following */
@@ -8503,10 +8502,11 @@
8503 typedef struct ShellState ShellState;
8504 struct ShellState {
8505 sqlite3 *db; /* The database */
8506 u8 autoExplain; /* Automatically turn on .explain mode */
8507 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
 
8508 u8 statsOn; /* True to display memory stats before each finalize */
8509 u8 scanstatsOn; /* True to display scan stats before each finalize */
8510 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8511 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
8512 u8 nEqpLevel; /* Depth of the EQP output graph */
@@ -8553,14 +8553,14 @@
8553 };
8554
8555
8556 /* Allowed values for ShellState.autoEQP
8557 */
8558 #define AUTOEQP_off 0
8559 #define AUTOEQP_on 1
8560 #define AUTOEQP_trigger 2
8561 #define AUTOEQP_full 3
8562
8563 /* Allowed values for ShellState.openMode
8564 */
8565 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
8566 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
@@ -9167,16 +9167,20 @@
9167 }
9168
9169 /*
9170 ** Add a new entry to the EXPLAIN QUERY PLAN data
9171 */
9172 static void eqp_append(ShellState *p, int iSelectId, const char *zText){
9173 EQPGraphRow *pNew;
9174 int nText = strlen30(zText);
 
 
 
9175 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9176 if( pNew==0 ) shell_out_of_memory();
9177 pNew->iSelectId = iSelectId;
 
9178 memcpy(pNew->zText, zText, nText+1);
9179 pNew->pNext = 0;
9180 if( p->sGraph.pLast ){
9181 p->sGraph.pLast->pNext = pNew;
9182 }else{
@@ -9196,50 +9200,33 @@
9196 sqlite3_free(pRow);
9197 }
9198 memset(&p->sGraph, 0, sizeof(p->sGraph));
9199 }
9200
9201 /* Return the next EXPLAIN QUERY PLAN line with iSelectId that occurs after
9202 ** pOld, or return the first such line if pOld is NULL
9203 */
9204 static EQPGraphRow *eqp_next_row(ShellState *p, int iSelectId, EQPGraphRow *pOld){
9205 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9206 while( pRow && pRow->iSelectId!=iSelectId ) pRow = pRow->pNext;
9207 return pRow;
9208 }
9209
9210 /* Render a single level of the graph shell having iSelectId. Called
9211 ** recursively to render sublevels.
9212 */
9213 static void eqp_render_level(ShellState *p, int iSelectId){
9214 EQPGraphRow *pRow, *pNext;
9215 int i;
9216 int n = strlen30(p->sGraph.zPrefix);
9217 char *z;
9218 for(pRow = eqp_next_row(p, iSelectId, 0); pRow; pRow = pNext){
9219 pNext = eqp_next_row(p, iSelectId, pRow);
9220 z = pRow->zText;
9221 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9222 if( n<sizeof(p->sGraph.zPrefix)-7 && (z = strstr(z, " SUBQUER"))!=0 ){
9223 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
9224 if( strncmp(z, " SUBQUERY ", 9)==0 && (i = atoi(z+10))>iSelectId ){
9225 eqp_render_level(p, i);
9226 }else if( strncmp(z, " SUBQUERIES ", 12)==0 ){
9227 i = atoi(z+12);
9228 if( i>iSelectId ){
9229 utf8_printf(p->out, "%s|--SUBQUERY %d\n", p->sGraph.zPrefix, i);
9230 memcpy(&p->sGraph.zPrefix[n+3],"| ",4);
9231 eqp_render_level(p, i);
9232 }
9233 z = strstr(z, " AND ");
9234 if( z && (i = atoi(z+5))>iSelectId ){
9235 p->sGraph.zPrefix[n+3] = 0;
9236 utf8_printf(p->out, "%s`--SUBQUERY %d\n", p->sGraph.zPrefix, i);
9237 memcpy(&p->sGraph.zPrefix[n+3]," ",4);
9238 eqp_render_level(p, i);
9239 }
9240 }
9241 p->sGraph.zPrefix[n] = 0;
9242 }
9243 }
9244 }
9245
@@ -9614,11 +9601,11 @@
9614 }
9615 utf8_printf(p->out, "%s", p->rowSeparator);
9616 break;
9617 }
9618 case MODE_EQP: {
9619 eqp_append(p, atoi(azArg[0]), azArg[3]);
9620 break;
9621 }
9622 }
9623 return 0;
9624 }
@@ -10456,13 +10443,14 @@
10456 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10457 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10458 if( rc==SQLITE_OK ){
10459 while( sqlite3_step(pExplain)==SQLITE_ROW ){
10460 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10461 int iSelectId = sqlite3_column_int(pExplain, 0);
 
10462 if( zEQPLine[0]=='-' ) eqp_render(pArg);
10463 eqp_append(pArg, iSelectId, zEQPLine);
10464 }
10465 eqp_render(pArg);
10466 }
10467 sqlite3_finalize(pExplain);
10468 sqlite3_free(zEQP);
@@ -10840,10 +10828,11 @@
10840 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
10841 ".changes on|off Show number of rows changed by SQL\n"
10842 ".check GLOB Fail if output since .testcase does not match\n"
10843 ".clone NEWDB Clone data into NEWDB from the existing database\n"
10844 ".databases List names and files of attached databases\n"
 
10845 ".dbinfo ?DB? Show status information about the database\n"
10846 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
10847 " If TABLE specified, only dump tables matching\n"
10848 " LIKE pattern TABLE.\n"
10849 ".echo on|off Turn command echo on or off\n"
@@ -13282,11 +13271,39 @@
13282 sqlite3_free(zErrMsg);
13283 rc = 1;
13284 }
13285 }else
13286
13287 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13288 rc = shell_dbinfo_command(p, nArg, azArg);
13289 }else
13290
13291 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13292 const char *zLike = 0;
@@ -13385,14 +13402,18 @@
13385 }
13386 }else
13387
13388 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13389 if( nArg==2 ){
 
13390 if( strcmp(azArg[1],"full")==0 ){
13391 p->autoEQP = AUTOEQP_full;
13392 }else if( strcmp(azArg[1],"trigger")==0 ){
13393 p->autoEQP = AUTOEQP_trigger;
 
 
 
13394 }else{
13395 p->autoEQP = (u8)booleanValue(azArg[1]);
13396 }
13397 }else{
13398 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
13399
--- src/shell.c
+++ src/shell.c
@@ -7617,13 +7617,13 @@
7617 idxHashClear(&hIdx);
7618 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7619 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7620 );
7621 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7622 /* int iId = sqlite3_column_int(pExplain, 0); */
7623 /* int iParent = sqlite3_column_int(pExplain, 1); */
7624 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7625 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7626 int nDetail = STRLEN(zDetail);
7627 int i;
7628
7629 for(i=0; i<nDetail; i++){
@@ -7646,13 +7646,11 @@
7646 }
7647 break;
7648 }
7649 }
7650
7651 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
 
 
7652 }
7653
7654 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7655 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7656 }
@@ -8481,11 +8479,12 @@
8479 };
8480
8481 /* A single line in the EQP output */
8482 typedef struct EQPGraphRow EQPGraphRow;
8483 struct EQPGraphRow {
8484 int iEqpId; /* ID for this row */
8485 int iParentId; /* ID of the parent row */
8486 EQPGraphRow *pNext; /* Next row in sequence */
8487 char zText[1]; /* Text to display for this row */
8488 };
8489
8490 /* All EQP output is collected into an instance of the following */
@@ -8503,10 +8502,11 @@
8502 typedef struct ShellState ShellState;
8503 struct ShellState {
8504 sqlite3 *db; /* The database */
8505 u8 autoExplain; /* Automatically turn on .explain mode */
8506 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8507 u8 autoEQPtest; /* autoEQP is in test mode */
8508 u8 statsOn; /* True to display memory stats before each finalize */
8509 u8 scanstatsOn; /* True to display scan stats before each finalize */
8510 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8511 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
8512 u8 nEqpLevel; /* Depth of the EQP output graph */
@@ -8553,14 +8553,14 @@
8553 };
8554
8555
8556 /* Allowed values for ShellState.autoEQP
8557 */
8558 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
8559 #define AUTOEQP_on 1 /* Automatic EQP is on */
8560 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
8561 #define AUTOEQP_full 3 /* Show full EXPLAIN */
8562
8563 /* Allowed values for ShellState.openMode
8564 */
8565 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
8566 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
@@ -9167,16 +9167,20 @@
9167 }
9168
9169 /*
9170 ** Add a new entry to the EXPLAIN QUERY PLAN data
9171 */
9172 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
9173 EQPGraphRow *pNew;
9174 int nText = strlen30(zText);
9175 if( p->autoEQPtest ){
9176 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9177 }
9178 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9179 if( pNew==0 ) shell_out_of_memory();
9180 pNew->iEqpId = iEqpId;
9181 pNew->iParentId = p2;
9182 memcpy(pNew->zText, zText, nText+1);
9183 pNew->pNext = 0;
9184 if( p->sGraph.pLast ){
9185 p->sGraph.pLast->pNext = pNew;
9186 }else{
@@ -9196,50 +9200,33 @@
9200 sqlite3_free(pRow);
9201 }
9202 memset(&p->sGraph, 0, sizeof(p->sGraph));
9203 }
9204
9205 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
9206 ** pOld, or return the first such line if pOld is NULL
9207 */
9208 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
9209 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9210 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
9211 return pRow;
9212 }
9213
9214 /* Render a single level of the graph that has iEqpId as its parent. Called
9215 ** recursively to render sublevels.
9216 */
9217 static void eqp_render_level(ShellState *p, int iEqpId){
9218 EQPGraphRow *pRow, *pNext;
 
9219 int n = strlen30(p->sGraph.zPrefix);
9220 char *z;
9221 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9222 pNext = eqp_next_row(p, iEqpId, pRow);
9223 z = pRow->zText;
9224 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9225 if( n<sizeof(p->sGraph.zPrefix)-7 ){
9226 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
9227 eqp_render_level(p, pRow->iEqpId);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9228 p->sGraph.zPrefix[n] = 0;
9229 }
9230 }
9231 }
9232
@@ -9614,11 +9601,11 @@
9601 }
9602 utf8_printf(p->out, "%s", p->rowSeparator);
9603 break;
9604 }
9605 case MODE_EQP: {
9606 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
9607 break;
9608 }
9609 }
9610 return 0;
9611 }
@@ -10456,13 +10443,14 @@
10443 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10444 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10445 if( rc==SQLITE_OK ){
10446 while( sqlite3_step(pExplain)==SQLITE_ROW ){
10447 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10448 int iEqpId = sqlite3_column_int(pExplain, 0);
10449 int iParentId = sqlite3_column_int(pExplain, 1);
10450 if( zEQPLine[0]=='-' ) eqp_render(pArg);
10451 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10452 }
10453 eqp_render(pArg);
10454 }
10455 sqlite3_finalize(pExplain);
10456 sqlite3_free(zEQP);
@@ -10840,10 +10828,11 @@
10828 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
10829 ".changes on|off Show number of rows changed by SQL\n"
10830 ".check GLOB Fail if output since .testcase does not match\n"
10831 ".clone NEWDB Clone data into NEWDB from the existing database\n"
10832 ".databases List names and files of attached databases\n"
10833 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options\n"
10834 ".dbinfo ?DB? Show status information about the database\n"
10835 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
10836 " If TABLE specified, only dump tables matching\n"
10837 " LIKE pattern TABLE.\n"
10838 ".echo on|off Turn command echo on or off\n"
@@ -13282,11 +13271,39 @@
13271 sqlite3_free(zErrMsg);
13272 rc = 1;
13273 }
13274 }else
13275
13276 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13277 static const struct DbConfigChoices {const char *zName; int op;} aDbConfig[] = {
13278 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
13279 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
13280 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
13281 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
13282 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
13283 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
13284 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
13285 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
13286 };
13287 int ii, v;
13288 open_db(p, 0);
13289 for(ii=0; ii<ArraySize(aDbConfig); ii++){
13290 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13291 if( nArg>=3 ){
13292 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13293 }
13294 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13295 utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13296 if( nArg>1 ) break;
13297 }
13298 if( nArg>1 && ii==ArraySize(aDbConfig) ){
13299 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13300 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13301 }
13302 }else
13303
13304 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13305 rc = shell_dbinfo_command(p, nArg, azArg);
13306 }else
13307
13308 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13309 const char *zLike = 0;
@@ -13385,14 +13402,18 @@
13402 }
13403 }else
13404
13405 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13406 if( nArg==2 ){
13407 p->autoEQPtest = 0;
13408 if( strcmp(azArg[1],"full")==0 ){
13409 p->autoEQP = AUTOEQP_full;
13410 }else if( strcmp(azArg[1],"trigger")==0 ){
13411 p->autoEQP = AUTOEQP_trigger;
13412 }else if( strcmp(azArg[1],"test")==0 ){
13413 p->autoEQP = AUTOEQP_on;
13414 p->autoEQPtest = 1;
13415 }else{
13416 p->autoEQP = (u8)booleanValue(azArg[1]);
13417 }
13418 }else{
13419 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
13420
+639 -463
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1150,11 +1150,11 @@
11501150
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11511151
** [sqlite_version()] and [sqlite_source_id()].
11521152
*/
11531153
#define SQLITE_VERSION "3.24.0"
11541154
#define SQLITE_VERSION_NUMBER 3024000
1155
-#define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825d83b"
1155
+#define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda"
11561156
11571157
/*
11581158
** CAPI3REF: Run-Time Library Version Numbers
11591159
** KEYWORDS: sqlite3_version sqlite3_sourceid
11601160
**
@@ -3137,10 +3137,25 @@
31373137
** or negative to leave the setting unchanged.
31383138
** The second parameter is a pointer to an integer into which is written
31393139
** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
31403140
** it is not disabled, 1 if it is.
31413141
** </dd>
3142
+**
3143
+** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3144
+** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3145
+** [VACUUM] in order to reset a database back to an empty database
3146
+** with no schema and no content. The following process works even for
3147
+** a badly corrupted database file:
3148
+** <ol>
3149
+** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3150
+** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3151
+** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3152
+** </ol>
3153
+** Because resetting a database is destructive and irreversible, the
3154
+** process requires the use of this obscure API and multiple steps to help
3155
+** ensure that it does not happen by accident.
3156
+** </dd>
31423157
** </dl>
31433158
*/
31443159
#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
31453160
#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
31463161
#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -3148,11 +3163,12 @@
31483163
#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
31493164
#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
31503165
#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
31513166
#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
31523167
#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
3153
-#define SQLITE_DBCONFIG_MAX 1008 /* Largest DBCONFIG */
3168
+#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
3169
+#define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */
31543170
31553171
/*
31563172
** CAPI3REF: Enable Or Disable Extended Result Codes
31573173
** METHOD: sqlite3
31583174
**
@@ -6534,10 +6550,45 @@
65346550
** made NULL or made to point to memory obtained from [sqlite3_malloc]
65356551
** or else the use of the [data_store_directory pragma] should be avoided.
65366552
*/
65376553
SQLITE_API char *sqlite3_data_directory;
65386554
6555
+/*
6556
+** CAPI3REF: Win32 Specific Interface
6557
+**
6558
+** These interfaces are available only on Windows. The
6559
+** [sqlite3_win32_set_directory] interface is used to set the value associated
6560
+** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6561
+** zValue, depending on the value of the type parameter. The zValue parameter
6562
+** should be NULL to cause the previous value to be freed via [sqlite3_free];
6563
+** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6564
+** prior to being used. The [sqlite3_win32_set_directory] interface returns
6565
+** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6566
+** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6567
+** [sqlite3_data_directory] variable is intended to act as a replacement for
6568
+** the current directory on the sub-platforms of Win32 where that concept is
6569
+** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6570
+** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6571
+** sqlite3_win32_set_directory interface except the string parameter must be
6572
+** UTF-8 or UTF-16, respectively.
6573
+*/
6574
+SQLITE_API int sqlite3_win32_set_directory(
6575
+ unsigned long type, /* Identifier for directory being set or reset */
6576
+ void *zValue /* New value for directory being set or reset */
6577
+);
6578
+SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6579
+SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6580
+
6581
+/*
6582
+** CAPI3REF: Win32 Directory Types
6583
+**
6584
+** These macros are only available on Windows. They define the allowed values
6585
+** for the type argument to the [sqlite3_win32_set_directory] interface.
6586
+*/
6587
+#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
6588
+#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
6589
+
65396590
/*
65406591
** CAPI3REF: Test For Auto-Commit Mode
65416592
** KEYWORDS: {autocommit mode}
65426593
** METHOD: sqlite3
65436594
**
@@ -14517,11 +14568,23 @@
1451714568
SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
1451814569
#else
1451914570
# define sqlite3VdbeVerifyNoMallocRequired(A,B)
1452014571
# define sqlite3VdbeVerifyNoResultRow(A)
1452114572
#endif
14522
-SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
14573
+SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
14574
+#ifndef SQLITE_OMIT_EXPLAIN
14575
+SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
14576
+SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
14577
+SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
14578
+# define ExplainQueryPlan(P) sqlite3VdbeExplain P
14579
+# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
14580
+# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
14581
+#else
14582
+# define ExplainQueryPlan(P)
14583
+# define ExplainQueryPlanPop(P)
14584
+# define ExplainQueryPlanParent(P) 0
14585
+#endif
1452314586
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
1452414587
SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
1452514588
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
1452614589
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
1452714590
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
@@ -15821,10 +15884,11 @@
1582115884
#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
1582215885
#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
1582315886
#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
1582415887
#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
1582515888
#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
15889
+#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
1582615890
1582715891
/* Flags used only if debugging */
1582815892
#ifdef SQLITE_DEBUG
1582915893
#define SQLITE_SqlTrace 0x08000000 /* Debug print SQL as it executes */
1583015894
#define SQLITE_VdbeListing 0x10000000 /* Debug listings of VDBE programs */
@@ -16923,13 +16987,10 @@
1692316987
unsigned isTabFunc :1; /* True if table-valued-function syntax */
1692416988
unsigned isCorrelated :1; /* True if sub-query is correlated */
1692516989
unsigned viaCoroutine :1; /* Implemented as a co-routine */
1692616990
unsigned isRecursive :1; /* True for recursive reference in WITH */
1692716991
} fg;
16928
-#ifndef SQLITE_OMIT_EXPLAIN
16929
- u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
16930
-#endif
1693116992
int iCursor; /* The VDBE cursor number used to access this table */
1693216993
Expr *pOn; /* The ON clause of a join */
1693316994
IdList *pUsing; /* The USING clause of a join */
1693416995
Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
1693516996
union {
@@ -17073,15 +17134,12 @@
1707317134
1707417135
/*
1707517136
** An instance of the following structure contains all information
1707617137
** needed to generate code for a single SELECT statement.
1707717138
**
17078
-** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
17079
-** If there is a LIMIT clause, the parser sets nLimit to the value of the
17080
-** limit and nOffset to the value of the offset (or 0 if there is not
17081
-** offset). But later on, nLimit and nOffset become the memory locations
17082
-** in the VDBE that record the limit and offset counters.
17139
+** See the header comment on the computeLimitRegisters() routine for a
17140
+** detailed description of the meaning of the iLimit and iOffset fields.
1708317141
**
1708417142
** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
1708517143
** These addresses must be stored so that we can go back and fill in
1708617144
** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
1708717145
** the number of columns in P2 can be computed at the same time
@@ -17097,11 +17155,10 @@
1709717155
LogEst nSelectRow; /* Estimated number of result rows */
1709817156
u32 selFlags; /* Various SF_* values */
1709917157
int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
1710017158
#if SELECTTRACE_ENABLED
1710117159
char zSelName[12]; /* Symbolic name of this SELECT use for debugging */
17102
- u32 iSelectId; /* EXPLAIN QUERY PLAN select ID */
1710317160
#endif
1710417161
int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
1710517162
SrcList *pSrc; /* The FROM clause */
1710617163
Expr *pWhere; /* The WHERE clause */
1710717164
ExprList *pGroupBy; /* The GROUP BY clause */
@@ -17138,12 +17195,11 @@
1713817195
#define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */
1713917196
#define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */
1714017197
#define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */
1714117198
#define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */
1714217199
#define SF_IncludeHidden 0x20000 /* Include hidden columns in output */
17143
-#define SF_ComplexResult 0x40000 /* Result set contains subquery or function */
17144
-
17200
+#define SF_ComplexResult 0x40000 /* Result contains subquery or function */
1714517201
1714617202
/*
1714717203
** The results of a SELECT can be distributed in several ways, as defined
1714817204
** by one of the following macros. The "SRT" prefix means "SELECT Result
1714917205
** Type".
@@ -17409,12 +17465,11 @@
1740917465
u8 declareVtab; /* True if inside sqlite3_declare_vtab() */
1741017466
int nVtabLock; /* Number of virtual tables to lock */
1741117467
#endif
1741217468
int nHeight; /* Expression tree height of current sub-select */
1741317469
#ifndef SQLITE_OMIT_EXPLAIN
17414
- int iSelectId; /* ID of current select for EXPLAIN output */
17415
- int iNextSelectId; /* Next available select ID for EXPLAIN output */
17470
+ int addrExplain; /* Address of current OP_Explain opcode */
1741617471
#endif
1741717472
VList *pVList; /* Mapping between variable names and numbers */
1741817473
Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
1741917474
const char *zTail; /* All SQL text past the last semicolon parsed */
1742017475
Table *pNewTable; /* A table being constructed by CREATE TABLE */
@@ -27718,14 +27773,14 @@
2771827773
sqlite3TreeViewPush(pView, 1);
2771927774
}
2772027775
do{
2772127776
#if SELECTTRACE_ENABLED
2772227777
sqlite3TreeViewLine(pView,
27723
- "SELECT%s%s (%s/%d/%p) selFlags=0x%x nSelectRow=%d",
27778
+ "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d",
2772427779
((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
2772527780
((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
27726
- p->zSelName, p->iSelectId, p, p->selFlags,
27781
+ p->zSelName, p, p->selFlags,
2772727782
(int)p->nSelectRow
2772827783
);
2772927784
#else
2773027785
sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
2773127786
((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
@@ -39693,26 +39748,10 @@
3969339748
*/
3969439749
#ifndef SQLITE_WIN32_DBG_BUF_SIZE
3969539750
# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
3969639751
#endif
3969739752
39698
-/*
39699
- * The value used with sqlite3_win32_set_directory() to specify that
39700
- * the data directory should be changed.
39701
- */
39702
-#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
39703
-# define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
39704
-#endif
39705
-
39706
-/*
39707
- * The value used with sqlite3_win32_set_directory() to specify that
39708
- * the temporary directory should be changed.
39709
- */
39710
-#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
39711
-# define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
39712
-#endif
39713
-
3971439753
/*
3971539754
* If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
3971639755
* various Win32 API heap functions instead of our own.
3971739756
*/
3971839757
#ifdef SQLITE_WIN32_MALLOC
@@ -41305,17 +41344,17 @@
4130541344
#endif
4130641345
return winUtf8ToMbcs(zText, useAnsi);
4130741346
}
4130841347
4130941348
/*
41310
-** This function sets the data directory or the temporary directory based on
41311
-** the provided arguments. The type argument must be 1 in order to set the
41312
-** data directory or 2 in order to set the temporary directory. The zValue
41313
-** argument is the name of the directory to use. The return value will be
41314
-** SQLITE_OK if successful.
41349
+** This function is the same as sqlite3_win32_set_directory (below); however,
41350
+** it accepts a UTF-8 string.
4131541351
*/
41316
-SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
41352
+SQLITE_API int sqlite3_win32_set_directory8(
41353
+ unsigned long type, /* Identifier for directory being set or reset */
41354
+ const char *zValue /* New value for directory being set or reset */
41355
+){
4131741356
char **ppDirectory = 0;
4131841357
#ifndef SQLITE_OMIT_AUTOINIT
4131941358
int rc = sqlite3_initialize();
4132041359
if( rc ) return rc;
4132141360
#endif
@@ -41327,23 +41366,56 @@
4132741366
assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
4132841367
|| type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
4132941368
);
4133041369
assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
4133141370
if( ppDirectory ){
41332
- char *zValueUtf8 = 0;
41371
+ char *zCopy = 0;
4133341372
if( zValue && zValue[0] ){
41334
- zValueUtf8 = winUnicodeToUtf8(zValue);
41335
- if ( zValueUtf8==0 ){
41373
+ zCopy = sqlite3_mprintf("%s", zValue);
41374
+ if ( zCopy==0 ){
4133641375
return SQLITE_NOMEM_BKPT;
4133741376
}
4133841377
}
4133941378
sqlite3_free(*ppDirectory);
41340
- *ppDirectory = zValueUtf8;
41379
+ *ppDirectory = zCopy;
4134141380
return SQLITE_OK;
4134241381
}
4134341382
return SQLITE_ERROR;
4134441383
}
41384
+
41385
+/*
41386
+** This function is the same as sqlite3_win32_set_directory (below); however,
41387
+** it accepts a UTF-16 string.
41388
+*/
41389
+SQLITE_API int sqlite3_win32_set_directory16(
41390
+ unsigned long type, /* Identifier for directory being set or reset */
41391
+ const void *zValue /* New value for directory being set or reset */
41392
+){
41393
+ int rc;
41394
+ char *zUtf8 = 0;
41395
+ if( zValue ){
41396
+ zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
41397
+ if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
41398
+ }
41399
+ rc = sqlite3_win32_set_directory8(type, zUtf8);
41400
+ if( zUtf8 ) sqlite3_free(zUtf8);
41401
+ return rc;
41402
+}
41403
+
41404
+/*
41405
+** This function sets the data directory or the temporary directory based on
41406
+** the provided arguments. The type argument must be 1 in order to set the
41407
+** data directory or 2 in order to set the temporary directory. The zValue
41408
+** argument is the name of the directory to use. The return value will be
41409
+** SQLITE_OK if successful.
41410
+*/
41411
+SQLITE_API int sqlite3_win32_set_directory(
41412
+ unsigned long type, /* Identifier for directory being set or reset */
41413
+ void *zValue /* New value for directory being set or reset */
41414
+){
41415
+ return sqlite3_win32_set_directory16(type, zValue);
41416
+}
4134541417
4134641418
/*
4134741419
** The return value of winGetLastErrorMsg
4134841420
** is zero if the error message fits in the buffer, or non-zero
4134941421
** otherwise (if the message was truncated).
@@ -64714,10 +64786,14 @@
6471464786
}
6471564787
}
6471664788
#else
6471764789
# define setDefaultSyncFlag(pBt,safety_level)
6471864790
#endif
64791
+
64792
+/* Forward declaration */
64793
+static int newDatabase(BtShared*);
64794
+
6471964795
6472064796
/*
6472164797
** Get a reference to pPage1 of the database file. This will
6472264798
** also acquire a readlock on that file.
6472364799
**
@@ -64745,10 +64821,13 @@
6474564821
*/
6474664822
nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
6474764823
sqlite3PagerPagecount(pBt->pPager, &nPageFile);
6474864824
if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
6474964825
nPage = nPageFile;
64826
+ }
64827
+ if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
64828
+ nPage = 0;
6475064829
}
6475164830
if( nPage>0 ){
6475264831
u32 pageSize;
6475364832
u32 usableSize;
6475464833
u8 *page1 = pPage1->aData;
@@ -67962,11 +68041,13 @@
6796268041
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6796368042
pPage->xParseCell(pPage, pCell, pInfo);
6796468043
if( pInfo->nLocal==pInfo->nPayload ){
6796568044
return SQLITE_OK; /* No overflow pages. Return without doing anything */
6796668045
}
67967
- if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
68046
+ testcase( pCell + pInfo->nSize == pPage->aDataEnd );
68047
+ testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
68048
+ if( pCell + pInfo->nSize > pPage->aDataEnd ){
6796868049
/* Cell extends past end of page */
6796968050
return SQLITE_CORRUPT_PAGE(pPage);
6797068051
}
6797168052
ovflPgno = get4byte(pCell + pInfo->nSize - 4);
6797268053
pBt = pPage->pBt;
@@ -74686,10 +74767,53 @@
7468674767
char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
7468774768
if( p4copy ) memcpy(p4copy, zP4, 8);
7468874769
return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
7468974770
}
7469074771
74772
+#ifndef SQLITE_OMIT_EXPLAIN
74773
+/*
74774
+** Return the address of the current EXPLAIN QUERY PLAN baseline.
74775
+** 0 means "none".
74776
+*/
74777
+SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
74778
+ VdbeOp *pOp;
74779
+ if( pParse->addrExplain==0 ) return 0;
74780
+ pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
74781
+ return pOp->p2;
74782
+}
74783
+
74784
+/*
74785
+** Add a new OP_Explain opcode.
74786
+**
74787
+** If the bPush flag is true, then make this opcode the parent for
74788
+** subsequent Explains until sqlite3VdbeExplainPop() is called.
74789
+*/
74790
+SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
74791
+ if( pParse->explain==2 ){
74792
+ char *zMsg;
74793
+ Vdbe *v = pParse->pVdbe;
74794
+ va_list ap;
74795
+ int iThis;
74796
+ va_start(ap, zFmt);
74797
+ zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
74798
+ va_end(ap);
74799
+ v = pParse->pVdbe;
74800
+ iThis = v->nOp;
74801
+ sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
74802
+ zMsg, P4_DYNAMIC);
74803
+ if( bPush) pParse->addrExplain = iThis;
74804
+ }
74805
+}
74806
+
74807
+/*
74808
+** Pop the EXPLAIN QUERY PLAN stack one level.
74809
+*/
74810
+SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
74811
+ pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
74812
+}
74813
+#endif /* SQLITE_OMIT_EXPLAIN */
74814
+
7469174815
/*
7469274816
** Add an OP_ParseSchema opcode. This routine is broken out from
7469374817
** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
7469474818
** as having been used.
7469574819
**
@@ -96622,15 +96746,12 @@
9662296746
9662396747
assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
9662496748
if( colUsed==(MASKBIT(nExpr)-1) ){
9662596749
/* If we reach this point, that means the index pIdx is usable */
9662696750
int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
96627
-#ifndef SQLITE_OMIT_EXPLAIN
96628
- sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
96629
- sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
96630
- P4_DYNAMIC);
96631
-#endif
96751
+ ExplainQueryPlan((pParse, 0,
96752
+ "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
9663296753
sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
9663396754
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
9663496755
VdbeComment((v, "%s", pIdx->zName));
9663596756
assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
9663696757
eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
@@ -96858,21 +96979,13 @@
9685896979
** table allocated and opened above.
9685996980
*/
9686096981
Select *pSelect = pExpr->x.pSelect;
9686196982
ExprList *pEList = pSelect->pEList;
9686296983
96863
-#ifndef SQLITE_OMIT_EXPLAIN
96864
- if( pParse->explain==2 ){
96865
- char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %sLIST SUBQUERY %d",
96866
- jmpIfDynamic>=0?"":"CORRELATED ",
96867
- pParse->iNextSelectId
96868
- );
96869
- sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg,
96870
- P4_DYNAMIC);
96871
- }
96872
-#endif
96873
-
96984
+ ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY",
96985
+ jmpIfDynamic>=0?"":"CORRELATED "
96986
+ ));
9687496987
assert( !isRowid );
9687596988
/* If the LHS and RHS of the IN operator do not match, that
9687696989
** error will have been caught long before we reach this point. */
9687796990
if( ALWAYS(pEList->nExpr==nVal) ){
9687896991
SelectDest dest;
@@ -96989,22 +97102,13 @@
9698997102
testcase( pExpr->op==TK_EXISTS );
9699097103
testcase( pExpr->op==TK_SELECT );
9699197104
assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
9699297105
assert( ExprHasProperty(pExpr, EP_xIsSelect) );
9699397106
96994
-#ifndef SQLITE_OMIT_EXPLAIN
96995
- if( pParse->explain==2 ){
96996
- char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %sSCALAR SUBQUERY %d",
96997
- jmpIfDynamic>=0?"":"CORRELATED ",
96998
- pParse->iNextSelectId
96999
- );
97000
- sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg,
97001
- P4_DYNAMIC);
97002
- }
97003
-#endif
97004
-
9700597107
pSel = pExpr->x.pSelect;
97108
+ ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
97109
+ jmpIfDynamic>=0?"":"CORRELATED "));
9700697110
nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
9700797111
sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
9700897112
pParse->nMem += nReg;
9700997113
if( pExpr->op==TK_SELECT ){
9701097114
dest.eDest = SRT_Mem;
@@ -97763,10 +97867,11 @@
9776397867
if( v==0 ){
9776497868
assert( pParse->db->mallocFailed );
9776597869
return 0;
9776697870
}
9776797871
97872
+expr_code_doover:
9776897873
if( pExpr==0 ){
9776997874
op = TK_NULL;
9777097875
}else{
9777197876
op = pExpr->op;
9777297877
}
@@ -98223,11 +98328,12 @@
9822398328
return target;
9822498329
}
9822598330
case TK_SPAN:
9822698331
case TK_COLLATE:
9822798332
case TK_UPLUS: {
98228
- return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
98333
+ pExpr = pExpr->pLeft;
98334
+ goto expr_code_doover;
9822998335
}
9823098336
9823198337
case TK_TRIGGER: {
9823298338
/* If the opcode is TK_TRIGGER, then the expression is a reference
9823398339
** to a column in the new.* or old.* pseudo-tables available to
@@ -106427,11 +106533,15 @@
106427106533
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
106428106534
&& db->init.busy==0
106429106535
#if SQLITE_USER_AUTHENTICATION
106430106536
&& sqlite3UserAuthTable(pTab->zName)==0
106431106537
#endif
106432
- && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
106538
+#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
106539
+ && sqlite3StrICmp(&pTab->zName[7],"master")!=0
106540
+#endif
106541
+ && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
106542
+ ){
106433106543
sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
106434106544
goto exit_create_index;
106435106545
}
106436106546
#ifndef SQLITE_OMIT_VIEW
106437106547
if( pTab->pSelect ){
@@ -120002,10 +120112,13 @@
120002120112
** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
120003120113
** the possible values of meta[4].
120004120114
*/
120005120115
for(i=0; i<ArraySize(meta); i++){
120006120116
sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
120117
+ }
120118
+ if( (db->flags & SQLITE_ResetDatabase)!=0 ){
120119
+ memset(meta, 0, sizeof(meta));
120007120120
}
120008120121
pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
120009120122
120010120123
/* If opening a non-empty database, check the text encoding. For the
120011120124
** main database, set sqlite3.enc to the encoding of the main database.
@@ -120400,11 +120513,11 @@
120400120513
120401120514
#ifndef SQLITE_OMIT_EXPLAIN
120402120515
if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
120403120516
static const char * const azColName[] = {
120404120517
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
120405
- "selectid", "order", "from", "detail"
120518
+ "id", "parent", "notused", "detail"
120406120519
};
120407120520
int iFirst, mx;
120408120521
if( sParse.explain==2 ){
120409120522
sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
120410120523
iFirst = 8;
@@ -120714,11 +120827,11 @@
120714120827
*/
120715120828
#if SELECTTRACE_ENABLED
120716120829
/***/ int sqlite3SelectTrace = 0;
120717120830
# define SELECTTRACE(K,P,S,X) \
120718120831
if(sqlite3SelectTrace&(K)) \
120719
- sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->iSelectId,(S)),\
120832
+ sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->addrExplain,(S)),\
120720120833
sqlite3DebugPrintf X
120721120834
#else
120722120835
# define SELECTTRACE(K,P,S,X)
120723120836
#endif
120724120837
@@ -120771,10 +120884,11 @@
120771120884
Table *pTab; /* Table definition */
120772120885
int iCsr; /* Cursor number for table */
120773120886
int nKey; /* Number of PK columns for table pTab (>=1) */
120774120887
} aDefer[4];
120775120888
#endif
120889
+ struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
120776120890
};
120777120891
#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
120778120892
120779120893
/*
120780120894
** Delete all the content of a Select structure. Deallocate the structure
@@ -121228,10 +121342,66 @@
121228121342
Parse *pParse, /* Parsing context */
121229121343
ExprList *pList, /* Form the KeyInfo object from this ExprList */
121230121344
int iStart, /* Begin with this column of pList */
121231121345
int nExtra /* Add this many extra columns to the end */
121232121346
);
121347
+
121348
+/*
121349
+** An instance of this object holds information (beyond pParse and pSelect)
121350
+** needed to load the next result row that is to be added to the sorter.
121351
+*/
121352
+typedef struct RowLoadInfo RowLoadInfo;
121353
+struct RowLoadInfo {
121354
+ int regResult; /* Store results in array of registers here */
121355
+ u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
121356
+#ifdef SQLITE_ENABLE_SORTER_REFERENCES
121357
+ ExprList *pExtra; /* Extra columns needed by sorter refs */
121358
+ int regExtraResult; /* Where to load the extra columns */
121359
+#endif
121360
+};
121361
+
121362
+/*
121363
+** This routine does the work of loading query data into an array of
121364
+** registers so that it can be added to the sorter.
121365
+*/
121366
+static void innerLoopLoadRow(
121367
+ Parse *pParse, /* Statement under construction */
121368
+ Select *pSelect, /* The query being coded */
121369
+ RowLoadInfo *pInfo /* Info needed to complete the row load */
121370
+){
121371
+ sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
121372
+ 0, pInfo->ecelFlags);
121373
+#ifdef SQLITE_ENABLE_SORTER_REFERENCES
121374
+ if( pInfo->pExtra ){
121375
+ sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
121376
+ sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
121377
+ }
121378
+#endif
121379
+}
121380
+
121381
+/*
121382
+** Code the OP_MakeRecord instruction that generates the entry to be
121383
+** added into the sorter.
121384
+**
121385
+** Return the register in which the result is stored.
121386
+*/
121387
+static int makeSorterRecord(
121388
+ Parse *pParse,
121389
+ SortCtx *pSort,
121390
+ Select *pSelect,
121391
+ int regBase,
121392
+ int nBase
121393
+){
121394
+ int nOBSat = pSort->nOBSat;
121395
+ Vdbe *v = pParse->pVdbe;
121396
+ int regOut = ++pParse->nMem;
121397
+ if( pSort->pDeferredRowLoad ){
121398
+ innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
121399
+ }
121400
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
121401
+ return regOut;
121402
+}
121233121403
121234121404
/*
121235121405
** Generate code that will push the record in registers regData
121236121406
** through regData+nData-1 onto the sorter.
121237121407
*/
@@ -121239,28 +121409,43 @@
121239121409
Parse *pParse, /* Parser context */
121240121410
SortCtx *pSort, /* Information about the ORDER BY clause */
121241121411
Select *pSelect, /* The whole SELECT statement */
121242121412
int regData, /* First register holding data to be sorted */
121243121413
int regOrigData, /* First register holding data before packing */
121244
- int nData, /* Number of elements in the data array */
121414
+ int nData, /* Number of elements in the regData data array */
121245121415
int nPrefixReg /* No. of reg prior to regData available for use */
121246121416
){
121247121417
Vdbe *v = pParse->pVdbe; /* Stmt under construction */
121248121418
int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
121249121419
int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
121250121420
int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
121251121421
int regBase; /* Regs for sorter record */
121252
- int regRecord = ++pParse->nMem; /* Assembled sorter record */
121422
+ int regRecord = 0; /* Assembled sorter record */
121253121423
int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
121254121424
int op; /* Opcode to add sorter record to sorter */
121255121425
int iLimit; /* LIMIT counter */
121426
+ int iSkip = 0; /* End of the sorter insert loop */
121256121427
121257121428
assert( bSeq==0 || bSeq==1 );
121429
+
121430
+ /* Three cases:
121431
+ ** (1) The data to be sorted has already been packed into a Record
121432
+ ** by a prior OP_MakeRecord. In this case nData==1 and regData
121433
+ ** will be completely unrelated to regOrigData.
121434
+ ** (2) All output columns are included in the sort record. In that
121435
+ ** case regData==regOrigData.
121436
+ ** (3) Some output columns are omitted from the sort record due to
121437
+ ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
121438
+ ** SQLITE_ECEL_OMITREF optimization. In that case, regOrigData==0
121439
+ ** to prevent this routine from trying to copy values that might
121440
+ ** not exist.
121441
+ */
121258121442
assert( nData==1 || regData==regOrigData || regOrigData==0 );
121443
+
121259121444
if( nPrefixReg ){
121260121445
assert( nPrefixReg==nExpr+bSeq );
121261
- regBase = regData - nExpr - bSeq;
121446
+ regBase = regData - nPrefixReg;
121262121447
}else{
121263121448
regBase = pParse->nMem + 1;
121264121449
pParse->nMem += nBase;
121265121450
}
121266121451
assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
@@ -121280,11 +121465,11 @@
121280121465
int addrJmp; /* Address of the OP_Jump opcode */
121281121466
VdbeOp *pOp; /* Opcode that opens the sorter */
121282121467
int nKey; /* Number of sorting key columns, including OP_Sequence */
121283121468
KeyInfo *pKI; /* Original KeyInfo on the sorter table */
121284121469
121285
- sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat,regRecord);
121470
+ regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
121286121471
regPrevKey = pParse->nMem+1;
121287121472
pParse->nMem += pSort->nOBSat;
121288121473
nKey = nExpr - pSort->nOBSat + bSeq;
121289121474
if( bSeq ){
121290121475
addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
@@ -121331,29 +121516,33 @@
121331121516
** pSort->bOrderedInnerLoop flag is set to indicate that the inner
121332121517
** loop delivers items in sorted order, jump to the next iteration
121333121518
** of the outer loop.
121334121519
*/
121335121520
int iCsr = pSort->iECursor;
121336
- int iJmp = sqlite3VdbeCurrentAddr(v)+5+(nOBSat<=0)+pSort->bOrderedInnerLoop;
121337
- assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 );
121338121521
sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
121339121522
VdbeCoverage(v);
121340121523
sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
121341
- sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp, regBase+nOBSat, nExpr-nOBSat);
121524
+ iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
121525
+ iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
121342121526
VdbeCoverage(v);
121343121527
sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
121344121528
}
121345
- if( nOBSat<=0 ){
121346
- sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat,regRecord);
121529
+ if( regRecord==0 ){
121530
+ regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
121347121531
}
121348121532
if( pSort->sortFlags & SORTFLAG_UseSorter ){
121349121533
op = OP_SorterInsert;
121350121534
}else{
121351121535
op = OP_IdxInsert;
121352121536
}
121353121537
sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
121354121538
regBase+nOBSat, nBase-nOBSat);
121539
+ if( iSkip ){
121540
+ assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 );
121541
+ sqlite3VdbeChangeP2(v, iSkip,
121542
+ sqlite3VdbeCurrentAddr(v) + pSort->bOrderedInnerLoop);
121543
+ }
121355121544
}
121356121545
121357121546
/*
121358121547
** Add code to implement the OFFSET
121359121548
*/
@@ -121435,13 +121624,10 @@
121435121624
if( pItem->u.x.iOrderByCol==0 ){
121436121625
Expr *pExpr = pItem->pExpr;
121437121626
Table *pTab = pExpr->pTab;
121438121627
if( pExpr->op==TK_COLUMN && pTab && !IsVirtual(pTab)
121439121628
&& (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
121440
-#if 0
121441
- && pTab->pSchema && pTab->pSelect==0 && !IsVirtual(pTab)
121442
-#endif
121443121629
){
121444121630
int j;
121445121631
for(j=0; j<nDefer; j++){
121446121632
if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
121447121633
}
@@ -121504,10 +121690,11 @@
121504121690
int hasDistinct; /* True if the DISTINCT keyword is present */
121505121691
int eDest = pDest->eDest; /* How to dispose of results */
121506121692
int iParm = pDest->iSDParm; /* First argument to disposal method */
121507121693
int nResultCol; /* Number of result columns */
121508121694
int nPrefixReg = 0; /* Number of extra registers before regResult */
121695
+ RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
121509121696
121510121697
/* Usually, regResult is the first cell in an array of memory cells
121511121698
** containing the current result row. In this case regOrig is set to the
121512121699
** same value. However, if the results are being sent to the sorter, the
121513121700
** values for any expressions that are also part of the sort-key are omitted
@@ -121556,11 +121743,12 @@
121556121743
ExprList *pExtra = 0;
121557121744
#endif
121558121745
/* If the destination is an EXISTS(...) expression, the actual
121559121746
** values returned by the SELECT are not required.
121560121747
*/
121561
- u8 ecelFlags;
121748
+ u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
121749
+ ExprList *pEList;
121562121750
if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
121563121751
ecelFlags = SQLITE_ECEL_DUP;
121564121752
}else{
121565121753
ecelFlags = 0;
121566121754
}
@@ -121570,10 +121758,11 @@
121570121758
** iOrderByCol value to one more than the index of the ORDER BY
121571121759
** expression within the sort-key that pushOntoSorter() will generate.
121572121760
** This allows the p->pEList field to be omitted from the sorted record,
121573121761
** saving space and CPU cycles. */
121574121762
ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
121763
+
121575121764
for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
121576121765
int j;
121577121766
if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
121578121767
p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
121579121768
}
@@ -121590,24 +121779,50 @@
121590121779
pOp->p2 += (pExtra->nExpr - pSort->nDefer);
121591121780
pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
121592121781
pParse->nMem += pExtra->nExpr;
121593121782
}
121594121783
#endif
121595
- regOrig = 0;
121784
+
121785
+ /* Adjust nResultCol to account for columns that are omitted
121786
+ ** from the sorter by the optimizations in this branch */
121787
+ pEList = p->pEList;
121788
+ for(i=0; i<pEList->nExpr; i++){
121789
+ if( pEList->a[i].u.x.iOrderByCol>0
121790
+#ifdef SQLITE_ENABLE_SORTER_REFERENCES
121791
+ || pEList->a[i].bSorterRef
121792
+#endif
121793
+ ){
121794
+ nResultCol--;
121795
+ regOrig = 0;
121796
+ }
121797
+ }
121798
+
121799
+ testcase( regOrig );
121800
+ testcase( eDest==SRT_Set );
121801
+ testcase( eDest==SRT_Mem );
121802
+ testcase( eDest==SRT_Coroutine );
121803
+ testcase( eDest==SRT_Output );
121596121804
assert( eDest==SRT_Set || eDest==SRT_Mem
121597121805
|| eDest==SRT_Coroutine || eDest==SRT_Output );
121598121806
}
121599
- nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
121600
- 0,ecelFlags);
121807
+ sRowLoadInfo.regResult = regResult;
121808
+ sRowLoadInfo.ecelFlags = ecelFlags;
121601121809
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
121602
- if( pExtra ){
121603
- nResultCol += sqlite3ExprCodeExprList(
121604
- pParse, pExtra, regResult + nResultCol, 0, 0
121605
- );
121606
- sqlite3ExprListDelete(pParse->db, pExtra);
121810
+ sRowLoadInfo.pExtra = pExtra;
121811
+ sRowLoadInfo.regExtraResult = regResult + nResultCol;
121812
+ if( pExtra ) nResultCol += pExtra->nExpr;
121813
+#endif
121814
+ if( p->iLimit
121815
+ && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
121816
+ && nPrefixReg>0
121817
+ ){
121818
+ assert( pSort!=0 );
121819
+ assert( hasDistinct==0 );
121820
+ pSort->pDeferredRowLoad = &sRowLoadInfo;
121821
+ }else{
121822
+ innerLoopLoadRow(pParse, p, &sRowLoadInfo);
121607121823
}
121608
-#endif
121609121824
}
121610121825
121611121826
/* If the DISTINCT keyword was present on the SELECT statement
121612121827
** and this row has been seen before, then do not make this row
121613121828
** part of the result.
@@ -121719,11 +121934,12 @@
121719121934
sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
121720121935
assert( pSort==0 );
121721121936
}
121722121937
#endif
121723121938
if( pSort ){
121724
- pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg);
121939
+ assert( regResult==regOrig );
121940
+ pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
121725121941
}else{
121726121942
int r2 = sqlite3GetTempReg(pParse);
121727121943
sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
121728121944
sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
121729121945
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
@@ -121986,15 +122202,11 @@
121986122202
**
121987122203
** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
121988122204
** is determined by the zUsage argument.
121989122205
*/
121990122206
static void explainTempTable(Parse *pParse, const char *zUsage){
121991
- if( pParse->explain==2 ){
121992
- Vdbe *v = pParse->pVdbe;
121993
- char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
121994
- sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
121995
- }
122207
+ ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
121996122208
}
121997122209
121998122210
/*
121999122211
** Assign expression b to lvalue a. A second, no-op, version of this macro
122000122212
** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
@@ -122008,46 +122220,10 @@
122008122220
/* No-op versions of the explainXXX() functions and macros. */
122009122221
# define explainTempTable(y,z)
122010122222
# define explainSetInteger(y,z)
122011122223
#endif
122012122224
122013
-#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
122014
-/*
122015
-** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
122016
-** is a no-op. Otherwise, it adds a single row of output to the EQP result,
122017
-** where the caption is of one of the two forms:
122018
-**
122019
-** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
122020
-** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
122021
-**
122022
-** where iSub1 and iSub2 are the integers passed as the corresponding
122023
-** function parameters, and op is the text representation of the parameter
122024
-** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
122025
-** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
122026
-** false, or the second form if it is true.
122027
-*/
122028
-static void explainComposite(
122029
- Parse *pParse, /* Parse context */
122030
- int op, /* One of TK_UNION, TK_EXCEPT etc. */
122031
- int iSub1, /* Subquery id 1 */
122032
- int iSub2, /* Subquery id 2 */
122033
- int bUseTmp /* True if a temp table was used */
122034
-){
122035
- assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
122036
- if( pParse->explain==2 ){
122037
- Vdbe *v = pParse->pVdbe;
122038
- char *zMsg = sqlite3MPrintf(
122039
- pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
122040
- bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
122041
- );
122042
- sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
122043
- }
122044
-}
122045
-#else
122046
-/* No-op versions of the explainXXX() functions and macros. */
122047
-# define explainComposite(v,w,x,y,z)
122048
-#endif
122049122225
122050122226
/*
122051122227
** If the inner loop was generated using a non-null pOrderBy argument,
122052122228
** then the results were placed in a sorter. After the loop is terminated
122053122229
** we need to run the sorter and output the results. The following
@@ -122493,11 +122669,11 @@
122493122669
if( pParse->explain ){
122494122670
return;
122495122671
}
122496122672
#endif
122497122673
122498
- if( pParse->colNamesSet || db->mallocFailed ) return;
122674
+ if( pParse->colNamesSet ) return;
122499122675
/* Column names are determined by the left-most term of a compound select */
122500122676
while( pSelect->pPrior ) pSelect = pSelect->pPrior;
122501122677
SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
122502122678
pTabList = pSelect->pSrc;
122503122679
pEList = pSelect->pEList;
@@ -123020,10 +123196,11 @@
123020123196
/* Detach the ORDER BY clause from the compound SELECT */
123021123197
p->pOrderBy = 0;
123022123198
123023123199
/* Store the results of the setup-query in Queue. */
123024123200
pSetup->pNext = 0;
123201
+ ExplainQueryPlan((pParse, 1, "SETUP"));
123025123202
rc = sqlite3Select(pParse, pSetup, &destQueue);
123026123203
pSetup->pNext = p;
123027123204
if( rc ) goto end_of_recursive_query;
123028123205
123029123206
/* Find the next row in the Queue and output that row */
@@ -123054,10 +123231,11 @@
123054123231
*/
123055123232
if( p->selFlags & SF_Aggregate ){
123056123233
sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
123057123234
}else{
123058123235
p->pPrior = 0;
123236
+ ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
123059123237
sqlite3Select(pParse, p, &destQueue);
123060123238
assert( p->pPrior==0 );
123061123239
p->pPrior = pSetup;
123062123240
}
123063123241
@@ -123099,30 +123277,28 @@
123099123277
static int multiSelectValues(
123100123278
Parse *pParse, /* Parsing context */
123101123279
Select *p, /* The right-most of SELECTs to be coded */
123102123280
SelectDest *pDest /* What to do with query results */
123103123281
){
123104
- Select *pPrior;
123105
- Select *pRightmost = p;
123106123282
int nRow = 1;
123107123283
int rc = 0;
123284
+ int bShowAll = p->pLimit==0;
123108123285
assert( p->selFlags & SF_MultiValue );
123109123286
do{
123110123287
assert( p->selFlags & SF_Values );
123111123288
assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
123112123289
assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
123113123290
if( p->pPrior==0 ) break;
123114123291
assert( p->pPrior->pNext==p );
123115123292
p = p->pPrior;
123116
- nRow++;
123293
+ nRow += bShowAll;
123117123294
}while(1);
123295
+ ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
123296
+ nRow==1 ? "" : "S"));
123118123297
while( p ){
123119
- pPrior = p->pPrior;
123120
- p->pPrior = 0;
123121
- rc = sqlite3Select(pParse, p, pDest);
123122
- p->pPrior = pPrior;
123123
- if( rc || pRightmost->pLimit ) break;
123298
+ selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
123299
+ if( !bShowAll ) break;
123124123300
p->nSelectRow = nRow;
123125123301
p = p->pNext;
123126123302
}
123127123303
return rc;
123128123304
}
@@ -123167,14 +123343,10 @@
123167123343
Select *pPrior; /* Another SELECT immediately to our left */
123168123344
Vdbe *v; /* Generate code to this VDBE */
123169123345
SelectDest dest; /* Alternative data destination */
123170123346
Select *pDelete = 0; /* Chain of simple selects to delete */
123171123347
sqlite3 *db; /* Database connection */
123172
-#ifndef SQLITE_OMIT_EXPLAIN
123173
- int iSub1 = 0; /* EQP id of left-hand query */
123174
- int iSub2 = 0; /* EQP id of right-hand query */
123175
-#endif
123176123348
123177123349
/* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
123178123350
** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
123179123351
*/
123180123352
assert( p && p->pPrior ); /* Calling function guarantees this much */
@@ -123221,221 +123393,235 @@
123221123393
123222123394
/* Compound SELECTs that have an ORDER BY clause are handled separately.
123223123395
*/
123224123396
if( p->pOrderBy ){
123225123397
return multiSelectOrderBy(pParse, p, pDest);
123226
- }else
123227
-
123228
- /* Generate code for the left and right SELECT statements.
123229
- */
123230
- switch( p->op ){
123231
- case TK_ALL: {
123232
- int addr = 0;
123233
- int nLimit;
123234
- assert( !pPrior->pLimit );
123235
- pPrior->iLimit = p->iLimit;
123236
- pPrior->iOffset = p->iOffset;
123237
- pPrior->pLimit = p->pLimit;
123238
- explainSetInteger(iSub1, pParse->iNextSelectId);
123239
- rc = sqlite3Select(pParse, pPrior, &dest);
123240
- p->pLimit = 0;
123241
- if( rc ){
123242
- goto multi_select_end;
123243
- }
123244
- p->pPrior = 0;
123245
- p->iLimit = pPrior->iLimit;
123246
- p->iOffset = pPrior->iOffset;
123247
- if( p->iLimit ){
123248
- addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
123249
- VdbeComment((v, "Jump ahead if LIMIT reached"));
123250
- if( p->iOffset ){
123251
- sqlite3VdbeAddOp3(v, OP_OffsetLimit,
123252
- p->iLimit, p->iOffset+1, p->iOffset);
123253
- }
123254
- }
123255
- explainSetInteger(iSub2, pParse->iNextSelectId);
123256
- rc = sqlite3Select(pParse, p, &dest);
123257
- testcase( rc!=SQLITE_OK );
123258
- pDelete = p->pPrior;
123259
- p->pPrior = pPrior;
123260
- p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123261
- if( pPrior->pLimit
123262
- && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
123263
- && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
123264
- ){
123265
- p->nSelectRow = sqlite3LogEst((u64)nLimit);
123266
- }
123267
- if( addr ){
123268
- sqlite3VdbeJumpHere(v, addr);
123269
- }
123270
- break;
123271
- }
123272
- case TK_EXCEPT:
123273
- case TK_UNION: {
123274
- int unionTab; /* Cursor number of the temporary table holding result */
123275
- u8 op = 0; /* One of the SRT_ operations to apply to self */
123276
- int priorOp; /* The SRT_ operation to apply to prior selects */
123277
- Expr *pLimit; /* Saved values of p->nLimit */
123278
- int addr;
123279
- SelectDest uniondest;
123280
-
123281
- testcase( p->op==TK_EXCEPT );
123282
- testcase( p->op==TK_UNION );
123283
- priorOp = SRT_Union;
123284
- if( dest.eDest==priorOp ){
123285
- /* We can reuse a temporary table generated by a SELECT to our
123286
- ** right.
123287
- */
123288
- assert( p->pLimit==0 ); /* Not allowed on leftward elements */
123289
- unionTab = dest.iSDParm;
123290
- }else{
123291
- /* We will need to create our own temporary table to hold the
123292
- ** intermediate results.
123293
- */
123294
- unionTab = pParse->nTab++;
123295
- assert( p->pOrderBy==0 );
123296
- addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
123297
- assert( p->addrOpenEphm[0] == -1 );
123298
- p->addrOpenEphm[0] = addr;
123299
- findRightmost(p)->selFlags |= SF_UsesEphemeral;
123300
- assert( p->pEList );
123301
- }
123302
-
123303
- /* Code the SELECT statements to our left
123304
- */
123305
- assert( !pPrior->pOrderBy );
123306
- sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
123307
- explainSetInteger(iSub1, pParse->iNextSelectId);
123308
- rc = sqlite3Select(pParse, pPrior, &uniondest);
123309
- if( rc ){
123310
- goto multi_select_end;
123311
- }
123312
-
123313
- /* Code the current SELECT statement
123314
- */
123315
- if( p->op==TK_EXCEPT ){
123316
- op = SRT_Except;
123317
- }else{
123318
- assert( p->op==TK_UNION );
123319
- op = SRT_Union;
123320
- }
123321
- p->pPrior = 0;
123322
- pLimit = p->pLimit;
123323
- p->pLimit = 0;
123324
- uniondest.eDest = op;
123325
- explainSetInteger(iSub2, pParse->iNextSelectId);
123326
- rc = sqlite3Select(pParse, p, &uniondest);
123327
- testcase( rc!=SQLITE_OK );
123328
- /* Query flattening in sqlite3Select() might refill p->pOrderBy.
123329
- ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
123330
- sqlite3ExprListDelete(db, p->pOrderBy);
123331
- pDelete = p->pPrior;
123332
- p->pPrior = pPrior;
123333
- p->pOrderBy = 0;
123334
- if( p->op==TK_UNION ){
123335
- p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123336
- }
123337
- sqlite3ExprDelete(db, p->pLimit);
123338
- p->pLimit = pLimit;
123339
- p->iLimit = 0;
123340
- p->iOffset = 0;
123341
-
123342
- /* Convert the data in the temporary table into whatever form
123343
- ** it is that we currently need.
123344
- */
123345
- assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
123346
- if( dest.eDest!=priorOp ){
123347
- int iCont, iBreak, iStart;
123348
- assert( p->pEList );
123349
- iBreak = sqlite3VdbeMakeLabel(v);
123350
- iCont = sqlite3VdbeMakeLabel(v);
123351
- computeLimitRegisters(pParse, p, iBreak);
123352
- sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
123353
- iStart = sqlite3VdbeCurrentAddr(v);
123354
- selectInnerLoop(pParse, p, unionTab,
123355
- 0, 0, &dest, iCont, iBreak);
123356
- sqlite3VdbeResolveLabel(v, iCont);
123357
- sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
123358
- sqlite3VdbeResolveLabel(v, iBreak);
123359
- sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
123360
- }
123361
- break;
123362
- }
123363
- default: assert( p->op==TK_INTERSECT ); {
123364
- int tab1, tab2;
123365
- int iCont, iBreak, iStart;
123366
- Expr *pLimit;
123367
- int addr;
123368
- SelectDest intersectdest;
123369
- int r1;
123370
-
123371
- /* INTERSECT is different from the others since it requires
123372
- ** two temporary tables. Hence it has its own case. Begin
123373
- ** by allocating the tables we will need.
123374
- */
123375
- tab1 = pParse->nTab++;
123376
- tab2 = pParse->nTab++;
123377
- assert( p->pOrderBy==0 );
123378
-
123379
- addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
123380
- assert( p->addrOpenEphm[0] == -1 );
123381
- p->addrOpenEphm[0] = addr;
123382
- findRightmost(p)->selFlags |= SF_UsesEphemeral;
123383
- assert( p->pEList );
123384
-
123385
- /* Code the SELECTs to our left into temporary table "tab1".
123386
- */
123387
- sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
123388
- explainSetInteger(iSub1, pParse->iNextSelectId);
123389
- rc = sqlite3Select(pParse, pPrior, &intersectdest);
123390
- if( rc ){
123391
- goto multi_select_end;
123392
- }
123393
-
123394
- /* Code the current SELECT into temporary table "tab2"
123395
- */
123396
- addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
123397
- assert( p->addrOpenEphm[1] == -1 );
123398
- p->addrOpenEphm[1] = addr;
123399
- p->pPrior = 0;
123400
- pLimit = p->pLimit;
123401
- p->pLimit = 0;
123402
- intersectdest.iSDParm = tab2;
123403
- explainSetInteger(iSub2, pParse->iNextSelectId);
123404
- rc = sqlite3Select(pParse, p, &intersectdest);
123405
- testcase( rc!=SQLITE_OK );
123406
- pDelete = p->pPrior;
123407
- p->pPrior = pPrior;
123408
- if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
123409
- sqlite3ExprDelete(db, p->pLimit);
123410
- p->pLimit = pLimit;
123411
-
123412
- /* Generate code to take the intersection of the two temporary
123413
- ** tables.
123414
- */
123415
- assert( p->pEList );
123416
- iBreak = sqlite3VdbeMakeLabel(v);
123417
- iCont = sqlite3VdbeMakeLabel(v);
123418
- computeLimitRegisters(pParse, p, iBreak);
123419
- sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
123420
- r1 = sqlite3GetTempReg(pParse);
123421
- iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
123422
- sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
123423
- sqlite3ReleaseTempReg(pParse, r1);
123424
- selectInnerLoop(pParse, p, tab1,
123425
- 0, 0, &dest, iCont, iBreak);
123426
- sqlite3VdbeResolveLabel(v, iCont);
123427
- sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
123428
- sqlite3VdbeResolveLabel(v, iBreak);
123429
- sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
123430
- sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
123431
- break;
123432
- }
123433
- }
123434
-
123435
- explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
123436
-
123398
+ }else{
123399
+
123400
+#ifndef SQLITE_OMIT_EXPLAIN
123401
+ if( pPrior->pPrior==0 ){
123402
+ ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
123403
+ ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
123404
+ }
123405
+#endif
123406
+
123407
+ /* Generate code for the left and right SELECT statements.
123408
+ */
123409
+ switch( p->op ){
123410
+ case TK_ALL: {
123411
+ int addr = 0;
123412
+ int nLimit;
123413
+ assert( !pPrior->pLimit );
123414
+ pPrior->iLimit = p->iLimit;
123415
+ pPrior->iOffset = p->iOffset;
123416
+ pPrior->pLimit = p->pLimit;
123417
+ rc = sqlite3Select(pParse, pPrior, &dest);
123418
+ p->pLimit = 0;
123419
+ if( rc ){
123420
+ goto multi_select_end;
123421
+ }
123422
+ p->pPrior = 0;
123423
+ p->iLimit = pPrior->iLimit;
123424
+ p->iOffset = pPrior->iOffset;
123425
+ if( p->iLimit ){
123426
+ addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
123427
+ VdbeComment((v, "Jump ahead if LIMIT reached"));
123428
+ if( p->iOffset ){
123429
+ sqlite3VdbeAddOp3(v, OP_OffsetLimit,
123430
+ p->iLimit, p->iOffset+1, p->iOffset);
123431
+ }
123432
+ }
123433
+ ExplainQueryPlan((pParse, 1, "UNION ALL"));
123434
+ rc = sqlite3Select(pParse, p, &dest);
123435
+ testcase( rc!=SQLITE_OK );
123436
+ pDelete = p->pPrior;
123437
+ p->pPrior = pPrior;
123438
+ p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123439
+ if( pPrior->pLimit
123440
+ && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
123441
+ && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
123442
+ ){
123443
+ p->nSelectRow = sqlite3LogEst((u64)nLimit);
123444
+ }
123445
+ if( addr ){
123446
+ sqlite3VdbeJumpHere(v, addr);
123447
+ }
123448
+ break;
123449
+ }
123450
+ case TK_EXCEPT:
123451
+ case TK_UNION: {
123452
+ int unionTab; /* Cursor number of the temp table holding result */
123453
+ u8 op = 0; /* One of the SRT_ operations to apply to self */
123454
+ int priorOp; /* The SRT_ operation to apply to prior selects */
123455
+ Expr *pLimit; /* Saved values of p->nLimit */
123456
+ int addr;
123457
+ SelectDest uniondest;
123458
+
123459
+ testcase( p->op==TK_EXCEPT );
123460
+ testcase( p->op==TK_UNION );
123461
+ priorOp = SRT_Union;
123462
+ if( dest.eDest==priorOp ){
123463
+ /* We can reuse a temporary table generated by a SELECT to our
123464
+ ** right.
123465
+ */
123466
+ assert( p->pLimit==0 ); /* Not allowed on leftward elements */
123467
+ unionTab = dest.iSDParm;
123468
+ }else{
123469
+ /* We will need to create our own temporary table to hold the
123470
+ ** intermediate results.
123471
+ */
123472
+ unionTab = pParse->nTab++;
123473
+ assert( p->pOrderBy==0 );
123474
+ addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
123475
+ assert( p->addrOpenEphm[0] == -1 );
123476
+ p->addrOpenEphm[0] = addr;
123477
+ findRightmost(p)->selFlags |= SF_UsesEphemeral;
123478
+ assert( p->pEList );
123479
+ }
123480
+
123481
+ /* Code the SELECT statements to our left
123482
+ */
123483
+ assert( !pPrior->pOrderBy );
123484
+ sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
123485
+ rc = sqlite3Select(pParse, pPrior, &uniondest);
123486
+ if( rc ){
123487
+ goto multi_select_end;
123488
+ }
123489
+
123490
+ /* Code the current SELECT statement
123491
+ */
123492
+ if( p->op==TK_EXCEPT ){
123493
+ op = SRT_Except;
123494
+ }else{
123495
+ assert( p->op==TK_UNION );
123496
+ op = SRT_Union;
123497
+ }
123498
+ p->pPrior = 0;
123499
+ pLimit = p->pLimit;
123500
+ p->pLimit = 0;
123501
+ uniondest.eDest = op;
123502
+ ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
123503
+ selectOpName(p->op)));
123504
+ rc = sqlite3Select(pParse, p, &uniondest);
123505
+ testcase( rc!=SQLITE_OK );
123506
+ /* Query flattening in sqlite3Select() might refill p->pOrderBy.
123507
+ ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
123508
+ sqlite3ExprListDelete(db, p->pOrderBy);
123509
+ pDelete = p->pPrior;
123510
+ p->pPrior = pPrior;
123511
+ p->pOrderBy = 0;
123512
+ if( p->op==TK_UNION ){
123513
+ p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123514
+ }
123515
+ sqlite3ExprDelete(db, p->pLimit);
123516
+ p->pLimit = pLimit;
123517
+ p->iLimit = 0;
123518
+ p->iOffset = 0;
123519
+
123520
+ /* Convert the data in the temporary table into whatever form
123521
+ ** it is that we currently need.
123522
+ */
123523
+ assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
123524
+ if( dest.eDest!=priorOp ){
123525
+ int iCont, iBreak, iStart;
123526
+ assert( p->pEList );
123527
+ iBreak = sqlite3VdbeMakeLabel(v);
123528
+ iCont = sqlite3VdbeMakeLabel(v);
123529
+ computeLimitRegisters(pParse, p, iBreak);
123530
+ sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
123531
+ iStart = sqlite3VdbeCurrentAddr(v);
123532
+ selectInnerLoop(pParse, p, unionTab,
123533
+ 0, 0, &dest, iCont, iBreak);
123534
+ sqlite3VdbeResolveLabel(v, iCont);
123535
+ sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
123536
+ sqlite3VdbeResolveLabel(v, iBreak);
123537
+ sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
123538
+ }
123539
+ break;
123540
+ }
123541
+ default: assert( p->op==TK_INTERSECT ); {
123542
+ int tab1, tab2;
123543
+ int iCont, iBreak, iStart;
123544
+ Expr *pLimit;
123545
+ int addr;
123546
+ SelectDest intersectdest;
123547
+ int r1;
123548
+
123549
+ /* INTERSECT is different from the others since it requires
123550
+ ** two temporary tables. Hence it has its own case. Begin
123551
+ ** by allocating the tables we will need.
123552
+ */
123553
+ tab1 = pParse->nTab++;
123554
+ tab2 = pParse->nTab++;
123555
+ assert( p->pOrderBy==0 );
123556
+
123557
+ addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
123558
+ assert( p->addrOpenEphm[0] == -1 );
123559
+ p->addrOpenEphm[0] = addr;
123560
+ findRightmost(p)->selFlags |= SF_UsesEphemeral;
123561
+ assert( p->pEList );
123562
+
123563
+ /* Code the SELECTs to our left into temporary table "tab1".
123564
+ */
123565
+ sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
123566
+ rc = sqlite3Select(pParse, pPrior, &intersectdest);
123567
+ if( rc ){
123568
+ goto multi_select_end;
123569
+ }
123570
+
123571
+ /* Code the current SELECT into temporary table "tab2"
123572
+ */
123573
+ addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
123574
+ assert( p->addrOpenEphm[1] == -1 );
123575
+ p->addrOpenEphm[1] = addr;
123576
+ p->pPrior = 0;
123577
+ pLimit = p->pLimit;
123578
+ p->pLimit = 0;
123579
+ intersectdest.iSDParm = tab2;
123580
+ ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
123581
+ selectOpName(p->op)));
123582
+ rc = sqlite3Select(pParse, p, &intersectdest);
123583
+ testcase( rc!=SQLITE_OK );
123584
+ pDelete = p->pPrior;
123585
+ p->pPrior = pPrior;
123586
+ if( p->nSelectRow>pPrior->nSelectRow ){
123587
+ p->nSelectRow = pPrior->nSelectRow;
123588
+ }
123589
+ sqlite3ExprDelete(db, p->pLimit);
123590
+ p->pLimit = pLimit;
123591
+
123592
+ /* Generate code to take the intersection of the two temporary
123593
+ ** tables.
123594
+ */
123595
+ assert( p->pEList );
123596
+ iBreak = sqlite3VdbeMakeLabel(v);
123597
+ iCont = sqlite3VdbeMakeLabel(v);
123598
+ computeLimitRegisters(pParse, p, iBreak);
123599
+ sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
123600
+ r1 = sqlite3GetTempReg(pParse);
123601
+ iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
123602
+ sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
123603
+ VdbeCoverage(v);
123604
+ sqlite3ReleaseTempReg(pParse, r1);
123605
+ selectInnerLoop(pParse, p, tab1,
123606
+ 0, 0, &dest, iCont, iBreak);
123607
+ sqlite3VdbeResolveLabel(v, iCont);
123608
+ sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
123609
+ sqlite3VdbeResolveLabel(v, iBreak);
123610
+ sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
123611
+ sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
123612
+ break;
123613
+ }
123614
+ }
123615
+
123616
+ #ifndef SQLITE_OMIT_EXPLAIN
123617
+ if( p->pNext==0 ){
123618
+ ExplainQueryPlanPop(pParse);
123619
+ }
123620
+ #endif
123621
+ }
123622
+
123437123623
/* Compute collating sequences used by
123438123624
** temporary tables needed to implement the compound select.
123439123625
** Attach the KeyInfo structure to all temporary tables.
123440123626
**
123441123627
** This section is run by the right-most SELECT statement only.
@@ -123769,14 +123955,10 @@
123769123955
KeyInfo *pKeyMerge; /* Comparison information for merging rows */
123770123956
sqlite3 *db; /* Database connection */
123771123957
ExprList *pOrderBy; /* The ORDER BY clause */
123772123958
int nOrderBy; /* Number of terms in the ORDER BY clause */
123773123959
int *aPermute; /* Mapping from ORDER BY terms to result set columns */
123774
-#ifndef SQLITE_OMIT_EXPLAIN
123775
- int iSub1; /* EQP id of left-hand query */
123776
- int iSub2; /* EQP id of right-hand query */
123777
-#endif
123778123960
123779123961
assert( p->pOrderBy!=0 );
123780123962
assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
123781123963
db = pParse->db;
123782123964
v = pParse->pVdbe;
@@ -123892,18 +124074,20 @@
123892124074
regOutA = ++pParse->nMem;
123893124075
regOutB = ++pParse->nMem;
123894124076
sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
123895124077
sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
123896124078
124079
+ ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
124080
+
123897124081
/* Generate a coroutine to evaluate the SELECT statement to the
123898124082
** left of the compound operator - the "A" select.
123899124083
*/
123900124084
addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
123901124085
addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
123902124086
VdbeComment((v, "left SELECT"));
123903124087
pPrior->iLimit = regLimitA;
123904
- explainSetInteger(iSub1, pParse->iNextSelectId);
124088
+ ExplainQueryPlan((pParse, 1, "LEFT"));
123905124089
sqlite3Select(pParse, pPrior, &destA);
123906124090
sqlite3VdbeEndCoroutine(v, regAddrA);
123907124091
sqlite3VdbeJumpHere(v, addr1);
123908124092
123909124093
/* Generate a coroutine to evaluate the SELECT statement on
@@ -123914,11 +124098,11 @@
123914124098
VdbeComment((v, "right SELECT"));
123915124099
savedLimit = p->iLimit;
123916124100
savedOffset = p->iOffset;
123917124101
p->iLimit = regLimitB;
123918124102
p->iOffset = 0;
123919
- explainSetInteger(iSub2, pParse->iNextSelectId);
124103
+ ExplainQueryPlan((pParse, 1, "RIGHT"));
123920124104
sqlite3Select(pParse, p, &destB);
123921124105
p->iLimit = savedLimit;
123922124106
p->iOffset = savedOffset;
123923124107
sqlite3VdbeEndCoroutine(v, regAddrB);
123924124108
@@ -124026,11 +124210,11 @@
124026124210
p->pPrior = pPrior;
124027124211
pPrior->pNext = p;
124028124212
124029124213
/*** TBD: Insert subroutine calls to close cursors on incomplete
124030124214
**** subqueries ****/
124031
- explainComposite(pParse, p->op, iSub1, iSub2, 0);
124215
+ ExplainQueryPlanPop(pParse);
124032124216
return pParse->nErr!=0;
124033124217
}
124034124218
#endif
124035124219
124036124220
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
@@ -125814,18 +125998,15 @@
125814125998
Table *pTab, /* Table being queried */
125815125999
Index *pIdx /* Index used to optimize scan, or NULL */
125816126000
){
125817126001
if( pParse->explain==2 ){
125818126002
int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
125819
- char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
126003
+ sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
125820126004
pTab->zName,
125821126005
bCover ? " USING COVERING INDEX " : "",
125822126006
bCover ? pIdx->zName : ""
125823126007
);
125824
- sqlite3VdbeAddOp4(
125825
- pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
125826
- );
125827126008
}
125828126009
}
125829126010
#else
125830126011
# define explainSimpleCount(a,b,c)
125831126012
#endif
@@ -126034,26 +126215,19 @@
126034126215
int iEnd; /* Address of the end of the query */
126035126216
sqlite3 *db; /* The database connection */
126036126217
ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
126037126218
u8 minMaxFlag; /* Flag for min/max queries */
126038126219
126039
-#ifndef SQLITE_OMIT_EXPLAIN
126040
- int iRestoreSelectId = pParse->iSelectId;
126041
- pParse->iSelectId = pParse->iNextSelectId++;
126042
-#endif
126043
-
126044126220
db = pParse->db;
126221
+ v = sqlite3GetVdbe(pParse);
126045126222
if( p==0 || db->mallocFailed || pParse->nErr ){
126046126223
return 1;
126047126224
}
126048126225
if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
126049126226
memset(&sAggInfo, 0, sizeof(sAggInfo));
126050126227
#if SELECTTRACE_ENABLED
126051
-#ifndef SQLITE_OMIT_EXPLAIN
126052
- p->iSelectId = pParse->iSelectId;
126053
-#endif
126054
- SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->iSelectId));
126228
+ SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
126055126229
if( sqlite3SelectTrace & 0x100 ){
126056126230
sqlite3TreeViewSelect(0, p, 0);
126057126231
}
126058126232
#endif
126059126233
@@ -126086,14 +126260,10 @@
126086126260
SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
126087126261
sqlite3TreeViewSelect(0, p, 0);
126088126262
}
126089126263
#endif
126090126264
126091
- /* Get a pointer the VDBE under construction, allocating a new VDBE if one
126092
- ** does not already exist */
126093
- v = sqlite3GetVdbe(pParse);
126094
- if( v==0 ) goto select_end;
126095126265
if( pDest->eDest==SRT_Output ){
126096126266
generateColumnNames(pParse, p);
126097126267
}
126098126268
126099126269
/* Try to various optimizations (flattening subqueries, and strength
@@ -126184,15 +126354,15 @@
126184126354
*/
126185126355
if( p->pPrior ){
126186126356
rc = multiSelect(pParse, p, pDest);
126187126357
#if SELECTTRACE_ENABLED
126188126358
SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
126189
- if( pParse->iSelectId==0 && (sqlite3SelectTrace & 0x2000)!=0 ){
126359
+ if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
126190126360
sqlite3TreeViewSelect(0, p, 0);
126191126361
}
126192126362
#endif
126193
- explainSetInteger(pParse->iSelectId, iRestoreSelectId);
126363
+ if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
126194126364
return rc;
126195126365
}
126196126366
#endif
126197126367
126198126368
/* For each term in the FROM clause, do two things:
@@ -126300,11 +126470,11 @@
126300126470
pItem->regReturn = ++pParse->nMem;
126301126471
sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
126302126472
VdbeComment((v, "%s", pItem->pTab->zName));
126303126473
pItem->addrFillSub = addrTop;
126304126474
sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
126305
- explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
126475
+ ExplainQueryPlan((pParse, 1, "CO-ROUTINE 0x%p", pSub));
126306126476
sqlite3Select(pParse, pSub, &dest);
126307126477
pItem->pTab->nRowLogEst = pSub->nSelectRow;
126308126478
pItem->fg.viaCoroutine = 1;
126309126479
pItem->regResult = dest.iSdst;
126310126480
sqlite3VdbeEndCoroutine(v, pItem->regReturn);
@@ -126335,16 +126505,15 @@
126335126505
VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
126336126506
}
126337126507
pPrior = isSelfJoinView(pTabList, pItem);
126338126508
if( pPrior ){
126339126509
sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
126340
- explainSetInteger(pItem->iSelectId, pPrior->iSelectId);
126341126510
assert( pPrior->pSelect!=0 );
126342126511
pSub->nSelectRow = pPrior->pSelect->nSelectRow;
126343126512
}else{
126344126513
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
126345
- explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
126514
+ ExplainQueryPlan((pParse, 1, "MATERIALIZE 0x%p", pSub));
126346126515
sqlite3Select(pParse, pSub, &dest);
126347126516
}
126348126517
pItem->pTab->nRowLogEst = pSub->nSelectRow;
126349126518
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
126350126519
retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
@@ -126978,15 +127147,15 @@
126978127147
sqlite3ExprListDelete(db, pMinMaxOrderBy);
126979127148
sqlite3DbFree(db, sAggInfo.aCol);
126980127149
sqlite3DbFree(db, sAggInfo.aFunc);
126981127150
#if SELECTTRACE_ENABLED
126982127151
SELECTTRACE(0x1,pParse,p,("end processing\n"));
126983
- if( pParse->iSelectId==0 && (sqlite3SelectTrace & 0x2000)!=0 ){
127152
+ if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
126984127153
sqlite3TreeViewSelect(0, p, 0);
126985127154
}
126986127155
#endif
126987
- explainSetInteger(pParse->iSelectId, iRestoreSelectId);
127156
+ ExplainQueryPlanPop(pParse);
126988127157
return rc;
126989127158
}
126990127159
126991127160
/************** End of select.c **********************************************/
126992127161
/************** Begin file table.c *******************************************/
@@ -129574,12 +129743,18 @@
129574129743
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
129575129744
if( rc!=SQLITE_OK ) return rc;
129576129745
while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
129577129746
const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
129578129747
assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
129579
- assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
129580
- if( zSubSql && zSubSql[0]!='S' ){
129748
+ /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
129749
+ ** or INSERT. Historically there have been attacks that first
129750
+ ** corrupt the sqlite_master.sql field with other kinds of statements
129751
+ ** then run VACUUM to get those statements to execute at inappropriate
129752
+ ** times. */
129753
+ if( zSubSql
129754
+ && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
129755
+ ){
129581129756
rc = execSql(db, pzErrMsg, zSubSql);
129582129757
if( rc!=SQLITE_OK ) break;
129583129758
}
129584129759
}
129585129760
assert( rc!=SQLITE_ROW );
@@ -129788,11 +129963,11 @@
129788129963
zDbMain
129789129964
);
129790129965
if( rc!=SQLITE_OK ) goto end_of_vacuum;
129791129966
rc = execSqlF(db, pzErrMsg,
129792129967
"SELECT sql FROM \"%w\".sqlite_master"
129793
- " WHERE type='index' AND length(sql)>10",
129968
+ " WHERE type='index'",
129794129969
zDbMain
129795129970
);
129796129971
if( rc!=SQLITE_OK ) goto end_of_vacuum;
129797129972
db->init.iDb = 0;
129798129973
@@ -131852,11 +132027,10 @@
131852132027
#endif
131853132028
{
131854132029
struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
131855132030
Vdbe *v = pParse->pVdbe; /* VM being constructed */
131856132031
sqlite3 *db = pParse->db; /* Database handle */
131857
- int iId = pParse->iSelectId; /* Select id (left-most output column) */
131858132032
int isSearch; /* True for a SEARCH. False for SCAN. */
131859132033
WhereLoop *pLoop; /* The controlling WhereLoop object */
131860132034
u32 flags; /* Flags that describe this loop */
131861132035
char *zMsg; /* Text to add to EQP output */
131862132036
StrAccum str; /* EQP output string */
@@ -131871,11 +132045,11 @@
131871132045
|| (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
131872132046
131873132047
sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
131874132048
sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
131875132049
if( pItem->pSelect ){
131876
- sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
132050
+ sqlite3XPrintf(&str, " SUBQUERY 0x%p", pItem->pSelect);
131877132051
}else{
131878132052
sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
131879132053
}
131880132054
131881132055
if( pItem->zAlias ){
@@ -131932,11 +132106,12 @@
131932132106
}else{
131933132107
sqlite3StrAccumAppend(&str, " (~1 row)", 9);
131934132108
}
131935132109
#endif
131936132110
zMsg = sqlite3StrAccumFinish(&str);
131937
- ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
132111
+ ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
132112
+ pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
131938132113
}
131939132114
return ret;
131940132115
}
131941132116
#endif /* SQLITE_OMIT_EXPLAIN */
131942132117
@@ -133652,10 +133827,11 @@
133652133827
/* Run a separate WHERE clause for each term of the OR clause. After
133653133828
** eliminating duplicates from other WHERE clauses, the action for each
133654133829
** sub-WHERE clause is to to invoke the main loop body as a subroutine.
133655133830
*/
133656133831
wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
133832
+ ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
133657133833
for(ii=0; ii<pOrWc->nTerm; ii++){
133658133834
WhereTerm *pOrTerm = &pOrWc->a[ii];
133659133835
if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
133660133836
WhereInfo *pSubWInfo; /* Info for single OR-term scan */
133661133837
Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -133772,10 +133948,11 @@
133772133948
/* Finish the loop through table entries that match term pOrTerm. */
133773133949
sqlite3WhereEnd(pSubWInfo);
133774133950
}
133775133951
}
133776133952
}
133953
+ ExplainQueryPlanPop(pParse);
133777133954
pLevel->u.pCovidx = pCov;
133778133955
if( pCov ) pLevel->iIdxCur = iCovCur;
133779133956
if( pAndExpr ){
133780133957
pAndExpr->pLeft = 0;
133781133958
sqlite3ExprDelete(db, pAndExpr);
@@ -140079,10 +140256,11 @@
140079140256
if( nTabList==0 ){
140080140257
if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
140081140258
if( wctrlFlags & WHERE_WANT_DISTINCT ){
140082140259
pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
140083140260
}
140261
+ ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
140084140262
}else{
140085140263
/* Assign a bit from the bitmask to every term in the FROM clause.
140086140264
**
140087140265
** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
140088140266
**
@@ -146939,10 +147117,11 @@
146939147117
{ SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
146940147118
{ SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
146941147119
{ SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
146942147120
{ SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
146943147121
{ SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
147122
+ { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
146944147123
};
146945147124
unsigned int i;
146946147125
rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
146947147126
for(i=0; i<ArraySize(aFlagOp); i++){
146948147127
if( aFlagOp[i].op==op ){
@@ -151832,11 +152011,11 @@
151832152011
SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
151833152012
char **, int, int, int, const char *, int, Fts3Expr **, char **
151834152013
);
151835152014
SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
151836152015
#ifdef SQLITE_TEST
151837
-SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
152016
+SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
151838152017
SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
151839152018
#endif
151840152019
151841152020
SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
151842152021
sqlite3_tokenizer_cursor **
@@ -155542,11 +155721,11 @@
155542155721
}
155543155722
}
155544155723
155545155724
#ifdef SQLITE_TEST
155546155725
if( rc==SQLITE_OK ){
155547
- rc = sqlite3Fts3ExprInitTestInterface(db);
155726
+ rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
155548155727
}
155549155728
#endif
155550155729
155551155730
/* Create the virtual table wrapper around the hash-table and overload
155552155731
** the four scalar functions. If this is successful, register the
@@ -159202,38 +159381,10 @@
159202159381
159203159382
#ifdef SQLITE_TEST
159204159383
159205159384
/* #include <stdio.h> */
159206159385
159207
-/*
159208
-** Function to query the hash-table of tokenizers (see README.tokenizers).
159209
-*/
159210
-static int queryTestTokenizer(
159211
- sqlite3 *db,
159212
- const char *zName,
159213
- const sqlite3_tokenizer_module **pp
159214
-){
159215
- int rc;
159216
- sqlite3_stmt *pStmt;
159217
- const char zSql[] = "SELECT fts3_tokenizer(?)";
159218
-
159219
- *pp = 0;
159220
- rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
159221
- if( rc!=SQLITE_OK ){
159222
- return rc;
159223
- }
159224
-
159225
- sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
159226
- if( SQLITE_ROW==sqlite3_step(pStmt) ){
159227
- if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
159228
- memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
159229
- }
159230
- }
159231
-
159232
- return sqlite3_finalize(pStmt);
159233
-}
159234
-
159235159386
/*
159236159387
** Return a pointer to a buffer containing a text representation of the
159237159388
** expression passed as the first argument. The buffer is obtained from
159238159389
** sqlite3_malloc(). It is the responsibility of the caller to use
159239159390
** sqlite3_free() to release the memory. If an OOM condition is encountered,
@@ -159297,51 +159448,47 @@
159297159448
** of a column of the fts3 table that the query expression may refer to.
159298159449
** For example:
159299159450
**
159300159451
** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
159301159452
*/
159302
-static void fts3ExprTest(
159453
+static void fts3ExprTestCommon(
159454
+ int bRebalance,
159303159455
sqlite3_context *context,
159304159456
int argc,
159305159457
sqlite3_value **argv
159306159458
){
159307
- sqlite3_tokenizer_module const *pModule = 0;
159308159459
sqlite3_tokenizer *pTokenizer = 0;
159309159460
int rc;
159310159461
char **azCol = 0;
159311159462
const char *zExpr;
159312159463
int nExpr;
159313159464
int nCol;
159314159465
int ii;
159315159466
Fts3Expr *pExpr;
159316159467
char *zBuf = 0;
159317
- sqlite3 *db = sqlite3_context_db_handle(context);
159468
+ Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
159469
+ const char *zTokenizer = 0;
159470
+ char *zErr = 0;
159318159471
159319159472
if( argc<3 ){
159320159473
sqlite3_result_error(context,
159321159474
"Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
159322159475
);
159323159476
return;
159324159477
}
159325159478
159326
- rc = queryTestTokenizer(db,
159327
- (const char *)sqlite3_value_text(argv[0]), &pModule);
159328
- if( rc==SQLITE_NOMEM ){
159329
- sqlite3_result_error_nomem(context);
159330
- goto exprtest_out;
159331
- }else if( !pModule ){
159332
- sqlite3_result_error(context, "No such tokenizer module", -1);
159333
- goto exprtest_out;
159334
- }
159335
-
159336
- rc = pModule->xCreate(0, 0, &pTokenizer);
159337
- assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
159338
- if( rc==SQLITE_NOMEM ){
159339
- sqlite3_result_error_nomem(context);
159340
- goto exprtest_out;
159341
- }
159342
- pTokenizer->pModule = pModule;
159479
+ zTokenizer = (const char*)sqlite3_value_text(argv[0]);
159480
+ rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
159481
+ if( rc!=SQLITE_OK ){
159482
+ if( rc==SQLITE_NOMEM ){
159483
+ sqlite3_result_error_nomem(context);
159484
+ }else{
159485
+ sqlite3_result_error(context, zErr, -1);
159486
+ }
159487
+ sqlite3_free(zErr);
159488
+ return;
159489
+ }
159343159490
159344159491
zExpr = (const char *)sqlite3_value_text(argv[1]);
159345159492
nExpr = sqlite3_value_bytes(argv[1]);
159346159493
nCol = argc-2;
159347159494
azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
@@ -159351,11 +159498,11 @@
159351159498
}
159352159499
for(ii=0; ii<nCol; ii++){
159353159500
azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
159354159501
}
159355159502
159356
- if( sqlite3_user_data(context) ){
159503
+ if( bRebalance ){
159357159504
char *zDummy = 0;
159358159505
rc = sqlite3Fts3ExprParse(
159359159506
pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
159360159507
);
159361159508
assert( rc==SQLITE_OK || pExpr==0 );
@@ -159377,27 +159524,42 @@
159377159524
}
159378159525
159379159526
sqlite3Fts3ExprFree(pExpr);
159380159527
159381159528
exprtest_out:
159382
- if( pModule && pTokenizer ){
159383
- rc = pModule->xDestroy(pTokenizer);
159529
+ if( pTokenizer ){
159530
+ rc = pTokenizer->pModule->xDestroy(pTokenizer);
159384159531
}
159385159532
sqlite3_free(azCol);
159386159533
}
159534
+
159535
+static void fts3ExprTest(
159536
+ sqlite3_context *context,
159537
+ int argc,
159538
+ sqlite3_value **argv
159539
+){
159540
+ fts3ExprTestCommon(0, context, argc, argv);
159541
+}
159542
+static void fts3ExprTestRebalance(
159543
+ sqlite3_context *context,
159544
+ int argc,
159545
+ sqlite3_value **argv
159546
+){
159547
+ fts3ExprTestCommon(1, context, argc, argv);
159548
+}
159387159549
159388159550
/*
159389159551
** Register the query expression parser test function fts3_exprtest()
159390159552
** with database connection db.
159391159553
*/
159392
-SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
159554
+SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
159393159555
int rc = sqlite3_create_function(
159394
- db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
159556
+ db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
159395159557
);
159396159558
if( rc==SQLITE_OK ){
159397159559
rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
159398
- -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
159560
+ -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
159399159561
);
159400159562
}
159401159563
return rc;
159402159564
}
159403159565
@@ -175650,10 +175812,14 @@
175650175812
** Valid if STAGE==1. The current change-counter cookie value in the
175651175813
** target db file.
175652175814
**
175653175815
** RBU_STATE_OALSZ:
175654175816
** Valid if STAGE==1. The size in bytes of the *-oal file.
175817
+**
175818
+** RBU_STATE_DATATBL:
175819
+** Only valid if STAGE==1. The RBU database name of the table
175820
+** currently being read.
175655175821
*/
175656175822
#define RBU_STATE_STAGE 1
175657175823
#define RBU_STATE_TBL 2
175658175824
#define RBU_STATE_IDX 3
175659175825
#define RBU_STATE_ROW 4
@@ -175660,10 +175826,11 @@
175660175826
#define RBU_STATE_PROGRESS 5
175661175827
#define RBU_STATE_CKPT 6
175662175828
#define RBU_STATE_COOKIE 7
175663175829
#define RBU_STATE_OALSZ 8
175664175830
#define RBU_STATE_PHASEONESTEP 9
175831
+#define RBU_STATE_DATATBL 10
175665175832
175666175833
#define RBU_STAGE_OAL 1
175667175834
#define RBU_STAGE_MOVE 2
175668175835
#define RBU_STAGE_CAPTURE 3
175669175836
#define RBU_STAGE_CKPT 4
@@ -175702,10 +175869,11 @@
175702175869
** A structure to store values read from the rbu_state table in memory.
175703175870
*/
175704175871
struct RbuState {
175705175872
int eStage;
175706175873
char *zTbl;
175874
+ char *zDataTbl;
175707175875
char *zIdx;
175708175876
i64 iWalCksum;
175709175877
int nRow;
175710175878
i64 nProgress;
175711175879
u32 iCookie;
@@ -177765,10 +177933,11 @@
177765177933
** Free an RbuState object allocated by rbuLoadState().
177766177934
*/
177767177935
static void rbuFreeState(RbuState *p){
177768177936
if( p ){
177769177937
sqlite3_free(p->zTbl);
177938
+ sqlite3_free(p->zDataTbl);
177770177939
sqlite3_free(p->zIdx);
177771177940
sqlite3_free(p);
177772177941
}
177773177942
}
177774177943
@@ -177834,10 +178003,14 @@
177834178003
break;
177835178004
177836178005
case RBU_STATE_PHASEONESTEP:
177837178006
pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
177838178007
break;
178008
+
178009
+ case RBU_STATE_DATATBL:
178010
+ pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
178011
+ break;
177839178012
177840178013
default:
177841178014
rc = SQLITE_CORRUPT;
177842178015
break;
177843178016
}
@@ -178609,21 +178782,23 @@
178609178782
"(%d, %d), "
178610178783
"(%d, %d), "
178611178784
"(%d, %lld), "
178612178785
"(%d, %lld), "
178613178786
"(%d, %lld), "
178614
- "(%d, %lld) ",
178787
+ "(%d, %lld), "
178788
+ "(%d, %Q) ",
178615178789
p->zStateDb,
178616178790
RBU_STATE_STAGE, eStage,
178617178791
RBU_STATE_TBL, p->objiter.zTbl,
178618178792
RBU_STATE_IDX, p->objiter.zIdx,
178619178793
RBU_STATE_ROW, p->nStep,
178620178794
RBU_STATE_PROGRESS, p->nProgress,
178621178795
RBU_STATE_CKPT, p->iWalCksum,
178622178796
RBU_STATE_COOKIE, (i64)pFd->iCookie,
178623178797
RBU_STATE_OALSZ, p->iOalSz,
178624
- RBU_STATE_PHASEONESTEP, p->nPhaseOneStep
178798
+ RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
178799
+ RBU_STATE_DATATBL, p->objiter.zDataTbl
178625178800
)
178626178801
);
178627178802
assert( pInsert==0 || rc==SQLITE_OK );
178628178803
178629178804
if( rc==SQLITE_OK ){
@@ -178875,11 +179050,12 @@
178875179050
RbuObjIter *pIter = &p->objiter;
178876179051
int rc = SQLITE_OK;
178877179052
178878179053
while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
178879179054
|| rbuStrCompare(pIter->zIdx, pState->zIdx)
178880
- || rbuStrCompare(pIter->zTbl, pState->zTbl)
179055
+ || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
179056
+ || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
178881179057
)){
178882179058
rc = rbuObjIterNext(p, pIter);
178883179059
}
178884179060
178885179061
if( rc==SQLITE_OK && !pIter->zTbl ){
@@ -206725,11 +206901,11 @@
206725206901
int nArg, /* Number of args */
206726206902
sqlite3_value **apUnused /* Function arguments */
206727206903
){
206728206904
assert( nArg==0 );
206729206905
UNUSED_PARAM2(nArg, apUnused);
206730
- sqlite3_result_text(pCtx, "fts5: 2018-04-26 12:27:03 368c14da868a843767344f6cc17c499fddd83244c0510337ed9a918e64ee2413", -1, SQLITE_TRANSIENT);
206906
+ sqlite3_result_text(pCtx, "fts5: 2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda", -1, SQLITE_TRANSIENT);
206731206907
}
206732206908
206733206909
static int fts5Init(sqlite3 *db){
206734206910
static const sqlite3_module fts5Mod = {
206735206911
/* iVersion */ 2,
@@ -210995,12 +211171,12 @@
210995211171
}
210996211172
#endif /* SQLITE_CORE */
210997211173
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
210998211174
210999211175
/************** End of stmt.c ************************************************/
211000
-#if __LINE__!=211000
211176
+#if __LINE__!=211176
211001211177
#undef SQLITE_SOURCE_ID
211002
-#define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825alt2"
211178
+#define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba7alt2"
211003211179
#endif
211004211180
/* Return the source-id for this library */
211005211181
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
211006211182
/************************** End of sqlite3.c ******************************/
211007211183
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1150,11 +1150,11 @@
1150 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1151 ** [sqlite_version()] and [sqlite_source_id()].
1152 */
1153 #define SQLITE_VERSION "3.24.0"
1154 #define SQLITE_VERSION_NUMBER 3024000
1155 #define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825d83b"
1156
1157 /*
1158 ** CAPI3REF: Run-Time Library Version Numbers
1159 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1160 **
@@ -3137,10 +3137,25 @@
3137 ** or negative to leave the setting unchanged.
3138 ** The second parameter is a pointer to an integer into which is written
3139 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3140 ** it is not disabled, 1 if it is.
3141 ** </dd>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3142 ** </dl>
3143 */
3144 #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
3145 #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
3146 #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -3148,11 +3163,12 @@
3148 #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
3149 #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
3150 #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
3151 #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
3152 #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
3153 #define SQLITE_DBCONFIG_MAX 1008 /* Largest DBCONFIG */
 
3154
3155 /*
3156 ** CAPI3REF: Enable Or Disable Extended Result Codes
3157 ** METHOD: sqlite3
3158 **
@@ -6534,10 +6550,45 @@
6534 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
6535 ** or else the use of the [data_store_directory pragma] should be avoided.
6536 */
6537 SQLITE_API char *sqlite3_data_directory;
6538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6539 /*
6540 ** CAPI3REF: Test For Auto-Commit Mode
6541 ** KEYWORDS: {autocommit mode}
6542 ** METHOD: sqlite3
6543 **
@@ -14517,11 +14568,23 @@
14517 SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
14518 #else
14519 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
14520 # define sqlite3VdbeVerifyNoResultRow(A)
14521 #endif
14522 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
 
 
 
 
 
 
 
 
 
 
 
 
14523 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
14524 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
14525 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
14526 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
14527 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
@@ -15821,10 +15884,11 @@
15821 #define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
15822 #define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
15823 #define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
15824 #define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
15825 #define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
 
15826
15827 /* Flags used only if debugging */
15828 #ifdef SQLITE_DEBUG
15829 #define SQLITE_SqlTrace 0x08000000 /* Debug print SQL as it executes */
15830 #define SQLITE_VdbeListing 0x10000000 /* Debug listings of VDBE programs */
@@ -16923,13 +16987,10 @@
16923 unsigned isTabFunc :1; /* True if table-valued-function syntax */
16924 unsigned isCorrelated :1; /* True if sub-query is correlated */
16925 unsigned viaCoroutine :1; /* Implemented as a co-routine */
16926 unsigned isRecursive :1; /* True for recursive reference in WITH */
16927 } fg;
16928 #ifndef SQLITE_OMIT_EXPLAIN
16929 u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
16930 #endif
16931 int iCursor; /* The VDBE cursor number used to access this table */
16932 Expr *pOn; /* The ON clause of a join */
16933 IdList *pUsing; /* The USING clause of a join */
16934 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
16935 union {
@@ -17073,15 +17134,12 @@
17073
17074 /*
17075 ** An instance of the following structure contains all information
17076 ** needed to generate code for a single SELECT statement.
17077 **
17078 ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
17079 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
17080 ** limit and nOffset to the value of the offset (or 0 if there is not
17081 ** offset). But later on, nLimit and nOffset become the memory locations
17082 ** in the VDBE that record the limit and offset counters.
17083 **
17084 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
17085 ** These addresses must be stored so that we can go back and fill in
17086 ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
17087 ** the number of columns in P2 can be computed at the same time
@@ -17097,11 +17155,10 @@
17097 LogEst nSelectRow; /* Estimated number of result rows */
17098 u32 selFlags; /* Various SF_* values */
17099 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
17100 #if SELECTTRACE_ENABLED
17101 char zSelName[12]; /* Symbolic name of this SELECT use for debugging */
17102 u32 iSelectId; /* EXPLAIN QUERY PLAN select ID */
17103 #endif
17104 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
17105 SrcList *pSrc; /* The FROM clause */
17106 Expr *pWhere; /* The WHERE clause */
17107 ExprList *pGroupBy; /* The GROUP BY clause */
@@ -17138,12 +17195,11 @@
17138 #define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */
17139 #define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */
17140 #define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */
17141 #define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */
17142 #define SF_IncludeHidden 0x20000 /* Include hidden columns in output */
17143 #define SF_ComplexResult 0x40000 /* Result set contains subquery or function */
17144
17145
17146 /*
17147 ** The results of a SELECT can be distributed in several ways, as defined
17148 ** by one of the following macros. The "SRT" prefix means "SELECT Result
17149 ** Type".
@@ -17409,12 +17465,11 @@
17409 u8 declareVtab; /* True if inside sqlite3_declare_vtab() */
17410 int nVtabLock; /* Number of virtual tables to lock */
17411 #endif
17412 int nHeight; /* Expression tree height of current sub-select */
17413 #ifndef SQLITE_OMIT_EXPLAIN
17414 int iSelectId; /* ID of current select for EXPLAIN output */
17415 int iNextSelectId; /* Next available select ID for EXPLAIN output */
17416 #endif
17417 VList *pVList; /* Mapping between variable names and numbers */
17418 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
17419 const char *zTail; /* All SQL text past the last semicolon parsed */
17420 Table *pNewTable; /* A table being constructed by CREATE TABLE */
@@ -27718,14 +27773,14 @@
27718 sqlite3TreeViewPush(pView, 1);
27719 }
27720 do{
27721 #if SELECTTRACE_ENABLED
27722 sqlite3TreeViewLine(pView,
27723 "SELECT%s%s (%s/%d/%p) selFlags=0x%x nSelectRow=%d",
27724 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
27725 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
27726 p->zSelName, p->iSelectId, p, p->selFlags,
27727 (int)p->nSelectRow
27728 );
27729 #else
27730 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
27731 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
@@ -39693,26 +39748,10 @@
39693 */
39694 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
39695 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
39696 #endif
39697
39698 /*
39699 * The value used with sqlite3_win32_set_directory() to specify that
39700 * the data directory should be changed.
39701 */
39702 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
39703 # define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
39704 #endif
39705
39706 /*
39707 * The value used with sqlite3_win32_set_directory() to specify that
39708 * the temporary directory should be changed.
39709 */
39710 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
39711 # define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
39712 #endif
39713
39714 /*
39715 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
39716 * various Win32 API heap functions instead of our own.
39717 */
39718 #ifdef SQLITE_WIN32_MALLOC
@@ -41305,17 +41344,17 @@
41305 #endif
41306 return winUtf8ToMbcs(zText, useAnsi);
41307 }
41308
41309 /*
41310 ** This function sets the data directory or the temporary directory based on
41311 ** the provided arguments. The type argument must be 1 in order to set the
41312 ** data directory or 2 in order to set the temporary directory. The zValue
41313 ** argument is the name of the directory to use. The return value will be
41314 ** SQLITE_OK if successful.
41315 */
41316 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
 
 
 
41317 char **ppDirectory = 0;
41318 #ifndef SQLITE_OMIT_AUTOINIT
41319 int rc = sqlite3_initialize();
41320 if( rc ) return rc;
41321 #endif
@@ -41327,23 +41366,56 @@
41327 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
41328 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
41329 );
41330 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
41331 if( ppDirectory ){
41332 char *zValueUtf8 = 0;
41333 if( zValue && zValue[0] ){
41334 zValueUtf8 = winUnicodeToUtf8(zValue);
41335 if ( zValueUtf8==0 ){
41336 return SQLITE_NOMEM_BKPT;
41337 }
41338 }
41339 sqlite3_free(*ppDirectory);
41340 *ppDirectory = zValueUtf8;
41341 return SQLITE_OK;
41342 }
41343 return SQLITE_ERROR;
41344 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41345
41346 /*
41347 ** The return value of winGetLastErrorMsg
41348 ** is zero if the error message fits in the buffer, or non-zero
41349 ** otherwise (if the message was truncated).
@@ -64714,10 +64786,14 @@
64714 }
64715 }
64716 #else
64717 # define setDefaultSyncFlag(pBt,safety_level)
64718 #endif
 
 
 
 
64719
64720 /*
64721 ** Get a reference to pPage1 of the database file. This will
64722 ** also acquire a readlock on that file.
64723 **
@@ -64745,10 +64821,13 @@
64745 */
64746 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
64747 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
64748 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
64749 nPage = nPageFile;
 
 
 
64750 }
64751 if( nPage>0 ){
64752 u32 pageSize;
64753 u32 usableSize;
64754 u8 *page1 = pPage1->aData;
@@ -67962,11 +68041,13 @@
67962 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67963 pPage->xParseCell(pPage, pCell, pInfo);
67964 if( pInfo->nLocal==pInfo->nPayload ){
67965 return SQLITE_OK; /* No overflow pages. Return without doing anything */
67966 }
67967 if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
 
 
67968 /* Cell extends past end of page */
67969 return SQLITE_CORRUPT_PAGE(pPage);
67970 }
67971 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
67972 pBt = pPage->pBt;
@@ -74686,10 +74767,53 @@
74686 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
74687 if( p4copy ) memcpy(p4copy, zP4, 8);
74688 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
74689 }
74690
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74691 /*
74692 ** Add an OP_ParseSchema opcode. This routine is broken out from
74693 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
74694 ** as having been used.
74695 **
@@ -96622,15 +96746,12 @@
96622
96623 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
96624 if( colUsed==(MASKBIT(nExpr)-1) ){
96625 /* If we reach this point, that means the index pIdx is usable */
96626 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
96627 #ifndef SQLITE_OMIT_EXPLAIN
96628 sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
96629 sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
96630 P4_DYNAMIC);
96631 #endif
96632 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
96633 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
96634 VdbeComment((v, "%s", pIdx->zName));
96635 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
96636 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
@@ -96858,21 +96979,13 @@
96858 ** table allocated and opened above.
96859 */
96860 Select *pSelect = pExpr->x.pSelect;
96861 ExprList *pEList = pSelect->pEList;
96862
96863 #ifndef SQLITE_OMIT_EXPLAIN
96864 if( pParse->explain==2 ){
96865 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %sLIST SUBQUERY %d",
96866 jmpIfDynamic>=0?"":"CORRELATED ",
96867 pParse->iNextSelectId
96868 );
96869 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg,
96870 P4_DYNAMIC);
96871 }
96872 #endif
96873
96874 assert( !isRowid );
96875 /* If the LHS and RHS of the IN operator do not match, that
96876 ** error will have been caught long before we reach this point. */
96877 if( ALWAYS(pEList->nExpr==nVal) ){
96878 SelectDest dest;
@@ -96989,22 +97102,13 @@
96989 testcase( pExpr->op==TK_EXISTS );
96990 testcase( pExpr->op==TK_SELECT );
96991 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
96992 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
96993
96994 #ifndef SQLITE_OMIT_EXPLAIN
96995 if( pParse->explain==2 ){
96996 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %sSCALAR SUBQUERY %d",
96997 jmpIfDynamic>=0?"":"CORRELATED ",
96998 pParse->iNextSelectId
96999 );
97000 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg,
97001 P4_DYNAMIC);
97002 }
97003 #endif
97004
97005 pSel = pExpr->x.pSelect;
 
 
97006 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
97007 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
97008 pParse->nMem += nReg;
97009 if( pExpr->op==TK_SELECT ){
97010 dest.eDest = SRT_Mem;
@@ -97763,10 +97867,11 @@
97763 if( v==0 ){
97764 assert( pParse->db->mallocFailed );
97765 return 0;
97766 }
97767
 
97768 if( pExpr==0 ){
97769 op = TK_NULL;
97770 }else{
97771 op = pExpr->op;
97772 }
@@ -98223,11 +98328,12 @@
98223 return target;
98224 }
98225 case TK_SPAN:
98226 case TK_COLLATE:
98227 case TK_UPLUS: {
98228 return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 
98229 }
98230
98231 case TK_TRIGGER: {
98232 /* If the opcode is TK_TRIGGER, then the expression is a reference
98233 ** to a column in the new.* or old.* pseudo-tables available to
@@ -106427,11 +106533,15 @@
106427 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
106428 && db->init.busy==0
106429 #if SQLITE_USER_AUTHENTICATION
106430 && sqlite3UserAuthTable(pTab->zName)==0
106431 #endif
106432 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
 
 
 
 
106433 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
106434 goto exit_create_index;
106435 }
106436 #ifndef SQLITE_OMIT_VIEW
106437 if( pTab->pSelect ){
@@ -120002,10 +120112,13 @@
120002 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
120003 ** the possible values of meta[4].
120004 */
120005 for(i=0; i<ArraySize(meta); i++){
120006 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
 
 
 
120007 }
120008 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
120009
120010 /* If opening a non-empty database, check the text encoding. For the
120011 ** main database, set sqlite3.enc to the encoding of the main database.
@@ -120400,11 +120513,11 @@
120400
120401 #ifndef SQLITE_OMIT_EXPLAIN
120402 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
120403 static const char * const azColName[] = {
120404 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
120405 "selectid", "order", "from", "detail"
120406 };
120407 int iFirst, mx;
120408 if( sParse.explain==2 ){
120409 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
120410 iFirst = 8;
@@ -120714,11 +120827,11 @@
120714 */
120715 #if SELECTTRACE_ENABLED
120716 /***/ int sqlite3SelectTrace = 0;
120717 # define SELECTTRACE(K,P,S,X) \
120718 if(sqlite3SelectTrace&(K)) \
120719 sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->iSelectId,(S)),\
120720 sqlite3DebugPrintf X
120721 #else
120722 # define SELECTTRACE(K,P,S,X)
120723 #endif
120724
@@ -120771,10 +120884,11 @@
120771 Table *pTab; /* Table definition */
120772 int iCsr; /* Cursor number for table */
120773 int nKey; /* Number of PK columns for table pTab (>=1) */
120774 } aDefer[4];
120775 #endif
 
120776 };
120777 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
120778
120779 /*
120780 ** Delete all the content of a Select structure. Deallocate the structure
@@ -121228,10 +121342,66 @@
121228 Parse *pParse, /* Parsing context */
121229 ExprList *pList, /* Form the KeyInfo object from this ExprList */
121230 int iStart, /* Begin with this column of pList */
121231 int nExtra /* Add this many extra columns to the end */
121232 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121233
121234 /*
121235 ** Generate code that will push the record in registers regData
121236 ** through regData+nData-1 onto the sorter.
121237 */
@@ -121239,28 +121409,43 @@
121239 Parse *pParse, /* Parser context */
121240 SortCtx *pSort, /* Information about the ORDER BY clause */
121241 Select *pSelect, /* The whole SELECT statement */
121242 int regData, /* First register holding data to be sorted */
121243 int regOrigData, /* First register holding data before packing */
121244 int nData, /* Number of elements in the data array */
121245 int nPrefixReg /* No. of reg prior to regData available for use */
121246 ){
121247 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
121248 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
121249 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
121250 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
121251 int regBase; /* Regs for sorter record */
121252 int regRecord = ++pParse->nMem; /* Assembled sorter record */
121253 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
121254 int op; /* Opcode to add sorter record to sorter */
121255 int iLimit; /* LIMIT counter */
 
121256
121257 assert( bSeq==0 || bSeq==1 );
 
 
 
 
 
 
 
 
 
 
 
 
 
121258 assert( nData==1 || regData==regOrigData || regOrigData==0 );
 
121259 if( nPrefixReg ){
121260 assert( nPrefixReg==nExpr+bSeq );
121261 regBase = regData - nExpr - bSeq;
121262 }else{
121263 regBase = pParse->nMem + 1;
121264 pParse->nMem += nBase;
121265 }
121266 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
@@ -121280,11 +121465,11 @@
121280 int addrJmp; /* Address of the OP_Jump opcode */
121281 VdbeOp *pOp; /* Opcode that opens the sorter */
121282 int nKey; /* Number of sorting key columns, including OP_Sequence */
121283 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
121284
121285 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat,regRecord);
121286 regPrevKey = pParse->nMem+1;
121287 pParse->nMem += pSort->nOBSat;
121288 nKey = nExpr - pSort->nOBSat + bSeq;
121289 if( bSeq ){
121290 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
@@ -121331,29 +121516,33 @@
121331 ** pSort->bOrderedInnerLoop flag is set to indicate that the inner
121332 ** loop delivers items in sorted order, jump to the next iteration
121333 ** of the outer loop.
121334 */
121335 int iCsr = pSort->iECursor;
121336 int iJmp = sqlite3VdbeCurrentAddr(v)+5+(nOBSat<=0)+pSort->bOrderedInnerLoop;
121337 assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 );
121338 sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
121339 VdbeCoverage(v);
121340 sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
121341 sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp, regBase+nOBSat, nExpr-nOBSat);
 
121342 VdbeCoverage(v);
121343 sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
121344 }
121345 if( nOBSat<=0 ){
121346 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat,regRecord);
121347 }
121348 if( pSort->sortFlags & SORTFLAG_UseSorter ){
121349 op = OP_SorterInsert;
121350 }else{
121351 op = OP_IdxInsert;
121352 }
121353 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
121354 regBase+nOBSat, nBase-nOBSat);
 
 
 
 
 
121355 }
121356
121357 /*
121358 ** Add code to implement the OFFSET
121359 */
@@ -121435,13 +121624,10 @@
121435 if( pItem->u.x.iOrderByCol==0 ){
121436 Expr *pExpr = pItem->pExpr;
121437 Table *pTab = pExpr->pTab;
121438 if( pExpr->op==TK_COLUMN && pTab && !IsVirtual(pTab)
121439 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
121440 #if 0
121441 && pTab->pSchema && pTab->pSelect==0 && !IsVirtual(pTab)
121442 #endif
121443 ){
121444 int j;
121445 for(j=0; j<nDefer; j++){
121446 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
121447 }
@@ -121504,10 +121690,11 @@
121504 int hasDistinct; /* True if the DISTINCT keyword is present */
121505 int eDest = pDest->eDest; /* How to dispose of results */
121506 int iParm = pDest->iSDParm; /* First argument to disposal method */
121507 int nResultCol; /* Number of result columns */
121508 int nPrefixReg = 0; /* Number of extra registers before regResult */
 
121509
121510 /* Usually, regResult is the first cell in an array of memory cells
121511 ** containing the current result row. In this case regOrig is set to the
121512 ** same value. However, if the results are being sent to the sorter, the
121513 ** values for any expressions that are also part of the sort-key are omitted
@@ -121556,11 +121743,12 @@
121556 ExprList *pExtra = 0;
121557 #endif
121558 /* If the destination is an EXISTS(...) expression, the actual
121559 ** values returned by the SELECT are not required.
121560 */
121561 u8 ecelFlags;
 
121562 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
121563 ecelFlags = SQLITE_ECEL_DUP;
121564 }else{
121565 ecelFlags = 0;
121566 }
@@ -121570,10 +121758,11 @@
121570 ** iOrderByCol value to one more than the index of the ORDER BY
121571 ** expression within the sort-key that pushOntoSorter() will generate.
121572 ** This allows the p->pEList field to be omitted from the sorted record,
121573 ** saving space and CPU cycles. */
121574 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
 
121575 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
121576 int j;
121577 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
121578 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
121579 }
@@ -121590,24 +121779,50 @@
121590 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
121591 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
121592 pParse->nMem += pExtra->nExpr;
121593 }
121594 #endif
121595 regOrig = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121596 assert( eDest==SRT_Set || eDest==SRT_Mem
121597 || eDest==SRT_Coroutine || eDest==SRT_Output );
121598 }
121599 nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
121600 0,ecelFlags);
121601 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
121602 if( pExtra ){
121603 nResultCol += sqlite3ExprCodeExprList(
121604 pParse, pExtra, regResult + nResultCol, 0, 0
121605 );
121606 sqlite3ExprListDelete(pParse->db, pExtra);
 
 
 
 
 
 
 
 
121607 }
121608 #endif
121609 }
121610
121611 /* If the DISTINCT keyword was present on the SELECT statement
121612 ** and this row has been seen before, then do not make this row
121613 ** part of the result.
@@ -121719,11 +121934,12 @@
121719 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
121720 assert( pSort==0 );
121721 }
121722 #endif
121723 if( pSort ){
121724 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg);
 
121725 }else{
121726 int r2 = sqlite3GetTempReg(pParse);
121727 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
121728 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
121729 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
@@ -121986,15 +122202,11 @@
121986 **
121987 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
121988 ** is determined by the zUsage argument.
121989 */
121990 static void explainTempTable(Parse *pParse, const char *zUsage){
121991 if( pParse->explain==2 ){
121992 Vdbe *v = pParse->pVdbe;
121993 char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
121994 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
121995 }
121996 }
121997
121998 /*
121999 ** Assign expression b to lvalue a. A second, no-op, version of this macro
122000 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
@@ -122008,46 +122220,10 @@
122008 /* No-op versions of the explainXXX() functions and macros. */
122009 # define explainTempTable(y,z)
122010 # define explainSetInteger(y,z)
122011 #endif
122012
122013 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
122014 /*
122015 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
122016 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
122017 ** where the caption is of one of the two forms:
122018 **
122019 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
122020 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
122021 **
122022 ** where iSub1 and iSub2 are the integers passed as the corresponding
122023 ** function parameters, and op is the text representation of the parameter
122024 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
122025 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
122026 ** false, or the second form if it is true.
122027 */
122028 static void explainComposite(
122029 Parse *pParse, /* Parse context */
122030 int op, /* One of TK_UNION, TK_EXCEPT etc. */
122031 int iSub1, /* Subquery id 1 */
122032 int iSub2, /* Subquery id 2 */
122033 int bUseTmp /* True if a temp table was used */
122034 ){
122035 assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
122036 if( pParse->explain==2 ){
122037 Vdbe *v = pParse->pVdbe;
122038 char *zMsg = sqlite3MPrintf(
122039 pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
122040 bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
122041 );
122042 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
122043 }
122044 }
122045 #else
122046 /* No-op versions of the explainXXX() functions and macros. */
122047 # define explainComposite(v,w,x,y,z)
122048 #endif
122049
122050 /*
122051 ** If the inner loop was generated using a non-null pOrderBy argument,
122052 ** then the results were placed in a sorter. After the loop is terminated
122053 ** we need to run the sorter and output the results. The following
@@ -122493,11 +122669,11 @@
122493 if( pParse->explain ){
122494 return;
122495 }
122496 #endif
122497
122498 if( pParse->colNamesSet || db->mallocFailed ) return;
122499 /* Column names are determined by the left-most term of a compound select */
122500 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
122501 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
122502 pTabList = pSelect->pSrc;
122503 pEList = pSelect->pEList;
@@ -123020,10 +123196,11 @@
123020 /* Detach the ORDER BY clause from the compound SELECT */
123021 p->pOrderBy = 0;
123022
123023 /* Store the results of the setup-query in Queue. */
123024 pSetup->pNext = 0;
 
123025 rc = sqlite3Select(pParse, pSetup, &destQueue);
123026 pSetup->pNext = p;
123027 if( rc ) goto end_of_recursive_query;
123028
123029 /* Find the next row in the Queue and output that row */
@@ -123054,10 +123231,11 @@
123054 */
123055 if( p->selFlags & SF_Aggregate ){
123056 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
123057 }else{
123058 p->pPrior = 0;
 
123059 sqlite3Select(pParse, p, &destQueue);
123060 assert( p->pPrior==0 );
123061 p->pPrior = pSetup;
123062 }
123063
@@ -123099,30 +123277,28 @@
123099 static int multiSelectValues(
123100 Parse *pParse, /* Parsing context */
123101 Select *p, /* The right-most of SELECTs to be coded */
123102 SelectDest *pDest /* What to do with query results */
123103 ){
123104 Select *pPrior;
123105 Select *pRightmost = p;
123106 int nRow = 1;
123107 int rc = 0;
 
123108 assert( p->selFlags & SF_MultiValue );
123109 do{
123110 assert( p->selFlags & SF_Values );
123111 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
123112 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
123113 if( p->pPrior==0 ) break;
123114 assert( p->pPrior->pNext==p );
123115 p = p->pPrior;
123116 nRow++;
123117 }while(1);
 
 
123118 while( p ){
123119 pPrior = p->pPrior;
123120 p->pPrior = 0;
123121 rc = sqlite3Select(pParse, p, pDest);
123122 p->pPrior = pPrior;
123123 if( rc || pRightmost->pLimit ) break;
123124 p->nSelectRow = nRow;
123125 p = p->pNext;
123126 }
123127 return rc;
123128 }
@@ -123167,14 +123343,10 @@
123167 Select *pPrior; /* Another SELECT immediately to our left */
123168 Vdbe *v; /* Generate code to this VDBE */
123169 SelectDest dest; /* Alternative data destination */
123170 Select *pDelete = 0; /* Chain of simple selects to delete */
123171 sqlite3 *db; /* Database connection */
123172 #ifndef SQLITE_OMIT_EXPLAIN
123173 int iSub1 = 0; /* EQP id of left-hand query */
123174 int iSub2 = 0; /* EQP id of right-hand query */
123175 #endif
123176
123177 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
123178 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
123179 */
123180 assert( p && p->pPrior ); /* Calling function guarantees this much */
@@ -123221,221 +123393,235 @@
123221
123222 /* Compound SELECTs that have an ORDER BY clause are handled separately.
123223 */
123224 if( p->pOrderBy ){
123225 return multiSelectOrderBy(pParse, p, pDest);
123226 }else
123227
123228 /* Generate code for the left and right SELECT statements.
123229 */
123230 switch( p->op ){
123231 case TK_ALL: {
123232 int addr = 0;
123233 int nLimit;
123234 assert( !pPrior->pLimit );
123235 pPrior->iLimit = p->iLimit;
123236 pPrior->iOffset = p->iOffset;
123237 pPrior->pLimit = p->pLimit;
123238 explainSetInteger(iSub1, pParse->iNextSelectId);
123239 rc = sqlite3Select(pParse, pPrior, &dest);
123240 p->pLimit = 0;
123241 if( rc ){
123242 goto multi_select_end;
123243 }
123244 p->pPrior = 0;
123245 p->iLimit = pPrior->iLimit;
123246 p->iOffset = pPrior->iOffset;
123247 if( p->iLimit ){
123248 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
123249 VdbeComment((v, "Jump ahead if LIMIT reached"));
123250 if( p->iOffset ){
123251 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
123252 p->iLimit, p->iOffset+1, p->iOffset);
123253 }
123254 }
123255 explainSetInteger(iSub2, pParse->iNextSelectId);
123256 rc = sqlite3Select(pParse, p, &dest);
123257 testcase( rc!=SQLITE_OK );
123258 pDelete = p->pPrior;
123259 p->pPrior = pPrior;
123260 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123261 if( pPrior->pLimit
123262 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
123263 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
123264 ){
123265 p->nSelectRow = sqlite3LogEst((u64)nLimit);
123266 }
123267 if( addr ){
123268 sqlite3VdbeJumpHere(v, addr);
123269 }
123270 break;
123271 }
123272 case TK_EXCEPT:
123273 case TK_UNION: {
123274 int unionTab; /* Cursor number of the temporary table holding result */
123275 u8 op = 0; /* One of the SRT_ operations to apply to self */
123276 int priorOp; /* The SRT_ operation to apply to prior selects */
123277 Expr *pLimit; /* Saved values of p->nLimit */
123278 int addr;
123279 SelectDest uniondest;
123280
123281 testcase( p->op==TK_EXCEPT );
123282 testcase( p->op==TK_UNION );
123283 priorOp = SRT_Union;
123284 if( dest.eDest==priorOp ){
123285 /* We can reuse a temporary table generated by a SELECT to our
123286 ** right.
123287 */
123288 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
123289 unionTab = dest.iSDParm;
123290 }else{
123291 /* We will need to create our own temporary table to hold the
123292 ** intermediate results.
123293 */
123294 unionTab = pParse->nTab++;
123295 assert( p->pOrderBy==0 );
123296 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
123297 assert( p->addrOpenEphm[0] == -1 );
123298 p->addrOpenEphm[0] = addr;
123299 findRightmost(p)->selFlags |= SF_UsesEphemeral;
123300 assert( p->pEList );
123301 }
123302
123303 /* Code the SELECT statements to our left
123304 */
123305 assert( !pPrior->pOrderBy );
123306 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
123307 explainSetInteger(iSub1, pParse->iNextSelectId);
123308 rc = sqlite3Select(pParse, pPrior, &uniondest);
123309 if( rc ){
123310 goto multi_select_end;
123311 }
123312
123313 /* Code the current SELECT statement
123314 */
123315 if( p->op==TK_EXCEPT ){
123316 op = SRT_Except;
123317 }else{
123318 assert( p->op==TK_UNION );
123319 op = SRT_Union;
123320 }
123321 p->pPrior = 0;
123322 pLimit = p->pLimit;
123323 p->pLimit = 0;
123324 uniondest.eDest = op;
123325 explainSetInteger(iSub2, pParse->iNextSelectId);
123326 rc = sqlite3Select(pParse, p, &uniondest);
123327 testcase( rc!=SQLITE_OK );
123328 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
123329 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
123330 sqlite3ExprListDelete(db, p->pOrderBy);
123331 pDelete = p->pPrior;
123332 p->pPrior = pPrior;
123333 p->pOrderBy = 0;
123334 if( p->op==TK_UNION ){
123335 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123336 }
123337 sqlite3ExprDelete(db, p->pLimit);
123338 p->pLimit = pLimit;
123339 p->iLimit = 0;
123340 p->iOffset = 0;
123341
123342 /* Convert the data in the temporary table into whatever form
123343 ** it is that we currently need.
123344 */
123345 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
123346 if( dest.eDest!=priorOp ){
123347 int iCont, iBreak, iStart;
123348 assert( p->pEList );
123349 iBreak = sqlite3VdbeMakeLabel(v);
123350 iCont = sqlite3VdbeMakeLabel(v);
123351 computeLimitRegisters(pParse, p, iBreak);
123352 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
123353 iStart = sqlite3VdbeCurrentAddr(v);
123354 selectInnerLoop(pParse, p, unionTab,
123355 0, 0, &dest, iCont, iBreak);
123356 sqlite3VdbeResolveLabel(v, iCont);
123357 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
123358 sqlite3VdbeResolveLabel(v, iBreak);
123359 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
123360 }
123361 break;
123362 }
123363 default: assert( p->op==TK_INTERSECT ); {
123364 int tab1, tab2;
123365 int iCont, iBreak, iStart;
123366 Expr *pLimit;
123367 int addr;
123368 SelectDest intersectdest;
123369 int r1;
123370
123371 /* INTERSECT is different from the others since it requires
123372 ** two temporary tables. Hence it has its own case. Begin
123373 ** by allocating the tables we will need.
123374 */
123375 tab1 = pParse->nTab++;
123376 tab2 = pParse->nTab++;
123377 assert( p->pOrderBy==0 );
123378
123379 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
123380 assert( p->addrOpenEphm[0] == -1 );
123381 p->addrOpenEphm[0] = addr;
123382 findRightmost(p)->selFlags |= SF_UsesEphemeral;
123383 assert( p->pEList );
123384
123385 /* Code the SELECTs to our left into temporary table "tab1".
123386 */
123387 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
123388 explainSetInteger(iSub1, pParse->iNextSelectId);
123389 rc = sqlite3Select(pParse, pPrior, &intersectdest);
123390 if( rc ){
123391 goto multi_select_end;
123392 }
123393
123394 /* Code the current SELECT into temporary table "tab2"
123395 */
123396 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
123397 assert( p->addrOpenEphm[1] == -1 );
123398 p->addrOpenEphm[1] = addr;
123399 p->pPrior = 0;
123400 pLimit = p->pLimit;
123401 p->pLimit = 0;
123402 intersectdest.iSDParm = tab2;
123403 explainSetInteger(iSub2, pParse->iNextSelectId);
123404 rc = sqlite3Select(pParse, p, &intersectdest);
123405 testcase( rc!=SQLITE_OK );
123406 pDelete = p->pPrior;
123407 p->pPrior = pPrior;
123408 if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
123409 sqlite3ExprDelete(db, p->pLimit);
123410 p->pLimit = pLimit;
123411
123412 /* Generate code to take the intersection of the two temporary
123413 ** tables.
123414 */
123415 assert( p->pEList );
123416 iBreak = sqlite3VdbeMakeLabel(v);
123417 iCont = sqlite3VdbeMakeLabel(v);
123418 computeLimitRegisters(pParse, p, iBreak);
123419 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
123420 r1 = sqlite3GetTempReg(pParse);
123421 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
123422 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
123423 sqlite3ReleaseTempReg(pParse, r1);
123424 selectInnerLoop(pParse, p, tab1,
123425 0, 0, &dest, iCont, iBreak);
123426 sqlite3VdbeResolveLabel(v, iCont);
123427 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
123428 sqlite3VdbeResolveLabel(v, iBreak);
123429 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
123430 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
123431 break;
123432 }
123433 }
123434
123435 explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
123436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123437 /* Compute collating sequences used by
123438 ** temporary tables needed to implement the compound select.
123439 ** Attach the KeyInfo structure to all temporary tables.
123440 **
123441 ** This section is run by the right-most SELECT statement only.
@@ -123769,14 +123955,10 @@
123769 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
123770 sqlite3 *db; /* Database connection */
123771 ExprList *pOrderBy; /* The ORDER BY clause */
123772 int nOrderBy; /* Number of terms in the ORDER BY clause */
123773 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
123774 #ifndef SQLITE_OMIT_EXPLAIN
123775 int iSub1; /* EQP id of left-hand query */
123776 int iSub2; /* EQP id of right-hand query */
123777 #endif
123778
123779 assert( p->pOrderBy!=0 );
123780 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
123781 db = pParse->db;
123782 v = pParse->pVdbe;
@@ -123892,18 +124074,20 @@
123892 regOutA = ++pParse->nMem;
123893 regOutB = ++pParse->nMem;
123894 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
123895 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
123896
 
 
123897 /* Generate a coroutine to evaluate the SELECT statement to the
123898 ** left of the compound operator - the "A" select.
123899 */
123900 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
123901 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
123902 VdbeComment((v, "left SELECT"));
123903 pPrior->iLimit = regLimitA;
123904 explainSetInteger(iSub1, pParse->iNextSelectId);
123905 sqlite3Select(pParse, pPrior, &destA);
123906 sqlite3VdbeEndCoroutine(v, regAddrA);
123907 sqlite3VdbeJumpHere(v, addr1);
123908
123909 /* Generate a coroutine to evaluate the SELECT statement on
@@ -123914,11 +124098,11 @@
123914 VdbeComment((v, "right SELECT"));
123915 savedLimit = p->iLimit;
123916 savedOffset = p->iOffset;
123917 p->iLimit = regLimitB;
123918 p->iOffset = 0;
123919 explainSetInteger(iSub2, pParse->iNextSelectId);
123920 sqlite3Select(pParse, p, &destB);
123921 p->iLimit = savedLimit;
123922 p->iOffset = savedOffset;
123923 sqlite3VdbeEndCoroutine(v, regAddrB);
123924
@@ -124026,11 +124210,11 @@
124026 p->pPrior = pPrior;
124027 pPrior->pNext = p;
124028
124029 /*** TBD: Insert subroutine calls to close cursors on incomplete
124030 **** subqueries ****/
124031 explainComposite(pParse, p->op, iSub1, iSub2, 0);
124032 return pParse->nErr!=0;
124033 }
124034 #endif
124035
124036 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
@@ -125814,18 +125998,15 @@
125814 Table *pTab, /* Table being queried */
125815 Index *pIdx /* Index used to optimize scan, or NULL */
125816 ){
125817 if( pParse->explain==2 ){
125818 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
125819 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
125820 pTab->zName,
125821 bCover ? " USING COVERING INDEX " : "",
125822 bCover ? pIdx->zName : ""
125823 );
125824 sqlite3VdbeAddOp4(
125825 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
125826 );
125827 }
125828 }
125829 #else
125830 # define explainSimpleCount(a,b,c)
125831 #endif
@@ -126034,26 +126215,19 @@
126034 int iEnd; /* Address of the end of the query */
126035 sqlite3 *db; /* The database connection */
126036 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
126037 u8 minMaxFlag; /* Flag for min/max queries */
126038
126039 #ifndef SQLITE_OMIT_EXPLAIN
126040 int iRestoreSelectId = pParse->iSelectId;
126041 pParse->iSelectId = pParse->iNextSelectId++;
126042 #endif
126043
126044 db = pParse->db;
 
126045 if( p==0 || db->mallocFailed || pParse->nErr ){
126046 return 1;
126047 }
126048 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
126049 memset(&sAggInfo, 0, sizeof(sAggInfo));
126050 #if SELECTTRACE_ENABLED
126051 #ifndef SQLITE_OMIT_EXPLAIN
126052 p->iSelectId = pParse->iSelectId;
126053 #endif
126054 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->iSelectId));
126055 if( sqlite3SelectTrace & 0x100 ){
126056 sqlite3TreeViewSelect(0, p, 0);
126057 }
126058 #endif
126059
@@ -126086,14 +126260,10 @@
126086 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
126087 sqlite3TreeViewSelect(0, p, 0);
126088 }
126089 #endif
126090
126091 /* Get a pointer the VDBE under construction, allocating a new VDBE if one
126092 ** does not already exist */
126093 v = sqlite3GetVdbe(pParse);
126094 if( v==0 ) goto select_end;
126095 if( pDest->eDest==SRT_Output ){
126096 generateColumnNames(pParse, p);
126097 }
126098
126099 /* Try to various optimizations (flattening subqueries, and strength
@@ -126184,15 +126354,15 @@
126184 */
126185 if( p->pPrior ){
126186 rc = multiSelect(pParse, p, pDest);
126187 #if SELECTTRACE_ENABLED
126188 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
126189 if( pParse->iSelectId==0 && (sqlite3SelectTrace & 0x2000)!=0 ){
126190 sqlite3TreeViewSelect(0, p, 0);
126191 }
126192 #endif
126193 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
126194 return rc;
126195 }
126196 #endif
126197
126198 /* For each term in the FROM clause, do two things:
@@ -126300,11 +126470,11 @@
126300 pItem->regReturn = ++pParse->nMem;
126301 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
126302 VdbeComment((v, "%s", pItem->pTab->zName));
126303 pItem->addrFillSub = addrTop;
126304 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
126305 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
126306 sqlite3Select(pParse, pSub, &dest);
126307 pItem->pTab->nRowLogEst = pSub->nSelectRow;
126308 pItem->fg.viaCoroutine = 1;
126309 pItem->regResult = dest.iSdst;
126310 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
@@ -126335,16 +126505,15 @@
126335 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
126336 }
126337 pPrior = isSelfJoinView(pTabList, pItem);
126338 if( pPrior ){
126339 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
126340 explainSetInteger(pItem->iSelectId, pPrior->iSelectId);
126341 assert( pPrior->pSelect!=0 );
126342 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
126343 }else{
126344 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
126345 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
126346 sqlite3Select(pParse, pSub, &dest);
126347 }
126348 pItem->pTab->nRowLogEst = pSub->nSelectRow;
126349 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
126350 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
@@ -126978,15 +127147,15 @@
126978 sqlite3ExprListDelete(db, pMinMaxOrderBy);
126979 sqlite3DbFree(db, sAggInfo.aCol);
126980 sqlite3DbFree(db, sAggInfo.aFunc);
126981 #if SELECTTRACE_ENABLED
126982 SELECTTRACE(0x1,pParse,p,("end processing\n"));
126983 if( pParse->iSelectId==0 && (sqlite3SelectTrace & 0x2000)!=0 ){
126984 sqlite3TreeViewSelect(0, p, 0);
126985 }
126986 #endif
126987 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
126988 return rc;
126989 }
126990
126991 /************** End of select.c **********************************************/
126992 /************** Begin file table.c *******************************************/
@@ -129574,12 +129743,18 @@
129574 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
129575 if( rc!=SQLITE_OK ) return rc;
129576 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
129577 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
129578 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
129579 assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
129580 if( zSubSql && zSubSql[0]!='S' ){
 
 
 
 
 
 
129581 rc = execSql(db, pzErrMsg, zSubSql);
129582 if( rc!=SQLITE_OK ) break;
129583 }
129584 }
129585 assert( rc!=SQLITE_ROW );
@@ -129788,11 +129963,11 @@
129788 zDbMain
129789 );
129790 if( rc!=SQLITE_OK ) goto end_of_vacuum;
129791 rc = execSqlF(db, pzErrMsg,
129792 "SELECT sql FROM \"%w\".sqlite_master"
129793 " WHERE type='index' AND length(sql)>10",
129794 zDbMain
129795 );
129796 if( rc!=SQLITE_OK ) goto end_of_vacuum;
129797 db->init.iDb = 0;
129798
@@ -131852,11 +132027,10 @@
131852 #endif
131853 {
131854 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
131855 Vdbe *v = pParse->pVdbe; /* VM being constructed */
131856 sqlite3 *db = pParse->db; /* Database handle */
131857 int iId = pParse->iSelectId; /* Select id (left-most output column) */
131858 int isSearch; /* True for a SEARCH. False for SCAN. */
131859 WhereLoop *pLoop; /* The controlling WhereLoop object */
131860 u32 flags; /* Flags that describe this loop */
131861 char *zMsg; /* Text to add to EQP output */
131862 StrAccum str; /* EQP output string */
@@ -131871,11 +132045,11 @@
131871 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
131872
131873 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
131874 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
131875 if( pItem->pSelect ){
131876 sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
131877 }else{
131878 sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
131879 }
131880
131881 if( pItem->zAlias ){
@@ -131932,11 +132106,12 @@
131932 }else{
131933 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
131934 }
131935 #endif
131936 zMsg = sqlite3StrAccumFinish(&str);
131937 ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
 
131938 }
131939 return ret;
131940 }
131941 #endif /* SQLITE_OMIT_EXPLAIN */
131942
@@ -133652,10 +133827,11 @@
133652 /* Run a separate WHERE clause for each term of the OR clause. After
133653 ** eliminating duplicates from other WHERE clauses, the action for each
133654 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
133655 */
133656 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
 
133657 for(ii=0; ii<pOrWc->nTerm; ii++){
133658 WhereTerm *pOrTerm = &pOrWc->a[ii];
133659 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
133660 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
133661 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -133772,10 +133948,11 @@
133772 /* Finish the loop through table entries that match term pOrTerm. */
133773 sqlite3WhereEnd(pSubWInfo);
133774 }
133775 }
133776 }
 
133777 pLevel->u.pCovidx = pCov;
133778 if( pCov ) pLevel->iIdxCur = iCovCur;
133779 if( pAndExpr ){
133780 pAndExpr->pLeft = 0;
133781 sqlite3ExprDelete(db, pAndExpr);
@@ -140079,10 +140256,11 @@
140079 if( nTabList==0 ){
140080 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
140081 if( wctrlFlags & WHERE_WANT_DISTINCT ){
140082 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
140083 }
 
140084 }else{
140085 /* Assign a bit from the bitmask to every term in the FROM clause.
140086 **
140087 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
140088 **
@@ -146939,10 +147117,11 @@
146939 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
146940 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
146941 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
146942 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
146943 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
 
146944 };
146945 unsigned int i;
146946 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
146947 for(i=0; i<ArraySize(aFlagOp); i++){
146948 if( aFlagOp[i].op==op ){
@@ -151832,11 +152011,11 @@
151832 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
151833 char **, int, int, int, const char *, int, Fts3Expr **, char **
151834 );
151835 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
151836 #ifdef SQLITE_TEST
151837 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
151838 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
151839 #endif
151840
151841 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
151842 sqlite3_tokenizer_cursor **
@@ -155542,11 +155721,11 @@
155542 }
155543 }
155544
155545 #ifdef SQLITE_TEST
155546 if( rc==SQLITE_OK ){
155547 rc = sqlite3Fts3ExprInitTestInterface(db);
155548 }
155549 #endif
155550
155551 /* Create the virtual table wrapper around the hash-table and overload
155552 ** the four scalar functions. If this is successful, register the
@@ -159202,38 +159381,10 @@
159202
159203 #ifdef SQLITE_TEST
159204
159205 /* #include <stdio.h> */
159206
159207 /*
159208 ** Function to query the hash-table of tokenizers (see README.tokenizers).
159209 */
159210 static int queryTestTokenizer(
159211 sqlite3 *db,
159212 const char *zName,
159213 const sqlite3_tokenizer_module **pp
159214 ){
159215 int rc;
159216 sqlite3_stmt *pStmt;
159217 const char zSql[] = "SELECT fts3_tokenizer(?)";
159218
159219 *pp = 0;
159220 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
159221 if( rc!=SQLITE_OK ){
159222 return rc;
159223 }
159224
159225 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
159226 if( SQLITE_ROW==sqlite3_step(pStmt) ){
159227 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
159228 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
159229 }
159230 }
159231
159232 return sqlite3_finalize(pStmt);
159233 }
159234
159235 /*
159236 ** Return a pointer to a buffer containing a text representation of the
159237 ** expression passed as the first argument. The buffer is obtained from
159238 ** sqlite3_malloc(). It is the responsibility of the caller to use
159239 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
@@ -159297,51 +159448,47 @@
159297 ** of a column of the fts3 table that the query expression may refer to.
159298 ** For example:
159299 **
159300 ** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
159301 */
159302 static void fts3ExprTest(
 
159303 sqlite3_context *context,
159304 int argc,
159305 sqlite3_value **argv
159306 ){
159307 sqlite3_tokenizer_module const *pModule = 0;
159308 sqlite3_tokenizer *pTokenizer = 0;
159309 int rc;
159310 char **azCol = 0;
159311 const char *zExpr;
159312 int nExpr;
159313 int nCol;
159314 int ii;
159315 Fts3Expr *pExpr;
159316 char *zBuf = 0;
159317 sqlite3 *db = sqlite3_context_db_handle(context);
 
 
159318
159319 if( argc<3 ){
159320 sqlite3_result_error(context,
159321 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
159322 );
159323 return;
159324 }
159325
159326 rc = queryTestTokenizer(db,
159327 (const char *)sqlite3_value_text(argv[0]), &pModule);
159328 if( rc==SQLITE_NOMEM ){
159329 sqlite3_result_error_nomem(context);
159330 goto exprtest_out;
159331 }else if( !pModule ){
159332 sqlite3_result_error(context, "No such tokenizer module", -1);
159333 goto exprtest_out;
159334 }
159335
159336 rc = pModule->xCreate(0, 0, &pTokenizer);
159337 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
159338 if( rc==SQLITE_NOMEM ){
159339 sqlite3_result_error_nomem(context);
159340 goto exprtest_out;
159341 }
159342 pTokenizer->pModule = pModule;
159343
159344 zExpr = (const char *)sqlite3_value_text(argv[1]);
159345 nExpr = sqlite3_value_bytes(argv[1]);
159346 nCol = argc-2;
159347 azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
@@ -159351,11 +159498,11 @@
159351 }
159352 for(ii=0; ii<nCol; ii++){
159353 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
159354 }
159355
159356 if( sqlite3_user_data(context) ){
159357 char *zDummy = 0;
159358 rc = sqlite3Fts3ExprParse(
159359 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
159360 );
159361 assert( rc==SQLITE_OK || pExpr==0 );
@@ -159377,27 +159524,42 @@
159377 }
159378
159379 sqlite3Fts3ExprFree(pExpr);
159380
159381 exprtest_out:
159382 if( pModule && pTokenizer ){
159383 rc = pModule->xDestroy(pTokenizer);
159384 }
159385 sqlite3_free(azCol);
159386 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159387
159388 /*
159389 ** Register the query expression parser test function fts3_exprtest()
159390 ** with database connection db.
159391 */
159392 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
159393 int rc = sqlite3_create_function(
159394 db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
159395 );
159396 if( rc==SQLITE_OK ){
159397 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
159398 -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
159399 );
159400 }
159401 return rc;
159402 }
159403
@@ -175650,10 +175812,14 @@
175650 ** Valid if STAGE==1. The current change-counter cookie value in the
175651 ** target db file.
175652 **
175653 ** RBU_STATE_OALSZ:
175654 ** Valid if STAGE==1. The size in bytes of the *-oal file.
 
 
 
 
175655 */
175656 #define RBU_STATE_STAGE 1
175657 #define RBU_STATE_TBL 2
175658 #define RBU_STATE_IDX 3
175659 #define RBU_STATE_ROW 4
@@ -175660,10 +175826,11 @@
175660 #define RBU_STATE_PROGRESS 5
175661 #define RBU_STATE_CKPT 6
175662 #define RBU_STATE_COOKIE 7
175663 #define RBU_STATE_OALSZ 8
175664 #define RBU_STATE_PHASEONESTEP 9
 
175665
175666 #define RBU_STAGE_OAL 1
175667 #define RBU_STAGE_MOVE 2
175668 #define RBU_STAGE_CAPTURE 3
175669 #define RBU_STAGE_CKPT 4
@@ -175702,10 +175869,11 @@
175702 ** A structure to store values read from the rbu_state table in memory.
175703 */
175704 struct RbuState {
175705 int eStage;
175706 char *zTbl;
 
175707 char *zIdx;
175708 i64 iWalCksum;
175709 int nRow;
175710 i64 nProgress;
175711 u32 iCookie;
@@ -177765,10 +177933,11 @@
177765 ** Free an RbuState object allocated by rbuLoadState().
177766 */
177767 static void rbuFreeState(RbuState *p){
177768 if( p ){
177769 sqlite3_free(p->zTbl);
 
177770 sqlite3_free(p->zIdx);
177771 sqlite3_free(p);
177772 }
177773 }
177774
@@ -177834,10 +178003,14 @@
177834 break;
177835
177836 case RBU_STATE_PHASEONESTEP:
177837 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
177838 break;
 
 
 
 
177839
177840 default:
177841 rc = SQLITE_CORRUPT;
177842 break;
177843 }
@@ -178609,21 +178782,23 @@
178609 "(%d, %d), "
178610 "(%d, %d), "
178611 "(%d, %lld), "
178612 "(%d, %lld), "
178613 "(%d, %lld), "
178614 "(%d, %lld) ",
 
178615 p->zStateDb,
178616 RBU_STATE_STAGE, eStage,
178617 RBU_STATE_TBL, p->objiter.zTbl,
178618 RBU_STATE_IDX, p->objiter.zIdx,
178619 RBU_STATE_ROW, p->nStep,
178620 RBU_STATE_PROGRESS, p->nProgress,
178621 RBU_STATE_CKPT, p->iWalCksum,
178622 RBU_STATE_COOKIE, (i64)pFd->iCookie,
178623 RBU_STATE_OALSZ, p->iOalSz,
178624 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep
 
178625 )
178626 );
178627 assert( pInsert==0 || rc==SQLITE_OK );
178628
178629 if( rc==SQLITE_OK ){
@@ -178875,11 +179050,12 @@
178875 RbuObjIter *pIter = &p->objiter;
178876 int rc = SQLITE_OK;
178877
178878 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
178879 || rbuStrCompare(pIter->zIdx, pState->zIdx)
178880 || rbuStrCompare(pIter->zTbl, pState->zTbl)
 
178881 )){
178882 rc = rbuObjIterNext(p, pIter);
178883 }
178884
178885 if( rc==SQLITE_OK && !pIter->zTbl ){
@@ -206725,11 +206901,11 @@
206725 int nArg, /* Number of args */
206726 sqlite3_value **apUnused /* Function arguments */
206727 ){
206728 assert( nArg==0 );
206729 UNUSED_PARAM2(nArg, apUnused);
206730 sqlite3_result_text(pCtx, "fts5: 2018-04-26 12:27:03 368c14da868a843767344f6cc17c499fddd83244c0510337ed9a918e64ee2413", -1, SQLITE_TRANSIENT);
206731 }
206732
206733 static int fts5Init(sqlite3 *db){
206734 static const sqlite3_module fts5Mod = {
206735 /* iVersion */ 2,
@@ -210995,12 +211171,12 @@
210995 }
210996 #endif /* SQLITE_CORE */
210997 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
210998
210999 /************** End of stmt.c ************************************************/
211000 #if __LINE__!=211000
211001 #undef SQLITE_SOURCE_ID
211002 #define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825alt2"
211003 #endif
211004 /* Return the source-id for this library */
211005 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
211006 /************************** End of sqlite3.c ******************************/
211007
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1150,11 +1150,11 @@
1150 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1151 ** [sqlite_version()] and [sqlite_source_id()].
1152 */
1153 #define SQLITE_VERSION "3.24.0"
1154 #define SQLITE_VERSION_NUMBER 3024000
1155 #define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda"
1156
1157 /*
1158 ** CAPI3REF: Run-Time Library Version Numbers
1159 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1160 **
@@ -3137,10 +3137,25 @@
3137 ** or negative to leave the setting unchanged.
3138 ** The second parameter is a pointer to an integer into which is written
3139 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3140 ** it is not disabled, 1 if it is.
3141 ** </dd>
3142 **
3143 ** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3144 ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3145 ** [VACUUM] in order to reset a database back to an empty database
3146 ** with no schema and no content. The following process works even for
3147 ** a badly corrupted database file:
3148 ** <ol>
3149 ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3150 ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3151 ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3152 ** </ol>
3153 ** Because resetting a database is destructive and irreversible, the
3154 ** process requires the use of this obscure API and multiple steps to help
3155 ** ensure that it does not happen by accident.
3156 ** </dd>
3157 ** </dl>
3158 */
3159 #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
3160 #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
3161 #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -3148,11 +3163,12 @@
3163 #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
3164 #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
3165 #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
3166 #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
3167 #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
3168 #define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
3169 #define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */
3170
3171 /*
3172 ** CAPI3REF: Enable Or Disable Extended Result Codes
3173 ** METHOD: sqlite3
3174 **
@@ -6534,10 +6550,45 @@
6550 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
6551 ** or else the use of the [data_store_directory pragma] should be avoided.
6552 */
6553 SQLITE_API char *sqlite3_data_directory;
6554
6555 /*
6556 ** CAPI3REF: Win32 Specific Interface
6557 **
6558 ** These interfaces are available only on Windows. The
6559 ** [sqlite3_win32_set_directory] interface is used to set the value associated
6560 ** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6561 ** zValue, depending on the value of the type parameter. The zValue parameter
6562 ** should be NULL to cause the previous value to be freed via [sqlite3_free];
6563 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6564 ** prior to being used. The [sqlite3_win32_set_directory] interface returns
6565 ** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6566 ** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6567 ** [sqlite3_data_directory] variable is intended to act as a replacement for
6568 ** the current directory on the sub-platforms of Win32 where that concept is
6569 ** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6570 ** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6571 ** sqlite3_win32_set_directory interface except the string parameter must be
6572 ** UTF-8 or UTF-16, respectively.
6573 */
6574 SQLITE_API int sqlite3_win32_set_directory(
6575 unsigned long type, /* Identifier for directory being set or reset */
6576 void *zValue /* New value for directory being set or reset */
6577 );
6578 SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6579 SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6580
6581 /*
6582 ** CAPI3REF: Win32 Directory Types
6583 **
6584 ** These macros are only available on Windows. They define the allowed values
6585 ** for the type argument to the [sqlite3_win32_set_directory] interface.
6586 */
6587 #define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
6588 #define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
6589
6590 /*
6591 ** CAPI3REF: Test For Auto-Commit Mode
6592 ** KEYWORDS: {autocommit mode}
6593 ** METHOD: sqlite3
6594 **
@@ -14517,11 +14568,23 @@
14568 SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
14569 #else
14570 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
14571 # define sqlite3VdbeVerifyNoResultRow(A)
14572 #endif
14573 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
14574 #ifndef SQLITE_OMIT_EXPLAIN
14575 SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
14576 SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
14577 SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
14578 # define ExplainQueryPlan(P) sqlite3VdbeExplain P
14579 # define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
14580 # define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
14581 #else
14582 # define ExplainQueryPlan(P)
14583 # define ExplainQueryPlanPop(P)
14584 # define ExplainQueryPlanParent(P) 0
14585 #endif
14586 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
14587 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
14588 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
14589 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
14590 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
@@ -15821,10 +15884,11 @@
15884 #define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
15885 #define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
15886 #define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
15887 #define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
15888 #define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
15889 #define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
15890
15891 /* Flags used only if debugging */
15892 #ifdef SQLITE_DEBUG
15893 #define SQLITE_SqlTrace 0x08000000 /* Debug print SQL as it executes */
15894 #define SQLITE_VdbeListing 0x10000000 /* Debug listings of VDBE programs */
@@ -16923,13 +16987,10 @@
16987 unsigned isTabFunc :1; /* True if table-valued-function syntax */
16988 unsigned isCorrelated :1; /* True if sub-query is correlated */
16989 unsigned viaCoroutine :1; /* Implemented as a co-routine */
16990 unsigned isRecursive :1; /* True for recursive reference in WITH */
16991 } fg;
 
 
 
16992 int iCursor; /* The VDBE cursor number used to access this table */
16993 Expr *pOn; /* The ON clause of a join */
16994 IdList *pUsing; /* The USING clause of a join */
16995 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
16996 union {
@@ -17073,15 +17134,12 @@
17134
17135 /*
17136 ** An instance of the following structure contains all information
17137 ** needed to generate code for a single SELECT statement.
17138 **
17139 ** See the header comment on the computeLimitRegisters() routine for a
17140 ** detailed description of the meaning of the iLimit and iOffset fields.
 
 
 
17141 **
17142 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
17143 ** These addresses must be stored so that we can go back and fill in
17144 ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
17145 ** the number of columns in P2 can be computed at the same time
@@ -17097,11 +17155,10 @@
17155 LogEst nSelectRow; /* Estimated number of result rows */
17156 u32 selFlags; /* Various SF_* values */
17157 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
17158 #if SELECTTRACE_ENABLED
17159 char zSelName[12]; /* Symbolic name of this SELECT use for debugging */
 
17160 #endif
17161 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
17162 SrcList *pSrc; /* The FROM clause */
17163 Expr *pWhere; /* The WHERE clause */
17164 ExprList *pGroupBy; /* The GROUP BY clause */
@@ -17138,12 +17195,11 @@
17195 #define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */
17196 #define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */
17197 #define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */
17198 #define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */
17199 #define SF_IncludeHidden 0x20000 /* Include hidden columns in output */
17200 #define SF_ComplexResult 0x40000 /* Result contains subquery or function */
 
17201
17202 /*
17203 ** The results of a SELECT can be distributed in several ways, as defined
17204 ** by one of the following macros. The "SRT" prefix means "SELECT Result
17205 ** Type".
@@ -17409,12 +17465,11 @@
17465 u8 declareVtab; /* True if inside sqlite3_declare_vtab() */
17466 int nVtabLock; /* Number of virtual tables to lock */
17467 #endif
17468 int nHeight; /* Expression tree height of current sub-select */
17469 #ifndef SQLITE_OMIT_EXPLAIN
17470 int addrExplain; /* Address of current OP_Explain opcode */
 
17471 #endif
17472 VList *pVList; /* Mapping between variable names and numbers */
17473 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
17474 const char *zTail; /* All SQL text past the last semicolon parsed */
17475 Table *pNewTable; /* A table being constructed by CREATE TABLE */
@@ -27718,14 +27773,14 @@
27773 sqlite3TreeViewPush(pView, 1);
27774 }
27775 do{
27776 #if SELECTTRACE_ENABLED
27777 sqlite3TreeViewLine(pView,
27778 "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d",
27779 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
27780 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
27781 p->zSelName, p, p->selFlags,
27782 (int)p->nSelectRow
27783 );
27784 #else
27785 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
27786 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
@@ -39693,26 +39748,10 @@
39748 */
39749 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
39750 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
39751 #endif
39752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39753 /*
39754 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
39755 * various Win32 API heap functions instead of our own.
39756 */
39757 #ifdef SQLITE_WIN32_MALLOC
@@ -41305,17 +41344,17 @@
41344 #endif
41345 return winUtf8ToMbcs(zText, useAnsi);
41346 }
41347
41348 /*
41349 ** This function is the same as sqlite3_win32_set_directory (below); however,
41350 ** it accepts a UTF-8 string.
 
 
 
41351 */
41352 SQLITE_API int sqlite3_win32_set_directory8(
41353 unsigned long type, /* Identifier for directory being set or reset */
41354 const char *zValue /* New value for directory being set or reset */
41355 ){
41356 char **ppDirectory = 0;
41357 #ifndef SQLITE_OMIT_AUTOINIT
41358 int rc = sqlite3_initialize();
41359 if( rc ) return rc;
41360 #endif
@@ -41327,23 +41366,56 @@
41366 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
41367 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
41368 );
41369 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
41370 if( ppDirectory ){
41371 char *zCopy = 0;
41372 if( zValue && zValue[0] ){
41373 zCopy = sqlite3_mprintf("%s", zValue);
41374 if ( zCopy==0 ){
41375 return SQLITE_NOMEM_BKPT;
41376 }
41377 }
41378 sqlite3_free(*ppDirectory);
41379 *ppDirectory = zCopy;
41380 return SQLITE_OK;
41381 }
41382 return SQLITE_ERROR;
41383 }
41384
41385 /*
41386 ** This function is the same as sqlite3_win32_set_directory (below); however,
41387 ** it accepts a UTF-16 string.
41388 */
41389 SQLITE_API int sqlite3_win32_set_directory16(
41390 unsigned long type, /* Identifier for directory being set or reset */
41391 const void *zValue /* New value for directory being set or reset */
41392 ){
41393 int rc;
41394 char *zUtf8 = 0;
41395 if( zValue ){
41396 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
41397 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
41398 }
41399 rc = sqlite3_win32_set_directory8(type, zUtf8);
41400 if( zUtf8 ) sqlite3_free(zUtf8);
41401 return rc;
41402 }
41403
41404 /*
41405 ** This function sets the data directory or the temporary directory based on
41406 ** the provided arguments. The type argument must be 1 in order to set the
41407 ** data directory or 2 in order to set the temporary directory. The zValue
41408 ** argument is the name of the directory to use. The return value will be
41409 ** SQLITE_OK if successful.
41410 */
41411 SQLITE_API int sqlite3_win32_set_directory(
41412 unsigned long type, /* Identifier for directory being set or reset */
41413 void *zValue /* New value for directory being set or reset */
41414 ){
41415 return sqlite3_win32_set_directory16(type, zValue);
41416 }
41417
41418 /*
41419 ** The return value of winGetLastErrorMsg
41420 ** is zero if the error message fits in the buffer, or non-zero
41421 ** otherwise (if the message was truncated).
@@ -64714,10 +64786,14 @@
64786 }
64787 }
64788 #else
64789 # define setDefaultSyncFlag(pBt,safety_level)
64790 #endif
64791
64792 /* Forward declaration */
64793 static int newDatabase(BtShared*);
64794
64795
64796 /*
64797 ** Get a reference to pPage1 of the database file. This will
64798 ** also acquire a readlock on that file.
64799 **
@@ -64745,10 +64821,13 @@
64821 */
64822 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
64823 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
64824 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
64825 nPage = nPageFile;
64826 }
64827 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
64828 nPage = 0;
64829 }
64830 if( nPage>0 ){
64831 u32 pageSize;
64832 u32 usableSize;
64833 u8 *page1 = pPage1->aData;
@@ -67962,11 +68041,13 @@
68041 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68042 pPage->xParseCell(pPage, pCell, pInfo);
68043 if( pInfo->nLocal==pInfo->nPayload ){
68044 return SQLITE_OK; /* No overflow pages. Return without doing anything */
68045 }
68046 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
68047 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
68048 if( pCell + pInfo->nSize > pPage->aDataEnd ){
68049 /* Cell extends past end of page */
68050 return SQLITE_CORRUPT_PAGE(pPage);
68051 }
68052 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
68053 pBt = pPage->pBt;
@@ -74686,10 +74767,53 @@
74767 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
74768 if( p4copy ) memcpy(p4copy, zP4, 8);
74769 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
74770 }
74771
74772 #ifndef SQLITE_OMIT_EXPLAIN
74773 /*
74774 ** Return the address of the current EXPLAIN QUERY PLAN baseline.
74775 ** 0 means "none".
74776 */
74777 SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
74778 VdbeOp *pOp;
74779 if( pParse->addrExplain==0 ) return 0;
74780 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
74781 return pOp->p2;
74782 }
74783
74784 /*
74785 ** Add a new OP_Explain opcode.
74786 **
74787 ** If the bPush flag is true, then make this opcode the parent for
74788 ** subsequent Explains until sqlite3VdbeExplainPop() is called.
74789 */
74790 SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
74791 if( pParse->explain==2 ){
74792 char *zMsg;
74793 Vdbe *v = pParse->pVdbe;
74794 va_list ap;
74795 int iThis;
74796 va_start(ap, zFmt);
74797 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
74798 va_end(ap);
74799 v = pParse->pVdbe;
74800 iThis = v->nOp;
74801 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
74802 zMsg, P4_DYNAMIC);
74803 if( bPush) pParse->addrExplain = iThis;
74804 }
74805 }
74806
74807 /*
74808 ** Pop the EXPLAIN QUERY PLAN stack one level.
74809 */
74810 SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
74811 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
74812 }
74813 #endif /* SQLITE_OMIT_EXPLAIN */
74814
74815 /*
74816 ** Add an OP_ParseSchema opcode. This routine is broken out from
74817 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
74818 ** as having been used.
74819 **
@@ -96622,15 +96746,12 @@
96746
96747 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
96748 if( colUsed==(MASKBIT(nExpr)-1) ){
96749 /* If we reach this point, that means the index pIdx is usable */
96750 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
96751 ExplainQueryPlan((pParse, 0,
96752 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
 
 
 
96753 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
96754 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
96755 VdbeComment((v, "%s", pIdx->zName));
96756 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
96757 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
@@ -96858,21 +96979,13 @@
96979 ** table allocated and opened above.
96980 */
96981 Select *pSelect = pExpr->x.pSelect;
96982 ExprList *pEList = pSelect->pEList;
96983
96984 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY",
96985 jmpIfDynamic>=0?"":"CORRELATED "
96986 ));
 
 
 
 
 
 
 
 
96987 assert( !isRowid );
96988 /* If the LHS and RHS of the IN operator do not match, that
96989 ** error will have been caught long before we reach this point. */
96990 if( ALWAYS(pEList->nExpr==nVal) ){
96991 SelectDest dest;
@@ -96989,22 +97102,13 @@
97102 testcase( pExpr->op==TK_EXISTS );
97103 testcase( pExpr->op==TK_SELECT );
97104 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
97105 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
97106
 
 
 
 
 
 
 
 
 
 
 
97107 pSel = pExpr->x.pSelect;
97108 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
97109 jmpIfDynamic>=0?"":"CORRELATED "));
97110 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
97111 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
97112 pParse->nMem += nReg;
97113 if( pExpr->op==TK_SELECT ){
97114 dest.eDest = SRT_Mem;
@@ -97763,10 +97867,11 @@
97867 if( v==0 ){
97868 assert( pParse->db->mallocFailed );
97869 return 0;
97870 }
97871
97872 expr_code_doover:
97873 if( pExpr==0 ){
97874 op = TK_NULL;
97875 }else{
97876 op = pExpr->op;
97877 }
@@ -98223,11 +98328,12 @@
98328 return target;
98329 }
98330 case TK_SPAN:
98331 case TK_COLLATE:
98332 case TK_UPLUS: {
98333 pExpr = pExpr->pLeft;
98334 goto expr_code_doover;
98335 }
98336
98337 case TK_TRIGGER: {
98338 /* If the opcode is TK_TRIGGER, then the expression is a reference
98339 ** to a column in the new.* or old.* pseudo-tables available to
@@ -106427,11 +106533,15 @@
106533 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
106534 && db->init.busy==0
106535 #if SQLITE_USER_AUTHENTICATION
106536 && sqlite3UserAuthTable(pTab->zName)==0
106537 #endif
106538 #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
106539 && sqlite3StrICmp(&pTab->zName[7],"master")!=0
106540 #endif
106541 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
106542 ){
106543 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
106544 goto exit_create_index;
106545 }
106546 #ifndef SQLITE_OMIT_VIEW
106547 if( pTab->pSelect ){
@@ -120002,10 +120112,13 @@
120112 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
120113 ** the possible values of meta[4].
120114 */
120115 for(i=0; i<ArraySize(meta); i++){
120116 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
120117 }
120118 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
120119 memset(meta, 0, sizeof(meta));
120120 }
120121 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
120122
120123 /* If opening a non-empty database, check the text encoding. For the
120124 ** main database, set sqlite3.enc to the encoding of the main database.
@@ -120400,11 +120513,11 @@
120513
120514 #ifndef SQLITE_OMIT_EXPLAIN
120515 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
120516 static const char * const azColName[] = {
120517 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
120518 "id", "parent", "notused", "detail"
120519 };
120520 int iFirst, mx;
120521 if( sParse.explain==2 ){
120522 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
120523 iFirst = 8;
@@ -120714,11 +120827,11 @@
120827 */
120828 #if SELECTTRACE_ENABLED
120829 /***/ int sqlite3SelectTrace = 0;
120830 # define SELECTTRACE(K,P,S,X) \
120831 if(sqlite3SelectTrace&(K)) \
120832 sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->addrExplain,(S)),\
120833 sqlite3DebugPrintf X
120834 #else
120835 # define SELECTTRACE(K,P,S,X)
120836 #endif
120837
@@ -120771,10 +120884,11 @@
120884 Table *pTab; /* Table definition */
120885 int iCsr; /* Cursor number for table */
120886 int nKey; /* Number of PK columns for table pTab (>=1) */
120887 } aDefer[4];
120888 #endif
120889 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
120890 };
120891 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
120892
120893 /*
120894 ** Delete all the content of a Select structure. Deallocate the structure
@@ -121228,10 +121342,66 @@
121342 Parse *pParse, /* Parsing context */
121343 ExprList *pList, /* Form the KeyInfo object from this ExprList */
121344 int iStart, /* Begin with this column of pList */
121345 int nExtra /* Add this many extra columns to the end */
121346 );
121347
121348 /*
121349 ** An instance of this object holds information (beyond pParse and pSelect)
121350 ** needed to load the next result row that is to be added to the sorter.
121351 */
121352 typedef struct RowLoadInfo RowLoadInfo;
121353 struct RowLoadInfo {
121354 int regResult; /* Store results in array of registers here */
121355 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
121356 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
121357 ExprList *pExtra; /* Extra columns needed by sorter refs */
121358 int regExtraResult; /* Where to load the extra columns */
121359 #endif
121360 };
121361
121362 /*
121363 ** This routine does the work of loading query data into an array of
121364 ** registers so that it can be added to the sorter.
121365 */
121366 static void innerLoopLoadRow(
121367 Parse *pParse, /* Statement under construction */
121368 Select *pSelect, /* The query being coded */
121369 RowLoadInfo *pInfo /* Info needed to complete the row load */
121370 ){
121371 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
121372 0, pInfo->ecelFlags);
121373 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
121374 if( pInfo->pExtra ){
121375 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
121376 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
121377 }
121378 #endif
121379 }
121380
121381 /*
121382 ** Code the OP_MakeRecord instruction that generates the entry to be
121383 ** added into the sorter.
121384 **
121385 ** Return the register in which the result is stored.
121386 */
121387 static int makeSorterRecord(
121388 Parse *pParse,
121389 SortCtx *pSort,
121390 Select *pSelect,
121391 int regBase,
121392 int nBase
121393 ){
121394 int nOBSat = pSort->nOBSat;
121395 Vdbe *v = pParse->pVdbe;
121396 int regOut = ++pParse->nMem;
121397 if( pSort->pDeferredRowLoad ){
121398 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
121399 }
121400 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
121401 return regOut;
121402 }
121403
121404 /*
121405 ** Generate code that will push the record in registers regData
121406 ** through regData+nData-1 onto the sorter.
121407 */
@@ -121239,28 +121409,43 @@
121409 Parse *pParse, /* Parser context */
121410 SortCtx *pSort, /* Information about the ORDER BY clause */
121411 Select *pSelect, /* The whole SELECT statement */
121412 int regData, /* First register holding data to be sorted */
121413 int regOrigData, /* First register holding data before packing */
121414 int nData, /* Number of elements in the regData data array */
121415 int nPrefixReg /* No. of reg prior to regData available for use */
121416 ){
121417 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
121418 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
121419 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
121420 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
121421 int regBase; /* Regs for sorter record */
121422 int regRecord = 0; /* Assembled sorter record */
121423 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
121424 int op; /* Opcode to add sorter record to sorter */
121425 int iLimit; /* LIMIT counter */
121426 int iSkip = 0; /* End of the sorter insert loop */
121427
121428 assert( bSeq==0 || bSeq==1 );
121429
121430 /* Three cases:
121431 ** (1) The data to be sorted has already been packed into a Record
121432 ** by a prior OP_MakeRecord. In this case nData==1 and regData
121433 ** will be completely unrelated to regOrigData.
121434 ** (2) All output columns are included in the sort record. In that
121435 ** case regData==regOrigData.
121436 ** (3) Some output columns are omitted from the sort record due to
121437 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
121438 ** SQLITE_ECEL_OMITREF optimization. In that case, regOrigData==0
121439 ** to prevent this routine from trying to copy values that might
121440 ** not exist.
121441 */
121442 assert( nData==1 || regData==regOrigData || regOrigData==0 );
121443
121444 if( nPrefixReg ){
121445 assert( nPrefixReg==nExpr+bSeq );
121446 regBase = regData - nPrefixReg;
121447 }else{
121448 regBase = pParse->nMem + 1;
121449 pParse->nMem += nBase;
121450 }
121451 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
@@ -121280,11 +121465,11 @@
121465 int addrJmp; /* Address of the OP_Jump opcode */
121466 VdbeOp *pOp; /* Opcode that opens the sorter */
121467 int nKey; /* Number of sorting key columns, including OP_Sequence */
121468 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
121469
121470 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
121471 regPrevKey = pParse->nMem+1;
121472 pParse->nMem += pSort->nOBSat;
121473 nKey = nExpr - pSort->nOBSat + bSeq;
121474 if( bSeq ){
121475 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
@@ -121331,29 +121516,33 @@
121516 ** pSort->bOrderedInnerLoop flag is set to indicate that the inner
121517 ** loop delivers items in sorted order, jump to the next iteration
121518 ** of the outer loop.
121519 */
121520 int iCsr = pSort->iECursor;
 
 
121521 sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
121522 VdbeCoverage(v);
121523 sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
121524 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
121525 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
121526 VdbeCoverage(v);
121527 sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
121528 }
121529 if( regRecord==0 ){
121530 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
121531 }
121532 if( pSort->sortFlags & SORTFLAG_UseSorter ){
121533 op = OP_SorterInsert;
121534 }else{
121535 op = OP_IdxInsert;
121536 }
121537 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
121538 regBase+nOBSat, nBase-nOBSat);
121539 if( iSkip ){
121540 assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 );
121541 sqlite3VdbeChangeP2(v, iSkip,
121542 sqlite3VdbeCurrentAddr(v) + pSort->bOrderedInnerLoop);
121543 }
121544 }
121545
121546 /*
121547 ** Add code to implement the OFFSET
121548 */
@@ -121435,13 +121624,10 @@
121624 if( pItem->u.x.iOrderByCol==0 ){
121625 Expr *pExpr = pItem->pExpr;
121626 Table *pTab = pExpr->pTab;
121627 if( pExpr->op==TK_COLUMN && pTab && !IsVirtual(pTab)
121628 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
 
 
 
121629 ){
121630 int j;
121631 for(j=0; j<nDefer; j++){
121632 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
121633 }
@@ -121504,10 +121690,11 @@
121690 int hasDistinct; /* True if the DISTINCT keyword is present */
121691 int eDest = pDest->eDest; /* How to dispose of results */
121692 int iParm = pDest->iSDParm; /* First argument to disposal method */
121693 int nResultCol; /* Number of result columns */
121694 int nPrefixReg = 0; /* Number of extra registers before regResult */
121695 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
121696
121697 /* Usually, regResult is the first cell in an array of memory cells
121698 ** containing the current result row. In this case regOrig is set to the
121699 ** same value. However, if the results are being sent to the sorter, the
121700 ** values for any expressions that are also part of the sort-key are omitted
@@ -121556,11 +121743,12 @@
121743 ExprList *pExtra = 0;
121744 #endif
121745 /* If the destination is an EXISTS(...) expression, the actual
121746 ** values returned by the SELECT are not required.
121747 */
121748 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
121749 ExprList *pEList;
121750 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
121751 ecelFlags = SQLITE_ECEL_DUP;
121752 }else{
121753 ecelFlags = 0;
121754 }
@@ -121570,10 +121758,11 @@
121758 ** iOrderByCol value to one more than the index of the ORDER BY
121759 ** expression within the sort-key that pushOntoSorter() will generate.
121760 ** This allows the p->pEList field to be omitted from the sorted record,
121761 ** saving space and CPU cycles. */
121762 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
121763
121764 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
121765 int j;
121766 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
121767 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
121768 }
@@ -121590,24 +121779,50 @@
121779 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
121780 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
121781 pParse->nMem += pExtra->nExpr;
121782 }
121783 #endif
121784
121785 /* Adjust nResultCol to account for columns that are omitted
121786 ** from the sorter by the optimizations in this branch */
121787 pEList = p->pEList;
121788 for(i=0; i<pEList->nExpr; i++){
121789 if( pEList->a[i].u.x.iOrderByCol>0
121790 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
121791 || pEList->a[i].bSorterRef
121792 #endif
121793 ){
121794 nResultCol--;
121795 regOrig = 0;
121796 }
121797 }
121798
121799 testcase( regOrig );
121800 testcase( eDest==SRT_Set );
121801 testcase( eDest==SRT_Mem );
121802 testcase( eDest==SRT_Coroutine );
121803 testcase( eDest==SRT_Output );
121804 assert( eDest==SRT_Set || eDest==SRT_Mem
121805 || eDest==SRT_Coroutine || eDest==SRT_Output );
121806 }
121807 sRowLoadInfo.regResult = regResult;
121808 sRowLoadInfo.ecelFlags = ecelFlags;
121809 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
121810 sRowLoadInfo.pExtra = pExtra;
121811 sRowLoadInfo.regExtraResult = regResult + nResultCol;
121812 if( pExtra ) nResultCol += pExtra->nExpr;
121813 #endif
121814 if( p->iLimit
121815 && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
121816 && nPrefixReg>0
121817 ){
121818 assert( pSort!=0 );
121819 assert( hasDistinct==0 );
121820 pSort->pDeferredRowLoad = &sRowLoadInfo;
121821 }else{
121822 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
121823 }
 
121824 }
121825
121826 /* If the DISTINCT keyword was present on the SELECT statement
121827 ** and this row has been seen before, then do not make this row
121828 ** part of the result.
@@ -121719,11 +121934,12 @@
121934 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
121935 assert( pSort==0 );
121936 }
121937 #endif
121938 if( pSort ){
121939 assert( regResult==regOrig );
121940 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
121941 }else{
121942 int r2 = sqlite3GetTempReg(pParse);
121943 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
121944 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
121945 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
@@ -121986,15 +122202,11 @@
122202 **
122203 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
122204 ** is determined by the zUsage argument.
122205 */
122206 static void explainTempTable(Parse *pParse, const char *zUsage){
122207 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
 
 
 
 
122208 }
122209
122210 /*
122211 ** Assign expression b to lvalue a. A second, no-op, version of this macro
122212 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
@@ -122008,46 +122220,10 @@
122220 /* No-op versions of the explainXXX() functions and macros. */
122221 # define explainTempTable(y,z)
122222 # define explainSetInteger(y,z)
122223 #endif
122224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122225
122226 /*
122227 ** If the inner loop was generated using a non-null pOrderBy argument,
122228 ** then the results were placed in a sorter. After the loop is terminated
122229 ** we need to run the sorter and output the results. The following
@@ -122493,11 +122669,11 @@
122669 if( pParse->explain ){
122670 return;
122671 }
122672 #endif
122673
122674 if( pParse->colNamesSet ) return;
122675 /* Column names are determined by the left-most term of a compound select */
122676 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
122677 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
122678 pTabList = pSelect->pSrc;
122679 pEList = pSelect->pEList;
@@ -123020,10 +123196,11 @@
123196 /* Detach the ORDER BY clause from the compound SELECT */
123197 p->pOrderBy = 0;
123198
123199 /* Store the results of the setup-query in Queue. */
123200 pSetup->pNext = 0;
123201 ExplainQueryPlan((pParse, 1, "SETUP"));
123202 rc = sqlite3Select(pParse, pSetup, &destQueue);
123203 pSetup->pNext = p;
123204 if( rc ) goto end_of_recursive_query;
123205
123206 /* Find the next row in the Queue and output that row */
@@ -123054,10 +123231,11 @@
123231 */
123232 if( p->selFlags & SF_Aggregate ){
123233 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
123234 }else{
123235 p->pPrior = 0;
123236 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
123237 sqlite3Select(pParse, p, &destQueue);
123238 assert( p->pPrior==0 );
123239 p->pPrior = pSetup;
123240 }
123241
@@ -123099,30 +123277,28 @@
123277 static int multiSelectValues(
123278 Parse *pParse, /* Parsing context */
123279 Select *p, /* The right-most of SELECTs to be coded */
123280 SelectDest *pDest /* What to do with query results */
123281 ){
 
 
123282 int nRow = 1;
123283 int rc = 0;
123284 int bShowAll = p->pLimit==0;
123285 assert( p->selFlags & SF_MultiValue );
123286 do{
123287 assert( p->selFlags & SF_Values );
123288 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
123289 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
123290 if( p->pPrior==0 ) break;
123291 assert( p->pPrior->pNext==p );
123292 p = p->pPrior;
123293 nRow += bShowAll;
123294 }while(1);
123295 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
123296 nRow==1 ? "" : "S"));
123297 while( p ){
123298 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
123299 if( !bShowAll ) break;
 
 
 
123300 p->nSelectRow = nRow;
123301 p = p->pNext;
123302 }
123303 return rc;
123304 }
@@ -123167,14 +123343,10 @@
123343 Select *pPrior; /* Another SELECT immediately to our left */
123344 Vdbe *v; /* Generate code to this VDBE */
123345 SelectDest dest; /* Alternative data destination */
123346 Select *pDelete = 0; /* Chain of simple selects to delete */
123347 sqlite3 *db; /* Database connection */
 
 
 
 
123348
123349 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
123350 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
123351 */
123352 assert( p && p->pPrior ); /* Calling function guarantees this much */
@@ -123221,221 +123393,235 @@
123393
123394 /* Compound SELECTs that have an ORDER BY clause are handled separately.
123395 */
123396 if( p->pOrderBy ){
123397 return multiSelectOrderBy(pParse, p, pDest);
123398 }else{
123399
123400 #ifndef SQLITE_OMIT_EXPLAIN
123401 if( pPrior->pPrior==0 ){
123402 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
123403 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
123404 }
123405 #endif
123406
123407 /* Generate code for the left and right SELECT statements.
123408 */
123409 switch( p->op ){
123410 case TK_ALL: {
123411 int addr = 0;
123412 int nLimit;
123413 assert( !pPrior->pLimit );
123414 pPrior->iLimit = p->iLimit;
123415 pPrior->iOffset = p->iOffset;
123416 pPrior->pLimit = p->pLimit;
123417 rc = sqlite3Select(pParse, pPrior, &dest);
123418 p->pLimit = 0;
123419 if( rc ){
123420 goto multi_select_end;
123421 }
123422 p->pPrior = 0;
123423 p->iLimit = pPrior->iLimit;
123424 p->iOffset = pPrior->iOffset;
123425 if( p->iLimit ){
123426 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
123427 VdbeComment((v, "Jump ahead if LIMIT reached"));
123428 if( p->iOffset ){
123429 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
123430 p->iLimit, p->iOffset+1, p->iOffset);
123431 }
123432 }
123433 ExplainQueryPlan((pParse, 1, "UNION ALL"));
123434 rc = sqlite3Select(pParse, p, &dest);
123435 testcase( rc!=SQLITE_OK );
123436 pDelete = p->pPrior;
123437 p->pPrior = pPrior;
123438 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123439 if( pPrior->pLimit
123440 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
123441 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
123442 ){
123443 p->nSelectRow = sqlite3LogEst((u64)nLimit);
123444 }
123445 if( addr ){
123446 sqlite3VdbeJumpHere(v, addr);
123447 }
123448 break;
123449 }
123450 case TK_EXCEPT:
123451 case TK_UNION: {
123452 int unionTab; /* Cursor number of the temp table holding result */
123453 u8 op = 0; /* One of the SRT_ operations to apply to self */
123454 int priorOp; /* The SRT_ operation to apply to prior selects */
123455 Expr *pLimit; /* Saved values of p->nLimit */
123456 int addr;
123457 SelectDest uniondest;
123458
123459 testcase( p->op==TK_EXCEPT );
123460 testcase( p->op==TK_UNION );
123461 priorOp = SRT_Union;
123462 if( dest.eDest==priorOp ){
123463 /* We can reuse a temporary table generated by a SELECT to our
123464 ** right.
123465 */
123466 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
123467 unionTab = dest.iSDParm;
123468 }else{
123469 /* We will need to create our own temporary table to hold the
123470 ** intermediate results.
123471 */
123472 unionTab = pParse->nTab++;
123473 assert( p->pOrderBy==0 );
123474 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
123475 assert( p->addrOpenEphm[0] == -1 );
123476 p->addrOpenEphm[0] = addr;
123477 findRightmost(p)->selFlags |= SF_UsesEphemeral;
123478 assert( p->pEList );
123479 }
123480
123481 /* Code the SELECT statements to our left
123482 */
123483 assert( !pPrior->pOrderBy );
123484 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
123485 rc = sqlite3Select(pParse, pPrior, &uniondest);
123486 if( rc ){
123487 goto multi_select_end;
123488 }
123489
123490 /* Code the current SELECT statement
123491 */
123492 if( p->op==TK_EXCEPT ){
123493 op = SRT_Except;
123494 }else{
123495 assert( p->op==TK_UNION );
123496 op = SRT_Union;
123497 }
123498 p->pPrior = 0;
123499 pLimit = p->pLimit;
123500 p->pLimit = 0;
123501 uniondest.eDest = op;
123502 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
123503 selectOpName(p->op)));
123504 rc = sqlite3Select(pParse, p, &uniondest);
123505 testcase( rc!=SQLITE_OK );
123506 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
123507 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
123508 sqlite3ExprListDelete(db, p->pOrderBy);
123509 pDelete = p->pPrior;
123510 p->pPrior = pPrior;
123511 p->pOrderBy = 0;
123512 if( p->op==TK_UNION ){
123513 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
123514 }
123515 sqlite3ExprDelete(db, p->pLimit);
123516 p->pLimit = pLimit;
123517 p->iLimit = 0;
123518 p->iOffset = 0;
123519
123520 /* Convert the data in the temporary table into whatever form
123521 ** it is that we currently need.
123522 */
123523 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
123524 if( dest.eDest!=priorOp ){
123525 int iCont, iBreak, iStart;
123526 assert( p->pEList );
123527 iBreak = sqlite3VdbeMakeLabel(v);
123528 iCont = sqlite3VdbeMakeLabel(v);
123529 computeLimitRegisters(pParse, p, iBreak);
123530 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
123531 iStart = sqlite3VdbeCurrentAddr(v);
123532 selectInnerLoop(pParse, p, unionTab,
123533 0, 0, &dest, iCont, iBreak);
123534 sqlite3VdbeResolveLabel(v, iCont);
123535 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
123536 sqlite3VdbeResolveLabel(v, iBreak);
123537 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
123538 }
123539 break;
123540 }
123541 default: assert( p->op==TK_INTERSECT ); {
123542 int tab1, tab2;
123543 int iCont, iBreak, iStart;
123544 Expr *pLimit;
123545 int addr;
123546 SelectDest intersectdest;
123547 int r1;
123548
123549 /* INTERSECT is different from the others since it requires
123550 ** two temporary tables. Hence it has its own case. Begin
123551 ** by allocating the tables we will need.
123552 */
123553 tab1 = pParse->nTab++;
123554 tab2 = pParse->nTab++;
123555 assert( p->pOrderBy==0 );
123556
123557 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
123558 assert( p->addrOpenEphm[0] == -1 );
123559 p->addrOpenEphm[0] = addr;
123560 findRightmost(p)->selFlags |= SF_UsesEphemeral;
123561 assert( p->pEList );
123562
123563 /* Code the SELECTs to our left into temporary table "tab1".
123564 */
123565 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
123566 rc = sqlite3Select(pParse, pPrior, &intersectdest);
123567 if( rc ){
123568 goto multi_select_end;
123569 }
123570
123571 /* Code the current SELECT into temporary table "tab2"
123572 */
123573 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
123574 assert( p->addrOpenEphm[1] == -1 );
123575 p->addrOpenEphm[1] = addr;
123576 p->pPrior = 0;
123577 pLimit = p->pLimit;
123578 p->pLimit = 0;
123579 intersectdest.iSDParm = tab2;
123580 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
123581 selectOpName(p->op)));
123582 rc = sqlite3Select(pParse, p, &intersectdest);
123583 testcase( rc!=SQLITE_OK );
123584 pDelete = p->pPrior;
123585 p->pPrior = pPrior;
123586 if( p->nSelectRow>pPrior->nSelectRow ){
123587 p->nSelectRow = pPrior->nSelectRow;
123588 }
123589 sqlite3ExprDelete(db, p->pLimit);
123590 p->pLimit = pLimit;
123591
123592 /* Generate code to take the intersection of the two temporary
123593 ** tables.
123594 */
123595 assert( p->pEList );
123596 iBreak = sqlite3VdbeMakeLabel(v);
123597 iCont = sqlite3VdbeMakeLabel(v);
123598 computeLimitRegisters(pParse, p, iBreak);
123599 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
123600 r1 = sqlite3GetTempReg(pParse);
123601 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
123602 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
123603 VdbeCoverage(v);
123604 sqlite3ReleaseTempReg(pParse, r1);
123605 selectInnerLoop(pParse, p, tab1,
123606 0, 0, &dest, iCont, iBreak);
123607 sqlite3VdbeResolveLabel(v, iCont);
123608 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
123609 sqlite3VdbeResolveLabel(v, iBreak);
123610 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
123611 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
123612 break;
123613 }
123614 }
123615
123616 #ifndef SQLITE_OMIT_EXPLAIN
123617 if( p->pNext==0 ){
123618 ExplainQueryPlanPop(pParse);
123619 }
123620 #endif
123621 }
123622
123623 /* Compute collating sequences used by
123624 ** temporary tables needed to implement the compound select.
123625 ** Attach the KeyInfo structure to all temporary tables.
123626 **
123627 ** This section is run by the right-most SELECT statement only.
@@ -123769,14 +123955,10 @@
123955 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
123956 sqlite3 *db; /* Database connection */
123957 ExprList *pOrderBy; /* The ORDER BY clause */
123958 int nOrderBy; /* Number of terms in the ORDER BY clause */
123959 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
 
 
 
 
123960
123961 assert( p->pOrderBy!=0 );
123962 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
123963 db = pParse->db;
123964 v = pParse->pVdbe;
@@ -123892,18 +124074,20 @@
124074 regOutA = ++pParse->nMem;
124075 regOutB = ++pParse->nMem;
124076 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
124077 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
124078
124079 ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
124080
124081 /* Generate a coroutine to evaluate the SELECT statement to the
124082 ** left of the compound operator - the "A" select.
124083 */
124084 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
124085 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
124086 VdbeComment((v, "left SELECT"));
124087 pPrior->iLimit = regLimitA;
124088 ExplainQueryPlan((pParse, 1, "LEFT"));
124089 sqlite3Select(pParse, pPrior, &destA);
124090 sqlite3VdbeEndCoroutine(v, regAddrA);
124091 sqlite3VdbeJumpHere(v, addr1);
124092
124093 /* Generate a coroutine to evaluate the SELECT statement on
@@ -123914,11 +124098,11 @@
124098 VdbeComment((v, "right SELECT"));
124099 savedLimit = p->iLimit;
124100 savedOffset = p->iOffset;
124101 p->iLimit = regLimitB;
124102 p->iOffset = 0;
124103 ExplainQueryPlan((pParse, 1, "RIGHT"));
124104 sqlite3Select(pParse, p, &destB);
124105 p->iLimit = savedLimit;
124106 p->iOffset = savedOffset;
124107 sqlite3VdbeEndCoroutine(v, regAddrB);
124108
@@ -124026,11 +124210,11 @@
124210 p->pPrior = pPrior;
124211 pPrior->pNext = p;
124212
124213 /*** TBD: Insert subroutine calls to close cursors on incomplete
124214 **** subqueries ****/
124215 ExplainQueryPlanPop(pParse);
124216 return pParse->nErr!=0;
124217 }
124218 #endif
124219
124220 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
@@ -125814,18 +125998,15 @@
125998 Table *pTab, /* Table being queried */
125999 Index *pIdx /* Index used to optimize scan, or NULL */
126000 ){
126001 if( pParse->explain==2 ){
126002 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
126003 sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
126004 pTab->zName,
126005 bCover ? " USING COVERING INDEX " : "",
126006 bCover ? pIdx->zName : ""
126007 );
 
 
 
126008 }
126009 }
126010 #else
126011 # define explainSimpleCount(a,b,c)
126012 #endif
@@ -126034,26 +126215,19 @@
126215 int iEnd; /* Address of the end of the query */
126216 sqlite3 *db; /* The database connection */
126217 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
126218 u8 minMaxFlag; /* Flag for min/max queries */
126219
 
 
 
 
 
126220 db = pParse->db;
126221 v = sqlite3GetVdbe(pParse);
126222 if( p==0 || db->mallocFailed || pParse->nErr ){
126223 return 1;
126224 }
126225 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
126226 memset(&sAggInfo, 0, sizeof(sAggInfo));
126227 #if SELECTTRACE_ENABLED
126228 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
 
 
 
126229 if( sqlite3SelectTrace & 0x100 ){
126230 sqlite3TreeViewSelect(0, p, 0);
126231 }
126232 #endif
126233
@@ -126086,14 +126260,10 @@
126260 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
126261 sqlite3TreeViewSelect(0, p, 0);
126262 }
126263 #endif
126264
 
 
 
 
126265 if( pDest->eDest==SRT_Output ){
126266 generateColumnNames(pParse, p);
126267 }
126268
126269 /* Try to various optimizations (flattening subqueries, and strength
@@ -126184,15 +126354,15 @@
126354 */
126355 if( p->pPrior ){
126356 rc = multiSelect(pParse, p, pDest);
126357 #if SELECTTRACE_ENABLED
126358 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
126359 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
126360 sqlite3TreeViewSelect(0, p, 0);
126361 }
126362 #endif
126363 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
126364 return rc;
126365 }
126366 #endif
126367
126368 /* For each term in the FROM clause, do two things:
@@ -126300,11 +126470,11 @@
126470 pItem->regReturn = ++pParse->nMem;
126471 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
126472 VdbeComment((v, "%s", pItem->pTab->zName));
126473 pItem->addrFillSub = addrTop;
126474 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
126475 ExplainQueryPlan((pParse, 1, "CO-ROUTINE 0x%p", pSub));
126476 sqlite3Select(pParse, pSub, &dest);
126477 pItem->pTab->nRowLogEst = pSub->nSelectRow;
126478 pItem->fg.viaCoroutine = 1;
126479 pItem->regResult = dest.iSdst;
126480 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
@@ -126335,16 +126505,15 @@
126505 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
126506 }
126507 pPrior = isSelfJoinView(pTabList, pItem);
126508 if( pPrior ){
126509 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
 
126510 assert( pPrior->pSelect!=0 );
126511 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
126512 }else{
126513 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
126514 ExplainQueryPlan((pParse, 1, "MATERIALIZE 0x%p", pSub));
126515 sqlite3Select(pParse, pSub, &dest);
126516 }
126517 pItem->pTab->nRowLogEst = pSub->nSelectRow;
126518 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
126519 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
@@ -126978,15 +127147,15 @@
127147 sqlite3ExprListDelete(db, pMinMaxOrderBy);
127148 sqlite3DbFree(db, sAggInfo.aCol);
127149 sqlite3DbFree(db, sAggInfo.aFunc);
127150 #if SELECTTRACE_ENABLED
127151 SELECTTRACE(0x1,pParse,p,("end processing\n"));
127152 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
127153 sqlite3TreeViewSelect(0, p, 0);
127154 }
127155 #endif
127156 ExplainQueryPlanPop(pParse);
127157 return rc;
127158 }
127159
127160 /************** End of select.c **********************************************/
127161 /************** Begin file table.c *******************************************/
@@ -129574,12 +129743,18 @@
129743 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
129744 if( rc!=SQLITE_OK ) return rc;
129745 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
129746 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
129747 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
129748 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
129749 ** or INSERT. Historically there have been attacks that first
129750 ** corrupt the sqlite_master.sql field with other kinds of statements
129751 ** then run VACUUM to get those statements to execute at inappropriate
129752 ** times. */
129753 if( zSubSql
129754 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
129755 ){
129756 rc = execSql(db, pzErrMsg, zSubSql);
129757 if( rc!=SQLITE_OK ) break;
129758 }
129759 }
129760 assert( rc!=SQLITE_ROW );
@@ -129788,11 +129963,11 @@
129963 zDbMain
129964 );
129965 if( rc!=SQLITE_OK ) goto end_of_vacuum;
129966 rc = execSqlF(db, pzErrMsg,
129967 "SELECT sql FROM \"%w\".sqlite_master"
129968 " WHERE type='index'",
129969 zDbMain
129970 );
129971 if( rc!=SQLITE_OK ) goto end_of_vacuum;
129972 db->init.iDb = 0;
129973
@@ -131852,11 +132027,10 @@
132027 #endif
132028 {
132029 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
132030 Vdbe *v = pParse->pVdbe; /* VM being constructed */
132031 sqlite3 *db = pParse->db; /* Database handle */
 
132032 int isSearch; /* True for a SEARCH. False for SCAN. */
132033 WhereLoop *pLoop; /* The controlling WhereLoop object */
132034 u32 flags; /* Flags that describe this loop */
132035 char *zMsg; /* Text to add to EQP output */
132036 StrAccum str; /* EQP output string */
@@ -131871,11 +132045,11 @@
132045 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
132046
132047 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
132048 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
132049 if( pItem->pSelect ){
132050 sqlite3XPrintf(&str, " SUBQUERY 0x%p", pItem->pSelect);
132051 }else{
132052 sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
132053 }
132054
132055 if( pItem->zAlias ){
@@ -131932,11 +132106,12 @@
132106 }else{
132107 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
132108 }
132109 #endif
132110 zMsg = sqlite3StrAccumFinish(&str);
132111 ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
132112 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
132113 }
132114 return ret;
132115 }
132116 #endif /* SQLITE_OMIT_EXPLAIN */
132117
@@ -133652,10 +133827,11 @@
133827 /* Run a separate WHERE clause for each term of the OR clause. After
133828 ** eliminating duplicates from other WHERE clauses, the action for each
133829 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
133830 */
133831 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
133832 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
133833 for(ii=0; ii<pOrWc->nTerm; ii++){
133834 WhereTerm *pOrTerm = &pOrWc->a[ii];
133835 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
133836 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
133837 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
@@ -133772,10 +133948,11 @@
133948 /* Finish the loop through table entries that match term pOrTerm. */
133949 sqlite3WhereEnd(pSubWInfo);
133950 }
133951 }
133952 }
133953 ExplainQueryPlanPop(pParse);
133954 pLevel->u.pCovidx = pCov;
133955 if( pCov ) pLevel->iIdxCur = iCovCur;
133956 if( pAndExpr ){
133957 pAndExpr->pLeft = 0;
133958 sqlite3ExprDelete(db, pAndExpr);
@@ -140079,10 +140256,11 @@
140256 if( nTabList==0 ){
140257 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
140258 if( wctrlFlags & WHERE_WANT_DISTINCT ){
140259 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
140260 }
140261 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
140262 }else{
140263 /* Assign a bit from the bitmask to every term in the FROM clause.
140264 **
140265 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
140266 **
@@ -146939,10 +147117,11 @@
147117 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
147118 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
147119 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
147120 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
147121 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
147122 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
147123 };
147124 unsigned int i;
147125 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
147126 for(i=0; i<ArraySize(aFlagOp); i++){
147127 if( aFlagOp[i].op==op ){
@@ -151832,11 +152011,11 @@
152011 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
152012 char **, int, int, int, const char *, int, Fts3Expr **, char **
152013 );
152014 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
152015 #ifdef SQLITE_TEST
152016 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
152017 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
152018 #endif
152019
152020 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
152021 sqlite3_tokenizer_cursor **
@@ -155542,11 +155721,11 @@
155721 }
155722 }
155723
155724 #ifdef SQLITE_TEST
155725 if( rc==SQLITE_OK ){
155726 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
155727 }
155728 #endif
155729
155730 /* Create the virtual table wrapper around the hash-table and overload
155731 ** the four scalar functions. If this is successful, register the
@@ -159202,38 +159381,10 @@
159381
159382 #ifdef SQLITE_TEST
159383
159384 /* #include <stdio.h> */
159385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159386 /*
159387 ** Return a pointer to a buffer containing a text representation of the
159388 ** expression passed as the first argument. The buffer is obtained from
159389 ** sqlite3_malloc(). It is the responsibility of the caller to use
159390 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
@@ -159297,51 +159448,47 @@
159448 ** of a column of the fts3 table that the query expression may refer to.
159449 ** For example:
159450 **
159451 ** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
159452 */
159453 static void fts3ExprTestCommon(
159454 int bRebalance,
159455 sqlite3_context *context,
159456 int argc,
159457 sqlite3_value **argv
159458 ){
 
159459 sqlite3_tokenizer *pTokenizer = 0;
159460 int rc;
159461 char **azCol = 0;
159462 const char *zExpr;
159463 int nExpr;
159464 int nCol;
159465 int ii;
159466 Fts3Expr *pExpr;
159467 char *zBuf = 0;
159468 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
159469 const char *zTokenizer = 0;
159470 char *zErr = 0;
159471
159472 if( argc<3 ){
159473 sqlite3_result_error(context,
159474 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
159475 );
159476 return;
159477 }
159478
159479 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
159480 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
159481 if( rc!=SQLITE_OK ){
159482 if( rc==SQLITE_NOMEM ){
159483 sqlite3_result_error_nomem(context);
159484 }else{
159485 sqlite3_result_error(context, zErr, -1);
159486 }
159487 sqlite3_free(zErr);
159488 return;
159489 }
 
 
 
 
 
 
159490
159491 zExpr = (const char *)sqlite3_value_text(argv[1]);
159492 nExpr = sqlite3_value_bytes(argv[1]);
159493 nCol = argc-2;
159494 azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
@@ -159351,11 +159498,11 @@
159498 }
159499 for(ii=0; ii<nCol; ii++){
159500 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
159501 }
159502
159503 if( bRebalance ){
159504 char *zDummy = 0;
159505 rc = sqlite3Fts3ExprParse(
159506 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
159507 );
159508 assert( rc==SQLITE_OK || pExpr==0 );
@@ -159377,27 +159524,42 @@
159524 }
159525
159526 sqlite3Fts3ExprFree(pExpr);
159527
159528 exprtest_out:
159529 if( pTokenizer ){
159530 rc = pTokenizer->pModule->xDestroy(pTokenizer);
159531 }
159532 sqlite3_free(azCol);
159533 }
159534
159535 static void fts3ExprTest(
159536 sqlite3_context *context,
159537 int argc,
159538 sqlite3_value **argv
159539 ){
159540 fts3ExprTestCommon(0, context, argc, argv);
159541 }
159542 static void fts3ExprTestRebalance(
159543 sqlite3_context *context,
159544 int argc,
159545 sqlite3_value **argv
159546 ){
159547 fts3ExprTestCommon(1, context, argc, argv);
159548 }
159549
159550 /*
159551 ** Register the query expression parser test function fts3_exprtest()
159552 ** with database connection db.
159553 */
159554 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
159555 int rc = sqlite3_create_function(
159556 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
159557 );
159558 if( rc==SQLITE_OK ){
159559 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
159560 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
159561 );
159562 }
159563 return rc;
159564 }
159565
@@ -175650,10 +175812,14 @@
175812 ** Valid if STAGE==1. The current change-counter cookie value in the
175813 ** target db file.
175814 **
175815 ** RBU_STATE_OALSZ:
175816 ** Valid if STAGE==1. The size in bytes of the *-oal file.
175817 **
175818 ** RBU_STATE_DATATBL:
175819 ** Only valid if STAGE==1. The RBU database name of the table
175820 ** currently being read.
175821 */
175822 #define RBU_STATE_STAGE 1
175823 #define RBU_STATE_TBL 2
175824 #define RBU_STATE_IDX 3
175825 #define RBU_STATE_ROW 4
@@ -175660,10 +175826,11 @@
175826 #define RBU_STATE_PROGRESS 5
175827 #define RBU_STATE_CKPT 6
175828 #define RBU_STATE_COOKIE 7
175829 #define RBU_STATE_OALSZ 8
175830 #define RBU_STATE_PHASEONESTEP 9
175831 #define RBU_STATE_DATATBL 10
175832
175833 #define RBU_STAGE_OAL 1
175834 #define RBU_STAGE_MOVE 2
175835 #define RBU_STAGE_CAPTURE 3
175836 #define RBU_STAGE_CKPT 4
@@ -175702,10 +175869,11 @@
175869 ** A structure to store values read from the rbu_state table in memory.
175870 */
175871 struct RbuState {
175872 int eStage;
175873 char *zTbl;
175874 char *zDataTbl;
175875 char *zIdx;
175876 i64 iWalCksum;
175877 int nRow;
175878 i64 nProgress;
175879 u32 iCookie;
@@ -177765,10 +177933,11 @@
177933 ** Free an RbuState object allocated by rbuLoadState().
177934 */
177935 static void rbuFreeState(RbuState *p){
177936 if( p ){
177937 sqlite3_free(p->zTbl);
177938 sqlite3_free(p->zDataTbl);
177939 sqlite3_free(p->zIdx);
177940 sqlite3_free(p);
177941 }
177942 }
177943
@@ -177834,10 +178003,14 @@
178003 break;
178004
178005 case RBU_STATE_PHASEONESTEP:
178006 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
178007 break;
178008
178009 case RBU_STATE_DATATBL:
178010 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
178011 break;
178012
178013 default:
178014 rc = SQLITE_CORRUPT;
178015 break;
178016 }
@@ -178609,21 +178782,23 @@
178782 "(%d, %d), "
178783 "(%d, %d), "
178784 "(%d, %lld), "
178785 "(%d, %lld), "
178786 "(%d, %lld), "
178787 "(%d, %lld), "
178788 "(%d, %Q) ",
178789 p->zStateDb,
178790 RBU_STATE_STAGE, eStage,
178791 RBU_STATE_TBL, p->objiter.zTbl,
178792 RBU_STATE_IDX, p->objiter.zIdx,
178793 RBU_STATE_ROW, p->nStep,
178794 RBU_STATE_PROGRESS, p->nProgress,
178795 RBU_STATE_CKPT, p->iWalCksum,
178796 RBU_STATE_COOKIE, (i64)pFd->iCookie,
178797 RBU_STATE_OALSZ, p->iOalSz,
178798 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
178799 RBU_STATE_DATATBL, p->objiter.zDataTbl
178800 )
178801 );
178802 assert( pInsert==0 || rc==SQLITE_OK );
178803
178804 if( rc==SQLITE_OK ){
@@ -178875,11 +179050,12 @@
179050 RbuObjIter *pIter = &p->objiter;
179051 int rc = SQLITE_OK;
179052
179053 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
179054 || rbuStrCompare(pIter->zIdx, pState->zIdx)
179055 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
179056 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
179057 )){
179058 rc = rbuObjIterNext(p, pIter);
179059 }
179060
179061 if( rc==SQLITE_OK && !pIter->zTbl ){
@@ -206725,11 +206901,11 @@
206901 int nArg, /* Number of args */
206902 sqlite3_value **apUnused /* Function arguments */
206903 ){
206904 assert( nArg==0 );
206905 UNUSED_PARAM2(nArg, apUnused);
206906 sqlite3_result_text(pCtx, "fts5: 2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda", -1, SQLITE_TRANSIENT);
206907 }
206908
206909 static int fts5Init(sqlite3 *db){
206910 static const sqlite3_module fts5Mod = {
206911 /* iVersion */ 2,
@@ -210995,12 +211171,12 @@
211171 }
211172 #endif /* SQLITE_CORE */
211173 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
211174
211175 /************** End of stmt.c ************************************************/
211176 #if __LINE__!=211176
211177 #undef SQLITE_SOURCE_ID
211178 #define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba7alt2"
211179 #endif
211180 /* Return the source-id for this library */
211181 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
211182 /************************** End of sqlite3.c ******************************/
211183
+53 -2
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126126
#define SQLITE_VERSION "3.24.0"
127127
#define SQLITE_VERSION_NUMBER 3024000
128
-#define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825d83b"
128
+#define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
@@ -2110,10 +2110,25 @@
21102110
** or negative to leave the setting unchanged.
21112111
** The second parameter is a pointer to an integer into which is written
21122112
** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
21132113
** it is not disabled, 1 if it is.
21142114
** </dd>
2115
+**
2116
+** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2117
+** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2118
+** [VACUUM] in order to reset a database back to an empty database
2119
+** with no schema and no content. The following process works even for
2120
+** a badly corrupted database file:
2121
+** <ol>
2122
+** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2123
+** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2124
+** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2125
+** </ol>
2126
+** Because resetting a database is destructive and irreversible, the
2127
+** process requires the use of this obscure API and multiple steps to help
2128
+** ensure that it does not happen by accident.
2129
+** </dd>
21152130
** </dl>
21162131
*/
21172132
#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
21182133
#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
21192134
#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -2121,11 +2136,12 @@
21212136
#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
21222137
#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
21232138
#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
21242139
#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
21252140
#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
2126
-#define SQLITE_DBCONFIG_MAX 1008 /* Largest DBCONFIG */
2141
+#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
2142
+#define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */
21272143
21282144
/*
21292145
** CAPI3REF: Enable Or Disable Extended Result Codes
21302146
** METHOD: sqlite3
21312147
**
@@ -5507,10 +5523,45 @@
55075523
** made NULL or made to point to memory obtained from [sqlite3_malloc]
55085524
** or else the use of the [data_store_directory pragma] should be avoided.
55095525
*/
55105526
SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
55115527
5528
+/*
5529
+** CAPI3REF: Win32 Specific Interface
5530
+**
5531
+** These interfaces are available only on Windows. The
5532
+** [sqlite3_win32_set_directory] interface is used to set the value associated
5533
+** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
5534
+** zValue, depending on the value of the type parameter. The zValue parameter
5535
+** should be NULL to cause the previous value to be freed via [sqlite3_free];
5536
+** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
5537
+** prior to being used. The [sqlite3_win32_set_directory] interface returns
5538
+** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
5539
+** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
5540
+** [sqlite3_data_directory] variable is intended to act as a replacement for
5541
+** the current directory on the sub-platforms of Win32 where that concept is
5542
+** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
5543
+** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
5544
+** sqlite3_win32_set_directory interface except the string parameter must be
5545
+** UTF-8 or UTF-16, respectively.
5546
+*/
5547
+SQLITE_API int sqlite3_win32_set_directory(
5548
+ unsigned long type, /* Identifier for directory being set or reset */
5549
+ void *zValue /* New value for directory being set or reset */
5550
+);
5551
+SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
5552
+SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
5553
+
5554
+/*
5555
+** CAPI3REF: Win32 Directory Types
5556
+**
5557
+** These macros are only available on Windows. They define the allowed values
5558
+** for the type argument to the [sqlite3_win32_set_directory] interface.
5559
+*/
5560
+#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
5561
+#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
5562
+
55125563
/*
55135564
** CAPI3REF: Test For Auto-Commit Mode
55145565
** KEYWORDS: {autocommit mode}
55155566
** METHOD: sqlite3
55165567
**
55175568
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.24.0"
127 #define SQLITE_VERSION_NUMBER 3024000
128 #define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825d83b"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -2110,10 +2110,25 @@
2110 ** or negative to leave the setting unchanged.
2111 ** The second parameter is a pointer to an integer into which is written
2112 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2113 ** it is not disabled, 1 if it is.
2114 ** </dd>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2115 ** </dl>
2116 */
2117 #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
2118 #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
2119 #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -2121,11 +2136,12 @@
2121 #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
2122 #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
2123 #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
2124 #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
2125 #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
2126 #define SQLITE_DBCONFIG_MAX 1008 /* Largest DBCONFIG */
 
2127
2128 /*
2129 ** CAPI3REF: Enable Or Disable Extended Result Codes
2130 ** METHOD: sqlite3
2131 **
@@ -5507,10 +5523,45 @@
5507 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
5508 ** or else the use of the [data_store_directory pragma] should be avoided.
5509 */
5510 SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
5511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5512 /*
5513 ** CAPI3REF: Test For Auto-Commit Mode
5514 ** KEYWORDS: {autocommit mode}
5515 ** METHOD: sqlite3
5516 **
5517
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.24.0"
127 #define SQLITE_VERSION_NUMBER 3024000
128 #define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -2110,10 +2110,25 @@
2110 ** or negative to leave the setting unchanged.
2111 ** The second parameter is a pointer to an integer into which is written
2112 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2113 ** it is not disabled, 1 if it is.
2114 ** </dd>
2115 **
2116 ** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2117 ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2118 ** [VACUUM] in order to reset a database back to an empty database
2119 ** with no schema and no content. The following process works even for
2120 ** a badly corrupted database file:
2121 ** <ol>
2122 ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2123 ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2124 ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2125 ** </ol>
2126 ** Because resetting a database is destructive and irreversible, the
2127 ** process requires the use of this obscure API and multiple steps to help
2128 ** ensure that it does not happen by accident.
2129 ** </dd>
2130 ** </dl>
2131 */
2132 #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
2133 #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
2134 #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -2121,11 +2136,12 @@
2136 #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
2137 #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
2138 #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
2139 #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
2140 #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
2141 #define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
2142 #define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */
2143
2144 /*
2145 ** CAPI3REF: Enable Or Disable Extended Result Codes
2146 ** METHOD: sqlite3
2147 **
@@ -5507,10 +5523,45 @@
5523 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
5524 ** or else the use of the [data_store_directory pragma] should be avoided.
5525 */
5526 SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
5527
5528 /*
5529 ** CAPI3REF: Win32 Specific Interface
5530 **
5531 ** These interfaces are available only on Windows. The
5532 ** [sqlite3_win32_set_directory] interface is used to set the value associated
5533 ** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
5534 ** zValue, depending on the value of the type parameter. The zValue parameter
5535 ** should be NULL to cause the previous value to be freed via [sqlite3_free];
5536 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
5537 ** prior to being used. The [sqlite3_win32_set_directory] interface returns
5538 ** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
5539 ** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
5540 ** [sqlite3_data_directory] variable is intended to act as a replacement for
5541 ** the current directory on the sub-platforms of Win32 where that concept is
5542 ** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
5543 ** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
5544 ** sqlite3_win32_set_directory interface except the string parameter must be
5545 ** UTF-8 or UTF-16, respectively.
5546 */
5547 SQLITE_API int sqlite3_win32_set_directory(
5548 unsigned long type, /* Identifier for directory being set or reset */
5549 void *zValue /* New value for directory being set or reset */
5550 );
5551 SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
5552 SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
5553
5554 /*
5555 ** CAPI3REF: Win32 Directory Types
5556 **
5557 ** These macros are only available on Windows. They define the allowed values
5558 ** for the type argument to the [sqlite3_win32_set_directory] interface.
5559 */
5560 #define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
5561 #define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
5562
5563 /*
5564 ** CAPI3REF: Test For Auto-Commit Mode
5565 ** KEYWORDS: {autocommit mode}
5566 ** METHOD: sqlite3
5567 **
5568

Keyboard Shortcuts

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