Fossil SCM

Update the built-in SQLite to the latest 3.32.0 alpha that includes fixes for the DBCONFIG_MAINDBNAME problem.

drh 2020-03-22 14:29 trunk
Commit 8d114c2aff3a24183204bc92024b8033dd2243534d4b17f1f11f3646a242e110
3 files changed +237 -52 +460 -270 +1 -1
+237 -52
--- src/shell.c
+++ src/shell.c
@@ -413,10 +413,19 @@
413413
/*
414414
** True if an interrupt (Control-C) has been received.
415415
*/
416416
static volatile int seenInterrupt = 0;
417417
418
+#ifdef SQLITE_DEBUG
419
+/*
420
+** Out-of-memory simulator variables
421
+*/
422
+static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
423
+static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
424
+static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
425
+#endif /* SQLITE_DEBUG */
426
+
418427
/*
419428
** This is the name of our program. It is set in main(), used
420429
** in a number of other places, mostly for error messages.
421430
*/
422431
static char *Argv0;
@@ -463,10 +472,53 @@
463472
/* Indicate out-of-memory and exit. */
464473
static void shell_out_of_memory(void){
465474
raw_printf(stderr,"Error: out of memory\n");
466475
exit(1);
467476
}
477
+
478
+#ifdef SQLITE_DEBUG
479
+/* This routine is called when a simulated OOM occurs. It is broken
480
+** out as a separate routine to make it easy to set a breakpoint on
481
+** the OOM
482
+*/
483
+void shellOomFault(void){
484
+ if( oomRepeat>0 ){
485
+ oomRepeat--;
486
+ }else{
487
+ oomCounter--;
488
+ }
489
+}
490
+#endif /* SQLITE_DEBUG */
491
+
492
+#ifdef SQLITE_DEBUG
493
+/* This routine is a replacement malloc() that is used to simulate
494
+** Out-Of-Memory (OOM) errors for testing purposes.
495
+*/
496
+static void *oomMalloc(int nByte){
497
+ if( oomCounter ){
498
+ if( oomCounter==1 ){
499
+ shellOomFault();
500
+ return 0;
501
+ }else{
502
+ oomCounter--;
503
+ }
504
+ }
505
+ return defaultMalloc(nByte);
506
+}
507
+#endif /* SQLITE_DEBUG */
508
+
509
+#ifdef SQLITE_DEBUG
510
+/* Register the OOM simulator. This must occur before any memory
511
+** allocations */
512
+static void registerOomSimulator(void){
513
+ sqlite3_mem_methods mem;
514
+ sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
515
+ defaultMalloc = mem.xMalloc;
516
+ mem.xMalloc = oomMalloc;
517
+ sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
518
+}
519
+#endif
468520
469521
/*
470522
** Write I/O traces to the following stream.
471523
*/
472524
#ifdef SQLITE_ENABLE_IOTRACE
@@ -12086,10 +12138,22 @@
1208612138
" Run \".filectrl\" with no arguments for details",
1208712139
".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
1208812140
".headers on|off Turn display of headers on or off",
1208912141
".help ?-all? ?PATTERN? Show help text for PATTERN",
1209012142
".import FILE TABLE Import data from FILE into TABLE",
12143
+ " Options:",
12144
+ " --ascii Use \\037 and \\036 as column and row separators",
12145
+ " --csv Use , and \\n as column and row separators",
12146
+ " --skip N Skip the first N rows of input",
12147
+ " -v \"Verbose\" - increase auxiliary output",
12148
+ " Notes:",
12149
+ " * If TABLE does not exist, it is created. The first row of input",
12150
+ " determines the column names.",
12151
+ " * If neither --csv or --ascii are used, the input mode is derived",
12152
+ " from the \".mode\" output mode",
12153
+ " * If FILE begins with \"|\" then it is a command that generates the",
12154
+ " input text.",
1209112155
#ifndef SQLITE_OMIT_TEST_CONTROL
1209212156
".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
1209312157
#endif
1209412158
".indexes ?TABLE? Show names of indexes",
1209512159
" If TABLE is specified, only show indexes for",
@@ -12121,10 +12185,13 @@
1212112185
".once (-e|-x|FILE) Output for the next SQL command only to FILE",
1212212186
" If FILE begins with '|' then open as a pipe",
1212312187
" Other options:",
1212412188
" -e Invoke system text editor",
1212512189
" -x Open in a spreadsheet",
12190
+#ifdef SQLITE_DEBUG
12191
+ ".oom [--repeat M] [N] Simulate an OOM error on the N-th allocation",
12192
+#endif
1212612193
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
1212712194
" Options:",
1212812195
" --append Use appendvfs to append database to the end of FILE",
1212912196
#ifdef SQLITE_ENABLE_DESERIALIZE
1213012197
" --deserialize Load into memory useing sqlite3_deserialize()",
@@ -13071,10 +13138,12 @@
1307113138
FILE *in; /* Read the CSV text from this input stream */
1307213139
char *z; /* Accumulated text for a field */
1307313140
int n; /* Number of bytes in z */
1307413141
int nAlloc; /* Space allocated for z[] */
1307513142
int nLine; /* Current line number */
13143
+ int nRow; /* Number of rows imported */
13144
+ int nErr; /* Number of errors encountered */
1307613145
int bNotFirst; /* True if one or more bytes already read */
1307713146
int cTerm; /* Character that terminated the most recent field */
1307813147
int cColSep; /* The column separator character. (Usually ",") */
1307913148
int cRowSep; /* The row separator character. (Usually "\n") */
1308013149
};
@@ -16131,12 +16200,12 @@
1613116200
showHelp(p->out, 0);
1613216201
}
1613316202
}else
1613416203
1613516204
if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16136
- char *zTable; /* Insert data into this table */
16137
- char *zFile; /* Name of file to extra content from */
16205
+ char *zTable = 0; /* Insert data into this table */
16206
+ char *zFile = 0; /* Name of file to extra content from */
1613816207
sqlite3_stmt *pStmt = NULL; /* A statement */
1613916208
int nCol; /* Number of columns in the table */
1614016209
int nByte; /* Number of bytes in an SQL string */
1614116210
int i, j; /* Loop counters */
1614216211
int needCommit; /* True to COMMIT or ROLLBACK at end */
@@ -16143,75 +16212,141 @@
1614316212
int nSep; /* Number of bytes in p->colSeparator[] */
1614416213
char *zSql; /* An SQL statement */
1614516214
ImportCtx sCtx; /* Reader context */
1614616215
char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
1614716216
int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
16148
-
16149
- if( nArg!=3 ){
16150
- raw_printf(stderr, "Usage: .import FILE TABLE\n");
16151
- goto meta_command_exit;
16152
- }
16153
- zFile = azArg[1];
16154
- zTable = azArg[2];
16155
- seenInterrupt = 0;
16217
+ int eVerbose = 0; /* Larger for more console output */
16218
+ int nSkip = 0; /* Initial lines to skip */
16219
+ int useOutputMode = 1; /* Use output mode to determine separators */
16220
+
1615616221
memset(&sCtx, 0, sizeof(sCtx));
16157
- open_db(p, 0);
16158
- nSep = strlen30(p->colSeparator);
16159
- if( nSep==0 ){
16160
- raw_printf(stderr,
16161
- "Error: non-null column separator required for import\n");
16162
- return 1;
16163
- }
16164
- if( nSep>1 ){
16165
- raw_printf(stderr, "Error: multi-character column separators not allowed"
16166
- " for import\n");
16167
- return 1;
16168
- }
16169
- nSep = strlen30(p->rowSeparator);
16170
- if( nSep==0 ){
16171
- raw_printf(stderr, "Error: non-null row separator required for import\n");
16172
- return 1;
16173
- }
16174
- if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
16175
- /* When importing CSV (only), if the row separator is set to the
16176
- ** default output row separator, change it to the default input
16177
- ** row separator. This avoids having to maintain different input
16178
- ** and output row separators. */
16179
- sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16180
- nSep = strlen30(p->rowSeparator);
16181
- }
16182
- if( nSep>1 ){
16183
- raw_printf(stderr, "Error: multi-character row separators not allowed"
16184
- " for import\n");
16185
- return 1;
16222
+ if( p->mode==MODE_Ascii ){
16223
+ xRead = ascii_read_one_field;
16224
+ }else{
16225
+ xRead = csv_read_one_field;
16226
+ }
16227
+ for(i=1; i<nArg; i++){
16228
+ char *z = azArg[i];
16229
+ if( z[0]=='-' && z[1]=='-' ) z++;
16230
+ if( z[0]!='-' ){
16231
+ if( zFile==0 ){
16232
+ zFile = z;
16233
+ }else if( zTable==0 ){
16234
+ zTable = z;
16235
+ }else{
16236
+ utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
16237
+ showHelp(p->out, "import");
16238
+ rc = 1;
16239
+ goto meta_command_exit;
16240
+ }
16241
+ }else if( strcmp(z,"-v")==0 ){
16242
+ eVerbose++;
16243
+ }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
16244
+ nSkip = integerValue(azArg[++i]);
16245
+ }else if( strcmp(z,"-ascii")==0 ){
16246
+ sCtx.cColSep = SEP_Unit[0];
16247
+ sCtx.cRowSep = SEP_Record[0];
16248
+ xRead = ascii_read_one_field;
16249
+ useOutputMode = 0;
16250
+ }else if( strcmp(z,"-csv")==0 ){
16251
+ sCtx.cColSep = ',';
16252
+ sCtx.cRowSep = '\n';
16253
+ xRead = csv_read_one_field;
16254
+ useOutputMode = 0;
16255
+ }else{
16256
+ utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
16257
+ showHelp(p->out, "import");
16258
+ rc = 1;
16259
+ goto meta_command_exit;
16260
+ }
16261
+ }
16262
+ if( zTable==0 ){
16263
+ utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
16264
+ zFile==0 ? "FILE" : "TABLE");
16265
+ showHelp(p->out, "import");
16266
+ rc = 1;
16267
+ goto meta_command_exit;
16268
+ }
16269
+ seenInterrupt = 0;
16270
+ open_db(p, 0);
16271
+ if( useOutputMode ){
16272
+ /* If neither the --csv or --ascii options are specified, then set
16273
+ ** the column and row separator characters from the output mode. */
16274
+ nSep = strlen30(p->colSeparator);
16275
+ if( nSep==0 ){
16276
+ raw_printf(stderr,
16277
+ "Error: non-null column separator required for import\n");
16278
+ rc = 1;
16279
+ goto meta_command_exit;
16280
+ }
16281
+ if( nSep>1 ){
16282
+ raw_printf(stderr,
16283
+ "Error: multi-character column separators not allowed"
16284
+ " for import\n");
16285
+ rc = 1;
16286
+ goto meta_command_exit;
16287
+ }
16288
+ nSep = strlen30(p->rowSeparator);
16289
+ if( nSep==0 ){
16290
+ raw_printf(stderr,
16291
+ "Error: non-null row separator required for import\n");
16292
+ rc = 1;
16293
+ goto meta_command_exit;
16294
+ }
16295
+ if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
16296
+ /* When importing CSV (only), if the row separator is set to the
16297
+ ** default output row separator, change it to the default input
16298
+ ** row separator. This avoids having to maintain different input
16299
+ ** and output row separators. */
16300
+ sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16301
+ nSep = strlen30(p->rowSeparator);
16302
+ }
16303
+ if( nSep>1 ){
16304
+ raw_printf(stderr, "Error: multi-character row separators not allowed"
16305
+ " for import\n");
16306
+ rc = 1;
16307
+ goto meta_command_exit;
16308
+ }
16309
+ sCtx.cColSep = p->colSeparator[0];
16310
+ sCtx.cRowSep = p->rowSeparator[0];
1618616311
}
1618716312
sCtx.zFile = zFile;
1618816313
sCtx.nLine = 1;
1618916314
if( sCtx.zFile[0]=='|' ){
1619016315
#ifdef SQLITE_OMIT_POPEN
1619116316
raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16192
- return 1;
16317
+ rc = 1;
16318
+ goto meta_command_exit;
1619316319
#else
1619416320
sCtx.in = popen(sCtx.zFile+1, "r");
1619516321
sCtx.zFile = "<pipe>";
1619616322
xCloser = pclose;
1619716323
#endif
1619816324
}else{
1619916325
sCtx.in = fopen(sCtx.zFile, "rb");
1620016326
xCloser = fclose;
1620116327
}
16202
- if( p->mode==MODE_Ascii ){
16203
- xRead = ascii_read_one_field;
16204
- }else{
16205
- xRead = csv_read_one_field;
16206
- }
1620716328
if( sCtx.in==0 ){
1620816329
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16209
- return 1;
16330
+ rc = 1;
16331
+ goto meta_command_exit;
1621016332
}
16211
- sCtx.cColSep = p->colSeparator[0];
16212
- sCtx.cRowSep = p->rowSeparator[0];
16333
+ if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
16334
+ char zSep[2];
16335
+ zSep[1] = 0;
16336
+ zSep[0] = sCtx.cColSep;
16337
+ utf8_printf(p->out, "Column separator ");
16338
+ output_c_string(p->out, zSep);
16339
+ utf8_printf(p->out, ", row separator ");
16340
+ zSep[0] = sCtx.cRowSep;
16341
+ output_c_string(p->out, zSep);
16342
+ utf8_printf(p->out, "\n");
16343
+ }
16344
+ while( (nSkip--)>0 ){
16345
+ while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
16346
+ sCtx.nLine++;
16347
+ }
1621316348
zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
1621416349
if( zSql==0 ){
1621516350
xCloser(sCtx.in);
1621616351
shell_out_of_memory();
1621716352
}
@@ -16229,30 +16364,36 @@
1622916364
if( cSep=='(' ){
1623016365
sqlite3_free(zCreate);
1623116366
sqlite3_free(sCtx.z);
1623216367
xCloser(sCtx.in);
1623316368
utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16234
- return 1;
16369
+ rc = 1;
16370
+ goto meta_command_exit;
1623516371
}
1623616372
zCreate = sqlite3_mprintf("%z\n)", zCreate);
16373
+ if( eVerbose>=1 ){
16374
+ utf8_printf(p->out, "%s\n", zCreate);
16375
+ }
1623716376
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
1623816377
sqlite3_free(zCreate);
1623916378
if( rc ){
1624016379
utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
1624116380
sqlite3_errmsg(p->db));
1624216381
sqlite3_free(sCtx.z);
1624316382
xCloser(sCtx.in);
16244
- return 1;
16383
+ rc = 1;
16384
+ goto meta_command_exit;
1624516385
}
1624616386
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
1624716387
}
1624816388
sqlite3_free(zSql);
1624916389
if( rc ){
1625016390
if (pStmt) sqlite3_finalize(pStmt);
1625116391
utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
1625216392
xCloser(sCtx.in);
16253
- return 1;
16393
+ rc = 1;
16394
+ goto meta_command_exit;
1625416395
}
1625516396
nCol = sqlite3_column_count(pStmt);
1625616397
sqlite3_finalize(pStmt);
1625716398
pStmt = 0;
1625816399
if( nCol==0 ) return 0; /* no columns, no error */
@@ -16267,17 +16408,21 @@
1626716408
zSql[j++] = ',';
1626816409
zSql[j++] = '?';
1626916410
}
1627016411
zSql[j++] = ')';
1627116412
zSql[j] = 0;
16413
+ if( eVerbose>=2 ){
16414
+ utf8_printf(p->out, "Insert using: %s\n", zSql);
16415
+ }
1627216416
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
1627316417
sqlite3_free(zSql);
1627416418
if( rc ){
1627516419
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
1627616420
if (pStmt) sqlite3_finalize(pStmt);
1627716421
xCloser(sCtx.in);
16278
- return 1;
16422
+ rc = 1;
16423
+ goto meta_command_exit;
1627916424
}
1628016425
needCommit = sqlite3_get_autocommit(p->db);
1628116426
if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1628216427
do{
1628316428
int startLine = sCtx.nLine;
@@ -16316,18 +16461,26 @@
1631616461
sqlite3_step(pStmt);
1631716462
rc = sqlite3_reset(pStmt);
1631816463
if( rc!=SQLITE_OK ){
1631916464
utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
1632016465
startLine, sqlite3_errmsg(p->db));
16466
+ sCtx.nErr++;
16467
+ }else{
16468
+ sCtx.nRow++;
1632116469
}
1632216470
}
1632316471
}while( sCtx.cTerm!=EOF );
1632416472
1632516473
xCloser(sCtx.in);
1632616474
sqlite3_free(sCtx.z);
1632716475
sqlite3_finalize(pStmt);
1632816476
if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
16477
+ if( eVerbose>0 ){
16478
+ utf8_printf(p->out,
16479
+ "Added %d rows with %d errors using %d lines of input\n",
16480
+ sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
16481
+ }
1632916482
}else
1633016483
1633116484
#ifndef SQLITE_UNTESTABLE
1633216485
if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
1633316486
char *zSql;
@@ -16602,10 +16755,38 @@
1660216755
raw_printf(stderr, "Usage: .nullvalue STRING\n");
1660316756
rc = 1;
1660416757
}
1660516758
}else
1660616759
16760
+#ifdef SQLITE_DEBUG
16761
+ if( c=='o' && strcmp(azArg[0],"oom")==0 ){
16762
+ int i;
16763
+ for(i=1; i<nArg; i++){
16764
+ const char *z = azArg[i];
16765
+ if( z[0]=='-' && z[1]=='-' ) z++;
16766
+ if( strcmp(z,"-repeat")==0 ){
16767
+ if( i==nArg-1 ){
16768
+ raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
16769
+ rc = 1;
16770
+ }else{
16771
+ oomRepeat = (int)integerValue(azArg[++i]);
16772
+ }
16773
+ }else if( IsDigit(z[0]) ){
16774
+ oomCounter = (int)integerValue(azArg[i]);
16775
+ }else{
16776
+ raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
16777
+ raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
16778
+ rc = 1;
16779
+ }
16780
+ }
16781
+ if( rc==0 ){
16782
+ raw_printf(p->out, "oomCounter = %d\n", oomCounter);
16783
+ raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
16784
+ }
16785
+ }else
16786
+#endif /* SQLITE_DEBUG */
16787
+
1660716788
if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
1660816789
char *zNewFilename; /* Name of the database file to open */
1660916790
int iName = 1; /* Index in azArg[] of the filename */
1661016791
int newFlag = 0; /* True to delete file before opening */
1661116792
/* Close the existing database */
@@ -18684,10 +18865,14 @@
1868418865
1868518866
setBinaryMode(stdin, 0);
1868618867
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
1868718868
stdin_is_interactive = isatty(0);
1868818869
stdout_is_console = isatty(1);
18870
+
18871
+#ifdef SQLITE_DEBUG
18872
+ registerOomSimulator();
18873
+#endif
1868918874
1869018875
#if !defined(_WIN32_WCE)
1869118876
if( getenv("SQLITE_DEBUG_BREAK") ){
1869218877
if( isatty(0) && isatty(2) ){
1869318878
fprintf(stderr,
1869418879
--- src/shell.c
+++ src/shell.c
@@ -413,10 +413,19 @@
413 /*
414 ** True if an interrupt (Control-C) has been received.
415 */
416 static volatile int seenInterrupt = 0;
417
 
 
 
 
 
 
 
 
 
418 /*
419 ** This is the name of our program. It is set in main(), used
420 ** in a number of other places, mostly for error messages.
421 */
422 static char *Argv0;
@@ -463,10 +472,53 @@
463 /* Indicate out-of-memory and exit. */
464 static void shell_out_of_memory(void){
465 raw_printf(stderr,"Error: out of memory\n");
466 exit(1);
467 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
469 /*
470 ** Write I/O traces to the following stream.
471 */
472 #ifdef SQLITE_ENABLE_IOTRACE
@@ -12086,10 +12138,22 @@
12086 " Run \".filectrl\" with no arguments for details",
12087 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
12088 ".headers on|off Turn display of headers on or off",
12089 ".help ?-all? ?PATTERN? Show help text for PATTERN",
12090 ".import FILE TABLE Import data from FILE into TABLE",
 
 
 
 
 
 
 
 
 
 
 
 
12091 #ifndef SQLITE_OMIT_TEST_CONTROL
12092 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
12093 #endif
12094 ".indexes ?TABLE? Show names of indexes",
12095 " If TABLE is specified, only show indexes for",
@@ -12121,10 +12185,13 @@
12121 ".once (-e|-x|FILE) Output for the next SQL command only to FILE",
12122 " If FILE begins with '|' then open as a pipe",
12123 " Other options:",
12124 " -e Invoke system text editor",
12125 " -x Open in a spreadsheet",
 
 
 
12126 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
12127 " Options:",
12128 " --append Use appendvfs to append database to the end of FILE",
12129 #ifdef SQLITE_ENABLE_DESERIALIZE
12130 " --deserialize Load into memory useing sqlite3_deserialize()",
@@ -13071,10 +13138,12 @@
13071 FILE *in; /* Read the CSV text from this input stream */
13072 char *z; /* Accumulated text for a field */
13073 int n; /* Number of bytes in z */
13074 int nAlloc; /* Space allocated for z[] */
13075 int nLine; /* Current line number */
 
 
13076 int bNotFirst; /* True if one or more bytes already read */
13077 int cTerm; /* Character that terminated the most recent field */
13078 int cColSep; /* The column separator character. (Usually ",") */
13079 int cRowSep; /* The row separator character. (Usually "\n") */
13080 };
@@ -16131,12 +16200,12 @@
16131 showHelp(p->out, 0);
16132 }
16133 }else
16134
16135 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16136 char *zTable; /* Insert data into this table */
16137 char *zFile; /* Name of file to extra content from */
16138 sqlite3_stmt *pStmt = NULL; /* A statement */
16139 int nCol; /* Number of columns in the table */
16140 int nByte; /* Number of bytes in an SQL string */
16141 int i, j; /* Loop counters */
16142 int needCommit; /* True to COMMIT or ROLLBACK at end */
@@ -16143,75 +16212,141 @@
16143 int nSep; /* Number of bytes in p->colSeparator[] */
16144 char *zSql; /* An SQL statement */
16145 ImportCtx sCtx; /* Reader context */
16146 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
16147 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
16148
16149 if( nArg!=3 ){
16150 raw_printf(stderr, "Usage: .import FILE TABLE\n");
16151 goto meta_command_exit;
16152 }
16153 zFile = azArg[1];
16154 zTable = azArg[2];
16155 seenInterrupt = 0;
16156 memset(&sCtx, 0, sizeof(sCtx));
16157 open_db(p, 0);
16158 nSep = strlen30(p->colSeparator);
16159 if( nSep==0 ){
16160 raw_printf(stderr,
16161 "Error: non-null column separator required for import\n");
16162 return 1;
16163 }
16164 if( nSep>1 ){
16165 raw_printf(stderr, "Error: multi-character column separators not allowed"
16166 " for import\n");
16167 return 1;
16168 }
16169 nSep = strlen30(p->rowSeparator);
16170 if( nSep==0 ){
16171 raw_printf(stderr, "Error: non-null row separator required for import\n");
16172 return 1;
16173 }
16174 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
16175 /* When importing CSV (only), if the row separator is set to the
16176 ** default output row separator, change it to the default input
16177 ** row separator. This avoids having to maintain different input
16178 ** and output row separators. */
16179 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16180 nSep = strlen30(p->rowSeparator);
16181 }
16182 if( nSep>1 ){
16183 raw_printf(stderr, "Error: multi-character row separators not allowed"
16184 " for import\n");
16185 return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16186 }
16187 sCtx.zFile = zFile;
16188 sCtx.nLine = 1;
16189 if( sCtx.zFile[0]=='|' ){
16190 #ifdef SQLITE_OMIT_POPEN
16191 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16192 return 1;
 
16193 #else
16194 sCtx.in = popen(sCtx.zFile+1, "r");
16195 sCtx.zFile = "<pipe>";
16196 xCloser = pclose;
16197 #endif
16198 }else{
16199 sCtx.in = fopen(sCtx.zFile, "rb");
16200 xCloser = fclose;
16201 }
16202 if( p->mode==MODE_Ascii ){
16203 xRead = ascii_read_one_field;
16204 }else{
16205 xRead = csv_read_one_field;
16206 }
16207 if( sCtx.in==0 ){
16208 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16209 return 1;
 
16210 }
16211 sCtx.cColSep = p->colSeparator[0];
16212 sCtx.cRowSep = p->rowSeparator[0];
 
 
 
 
 
 
 
 
 
 
 
 
 
16213 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16214 if( zSql==0 ){
16215 xCloser(sCtx.in);
16216 shell_out_of_memory();
16217 }
@@ -16229,30 +16364,36 @@
16229 if( cSep=='(' ){
16230 sqlite3_free(zCreate);
16231 sqlite3_free(sCtx.z);
16232 xCloser(sCtx.in);
16233 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16234 return 1;
 
16235 }
16236 zCreate = sqlite3_mprintf("%z\n)", zCreate);
 
 
 
16237 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
16238 sqlite3_free(zCreate);
16239 if( rc ){
16240 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
16241 sqlite3_errmsg(p->db));
16242 sqlite3_free(sCtx.z);
16243 xCloser(sCtx.in);
16244 return 1;
 
16245 }
16246 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16247 }
16248 sqlite3_free(zSql);
16249 if( rc ){
16250 if (pStmt) sqlite3_finalize(pStmt);
16251 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
16252 xCloser(sCtx.in);
16253 return 1;
 
16254 }
16255 nCol = sqlite3_column_count(pStmt);
16256 sqlite3_finalize(pStmt);
16257 pStmt = 0;
16258 if( nCol==0 ) return 0; /* no columns, no error */
@@ -16267,17 +16408,21 @@
16267 zSql[j++] = ',';
16268 zSql[j++] = '?';
16269 }
16270 zSql[j++] = ')';
16271 zSql[j] = 0;
 
 
 
16272 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16273 sqlite3_free(zSql);
16274 if( rc ){
16275 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16276 if (pStmt) sqlite3_finalize(pStmt);
16277 xCloser(sCtx.in);
16278 return 1;
 
16279 }
16280 needCommit = sqlite3_get_autocommit(p->db);
16281 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
16282 do{
16283 int startLine = sCtx.nLine;
@@ -16316,18 +16461,26 @@
16316 sqlite3_step(pStmt);
16317 rc = sqlite3_reset(pStmt);
16318 if( rc!=SQLITE_OK ){
16319 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
16320 startLine, sqlite3_errmsg(p->db));
 
 
 
16321 }
16322 }
16323 }while( sCtx.cTerm!=EOF );
16324
16325 xCloser(sCtx.in);
16326 sqlite3_free(sCtx.z);
16327 sqlite3_finalize(pStmt);
16328 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
 
 
 
 
 
16329 }else
16330
16331 #ifndef SQLITE_UNTESTABLE
16332 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
16333 char *zSql;
@@ -16602,10 +16755,38 @@
16602 raw_printf(stderr, "Usage: .nullvalue STRING\n");
16603 rc = 1;
16604 }
16605 }else
16606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16607 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
16608 char *zNewFilename; /* Name of the database file to open */
16609 int iName = 1; /* Index in azArg[] of the filename */
16610 int newFlag = 0; /* True to delete file before opening */
16611 /* Close the existing database */
@@ -18684,10 +18865,14 @@
18684
18685 setBinaryMode(stdin, 0);
18686 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
18687 stdin_is_interactive = isatty(0);
18688 stdout_is_console = isatty(1);
 
 
 
 
18689
18690 #if !defined(_WIN32_WCE)
18691 if( getenv("SQLITE_DEBUG_BREAK") ){
18692 if( isatty(0) && isatty(2) ){
18693 fprintf(stderr,
18694
--- src/shell.c
+++ src/shell.c
@@ -413,10 +413,19 @@
413 /*
414 ** True if an interrupt (Control-C) has been received.
415 */
416 static volatile int seenInterrupt = 0;
417
418 #ifdef SQLITE_DEBUG
419 /*
420 ** Out-of-memory simulator variables
421 */
422 static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
423 static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
424 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
425 #endif /* SQLITE_DEBUG */
426
427 /*
428 ** This is the name of our program. It is set in main(), used
429 ** in a number of other places, mostly for error messages.
430 */
431 static char *Argv0;
@@ -463,10 +472,53 @@
472 /* Indicate out-of-memory and exit. */
473 static void shell_out_of_memory(void){
474 raw_printf(stderr,"Error: out of memory\n");
475 exit(1);
476 }
477
478 #ifdef SQLITE_DEBUG
479 /* This routine is called when a simulated OOM occurs. It is broken
480 ** out as a separate routine to make it easy to set a breakpoint on
481 ** the OOM
482 */
483 void shellOomFault(void){
484 if( oomRepeat>0 ){
485 oomRepeat--;
486 }else{
487 oomCounter--;
488 }
489 }
490 #endif /* SQLITE_DEBUG */
491
492 #ifdef SQLITE_DEBUG
493 /* This routine is a replacement malloc() that is used to simulate
494 ** Out-Of-Memory (OOM) errors for testing purposes.
495 */
496 static void *oomMalloc(int nByte){
497 if( oomCounter ){
498 if( oomCounter==1 ){
499 shellOomFault();
500 return 0;
501 }else{
502 oomCounter--;
503 }
504 }
505 return defaultMalloc(nByte);
506 }
507 #endif /* SQLITE_DEBUG */
508
509 #ifdef SQLITE_DEBUG
510 /* Register the OOM simulator. This must occur before any memory
511 ** allocations */
512 static void registerOomSimulator(void){
513 sqlite3_mem_methods mem;
514 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
515 defaultMalloc = mem.xMalloc;
516 mem.xMalloc = oomMalloc;
517 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
518 }
519 #endif
520
521 /*
522 ** Write I/O traces to the following stream.
523 */
524 #ifdef SQLITE_ENABLE_IOTRACE
@@ -12086,10 +12138,22 @@
12138 " Run \".filectrl\" with no arguments for details",
12139 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
12140 ".headers on|off Turn display of headers on or off",
12141 ".help ?-all? ?PATTERN? Show help text for PATTERN",
12142 ".import FILE TABLE Import data from FILE into TABLE",
12143 " Options:",
12144 " --ascii Use \\037 and \\036 as column and row separators",
12145 " --csv Use , and \\n as column and row separators",
12146 " --skip N Skip the first N rows of input",
12147 " -v \"Verbose\" - increase auxiliary output",
12148 " Notes:",
12149 " * If TABLE does not exist, it is created. The first row of input",
12150 " determines the column names.",
12151 " * If neither --csv or --ascii are used, the input mode is derived",
12152 " from the \".mode\" output mode",
12153 " * If FILE begins with \"|\" then it is a command that generates the",
12154 " input text.",
12155 #ifndef SQLITE_OMIT_TEST_CONTROL
12156 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
12157 #endif
12158 ".indexes ?TABLE? Show names of indexes",
12159 " If TABLE is specified, only show indexes for",
@@ -12121,10 +12185,13 @@
12185 ".once (-e|-x|FILE) Output for the next SQL command only to FILE",
12186 " If FILE begins with '|' then open as a pipe",
12187 " Other options:",
12188 " -e Invoke system text editor",
12189 " -x Open in a spreadsheet",
12190 #ifdef SQLITE_DEBUG
12191 ".oom [--repeat M] [N] Simulate an OOM error on the N-th allocation",
12192 #endif
12193 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
12194 " Options:",
12195 " --append Use appendvfs to append database to the end of FILE",
12196 #ifdef SQLITE_ENABLE_DESERIALIZE
12197 " --deserialize Load into memory useing sqlite3_deserialize()",
@@ -13071,10 +13138,12 @@
13138 FILE *in; /* Read the CSV text from this input stream */
13139 char *z; /* Accumulated text for a field */
13140 int n; /* Number of bytes in z */
13141 int nAlloc; /* Space allocated for z[] */
13142 int nLine; /* Current line number */
13143 int nRow; /* Number of rows imported */
13144 int nErr; /* Number of errors encountered */
13145 int bNotFirst; /* True if one or more bytes already read */
13146 int cTerm; /* Character that terminated the most recent field */
13147 int cColSep; /* The column separator character. (Usually ",") */
13148 int cRowSep; /* The row separator character. (Usually "\n") */
13149 };
@@ -16131,12 +16200,12 @@
16200 showHelp(p->out, 0);
16201 }
16202 }else
16203
16204 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16205 char *zTable = 0; /* Insert data into this table */
16206 char *zFile = 0; /* Name of file to extra content from */
16207 sqlite3_stmt *pStmt = NULL; /* A statement */
16208 int nCol; /* Number of columns in the table */
16209 int nByte; /* Number of bytes in an SQL string */
16210 int i, j; /* Loop counters */
16211 int needCommit; /* True to COMMIT or ROLLBACK at end */
@@ -16143,75 +16212,141 @@
16212 int nSep; /* Number of bytes in p->colSeparator[] */
16213 char *zSql; /* An SQL statement */
16214 ImportCtx sCtx; /* Reader context */
16215 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
16216 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
16217 int eVerbose = 0; /* Larger for more console output */
16218 int nSkip = 0; /* Initial lines to skip */
16219 int useOutputMode = 1; /* Use output mode to determine separators */
16220
 
 
 
 
16221 memset(&sCtx, 0, sizeof(sCtx));
16222 if( p->mode==MODE_Ascii ){
16223 xRead = ascii_read_one_field;
16224 }else{
16225 xRead = csv_read_one_field;
16226 }
16227 for(i=1; i<nArg; i++){
16228 char *z = azArg[i];
16229 if( z[0]=='-' && z[1]=='-' ) z++;
16230 if( z[0]!='-' ){
16231 if( zFile==0 ){
16232 zFile = z;
16233 }else if( zTable==0 ){
16234 zTable = z;
16235 }else{
16236 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
16237 showHelp(p->out, "import");
16238 rc = 1;
16239 goto meta_command_exit;
16240 }
16241 }else if( strcmp(z,"-v")==0 ){
16242 eVerbose++;
16243 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
16244 nSkip = integerValue(azArg[++i]);
16245 }else if( strcmp(z,"-ascii")==0 ){
16246 sCtx.cColSep = SEP_Unit[0];
16247 sCtx.cRowSep = SEP_Record[0];
16248 xRead = ascii_read_one_field;
16249 useOutputMode = 0;
16250 }else if( strcmp(z,"-csv")==0 ){
16251 sCtx.cColSep = ',';
16252 sCtx.cRowSep = '\n';
16253 xRead = csv_read_one_field;
16254 useOutputMode = 0;
16255 }else{
16256 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
16257 showHelp(p->out, "import");
16258 rc = 1;
16259 goto meta_command_exit;
16260 }
16261 }
16262 if( zTable==0 ){
16263 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
16264 zFile==0 ? "FILE" : "TABLE");
16265 showHelp(p->out, "import");
16266 rc = 1;
16267 goto meta_command_exit;
16268 }
16269 seenInterrupt = 0;
16270 open_db(p, 0);
16271 if( useOutputMode ){
16272 /* If neither the --csv or --ascii options are specified, then set
16273 ** the column and row separator characters from the output mode. */
16274 nSep = strlen30(p->colSeparator);
16275 if( nSep==0 ){
16276 raw_printf(stderr,
16277 "Error: non-null column separator required for import\n");
16278 rc = 1;
16279 goto meta_command_exit;
16280 }
16281 if( nSep>1 ){
16282 raw_printf(stderr,
16283 "Error: multi-character column separators not allowed"
16284 " for import\n");
16285 rc = 1;
16286 goto meta_command_exit;
16287 }
16288 nSep = strlen30(p->rowSeparator);
16289 if( nSep==0 ){
16290 raw_printf(stderr,
16291 "Error: non-null row separator required for import\n");
16292 rc = 1;
16293 goto meta_command_exit;
16294 }
16295 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
16296 /* When importing CSV (only), if the row separator is set to the
16297 ** default output row separator, change it to the default input
16298 ** row separator. This avoids having to maintain different input
16299 ** and output row separators. */
16300 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16301 nSep = strlen30(p->rowSeparator);
16302 }
16303 if( nSep>1 ){
16304 raw_printf(stderr, "Error: multi-character row separators not allowed"
16305 " for import\n");
16306 rc = 1;
16307 goto meta_command_exit;
16308 }
16309 sCtx.cColSep = p->colSeparator[0];
16310 sCtx.cRowSep = p->rowSeparator[0];
16311 }
16312 sCtx.zFile = zFile;
16313 sCtx.nLine = 1;
16314 if( sCtx.zFile[0]=='|' ){
16315 #ifdef SQLITE_OMIT_POPEN
16316 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16317 rc = 1;
16318 goto meta_command_exit;
16319 #else
16320 sCtx.in = popen(sCtx.zFile+1, "r");
16321 sCtx.zFile = "<pipe>";
16322 xCloser = pclose;
16323 #endif
16324 }else{
16325 sCtx.in = fopen(sCtx.zFile, "rb");
16326 xCloser = fclose;
16327 }
 
 
 
 
 
16328 if( sCtx.in==0 ){
16329 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16330 rc = 1;
16331 goto meta_command_exit;
16332 }
16333 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
16334 char zSep[2];
16335 zSep[1] = 0;
16336 zSep[0] = sCtx.cColSep;
16337 utf8_printf(p->out, "Column separator ");
16338 output_c_string(p->out, zSep);
16339 utf8_printf(p->out, ", row separator ");
16340 zSep[0] = sCtx.cRowSep;
16341 output_c_string(p->out, zSep);
16342 utf8_printf(p->out, "\n");
16343 }
16344 while( (nSkip--)>0 ){
16345 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
16346 sCtx.nLine++;
16347 }
16348 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16349 if( zSql==0 ){
16350 xCloser(sCtx.in);
16351 shell_out_of_memory();
16352 }
@@ -16229,30 +16364,36 @@
16364 if( cSep=='(' ){
16365 sqlite3_free(zCreate);
16366 sqlite3_free(sCtx.z);
16367 xCloser(sCtx.in);
16368 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16369 rc = 1;
16370 goto meta_command_exit;
16371 }
16372 zCreate = sqlite3_mprintf("%z\n)", zCreate);
16373 if( eVerbose>=1 ){
16374 utf8_printf(p->out, "%s\n", zCreate);
16375 }
16376 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
16377 sqlite3_free(zCreate);
16378 if( rc ){
16379 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
16380 sqlite3_errmsg(p->db));
16381 sqlite3_free(sCtx.z);
16382 xCloser(sCtx.in);
16383 rc = 1;
16384 goto meta_command_exit;
16385 }
16386 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16387 }
16388 sqlite3_free(zSql);
16389 if( rc ){
16390 if (pStmt) sqlite3_finalize(pStmt);
16391 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
16392 xCloser(sCtx.in);
16393 rc = 1;
16394 goto meta_command_exit;
16395 }
16396 nCol = sqlite3_column_count(pStmt);
16397 sqlite3_finalize(pStmt);
16398 pStmt = 0;
16399 if( nCol==0 ) return 0; /* no columns, no error */
@@ -16267,17 +16408,21 @@
16408 zSql[j++] = ',';
16409 zSql[j++] = '?';
16410 }
16411 zSql[j++] = ')';
16412 zSql[j] = 0;
16413 if( eVerbose>=2 ){
16414 utf8_printf(p->out, "Insert using: %s\n", zSql);
16415 }
16416 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16417 sqlite3_free(zSql);
16418 if( rc ){
16419 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16420 if (pStmt) sqlite3_finalize(pStmt);
16421 xCloser(sCtx.in);
16422 rc = 1;
16423 goto meta_command_exit;
16424 }
16425 needCommit = sqlite3_get_autocommit(p->db);
16426 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
16427 do{
16428 int startLine = sCtx.nLine;
@@ -16316,18 +16461,26 @@
16461 sqlite3_step(pStmt);
16462 rc = sqlite3_reset(pStmt);
16463 if( rc!=SQLITE_OK ){
16464 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
16465 startLine, sqlite3_errmsg(p->db));
16466 sCtx.nErr++;
16467 }else{
16468 sCtx.nRow++;
16469 }
16470 }
16471 }while( sCtx.cTerm!=EOF );
16472
16473 xCloser(sCtx.in);
16474 sqlite3_free(sCtx.z);
16475 sqlite3_finalize(pStmt);
16476 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
16477 if( eVerbose>0 ){
16478 utf8_printf(p->out,
16479 "Added %d rows with %d errors using %d lines of input\n",
16480 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
16481 }
16482 }else
16483
16484 #ifndef SQLITE_UNTESTABLE
16485 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
16486 char *zSql;
@@ -16602,10 +16755,38 @@
16755 raw_printf(stderr, "Usage: .nullvalue STRING\n");
16756 rc = 1;
16757 }
16758 }else
16759
16760 #ifdef SQLITE_DEBUG
16761 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
16762 int i;
16763 for(i=1; i<nArg; i++){
16764 const char *z = azArg[i];
16765 if( z[0]=='-' && z[1]=='-' ) z++;
16766 if( strcmp(z,"-repeat")==0 ){
16767 if( i==nArg-1 ){
16768 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
16769 rc = 1;
16770 }else{
16771 oomRepeat = (int)integerValue(azArg[++i]);
16772 }
16773 }else if( IsDigit(z[0]) ){
16774 oomCounter = (int)integerValue(azArg[i]);
16775 }else{
16776 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
16777 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
16778 rc = 1;
16779 }
16780 }
16781 if( rc==0 ){
16782 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
16783 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
16784 }
16785 }else
16786 #endif /* SQLITE_DEBUG */
16787
16788 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
16789 char *zNewFilename; /* Name of the database file to open */
16790 int iName = 1; /* Index in azArg[] of the filename */
16791 int newFlag = 0; /* True to delete file before opening */
16792 /* Close the existing database */
@@ -18684,10 +18865,14 @@
18865
18866 setBinaryMode(stdin, 0);
18867 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
18868 stdin_is_interactive = isatty(0);
18869 stdout_is_console = isatty(1);
18870
18871 #ifdef SQLITE_DEBUG
18872 registerOomSimulator();
18873 #endif
18874
18875 #if !defined(_WIN32_WCE)
18876 if( getenv("SQLITE_DEBUG_BREAK") ){
18877 if( isatty(0) && isatty(2) ){
18878 fprintf(stderr,
18879
+460 -270
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1162,11 +1162,11 @@
11621162
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11631163
** [sqlite_version()] and [sqlite_source_id()].
11641164
*/
11651165
#define SQLITE_VERSION "3.32.0"
11661166
#define SQLITE_VERSION_NUMBER 3032000
1167
-#define SQLITE_SOURCE_ID "2020-02-27 16:21:39 951b39ca74c9bd933139e099d5555283278db475f410f202c162e5d1e6aef933"
1167
+#define SQLITE_SOURCE_ID "2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84f525"
11681168
11691169
/*
11701170
** CAPI3REF: Run-Time Library Version Numbers
11711171
** KEYWORDS: sqlite3_version sqlite3_sourceid
11721172
**
@@ -16539,11 +16539,10 @@
1653916539
** have been filled out. If the schema changes, these column names might
1654016540
** changes and so the view will need to be reset.
1654116541
*/
1654216542
#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
1654316543
#define DB_UnresetViews 0x0002 /* Some views have defined column names */
16544
-#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
1654516544
#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
1654616545
1654716546
/*
1654816547
** The number of different kinds of things that can be limited
1654916548
** using the sqlite3_limit() interface.
@@ -16697,11 +16696,11 @@
1669716696
** Each database connection is an instance of the following structure.
1669816697
*/
1669916698
struct sqlite3 {
1670016699
sqlite3_vfs *pVfs; /* OS Interface */
1670116700
struct Vdbe *pVdbe; /* List of active virtual machines */
16702
- CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
16701
+ CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
1670316702
sqlite3_mutex *mutex; /* Connection mutex */
1670416703
Db *aDb; /* All backends */
1670516704
int nDb; /* Number of backends currently in use */
1670616705
u32 mDbFlags; /* flags recording internal state */
1670716706
u64 flags; /* flags settable by pragmas. See below */
@@ -16906,10 +16905,11 @@
1690616905
#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
1690716906
#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
1690816907
#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
1690916908
#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
1691016909
#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
16910
+#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
1691116911
1691216912
/*
1691316913
** Bits of the sqlite3.dbOptFlags field that are used by the
1691416914
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
1691516915
** selectively disable various optimizations.
@@ -17869,10 +17869,13 @@
1786917869
char affExpr; /* affinity, or RAISE type */
1787017870
u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
1787117871
** TK_COLUMN: the value of p5 for OP_Column
1787217872
** TK_AGG_FUNCTION: nesting depth
1787317873
** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
17874
+#ifdef SQLITE_DEBUG
17875
+ u8 vvaFlags; /* Verification flags. */
17876
+#endif
1787417877
u32 flags; /* Various flags. EP_* See below */
1787517878
union {
1787617879
char *zToken; /* Token value. Zero terminated and dequoted */
1787717880
int iValue; /* Non-negative integer value if EP_IntValue */
1787817881
} u;
@@ -17943,11 +17946,11 @@
1794317946
#define EP_Skip 0x001000 /* Operator does not contribute to affinity */
1794417947
#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
1794517948
#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
1794617949
#define EP_Win 0x008000 /* Contains window functions */
1794717950
#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17948
-#define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
17951
+ /* 0x020000 // available for reuse */
1794917952
#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
1795017953
#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
1795117954
#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
1795217955
#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
1795317956
#define EP_Alias 0x400000 /* Is an alias for a result set column */
@@ -17957,10 +17960,11 @@
1795717960
#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
1795817961
#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
1795917962
#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
1796017963
#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
1796117964
#define EP_FromDDL 0x40000000 /* Originates from sqlite_master */
17965
+ /* 0x80000000 // Available */
1796217966
1796317967
/*
1796417968
** The EP_Propagate mask is a set of properties that automatically propagate
1796517969
** upwards into parent nodes.
1796617970
*/
@@ -17975,18 +17979,28 @@
1797517979
#define ExprSetProperty(E,P) (E)->flags|=(P)
1797617980
#define ExprClearProperty(E,P) (E)->flags&=~(P)
1797717981
#define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
1797817982
#define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
1797917983
17984
+
17985
+/* Flags for use with Expr.vvaFlags
17986
+*/
17987
+#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
17988
+#define EP_Immutable 0x02 /* Do not change this Expr node */
17989
+
1798017990
/* The ExprSetVVAProperty() macro is used for Verification, Validation,
1798117991
** and Accreditation only. It works like ExprSetProperty() during VVA
1798217992
** processes but is a no-op for delivery.
1798317993
*/
1798417994
#ifdef SQLITE_DEBUG
17985
-# define ExprSetVVAProperty(E,P) (E)->flags|=(P)
17995
+# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
17996
+# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
17997
+# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
1798617998
#else
1798717999
# define ExprSetVVAProperty(E,P)
18000
+# define ExprHasVVAProperty(E,P) 0
18001
+# define ExprClearVVAProperties(E)
1798818002
#endif
1798918003
1799018004
/*
1799118005
** Macros to determine the number of bytes required by a normal Expr
1799218006
** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
@@ -18956,10 +18970,11 @@
1895618970
Select *pSelect; /* HAVING to WHERE clause ctx */
1895718971
struct WindowRewrite *pRewrite; /* Window rewrite context */
1895818972
struct WhereConst *pConst; /* WHERE clause constants */
1895918973
struct RenameCtx *pRename; /* RENAME COLUMN context */
1896018974
struct Table *pTab; /* Table of generated column */
18975
+ struct SrcList_item *pSrcItem; /* A single FROM clause item */
1896118976
} u;
1896218977
};
1896318978
1896418979
/* Forward declarations */
1896518980
SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -19500,11 +19515,11 @@
1950019515
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
1950119516
SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
1950219517
#endif
1950319518
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
1950419519
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
19505
-SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
19520
+SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
1950619521
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
1950719522
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
1950819523
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
1950919524
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
1951019525
#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
@@ -19655,10 +19670,11 @@
1965519670
# define sqlite3AuthRead(a,b,c,d)
1965619671
# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
1965719672
# define sqlite3AuthContextPush(a,b,c)
1965819673
# define sqlite3AuthContextPop(a) ((void)(a))
1965919674
#endif
19675
+SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
1966019676
SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
1966119677
SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
1966219678
SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
1966319679
SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
1966419680
SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
@@ -19714,14 +19730,14 @@
1971419730
#define putVarint sqlite3PutVarint
1971519731
1971619732
1971719733
SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
1971819734
SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19719
-SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
19720
-SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
19735
+SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
19736
+SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
1972119737
SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
19722
-SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
19738
+SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
1972319739
SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
1972419740
SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
1972519741
SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
1972619742
SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
1972719743
SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
@@ -19740,13 +19756,14 @@
1974019756
SQLITE_PRIVATE const char *sqlite3ErrStr(int);
1974119757
SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
1974219758
SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
1974319759
SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
1974419760
SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19745
-SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
19746
-SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
19747
-SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
19761
+SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8);
19762
+SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
19763
+SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
19764
+SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
1974819765
SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
1974919766
SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
1975019767
SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
1975119768
SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
1975219769
SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
@@ -19809,10 +19826,11 @@
1980919826
const struct ExprList_item*,
1981019827
const char*,
1981119828
const char*,
1981219829
const char*
1981319830
);
19831
+SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
1981419832
SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
1981519833
SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
1981619834
SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
1981719835
SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
1981819836
SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
@@ -19973,12 +19991,12 @@
1997319991
#ifdef SQLITE_ENABLE_NORMALIZE
1997419992
SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
1997519993
#endif
1997619994
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
1997719995
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
19978
-SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,Expr*);
19979
-SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
19996
+SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
19997
+SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
1998019998
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
1998119999
SQLITE_PRIVATE const char *sqlite3JournalModename(int);
1998220000
#ifndef SQLITE_OMIT_WAL
1998320001
SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
1998420002
SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
@@ -20947,13 +20965,13 @@
2094720965
#endif
2094820966
u16 nResColumn; /* Number of columns in one row of the result set */
2094920967
u8 errorAction; /* Recovery action to do in case of an error */
2095020968
u8 minWriteFileFormat; /* Minimum file format for writable database files */
2095120969
u8 prepFlags; /* SQLITE_PREPARE_* flags */
20970
+ u8 doingRerun; /* True if rerunning after an auto-reprepare */
2095220971
bft expired:2; /* 1: recompile VM immediately 2: when convenient */
2095320972
bft explain:2; /* True if EXPLAIN present on SQL command */
20954
- bft doingRerun:1; /* True if rerunning after an auto-reprepare */
2095520973
bft changeCntOn:1; /* True to update the change-counter */
2095620974
bft runOnlyOnce:1; /* Automatically expire on reset */
2095720975
bft usesStmtJournal:1; /* True if uses a statement journal */
2095820976
bft readOnly:1; /* True for statements that do not write */
2095920977
bft bIsReader:1; /* True for statements that read */
@@ -29390,12 +29408,12 @@
2939029408
sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
2939129409
}else if( pItem->zName ){
2939229410
sqlite3_str_appendf(&x, " %s", pItem->zName);
2939329411
}
2939429412
if( pItem->pTab ){
29395
- sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p",
29396
- pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab);
29413
+ sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
29414
+ pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
2939729415
}
2939829416
if( pItem->zAlias ){
2939929417
sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
2940029418
}
2940129419
if( pItem->fg.jointype & JT_LEFT ){
@@ -29650,27 +29668,30 @@
2965029668
** Generate a human-readable explanation of an expression tree.
2965129669
*/
2965229670
SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
2965329671
const char *zBinOp = 0; /* Binary operator */
2965429672
const char *zUniOp = 0; /* Unary operator */
29655
- char zFlgs[60];
29673
+ char zFlgs[200];
2965629674
pView = sqlite3TreeViewPush(pView, moreToFollow);
2965729675
if( pExpr==0 ){
2965829676
sqlite3TreeViewLine(pView, "nil");
2965929677
sqlite3TreeViewPop(pView);
2966029678
return;
2966129679
}
29662
- if( pExpr->flags || pExpr->affExpr ){
29680
+ if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
2966329681
StrAccum x;
2966429682
sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
2966529683
sqlite3_str_appendf(&x, " fg.af=%x.%c",
2966629684
pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
2966729685
if( ExprHasProperty(pExpr, EP_FromJoin) ){
2966829686
sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
2966929687
}
2967029688
if( ExprHasProperty(pExpr, EP_FromDDL) ){
2967129689
sqlite3_str_appendf(&x, " DDL");
29690
+ }
29691
+ if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
29692
+ sqlite3_str_appendf(&x, " IMMUTABLE");
2967229693
}
2967329694
sqlite3StrAccumFinish(&x);
2967429695
}else{
2967529696
zFlgs[0] = 0;
2967629697
}
@@ -29774,10 +29795,11 @@
2977429795
case TK_SLASH: zBinOp = "DIV"; break;
2977529796
case TK_LSHIFT: zBinOp = "LSHIFT"; break;
2977629797
case TK_RSHIFT: zBinOp = "RSHIFT"; break;
2977729798
case TK_CONCAT: zBinOp = "CONCAT"; break;
2977829799
case TK_DOT: zBinOp = "DOT"; break;
29800
+ case TK_LIMIT: zBinOp = "LIMIT"; break;
2977929801
2978029802
case TK_UMINUS: zUniOp = "UMINUS"; break;
2978129803
case TK_UPLUS: zUniOp = "UPLUS"; break;
2978229804
case TK_BITNOT: zUniOp = "BITNOT"; break;
2978329805
case TK_NOT: zUniOp = "NOT"; break;
@@ -50895,11 +50917,11 @@
5089550917
}
5089650918
5089750919
/*
5089850920
** Allocate a new RowSetEntry object that is associated with the
5089950921
** given RowSet. Return a pointer to the new and completely uninitialized
50900
-** objected.
50922
+** object.
5090150923
**
5090250924
** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
5090350925
** routine returns NULL.
5090450926
*/
5090550927
static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
@@ -51171,11 +51193,11 @@
5117151193
if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
5117251194
p = pRowSet->pEntry;
5117351195
if( p ){
5117451196
struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
5117551197
if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51176
- /* Only sort the current set of entiries if they need it */
51198
+ /* Only sort the current set of entries if they need it */
5117751199
p = rowSetEntrySort(p);
5117851200
}
5117951201
for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
5118051202
ppPrevTree = &pTree->pRight;
5118151203
if( pTree->pLeft==0 ){
@@ -64536,11 +64558,11 @@
6453664558
** free-list for reuse. It returns false if it is safe to retrieve the
6453764559
** page from the pager layer with the 'no-content' flag set. True otherwise.
6453864560
*/
6453964561
static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
6454064562
Bitvec *p = pBt->pHasContent;
64541
- return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
64563
+ return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
6454264564
}
6454364565
6454464566
/*
6454564567
** Clear (destroy) the BtShared.pHasContent bitvec. This should be
6454664568
** invoked at the conclusion of each write-transaction.
@@ -76180,23 +76202,18 @@
7618076202
u16 mFlags;
7618176203
if( pVdbe->db->flags & SQLITE_VdbeTrace ){
7618276204
sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
7618376205
(int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
7618476206
}
76185
- /* If pX is marked as a shallow copy of pMem, then verify that
76207
+ /* If pX is marked as a shallow copy of pMem, then try to verify that
7618676208
** no significant changes have been made to pX since the OP_SCopy.
7618776209
** A significant change would indicated a missed call to this
7618876210
** function for pX. Minor changes, such as adding or removing a
7618976211
** dual type, are allowed, as long as the underlying value is the
7619076212
** same. */
7619176213
mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
7619276214
assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
76193
- /* assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r ); */
76194
- /* ^^ */
76195
- /* Cannot reliably compare doubles for equality */
76196
- assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) );
76197
- assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 );
7619876215
7619976216
/* pMem is the register that is changing. But also mark pX as
7620076217
** undefined so that we can quickly detect the shallow-copy error */
7620176218
pX->flags = MEM_Undefined;
7620276219
pX->pScopyFrom = 0;
@@ -77547,11 +77564,11 @@
7754777564
(void)z2;
7754877565
}
7754977566
#endif
7755077567
7755177568
/*
77552
-** Add a new OP_ opcode.
77569
+** Add a new OP_Explain opcode.
7755377570
**
7755477571
** If the bPush flag is true, then make this opcode the parent for
7755577572
** subsequent Explains until sqlite3VdbeExplainPop() is called.
7755677573
*/
7755777574
SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
@@ -78781,12 +78798,15 @@
7878178798
displayP4Expr(&x, pOp->p4.pExpr);
7878278799
break;
7878378800
}
7878478801
#endif
7878578802
case P4_COLLSEQ: {
78803
+ static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
7878678804
CollSeq *pColl = pOp->p4.pColl;
78787
- sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
78805
+ assert( pColl->enc>=0 && pColl->enc<4 );
78806
+ sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
78807
+ encnames[pColl->enc]);
7878878808
break;
7878978809
}
7879078810
case P4_FUNCDEF: {
7879178811
FuncDef *pDef = pOp->p4.pFunc;
7879278812
sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
@@ -79495,10 +79515,11 @@
7949579515
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
7949679516
"id", "parent", "notused", "detail"
7949779517
};
7949879518
int iFirst, mx, i;
7949979519
if( nMem<10 ) nMem = 10;
79520
+ p->explain = pParse->explain;
7950079521
if( pParse->explain==2 ){
7950179522
sqlite3VdbeSetNumCols(p, 4);
7950279523
iFirst = 8;
7950379524
mx = 12;
7950479525
}else{
@@ -79545,11 +79566,10 @@
7954579566
}
7954679567
}
7954779568
7954879569
p->pVList = pParse->pVList;
7954979570
pParse->pVList = 0;
79550
- p->explain = pParse->explain;
7955179571
if( db->mallocFailed ){
7955279572
p->nVar = 0;
7955379573
p->nCursor = 0;
7955479574
p->nMem = 0;
7955579575
}else{
@@ -86230,11 +86250,10 @@
8623086250
u16 flags2; /* Initial flags for P2 */
8623186251
8623286252
pIn1 = &aMem[pOp->p1];
8623386253
pIn2 = &aMem[pOp->p2];
8623486254
pOut = &aMem[pOp->p3];
86235
- testcase( pIn1==pIn2 );
8623686255
testcase( pOut==pIn2 );
8623786256
assert( pIn1!=pOut );
8623886257
flags1 = pIn1->flags;
8623986258
testcase( flags1 & MEM_Null );
8624086259
testcase( pIn2->flags & MEM_Null );
@@ -88351,11 +88370,11 @@
8835188370
**
8835288371
** Allowed P5 bits:
8835388372
** <ul>
8835488373
** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
8835588374
** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88356
-** of OP_SeekLE/OP_IdxGT)
88375
+** of OP_SeekLE/OP_IdxLT)
8835788376
** </ul>
8835888377
**
8835988378
** The P4 value may be either an integer (P4_INT32) or a pointer to
8836088379
** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
8836188380
** object, then table being opened must be an [index b-tree] where the
@@ -88381,11 +88400,11 @@
8838188400
**
8838288401
** Allowed P5 bits:
8838388402
** <ul>
8838488403
** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
8838588404
** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88386
-** of OP_SeekLE/OP_IdxGT)
88405
+** of OP_SeekLE/OP_IdxLT)
8838788406
** </ul>
8838888407
**
8838988408
** See also: OP_OpenRead, OP_OpenWrite
8839088409
*/
8839188410
/* Opcode: OpenWrite P1 P2 P3 P4 P5
@@ -88405,11 +88424,11 @@
8840588424
**
8840688425
** Allowed P5 bits:
8840788426
** <ul>
8840888427
** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
8840988428
** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88410
-** of OP_SeekLE/OP_IdxGT)
88429
+** of OP_SeekLE/OP_IdxLT)
8841188430
** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
8841288431
** and subsequently delete entries in an index btree. This is a
8841388432
** hint to the storage engine that the storage engine is allowed to
8841488433
** ignore. The hint is not used by the official SQLite b*tree storage
8841588434
** engine, but is used by COMDB2.
@@ -88517,13 +88536,11 @@
8851788536
8851888537
open_cursor_set_hints:
8851988538
assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
8852088539
assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
8852188540
testcase( pOp->p5 & OPFLAG_BULKCSR );
88522
-#ifdef SQLITE_ENABLE_CURSOR_HINTS
8852388541
testcase( pOp->p2 & OPFLAG_SEEKEQ );
88524
-#endif
8852588542
sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
8852688543
(pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
8852788544
if( rc ) goto abort_due_to_error;
8852888545
break;
8852988546
}
@@ -88775,15 +88792,17 @@
8877588792
** Reposition cursor P1 so that it points to the smallest entry that
8877688793
** is greater than or equal to the key value. If there are no records
8877788794
** greater than or equal to the key and P2 is not zero, then jump to P2.
8877888795
**
8877988796
** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
88780
-** opcode will always land on a record that equally equals the key, or
88781
-** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
88782
-** opcode must be followed by an IdxLE opcode with the same arguments.
88783
-** The IdxLE opcode will be skipped if this opcode succeeds, but the
88784
-** IdxLE opcode will be used on subsequent loop iterations.
88797
+** opcode will either land on a record that exactly matches the key, or
88798
+** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
88799
+** this opcode must be followed by an IdxLE opcode with the same arguments.
88800
+** The IdxGT opcode will be skipped if this opcode succeeds, but the
88801
+** IdxGT opcode will be used on subsequent loop iterations. The
88802
+** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
88803
+** is an equality search.
8878588804
**
8878688805
** This opcode leaves the cursor configured to move in forward order,
8878788806
** from the beginning toward the end. In other words, the cursor is
8878888807
** configured to use Next, not Prev.
8878988808
**
@@ -88795,11 +88814,11 @@
8879588814
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
8879688815
** use the value in register P3 as a key. If cursor P1 refers
8879788816
** to an SQL index, then P3 is the first in an array of P4 registers
8879888817
** that are used as an unpacked index key.
8879988818
**
88800
-** Reposition cursor P1 so that it points to the smallest entry that
88819
+** Reposition cursor P1 so that it points to the smallest entry that
8880188820
** is greater than the key value. If there are no records greater than
8880288821
** the key and P2 is not zero, then jump to P2.
8880388822
**
8880488823
** This opcode leaves the cursor configured to move in forward order,
8880588824
** from the beginning toward the end. In other words, the cursor is
@@ -88840,15 +88859,17 @@
8884088859
** This opcode leaves the cursor configured to move in reverse order,
8884188860
** from the end toward the beginning. In other words, the cursor is
8884288861
** configured to use Prev, not Next.
8884388862
**
8884488863
** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
88845
-** opcode will always land on a record that equally equals the key, or
88846
-** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
88847
-** opcode must be followed by an IdxGE opcode with the same arguments.
88864
+** opcode will either land on a record that exactly matches the key, or
88865
+** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
88866
+** this opcode must be followed by an IdxLE opcode with the same arguments.
8884888867
** The IdxGE opcode will be skipped if this opcode succeeds, but the
88849
-** IdxGE opcode will be used on subsequent loop iterations.
88868
+** IdxGE opcode will be used on subsequent loop iterations. The
88869
+** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
88870
+** is an equality search.
8885088871
**
8885188872
** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
8885288873
*/
8885388874
case OP_SeekLT: /* jump, in3, group */
8885488875
case OP_SeekLE: /* jump, in3, group */
@@ -88881,11 +88902,11 @@
8888188902
8888288903
pC->deferredMoveto = 0;
8888388904
pC->cacheStatus = CACHE_STALE;
8888488905
if( pC->isTable ){
8888588906
u16 flags3, newType;
88886
- /* The BTREE_SEEK_EQ flag is only set on index cursors */
88907
+ /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
8888788908
assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
8888888909
|| CORRUPT_DB );
8888988910
8889088911
/* The input value in P3 might be of any type: integer, real, string,
8889188912
** blob, or NULL. But it needs to be an integer before we can do
@@ -88940,18 +88961,21 @@
8894088961
pC->movetoTarget = iKey; /* Used by OP_Delete */
8894188962
if( rc!=SQLITE_OK ){
8894288963
goto abort_due_to_error;
8894388964
}
8894488965
}else{
88945
- /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
88946
- ** OP_SeekLE opcodes are allowed, and these must be immediately followed
88947
- ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
88966
+ /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
88967
+ ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
88968
+ ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
88969
+ ** with the same key.
8894888970
*/
8894988971
if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
8895088972
eqOnly = 1;
8895188973
assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
8895288974
assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
88975
+ assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
88976
+ assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
8895388977
assert( pOp[1].p1==pOp[0].p1 );
8895488978
assert( pOp[1].p2==pOp[0].p2 );
8895588979
assert( pOp[1].p3==pOp[0].p3 );
8895688980
assert( pOp[1].p4.i==pOp[0].p4.i );
8895788981
}
@@ -91162,11 +91186,11 @@
9116291186
** try to reuse register values from the first use. */
9116391187
{
9116491188
int i;
9116591189
for(i=0; i<p->nMem; i++){
9116691190
aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
91167
- aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
91191
+ MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
9116891192
}
9116991193
}
9117091194
#endif
9117191195
pOp = &aOp[-1];
9117291196
goto check_for_interrupt;
@@ -96555,19 +96579,20 @@
9655596579
SrcList *pSrc;
9655696580
int i;
9655796581
struct SrcList_item *pItem;
9655896582
9655996583
pSrc = p->pSrc;
96560
- assert( pSrc!=0 );
96561
- for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
96562
- if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
96563
- return WRC_Abort;
96564
- }
96565
- if( pItem->fg.isTabFunc
96566
- && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
96567
- ){
96568
- return WRC_Abort;
96584
+ if( pSrc ){
96585
+ for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
96586
+ if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
96587
+ return WRC_Abort;
96588
+ }
96589
+ if( pItem->fg.isTabFunc
96590
+ && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
96591
+ ){
96592
+ return WRC_Abort;
96593
+ }
9656996594
}
9657096595
}
9657196596
return WRC_Continue;
9657296597
}
9657396598
@@ -96784,10 +96809,35 @@
9678496809
}else{
9678596810
/* Currently parsing a DML statement */
9678696811
return (db->flags & SQLITE_DqsDML)!=0;
9678796812
}
9678896813
}
96814
+
96815
+/*
96816
+** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
96817
+** return the appropriate colUsed mask.
96818
+*/
96819
+SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr *pExpr){
96820
+ int n;
96821
+ Table *pExTab;
96822
+
96823
+ n = pExpr->iColumn;
96824
+ pExTab = pExpr->y.pTab;
96825
+ assert( pExTab!=0 );
96826
+ if( (pExTab->tabFlags & TF_HasGenerated)!=0
96827
+ && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
96828
+ ){
96829
+ testcase( pExTab->nCol==BMS-1 );
96830
+ testcase( pExTab->nCol==BMS );
96831
+ return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
96832
+ }else{
96833
+ testcase( n==BMS-1 );
96834
+ testcase( n==BMS );
96835
+ if( n>=BMS ) n = BMS-1;
96836
+ return ((Bitmask)1)<<n;
96837
+ }
96838
+}
9678996839
9679096840
/*
9679196841
** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
9679296842
** that name in the set of source tables in pSrcList and make the pExpr
9679396843
** expression node refer back to that source column. The following changes
@@ -96861,10 +96911,16 @@
9686196911
assert( db->aDb[i].zDbSName );
9686296912
if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
9686396913
pSchema = db->aDb[i].pSchema;
9686496914
break;
9686596915
}
96916
+ }
96917
+ if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
96918
+ /* This branch is taken when the main database has been renamed
96919
+ ** using SQLITE_DBCONFIG_MAINDBNAME. */
96920
+ pSchema = db->aDb[0].pSchema;
96921
+ zDb = db->aDb[0].zDbSName;
9686696922
}
9686796923
}
9686896924
}
9686996925
9687096926
/* Start at the inner-most context and move outward until a match is found */
@@ -97181,26 +97237,11 @@
9718197237
**
9718297238
** If a generated column is referenced, set bits for every column
9718397239
** of the table.
9718497240
*/
9718597241
if( pExpr->iColumn>=0 && pMatch!=0 ){
97186
- int n = pExpr->iColumn;
97187
- Table *pExTab = pExpr->y.pTab;
97188
- assert( pExTab!=0 );
97189
- assert( pMatch->iCursor==pExpr->iTable );
97190
- if( (pExTab->tabFlags & TF_HasGenerated)!=0
97191
- && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
97192
- ){
97193
- testcase( pExTab->nCol==BMS-1 );
97194
- testcase( pExTab->nCol==BMS );
97195
- pMatch->colUsed = pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
97196
- }else{
97197
- testcase( n==BMS-1 );
97198
- testcase( n==BMS );
97199
- if( n>=BMS ) n = BMS-1;
97200
- pMatch->colUsed |= ((Bitmask)1)<<n;
97201
- }
97242
+ pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
9720297243
}
9720397244
9720497245
/* Clean up and return
9720597246
*/
9720697247
sqlite3ExprDelete(db, pExpr->pLeft);
@@ -98557,11 +98598,11 @@
9855798598
** CREATE TABLE t1(a);
9855898599
** SELECT * FROM t1 WHERE a;
9855998600
** SELECT a AS b FROM t1 WHERE b;
9856098601
** SELECT * FROM t1 WHERE (select a from t1);
9856198602
*/
98562
-SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
98603
+SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
9856398604
int op;
9856498605
while( ExprHasProperty(pExpr, EP_Skip) ){
9856598606
assert( pExpr->op==TK_COLLATE );
9856698607
pExpr = pExpr->pLeft;
9856798608
assert( pExpr!=0 );
@@ -98667,14 +98708,14 @@
9866798708
** The collating sequence might be determined by a COLLATE operator
9866898709
** or by the presence of a column with a defined collating sequence.
9866998710
** COLLATE operators take first precedence. Left operands take
9867098711
** precedence over right operands.
9867198712
*/
98672
-SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
98713
+SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
9867398714
sqlite3 *db = pParse->db;
9867498715
CollSeq *pColl = 0;
98675
- Expr *p = pExpr;
98716
+ const Expr *p = pExpr;
9867698717
while( p ){
9867798718
int op = p->op;
9867898719
if( op==TK_REGISTER ) op = p->op2;
9867998720
if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
9868098721
&& p->y.pTab!=0
@@ -98739,21 +98780,21 @@
9873998780
** See also: sqlite3ExprCollSeq()
9874098781
**
9874198782
** The sqlite3ExprCollSeq() routine works the same except that it
9874298783
** returns NULL if there is no defined collation.
9874398784
*/
98744
-SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
98785
+SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
9874598786
CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
9874698787
if( p==0 ) p = pParse->db->pDfltColl;
9874798788
assert( p!=0 );
9874898789
return p;
9874998790
}
9875098791
9875198792
/*
9875298793
** Return TRUE if the two expressions have equivalent collating sequences.
9875398794
*/
98754
-SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
98795
+SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
9875598796
CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
9875698797
CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
9875798798
return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
9875898799
}
9875998800
@@ -98760,11 +98801,11 @@
9876098801
/*
9876198802
** pExpr is an operand of a comparison operator. aff2 is the
9876298803
** type affinity of the other operand. This routine returns the
9876398804
** type affinity that should be used for the comparison operator.
9876498805
*/
98765
-SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
98806
+SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
9876698807
char aff1 = sqlite3ExprAffinity(pExpr);
9876798808
if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
9876898809
/* Both sides of the comparison are columns. If one has numeric
9876998810
** affinity, use that. Otherwise use no affinity.
9877098811
*/
@@ -98782,11 +98823,11 @@
9878298823
9878398824
/*
9878498825
** pExpr is a comparison operator. Return the type affinity that should
9878598826
** be applied to both operands prior to doing the comparison.
9878698827
*/
98787
-static char comparisonAffinity(Expr *pExpr){
98828
+static char comparisonAffinity(const Expr *pExpr){
9878898829
char aff;
9878998830
assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
9879098831
pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
9879198832
pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
9879298833
assert( pExpr->pLeft );
@@ -98805,11 +98846,11 @@
9880598846
** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
9880698847
** idx_affinity is the affinity of an indexed column. Return true
9880798848
** if the index with affinity idx_affinity may be used to implement
9880898849
** the comparison in pExpr.
9880998850
*/
98810
-SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
98851
+SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
9881198852
char aff = comparisonAffinity(pExpr);
9881298853
if( aff<SQLITE_AFF_TEXT ){
9881398854
return 1;
9881498855
}
9881598856
if( aff==SQLITE_AFF_TEXT ){
@@ -98820,11 +98861,15 @@
9882098861
9882198862
/*
9882298863
** Return the P5 value that should be used for a binary comparison
9882398864
** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
9882498865
*/
98825
-static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
98866
+static u8 binaryCompareP5(
98867
+ const Expr *pExpr1, /* Left operand */
98868
+ const Expr *pExpr2, /* Right operand */
98869
+ int jumpIfNull /* Extra flags added to P5 */
98870
+){
9882698871
u8 aff = (char)sqlite3ExprAffinity(pExpr2);
9882798872
aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
9882898873
return aff;
9882998874
}
9883098875
@@ -98840,12 +98885,12 @@
9884098885
** Argument pRight (but not pLeft) may be a null pointer. In this case,
9884198886
** it is not considered.
9884298887
*/
9884398888
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
9884498889
Parse *pParse,
98845
- Expr *pLeft,
98846
- Expr *pRight
98890
+ const Expr *pLeft,
98891
+ const Expr *pRight
9884798892
){
9884898893
CollSeq *pColl;
9884998894
assert( pLeft );
9885098895
if( pLeft->flags & EP_Collate ){
9885198896
pColl = sqlite3ExprCollSeq(pParse, pLeft);
@@ -98866,11 +98911,11 @@
9886698911
** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
9886798912
** However, if the OP_Commuted flag is set, then the order of the operands
9886898913
** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
9886998914
** correct collating sequence is found.
9887098915
*/
98871
-SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, Expr *p){
98916
+SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
9887298917
if( ExprHasProperty(p, EP_Commuted) ){
9887398918
return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
9887498919
}else{
9887598920
return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
9887698921
}
@@ -99109,10 +99154,11 @@
9910999154
int regRight = 0;
9911099155
u8 opx = op;
9911199156
int addrDone = sqlite3VdbeMakeLabel(pParse);
9911299157
int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
9911399158
99159
+ assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
9911499160
if( pParse->nErr ) return;
9911599161
if( nLeft!=sqlite3ExprVectorSize(pRight) ){
9911699162
sqlite3ErrorMsg(pParse, "row value misused");
9911799163
return;
9911899164
}
@@ -99721,11 +99767,11 @@
9972199767
nSize = EXPR_FULLSIZE;
9972299768
}else{
9972399769
assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
9972499770
assert( !ExprHasProperty(p, EP_FromJoin) );
9972599771
assert( !ExprHasProperty(p, EP_MemToken) );
99726
- assert( !ExprHasProperty(p, EP_NoReduce) );
99772
+ assert( !ExprHasVVAProperty(p, EP_NoReduce) );
9972799773
if( p->pLeft || p->x.pList ){
9972899774
nSize = EXPR_REDUCEDSIZE | EP_Reduced;
9972999775
}else{
9973099776
assert( p->pRight==0 );
9973199777
nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
@@ -99826,10 +99872,14 @@
9982699872
9982799873
/* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
9982899874
pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
9982999875
pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
9983099876
pNew->flags |= staticFlag;
99877
+ ExprClearVVAProperties(pNew);
99878
+ if( dupFlags ){
99879
+ ExprSetVVAProperty(pNew, EP_Immutable);
99880
+ }
9983199881
9983299882
/* Copy the p->u.zToken string, if any. */
9983399883
if( nToken ){
9983499884
char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
9983599885
memcpy(zToken, p->u.zToken, nToken);
@@ -100602,11 +100652,11 @@
100602100652
** (3) the expression does not contain any EP_FixedCol TK_COLUMN
100603100653
** operands created by the constant propagation optimization.
100604100654
**
100605100655
** When this routine returns true, it indicates that the expression
100606100656
** can be added to the pParse->pConstExpr list and evaluated once when
100607
-** the prepared statement starts up. See sqlite3ExprCodeAtInit().
100657
+** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
100608100658
*/
100609100659
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
100610100660
return exprIsConst(p, 2, 0);
100611100661
}
100612100662
@@ -101365,10 +101415,11 @@
101365101415
return;
101366101416
}
101367101417
101368101418
/* Begin coding the subroutine */
101369101419
ExprSetProperty(pExpr, EP_Subrtn);
101420
+ assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
101370101421
pExpr->y.sub.regReturn = ++pParse->nMem;
101371101422
pExpr->y.sub.iAddr =
101372101423
sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
101373101424
VdbeComment((v, "return address"));
101374101425
@@ -101685,10 +101736,11 @@
101685101736
int addrTruthOp; /* Address of opcode that determines the IN is true */
101686101737
int destNotNull; /* Jump here if a comparison is not true in step 6 */
101687101738
int addrTop; /* Top of the step-6 loop */
101688101739
int iTab = 0; /* Index to use */
101689101740
101741
+ assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
101690101742
pLeft = pExpr->pLeft;
101691101743
if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
101692101744
zAff = exprINAffinity(pParse, pExpr);
101693101745
nVector = sqlite3ExprVectorSize(pExpr->pLeft);
101694101746
aiMap = (int*)sqlite3DbMallocZero(
@@ -102011,11 +102063,11 @@
102011102063
if( pParse->iSelfTab>0 ){
102012102064
iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
102013102065
}else{
102014102066
iAddr = 0;
102015102067
}
102016
- sqlite3ExprCode(pParse, pCol->pDflt, regOut);
102068
+ sqlite3ExprCodeCopy(pParse, pCol->pDflt, regOut);
102017102069
if( pCol->affinity>=SQLITE_AFF_TEXT ){
102018102070
sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
102019102071
}
102020102072
if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
102021102073
}
@@ -102295,10 +102347,11 @@
102295102347
102296102348
expr_code_doover:
102297102349
if( pExpr==0 ){
102298102350
op = TK_NULL;
102299102351
}else{
102352
+ assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
102300102353
op = pExpr->op;
102301102354
}
102302102355
switch( op ){
102303102356
case TK_AGG_COLUMN: {
102304102357
AggInfo *pAggInfo = pExpr->pAggInfo;
@@ -102305,12 +102358,21 @@
102305102358
struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
102306102359
if( !pAggInfo->directMode ){
102307102360
assert( pCol->iMem>0 );
102308102361
return pCol->iMem;
102309102362
}else if( pAggInfo->useSortingIdx ){
102363
+ Table *pTab = pCol->pTab;
102310102364
sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
102311102365
pCol->iSorterColumn, target);
102366
+ if( pCol->iColumn<0 ){
102367
+ VdbeComment((v,"%s.rowid",pTab->zName));
102368
+ }else{
102369
+ VdbeComment((v,"%s.%s",pTab->zName,pTab->aCol[pCol->iColumn].zName));
102370
+ if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
102371
+ sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
102372
+ }
102373
+ }
102312102374
return target;
102313102375
}
102314102376
/* Otherwise, fall thru into the TK_COLUMN case */
102315102377
}
102316102378
case TK_COLUMN: {
@@ -102549,10 +102611,11 @@
102549102611
#endif
102550102612
}else{
102551102613
tempX.op = TK_INTEGER;
102552102614
tempX.flags = EP_IntValue|EP_TokenOnly;
102553102615
tempX.u.iValue = 0;
102616
+ ExprClearVVAProperties(&tempX);
102554102617
r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
102555102618
r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
102556102619
sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
102557102620
testcase( regFree2==0 );
102558102621
}
@@ -102620,20 +102683,17 @@
102620102683
return pExpr->y.pWin->regResult;
102621102684
}
102622102685
#endif
102623102686
102624102687
if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
102625
- /* SQL functions can be expensive. So try to move constant functions
102626
- ** out of the inner loop, even if that means an extra OP_Copy. */
102627
- return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
102688
+ /* SQL functions can be expensive. So try to avoid running them
102689
+ ** multiple times if we know they always give the same result */
102690
+ return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
102628102691
}
102629102692
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
102630
- if( ExprHasProperty(pExpr, EP_TokenOnly) ){
102631
- pFarg = 0;
102632
- }else{
102633
- pFarg = pExpr->x.pList;
102634
- }
102693
+ assert( !ExprHasProperty(pExpr, EP_TokenOnly) );
102694
+ pFarg = pExpr->x.pList;
102635102695
nFarg = pFarg ? pFarg->nExpr : 0;
102636102696
assert( !ExprHasProperty(pExpr, EP_IntValue) );
102637102697
zId = pExpr->u.zToken;
102638102698
pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
102639102699
#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
@@ -103003,19 +103063,27 @@
103003103063
sqlite3ReleaseTempReg(pParse, regFree2);
103004103064
return inReg;
103005103065
}
103006103066
103007103067
/*
103008
-** Factor out the code of the given expression to initialization time.
103068
+** Generate code that will evaluate expression pExpr just one time
103069
+** per prepared statement execution.
103070
+**
103071
+** If the expression uses functions (that might throw an exception) then
103072
+** guard them with an OP_Once opcode to ensure that the code is only executed
103073
+** once. If no functions are involved, then factor the code out and put it at
103074
+** the end of the prepared statement in the initialization section.
103009103075
**
103010103076
** If regDest>=0 then the result is always stored in that register and the
103011103077
** result is not reusable. If regDest<0 then this routine is free to
103012103078
** store the value whereever it wants. The register where the expression
103013
-** is stored is returned. When regDest<0, two identical expressions will
103014
-** code to the same register.
103079
+** is stored is returned. When regDest<0, two identical expressions might
103080
+** code to the same register, if they do not contain function calls and hence
103081
+** are factored out into the initialization section at the end of the
103082
+** prepared statement.
103015103083
*/
103016
-SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
103084
+SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
103017103085
Parse *pParse, /* Parsing context */
103018103086
Expr *pExpr, /* The expression to code when the VDBE initializes */
103019103087
int regDest /* Store the value in this register */
103020103088
){
103021103089
ExprList *p;
@@ -103029,18 +103097,33 @@
103029103097
return pItem->u.iConstExprReg;
103030103098
}
103031103099
}
103032103100
}
103033103101
pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
103034
- p = sqlite3ExprListAppend(pParse, p, pExpr);
103035
- if( p ){
103036
- struct ExprList_item *pItem = &p->a[p->nExpr-1];
103037
- pItem->reusable = regDest<0;
103038
- if( regDest<0 ) regDest = ++pParse->nMem;
103039
- pItem->u.iConstExprReg = regDest;
103040
- }
103041
- pParse->pConstExpr = p;
103102
+ if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){
103103
+ Vdbe *v = pParse->pVdbe;
103104
+ int addr;
103105
+ assert( v );
103106
+ addr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
103107
+ pParse->okConstFactor = 0;
103108
+ if( !pParse->db->mallocFailed ){
103109
+ if( regDest<0 ) regDest = ++pParse->nMem;
103110
+ sqlite3ExprCode(pParse, pExpr, regDest);
103111
+ }
103112
+ pParse->okConstFactor = 1;
103113
+ sqlite3ExprDelete(pParse->db, pExpr);
103114
+ sqlite3VdbeJumpHere(v, addr);
103115
+ }else{
103116
+ p = sqlite3ExprListAppend(pParse, p, pExpr);
103117
+ if( p ){
103118
+ struct ExprList_item *pItem = &p->a[p->nExpr-1];
103119
+ pItem->reusable = regDest<0;
103120
+ if( regDest<0 ) regDest = ++pParse->nMem;
103121
+ pItem->u.iConstExprReg = regDest;
103122
+ }
103123
+ pParse->pConstExpr = p;
103124
+ }
103042103125
return regDest;
103043103126
}
103044103127
103045103128
/*
103046103129
** Generate code to evaluate an expression and store the results
@@ -103061,11 +103144,11 @@
103061103144
if( ConstFactorOk(pParse)
103062103145
&& pExpr->op!=TK_REGISTER
103063103146
&& sqlite3ExprIsConstantNotJoin(pExpr)
103064103147
){
103065103148
*pReg = 0;
103066
- r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
103149
+ r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
103067103150
}else{
103068103151
int r1 = sqlite3GetTempReg(pParse);
103069103152
r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
103070103153
if( r2==r1 ){
103071103154
*pReg = r1;
@@ -103083,10 +103166,11 @@
103083103166
** in register target.
103084103167
*/
103085103168
SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
103086103169
int inReg;
103087103170
103171
+ assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) );
103088103172
assert( target>0 && target<=pParse->nMem );
103089103173
inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
103090103174
assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
103091103175
if( inReg!=target && pParse->pVdbe ){
103092103176
u8 op;
@@ -103117,13 +103201,13 @@
103117103201
** in register target. If the expression is constant, then this routine
103118103202
** might choose to code the expression at initialization time.
103119103203
*/
103120103204
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
103121103205
if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
103122
- sqlite3ExprCodeAtInit(pParse, pExpr, target);
103206
+ sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
103123103207
}else{
103124
- sqlite3ExprCode(pParse, pExpr, target);
103208
+ sqlite3ExprCodeCopy(pParse, pExpr, target);
103125103209
}
103126103210
}
103127103211
103128103212
/*
103129103213
** Generate code that pushes the value of every element of the given
@@ -103177,11 +103261,11 @@
103177103261
sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
103178103262
}
103179103263
}else if( (flags & SQLITE_ECEL_FACTOR)!=0
103180103264
&& sqlite3ExprIsConstantNotJoin(pExpr)
103181103265
){
103182
- sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
103266
+ sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
103183103267
}else{
103184103268
int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
103185103269
if( inReg!=target+i ){
103186103270
VdbeOp *pOp;
103187103271
if( copyOp==OP_Copy
@@ -103300,10 +103384,11 @@
103300103384
int r1, r2;
103301103385
103302103386
assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
103303103387
if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
103304103388
if( NEVER(pExpr==0) ) return; /* No way this can happen */
103389
+ assert( !ExprHasVVAProperty(pExpr, EP_Immutable) );
103305103390
op = pExpr->op;
103306103391
switch( op ){
103307103392
case TK_AND:
103308103393
case TK_OR: {
103309103394
Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
@@ -103441,10 +103526,11 @@
103441103526
int r1, r2;
103442103527
103443103528
assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
103444103529
if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
103445103530
if( pExpr==0 ) return;
103531
+ assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
103446103532
103447103533
/* The value of pExpr->op and op are related as follows:
103448103534
**
103449103535
** pExpr->op op
103450103536
** --------- ----------
@@ -103724,36 +103810,22 @@
103724103810
return 2;
103725103811
}
103726103812
}
103727103813
if( (pA->flags & (EP_Distinct|EP_Commuted))
103728103814
!= (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
103729
- if( (combinedFlags & EP_TokenOnly)==0 ){
103815
+ if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
103730103816
if( combinedFlags & EP_xIsSelect ) return 2;
103731103817
if( (combinedFlags & EP_FixedCol)==0
103732103818
&& sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
103733103819
if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
103734103820
if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
103735103821
if( pA->op!=TK_STRING
103736103822
&& pA->op!=TK_TRUEFALSE
103737
- && (combinedFlags & EP_Reduced)==0
103823
+ && ALWAYS((combinedFlags & EP_Reduced)==0)
103738103824
){
103739103825
if( pA->iColumn!=pB->iColumn ) return 2;
103740
- if( pA->op2!=pB->op2 ){
103741
- if( pA->op==TK_TRUTH ) return 2;
103742
- if( pA->op==TK_FUNCTION && iTab<0 ){
103743
- /* Ex: CREATE TABLE t1(a CHECK( a<julianday('now') ));
103744
- ** INSERT INTO t1(a) VALUES(julianday('now')+10);
103745
- ** Without this test, sqlite3ExprCodeAtInit() will run on the
103746
- ** the julianday() of INSERT first, and remember that expression.
103747
- ** Then sqlite3ExprCodeInit() will see the julianday() in the CHECK
103748
- ** constraint as redundant, reusing the one from the INSERT, even
103749
- ** though the julianday() in INSERT lacks the critical NC_IsCheck
103750
- ** flag. See ticket [830277d9db6c3ba1] (2019-10-30)
103751
- */
103752
- return 2;
103753
- }
103754
- }
103826
+ if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
103755103827
if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
103756103828
return 2;
103757103829
}
103758103830
}
103759103831
}
@@ -106442,13 +106514,13 @@
106442106514
/*
106443106515
** Three SQL functions - stat_init(), stat_push(), and stat_get() -
106444106516
** share an instance of the following structure to hold their state
106445106517
** information.
106446106518
*/
106447
-typedef struct Stat4Accum Stat4Accum;
106448
-typedef struct Stat4Sample Stat4Sample;
106449
-struct Stat4Sample {
106519
+typedef struct StatAccum StatAccum;
106520
+typedef struct StatSample StatSample;
106521
+struct StatSample {
106450106522
tRowcnt *anEq; /* sqlite_stat4.nEq */
106451106523
tRowcnt *anDLt; /* sqlite_stat4.nDLt */
106452106524
#ifdef SQLITE_ENABLE_STAT4
106453106525
tRowcnt *anLt; /* sqlite_stat4.nLt */
106454106526
union {
@@ -106459,31 +106531,33 @@
106459106531
u8 isPSample; /* True if a periodic sample */
106460106532
int iCol; /* If !isPSample, the reason for inclusion */
106461106533
u32 iHash; /* Tiebreaker hash */
106462106534
#endif
106463106535
};
106464
-struct Stat4Accum {
106536
+struct StatAccum {
106537
+ sqlite3 *db; /* Database connection, for malloc() */
106465106538
tRowcnt nRow; /* Number of rows in the entire table */
106466
- tRowcnt nPSample; /* How often to do a periodic sample */
106467106539
int nCol; /* Number of columns in index + pk/rowid */
106468106540
int nKeyCol; /* Number of index columns w/o the pk/rowid */
106541
+ StatSample current; /* Current row as a StatSample */
106542
+#ifdef SQLITE_ENABLE_STAT4
106543
+ tRowcnt nPSample; /* How often to do a periodic sample */
106469106544
int mxSample; /* Maximum number of samples to accumulate */
106470
- Stat4Sample current; /* Current row as a Stat4Sample */
106471106545
u32 iPrn; /* Pseudo-random number used for sampling */
106472
- Stat4Sample *aBest; /* Array of nCol best samples */
106546
+ StatSample *aBest; /* Array of nCol best samples */
106473106547
int iMin; /* Index in a[] of entry with minimum score */
106474106548
int nSample; /* Current number of samples */
106475106549
int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
106476106550
int iGet; /* Index of current sample accessed by stat_get() */
106477
- Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
106478
- sqlite3 *db; /* Database connection, for malloc() */
106551
+ StatSample *a; /* Array of mxSample StatSample objects */
106552
+#endif
106479106553
};
106480106554
106481
-/* Reclaim memory used by a Stat4Sample
106555
+/* Reclaim memory used by a StatSample
106482106556
*/
106483106557
#ifdef SQLITE_ENABLE_STAT4
106484
-static void sampleClear(sqlite3 *db, Stat4Sample *p){
106558
+static void sampleClear(sqlite3 *db, StatSample *p){
106485106559
assert( db!=0 );
106486106560
if( p->nRowid ){
106487106561
sqlite3DbFree(db, p->u.aRowid);
106488106562
p->nRowid = 0;
106489106563
}
@@ -106491,11 +106565,11 @@
106491106565
#endif
106492106566
106493106567
/* Initialize the BLOB value of a ROWID
106494106568
*/
106495106569
#ifdef SQLITE_ENABLE_STAT4
106496
-static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
106570
+static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
106497106571
assert( db!=0 );
106498106572
if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
106499106573
p->u.aRowid = sqlite3DbMallocRawNN(db, n);
106500106574
if( p->u.aRowid ){
106501106575
p->nRowid = n;
@@ -106507,11 +106581,11 @@
106507106581
#endif
106508106582
106509106583
/* Initialize the INTEGER value of a ROWID.
106510106584
*/
106511106585
#ifdef SQLITE_ENABLE_STAT4
106512
-static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
106586
+static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
106513106587
assert( db!=0 );
106514106588
if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
106515106589
p->nRowid = 0;
106516106590
p->u.iRowid = iRowid;
106517106591
}
@@ -106520,11 +106594,11 @@
106520106594
106521106595
/*
106522106596
** Copy the contents of object (*pFrom) into (*pTo).
106523106597
*/
106524106598
#ifdef SQLITE_ENABLE_STAT4
106525
-static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
106599
+static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
106526106600
pTo->isPSample = pFrom->isPSample;
106527106601
pTo->iCol = pFrom->iCol;
106528106602
pTo->iHash = pFrom->iHash;
106529106603
memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
106530106604
memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
@@ -106536,14 +106610,14 @@
106536106610
}
106537106611
}
106538106612
#endif
106539106613
106540106614
/*
106541
-** Reclaim all memory of a Stat4Accum structure.
106615
+** Reclaim all memory of a StatAccum structure.
106542106616
*/
106543
-static void stat4Destructor(void *pOld){
106544
- Stat4Accum *p = (Stat4Accum*)pOld;
106617
+static void statAccumDestructor(void *pOld){
106618
+ StatAccum *p = (StatAccum*)pOld;
106545106619
#ifdef SQLITE_ENABLE_STAT4
106546106620
int i;
106547106621
for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
106548106622
for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
106549106623
sampleClear(p->db, &p->current);
@@ -106567,21 +106641,21 @@
106567106641
** For indexes on ordinary rowid tables, N==K+1. But for indexes on
106568106642
** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
106569106643
** PRIMARY KEY of the table. The covering index that implements the
106570106644
** original WITHOUT ROWID table as N==K as a special case.
106571106645
**
106572
-** This routine allocates the Stat4Accum object in heap memory. The return
106573
-** value is a pointer to the Stat4Accum object. The datatype of the
106574
-** return value is BLOB, but it is really just a pointer to the Stat4Accum
106646
+** This routine allocates the StatAccum object in heap memory. The return
106647
+** value is a pointer to the StatAccum object. The datatype of the
106648
+** return value is BLOB, but it is really just a pointer to the StatAccum
106575106649
** object.
106576106650
*/
106577106651
static void statInit(
106578106652
sqlite3_context *context,
106579106653
int argc,
106580106654
sqlite3_value **argv
106581106655
){
106582
- Stat4Accum *p;
106656
+ StatAccum *p;
106583106657
int nCol; /* Number of columns in index being sampled */
106584106658
int nKeyCol; /* Number of key columns */
106585106659
int nColUp; /* nCol rounded up for alignment */
106586106660
int n; /* Bytes of space to allocate */
106587106661
sqlite3 *db; /* Database connection */
@@ -106596,17 +106670,17 @@
106596106670
nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
106597106671
nKeyCol = sqlite3_value_int(argv[1]);
106598106672
assert( nKeyCol<=nCol );
106599106673
assert( nKeyCol>0 );
106600106674
106601
- /* Allocate the space required for the Stat4Accum object */
106675
+ /* Allocate the space required for the StatAccum object */
106602106676
n = sizeof(*p)
106603
- + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
106604
- + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
106677
+ + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
106678
+ + sizeof(tRowcnt)*nColUp /* StatAccum.anDLt */
106605106679
#ifdef SQLITE_ENABLE_STAT4
106606
- + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
106607
- + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
106680
+ + sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
106681
+ + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
106608106682
+ sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
106609106683
#endif
106610106684
;
106611106685
db = sqlite3_context_db_handle(context);
106612106686
p = sqlite3DbMallocZero(db, n);
@@ -106631,12 +106705,12 @@
106631106705
p->mxSample = mxSample;
106632106706
p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
106633106707
p->current.anLt = &p->current.anEq[nColUp];
106634106708
p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
106635106709
106636
- /* Set up the Stat4Accum.a[] and aBest[] arrays */
106637
- p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
106710
+ /* Set up the StatAccum.a[] and aBest[] arrays */
106711
+ p->a = (struct StatSample*)&p->current.anLt[nColUp];
106638106712
p->aBest = &p->a[mxSample];
106639106713
pSpace = (u8*)(&p->a[mxSample+nCol]);
106640106714
for(i=0; i<(mxSample+nCol); i++){
106641106715
p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
106642106716
p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
@@ -106652,11 +106726,11 @@
106652106726
106653106727
/* Return a pointer to the allocated object to the caller. Note that
106654106728
** only the pointer (the 2nd parameter) matters. The size of the object
106655106729
** (given by the 3rd parameter) is never used and can be any positive
106656106730
** value. */
106657
- sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
106731
+ sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
106658106732
}
106659106733
static const FuncDef statInitFuncdef = {
106660106734
2+IsStat4, /* nArg */
106661106735
SQLITE_UTF8, /* funcFlags */
106662106736
0, /* pUserData */
@@ -106679,13 +106753,13 @@
106679106753
**
106680106754
** This function assumes that for each argument sample, the contents of
106681106755
** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
106682106756
*/
106683106757
static int sampleIsBetterPost(
106684
- Stat4Accum *pAccum,
106685
- Stat4Sample *pNew,
106686
- Stat4Sample *pOld
106758
+ StatAccum *pAccum,
106759
+ StatSample *pNew,
106760
+ StatSample *pOld
106687106761
){
106688106762
int nCol = pAccum->nCol;
106689106763
int i;
106690106764
assert( pNew->iCol==pOld->iCol );
106691106765
for(i=pNew->iCol+1; i<nCol; i++){
@@ -106703,13 +106777,13 @@
106703106777
**
106704106778
** This function assumes that for each argument sample, the contents of
106705106779
** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
106706106780
*/
106707106781
static int sampleIsBetter(
106708
- Stat4Accum *pAccum,
106709
- Stat4Sample *pNew,
106710
- Stat4Sample *pOld
106782
+ StatAccum *pAccum,
106783
+ StatSample *pNew,
106784
+ StatSample *pOld
106711106785
){
106712106786
tRowcnt nEqNew = pNew->anEq[pNew->iCol];
106713106787
tRowcnt nEqOld = pOld->anEq[pOld->iCol];
106714106788
106715106789
assert( pOld->isPSample==0 && pNew->isPSample==0 );
@@ -106725,34 +106799,34 @@
106725106799
106726106800
/*
106727106801
** Copy the contents of sample *pNew into the p->a[] array. If necessary,
106728106802
** remove the least desirable sample from p->a[] to make room.
106729106803
*/
106730
-static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
106731
- Stat4Sample *pSample = 0;
106804
+static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
106805
+ StatSample *pSample = 0;
106732106806
int i;
106733106807
106734106808
assert( IsStat4 || nEqZero==0 );
106735106809
106736
- /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
106737
- ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
106810
+ /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
106811
+ ** values in the anEq[] array of any sample in StatAccum.a[]. In
106738106812
** other words, if nMaxEqZero is n, then it is guaranteed that there
106739
- ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
106813
+ ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
106740106814
if( nEqZero>p->nMaxEqZero ){
106741106815
p->nMaxEqZero = nEqZero;
106742106816
}
106743106817
if( pNew->isPSample==0 ){
106744
- Stat4Sample *pUpgrade = 0;
106818
+ StatSample *pUpgrade = 0;
106745106819
assert( pNew->anEq[pNew->iCol]>0 );
106746106820
106747106821
/* This sample is being added because the prefix that ends in column
106748106822
** iCol occurs many times in the table. However, if we have already
106749106823
** added a sample that shares this prefix, there is no need to add
106750106824
** this one. Instead, upgrade the priority of the highest priority
106751106825
** existing sample that shares this prefix. */
106752106826
for(i=p->nSample-1; i>=0; i--){
106753
- Stat4Sample *pOld = &p->a[i];
106827
+ StatSample *pOld = &p->a[i];
106754106828
if( pOld->anEq[pNew->iCol]==0 ){
106755106829
if( pOld->isPSample ) return;
106756106830
assert( pOld->iCol>pNew->iCol );
106757106831
assert( sampleIsBetter(p, pNew, pOld) );
106758106832
if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
@@ -106767,11 +106841,11 @@
106767106841
}
106768106842
}
106769106843
106770106844
/* If necessary, remove sample iMin to make room for the new sample. */
106771106845
if( p->nSample>=p->mxSample ){
106772
- Stat4Sample *pMin = &p->a[p->iMin];
106846
+ StatSample *pMin = &p->a[p->iMin];
106773106847
tRowcnt *anEq = pMin->anEq;
106774106848
tRowcnt *anLt = pMin->anLt;
106775106849
tRowcnt *anDLt = pMin->anDLt;
106776106850
sampleClear(p->db, pMin);
106777106851
memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
@@ -106810,24 +106884,24 @@
106810106884
p->iMin = iMin;
106811106885
}
106812106886
}
106813106887
#endif /* SQLITE_ENABLE_STAT4 */
106814106888
106889
+#ifdef SQLITE_ENABLE_STAT4
106815106890
/*
106816106891
** Field iChng of the index being scanned has changed. So at this point
106817106892
** p->current contains a sample that reflects the previous row of the
106818106893
** index. The value of anEq[iChng] and subsequent anEq[] elements are
106819106894
** correct at this point.
106820106895
*/
106821
-static void samplePushPrevious(Stat4Accum *p, int iChng){
106822
-#ifdef SQLITE_ENABLE_STAT4
106896
+static void samplePushPrevious(StatAccum *p, int iChng){
106823106897
int i;
106824106898
106825106899
/* Check if any samples from the aBest[] array should be pushed
106826106900
** into IndexSample.a[] at this point. */
106827106901
for(i=(p->nCol-2); i>=iChng; i--){
106828
- Stat4Sample *pBest = &p->aBest[i];
106902
+ StatSample *pBest = &p->aBest[i];
106829106903
pBest->anEq[i] = p->current.anEq[i];
106830106904
if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
106831106905
sampleInsert(p, pBest, i);
106832106906
}
106833106907
}
@@ -106847,29 +106921,24 @@
106847106921
if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
106848106922
}
106849106923
}
106850106924
p->nMaxEqZero = iChng;
106851106925
}
106852
-#endif
106853
-
106854
-#ifndef SQLITE_ENABLE_STAT4
106855
- UNUSED_PARAMETER( p );
106856
- UNUSED_PARAMETER( iChng );
106857
-#endif
106858106926
}
106927
+#endif /* SQLITE_ENABLE_STAT4 */
106859106928
106860106929
/*
106861106930
** Implementation of the stat_push SQL function: stat_push(P,C,R)
106862106931
** Arguments:
106863106932
**
106864
-** P Pointer to the Stat4Accum object created by stat_init()
106933
+** P Pointer to the StatAccum object created by stat_init()
106865106934
** C Index of left-most column to differ from previous row
106866106935
** R Rowid for the current row. Might be a key record for
106867106936
** WITHOUT ROWID tables.
106868106937
**
106869106938
** This SQL function always returns NULL. It's purpose it to accumulate
106870
-** statistical data and/or samples in the Stat4Accum object about the
106939
+** statistical data and/or samples in the StatAccum object about the
106871106940
** index being analyzed. The stat_get() SQL function will later be used to
106872106941
** extract relevant information for constructing the sqlite_statN tables.
106873106942
**
106874106943
** The R parameter is only used for STAT4
106875106944
*/
@@ -106879,11 +106948,11 @@
106879106948
sqlite3_value **argv
106880106949
){
106881106950
int i;
106882106951
106883106952
/* The three function arguments */
106884
- Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
106953
+ StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
106885106954
int iChng = sqlite3_value_int(argv[1]);
106886106955
106887106956
UNUSED_PARAMETER( argc );
106888106957
UNUSED_PARAMETER( context );
106889106958
assert( p->nCol>0 );
@@ -106892,11 +106961,13 @@
106892106961
if( p->nRow==0 ){
106893106962
/* This is the first call to this function. Do initialization. */
106894106963
for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
106895106964
}else{
106896106965
/* Second and subsequent calls get processed here */
106966
+#ifdef SQLITE_ENABLE_STAT4
106897106967
samplePushPrevious(p, iChng);
106968
+#endif
106898106969
106899106970
/* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
106900106971
** to the current row of the index. */
106901106972
for(i=0; i<iChng; i++){
106902106973
p->current.anEq[i]++;
@@ -106961,19 +107032,19 @@
106961107032
#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
106962107033
106963107034
/*
106964107035
** Implementation of the stat_get(P,J) SQL function. This routine is
106965107036
** used to query statistical information that has been gathered into
106966
-** the Stat4Accum object by prior calls to stat_push(). The P parameter
106967
-** has type BLOB but it is really just a pointer to the Stat4Accum object.
107037
+** the StatAccum object by prior calls to stat_push(). The P parameter
107038
+** has type BLOB but it is really just a pointer to the StatAccum object.
106968107039
** The content to returned is determined by the parameter J
106969107040
** which is one of the STAT_GET_xxxx values defined above.
106970107041
**
106971107042
** The stat_get(P,J) function is not available to generic SQL. It is
106972107043
** inserted as part of a manually constructed bytecode program. (See
106973107044
** the callStatGet() routine below.) It is guaranteed that the P
106974
-** parameter will always be a poiner to a Stat4Accum object, never a
107045
+** parameter will always be a pointer to a StatAccum object, never a
106975107046
** NULL.
106976107047
**
106977107048
** If STAT4 is not enabled, then J is always
106978107049
** STAT_GET_STAT1 and is hence omitted and this routine becomes
106979107050
** a one-parameter function, stat_get(P), that always returns the
@@ -106982,11 +107053,11 @@
106982107053
static void statGet(
106983107054
sqlite3_context *context,
106984107055
int argc,
106985107056
sqlite3_value **argv
106986107057
){
106987
- Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
107058
+ StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
106988107059
#ifdef SQLITE_ENABLE_STAT4
106989107060
/* STAT4 has a parameter on this routine. */
106990107061
int eCall = sqlite3_value_int(argv[1]);
106991107062
assert( argc==2 );
106992107063
assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
@@ -107003,11 +107074,11 @@
107003107074
**
107004107075
** The value is a string composed of a list of integers describing
107005107076
** the index. The first integer in the list is the total number of
107006107077
** entries in the index. There is one additional integer in the list
107007107078
** for each indexed column. This additional integer is an estimate of
107008
- ** the number of rows matched by a stabbing query on the index using
107079
+ ** the number of rows matched by a equality query on the index using
107009107080
** a key with the corresponding number of fields. In other words,
107010107081
** if the index is on columns (a,b) and the sqlite_stat1 value is
107011107082
** "100 10 2", then SQLite estimates that:
107012107083
**
107013107084
** * the index contains 100 rows,
@@ -107046,11 +107117,11 @@
107046107117
if( p->iGet<0 ){
107047107118
samplePushPrevious(p, 0);
107048107119
p->iGet = 0;
107049107120
}
107050107121
if( p->iGet<p->nSample ){
107051
- Stat4Sample *pS = p->a + p->iGet;
107122
+ StatSample *pS = p->a + p->iGet;
107052107123
if( pS->nRowid==0 ){
107053107124
sqlite3_result_int64(context, pS->u.iRowid);
107054107125
}else{
107055107126
sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
107056107127
SQLITE_TRANSIENT);
@@ -107137,11 +107208,11 @@
107137107208
int i; /* Loop counter */
107138107209
int jZeroRows = -1; /* Jump from here if number of rows is zero */
107139107210
int iDb; /* Index of database containing pTab */
107140107211
u8 needTableCnt = 1; /* True to count the table */
107141107212
int regNewRowid = iMem++; /* Rowid for the inserted record */
107142
- int regStat4 = iMem++; /* Register to hold Stat4Accum object */
107213
+ int regStat4 = iMem++; /* Register to hold StatAccum object */
107143107214
int regChng = iMem++; /* Index of changed index field */
107144107215
#ifdef SQLITE_ENABLE_STAT4
107145107216
int regRowid = iMem++; /* Rowid argument passed to stat_push() */
107146107217
#endif
107147107218
int regTemp = iMem++; /* Temporary use register */
@@ -108105,10 +108176,21 @@
108105108176
pExpr->op = TK_STRING;
108106108177
}
108107108178
}
108108108179
return rc;
108109108180
}
108181
+
108182
+/*
108183
+** Return true if zName points to a name that may be used to refer to
108184
+** database iDb attached to handle db.
108185
+*/
108186
+SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
108187
+ return (
108188
+ sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
108189
+ || (iDb==0 && sqlite3StrICmp("main", zName)==0)
108190
+ );
108191
+}
108110108192
108111108193
/*
108112108194
** An SQL user-function registered to do the work of an ATTACH statement. The
108113108195
** three arguments to the function come directly from an attach statement:
108114108196
**
@@ -108178,13 +108260,12 @@
108178108260
db->aLimit[SQLITE_LIMIT_ATTACHED]
108179108261
);
108180108262
goto attach_error;
108181108263
}
108182108264
for(i=0; i<db->nDb; i++){
108183
- char *z = db->aDb[i].zDbSName;
108184
- assert( z && zName );
108185
- if( sqlite3StrICmp(z, zName)==0 ){
108265
+ assert( zName );
108266
+ if( sqlite3DbIsNamed(db, i, zName) ){
108186108267
zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
108187108268
goto attach_error;
108188108269
}
108189108270
}
108190108271
@@ -108333,11 +108414,11 @@
108333108414
108334108415
if( zName==0 ) zName = "";
108335108416
for(i=0; i<db->nDb; i++){
108336108417
pDb = &db->aDb[i];
108337108418
if( pDb->pBt==0 ) continue;
108338
- if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
108419
+ if( sqlite3DbIsNamed(db, i, zName) ) break;
108339108420
}
108340108421
108341108422
if( i>=db->nDb ){
108342108423
sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
108343108424
goto detach_error;
@@ -108524,24 +108605,25 @@
108524108605
SQLITE_PRIVATE int sqlite3FixSrcList(
108525108606
DbFixer *pFix, /* Context of the fixation */
108526108607
SrcList *pList /* The Source list to check and modify */
108527108608
){
108528108609
int i;
108529
- const char *zDb;
108530108610
struct SrcList_item *pItem;
108611
+ sqlite3 *db = pFix->pParse->db;
108612
+ int iDb = sqlite3FindDbName(db, pFix->zDb);
108531108613
108532108614
if( NEVER(pList==0) ) return 0;
108533
- zDb = pFix->zDb;
108615
+
108534108616
for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
108535108617
if( pFix->bTemp==0 ){
108536
- if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
108618
+ if( pItem->zDatabase && iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
108537108619
sqlite3ErrorMsg(pFix->pParse,
108538108620
"%s %T cannot reference objects in database %s",
108539108621
pFix->zType, pFix->pName, pItem->zDatabase);
108540108622
return 1;
108541108623
}
108542
- sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
108624
+ sqlite3DbFree(db, pItem->zDatabase);
108543108625
pItem->zDatabase = 0;
108544108626
pItem->pSchema = pFix->pSchema;
108545108627
pItem->fg.fromDDL = 1;
108546108628
}
108547108629
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
@@ -109262,11 +109344,11 @@
109262109344
}
109263109345
#endif
109264109346
while(1){
109265109347
for(i=OMIT_TEMPDB; i<db->nDb; i++){
109266109348
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
109267
- if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
109349
+ if( zDatabase==0 || sqlite3DbIsNamed(db, j, zDatabase) ){
109268109350
assert( sqlite3SchemaMutexHeld(db, j, 0) );
109269109351
p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
109270109352
if( p ) return p;
109271109353
}
109272109354
}
@@ -109384,11 +109466,11 @@
109384109466
assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
109385109467
for(i=OMIT_TEMPDB; i<db->nDb; i++){
109386109468
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
109387109469
Schema *pSchema = db->aDb[j].pSchema;
109388109470
assert( pSchema );
109389
- if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
109471
+ if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
109390109472
assert( sqlite3SchemaMutexHeld(db, j, 0) );
109391109473
p = sqlite3HashFind(&pSchema->idxHash, zName);
109392109474
if( p ) break;
109393109475
}
109394109476
return p;
@@ -111103,10 +111185,36 @@
111103111185
if( pMod->pModule->iVersion<3 ) return 0;
111104111186
if( pMod->pModule->xShadowName==0 ) return 0;
111105111187
return pMod->pModule->xShadowName(zTail+1);
111106111188
}
111107111189
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
111190
+
111191
+#ifdef SQLITE_DEBUG
111192
+/*
111193
+** Mark all nodes of an expression as EP_Immutable, indicating that
111194
+** they should not be changed. Expressions attached to a table or
111195
+** index definition are tagged this way to help ensure that we do
111196
+** not pass them into code generator routines by mistake.
111197
+*/
111198
+static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
111199
+ ExprSetVVAProperty(pExpr, EP_Immutable);
111200
+ return WRC_Continue;
111201
+}
111202
+static void markExprListImmutable(ExprList *pList){
111203
+ if( pList ){
111204
+ Walker w;
111205
+ memset(&w, 0, sizeof(w));
111206
+ w.xExprCallback = markImmutableExprStep;
111207
+ w.xSelectCallback = sqlite3SelectWalkNoop;
111208
+ w.xSelectCallback2 = 0;
111209
+ sqlite3WalkExprList(&w, pList);
111210
+ }
111211
+}
111212
+#else
111213
+#define markExprListImmutable(X) /* no-op */
111214
+#endif /* SQLITE_DEBUG */
111215
+
111108111216
111109111217
/*
111110111218
** This routine is called to report the final ")" that terminates
111111111219
** a CREATE TABLE statement.
111112111220
**
@@ -111196,10 +111304,12 @@
111196111304
if( pParse->nErr ){
111197111305
/* If errors are seen, delete the CHECK constraints now, else they might
111198111306
** actually be used if PRAGMA writable_schema=ON is set. */
111199111307
sqlite3ExprListDelete(db, p->pCheck);
111200111308
p->pCheck = 0;
111309
+ }else{
111310
+ markExprListImmutable(p->pCheck);
111201111311
}
111202111312
}
111203111313
#endif /* !defined(SQLITE_OMIT_CHECK) */
111204111314
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111205111315
if( p->tabFlags & TF_HasGenerated ){
@@ -114137,20 +114247,33 @@
114137114247
u8 enc, /* Desired text encoding */
114138114248
const char *zName, /* Name of the collating sequence. Might be NULL */
114139114249
int create /* True to create CollSeq if doesn't already exist */
114140114250
){
114141114251
CollSeq *pColl;
114252
+ assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
114253
+ assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
114142114254
if( zName ){
114143114255
pColl = findCollSeqEntry(db, zName, create);
114256
+ if( pColl ) pColl += enc-1;
114144114257
}else{
114145114258
pColl = db->pDfltColl;
114146114259
}
114147
- assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
114148
- assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
114149
- if( pColl ) pColl += enc-1;
114150114260
return pColl;
114151114261
}
114262
+
114263
+/*
114264
+** Change the text encoding for a database connection. This means that
114265
+** the pDfltColl must change as well.
114266
+*/
114267
+SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
114268
+ assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
114269
+ db->enc = enc;
114270
+ /* EVIDENCE-OF: R-08308-17224 The default collating function for all
114271
+ ** strings is BINARY.
114272
+ */
114273
+ db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
114274
+}
114152114275
114153114276
/*
114154114277
** This function is responsible for invoking the collation factory callback
114155114278
** or substituting a collation sequence of a different encoding when the
114156114279
** requested collation sequence is not available in the desired encoding.
@@ -116321,10 +116444,11 @@
116321116444
const unsigned char *zA, *zB;
116322116445
u32 escape;
116323116446
int nPat;
116324116447
sqlite3 *db = sqlite3_context_db_handle(context);
116325116448
struct compareInfo *pInfo = sqlite3_user_data(context);
116449
+ struct compareInfo backupInfo;
116326116450
116327116451
#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
116328116452
if( sqlite3_value_type(argv[0])==SQLITE_BLOB
116329116453
|| sqlite3_value_type(argv[1])==SQLITE_BLOB
116330116454
){
@@ -116356,10 +116480,16 @@
116356116480
sqlite3_result_error(context,
116357116481
"ESCAPE expression must be a single character", -1);
116358116482
return;
116359116483
}
116360116484
escape = sqlite3Utf8Read(&zEsc);
116485
+ if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
116486
+ memcpy(&backupInfo, pInfo, sizeof(backupInfo));
116487
+ pInfo = &backupInfo;
116488
+ if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
116489
+ if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
116490
+ }
116361116491
}else{
116362116492
escape = pInfo->matchSet;
116363116493
}
116364116494
zB = sqlite3_value_text(argv[0]);
116365116495
zA = sqlite3_value_text(argv[1]);
@@ -117338,29 +117468,33 @@
117338117468
if( pDef==0 ) return 0;
117339117469
#endif
117340117470
if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
117341117471
return 0;
117342117472
}
117343
- if( nExpr<3 ){
117344
- aWc[3] = 0;
117345
- }else{
117346
- Expr *pEscape = pExpr->x.pList->a[2].pExpr;
117347
- char *zEscape;
117348
- if( pEscape->op!=TK_STRING ) return 0;
117349
- zEscape = pEscape->u.zToken;
117350
- if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
117351
- aWc[3] = zEscape[0];
117352
- }
117353117473
117354117474
/* The memcpy() statement assumes that the wildcard characters are
117355117475
** the first three statements in the compareInfo structure. The
117356117476
** asserts() that follow verify that assumption
117357117477
*/
117358117478
memcpy(aWc, pDef->pUserData, 3);
117359117479
assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
117360117480
assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
117361117481
assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
117482
+
117483
+ if( nExpr<3 ){
117484
+ aWc[3] = 0;
117485
+ }else{
117486
+ Expr *pEscape = pExpr->x.pList->a[2].pExpr;
117487
+ char *zEscape;
117488
+ if( pEscape->op!=TK_STRING ) return 0;
117489
+ zEscape = pEscape->u.zToken;
117490
+ if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
117491
+ if( zEscape[0]==aWc[0] ) return 0;
117492
+ if( zEscape[0]==aWc[1] ) return 0;
117493
+ aWc[3] = zEscape[0];
117494
+ }
117495
+
117362117496
*pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
117363117497
return 1;
117364117498
}
117365117499
117366117500
/*
@@ -120564,11 +120698,11 @@
120564120698
case OE_Replace: {
120565120699
int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
120566120700
VdbeCoverage(v);
120567120701
assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
120568120702
nSeenReplace++;
120569
- sqlite3ExprCode(pParse, pCol->pDflt, iReg);
120703
+ sqlite3ExprCodeCopy(pParse, pCol->pDflt, iReg);
120570120704
sqlite3VdbeJumpHere(v, addr1);
120571120705
break;
120572120706
}
120573120707
case OE_Abort:
120574120708
sqlite3MayAbort(pParse);
@@ -120619,10 +120753,11 @@
120619120753
ExprList *pCheck = pTab->pCheck;
120620120754
pParse->iSelfTab = -(regNewData+1);
120621120755
onError = overrideError!=OE_Default ? overrideError : OE_Abort;
120622120756
for(i=0; i<pCheck->nExpr; i++){
120623120757
int allOk;
120758
+ Expr *pCopy;
120624120759
Expr *pExpr = pCheck->a[i].pExpr;
120625120760
if( aiChng
120626120761
&& !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
120627120762
){
120628120763
/* The check constraints do not reference any of the columns being
@@ -120633,11 +120768,15 @@
120633120768
sqlite3TableAffinity(v, pTab, regNewData+1);
120634120769
bAffinityDone = 1;
120635120770
}
120636120771
allOk = sqlite3VdbeMakeLabel(pParse);
120637120772
sqlite3VdbeVerifyAbortable(v, onError);
120638
- sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
120773
+ pCopy = sqlite3ExprDup(db, pExpr, 0);
120774
+ if( !db->mallocFailed ){
120775
+ sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL);
120776
+ }
120777
+ sqlite3ExprDelete(db, pCopy);
120639120778
if( onError==OE_Ignore ){
120640120779
sqlite3VdbeGoto(v, ignoreDest);
120641120780
}else{
120642120781
char *zName = pCheck->a[i].zEName;
120643120782
if( zName==0 ) zName = pTab->zName;
@@ -125952,25 +126091,16 @@
125952126091
/* Only change the value of sqlite.enc if the database handle is not
125953126092
** initialized. If the main database exists, the new sqlite.enc value
125954126093
** will be overwritten when the schema is next loaded. If it does not
125955126094
** already exists, it will be created to use the new encoding value.
125956126095
*/
125957
- int canChangeEnc = 1; /* True if allowed to change the encoding */
125958
- int i; /* For looping over all attached databases */
125959
- for(i=0; i<db->nDb; i++){
125960
- if( db->aDb[i].pBt!=0
125961
- && DbHasProperty(db,i,DB_SchemaLoaded)
125962
- && !DbHasProperty(db,i,DB_Empty)
125963
- ){
125964
- canChangeEnc = 0;
125965
- }
125966
- }
125967
- if( canChangeEnc ){
126096
+ if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
125968126097
for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
125969126098
if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
125970
- SCHEMA_ENC(db) = ENC(db) =
125971
- pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
126099
+ u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
126100
+ SCHEMA_ENC(db) = enc;
126101
+ sqlite3SetTextEncoding(db, enc);
125972126102
break;
125973126103
}
125974126104
}
125975126105
if( !pEnc->zName ){
125976126106
sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
@@ -126775,11 +126905,11 @@
126775126905
int iDb = pData->iDb;
126776126906
126777126907
assert( argc==5 );
126778126908
UNUSED_PARAMETER2(NotUsed, argc);
126779126909
assert( sqlite3_mutex_held(db->mutex) );
126780
- DbClearProperty(db, iDb, DB_Empty);
126910
+ db->mDbFlags |= DBFLAG_EncodingFixed;
126781126911
pData->nInitRow++;
126782126912
if( db->mallocFailed ){
126783126913
corruptSchema(pData, argv[1], 0);
126784126914
return 1;
126785126915
}
@@ -126863,10 +126993,11 @@
126863126993
char const *azArg[6];
126864126994
int meta[5];
126865126995
InitData initData;
126866126996
const char *zMasterName;
126867126997
int openedTransaction = 0;
126998
+ int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
126868126999
126869127000
assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
126870127001
assert( iDb>=0 && iDb<db->nDb );
126871127002
assert( db->aDb[iDb].pSchema );
126872127003
assert( sqlite3_mutex_held(db->mutex) );
@@ -126891,10 +127022,11 @@
126891127022
initData.rc = SQLITE_OK;
126892127023
initData.pzErrMsg = pzErrMsg;
126893127024
initData.mInitFlags = mFlags;
126894127025
initData.nInitRow = 0;
126895127026
sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
127027
+ db->mDbFlags &= mask;
126896127028
if( initData.rc ){
126897127029
rc = initData.rc;
126898127030
goto error_out;
126899127031
}
126900127032
@@ -126950,31 +127082,29 @@
126950127082
** main database, set sqlite3.enc to the encoding of the main database.
126951127083
** For an attached db, it is an error if the encoding is not the same
126952127084
** as sqlite3.enc.
126953127085
*/
126954127086
if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
126955
- if( iDb==0 ){
126956
-#ifndef SQLITE_OMIT_UTF16
127087
+ if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
126957127088
u8 encoding;
127089
+#ifndef SQLITE_OMIT_UTF16
126958127090
/* If opening the main database, set ENC(db). */
126959127091
encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
126960127092
if( encoding==0 ) encoding = SQLITE_UTF8;
126961
- ENC(db) = encoding;
126962127093
#else
126963
- ENC(db) = SQLITE_UTF8;
127094
+ encoding = SQLITE_UTF8;
126964127095
#endif
127096
+ sqlite3SetTextEncoding(db, encoding);
126965127097
}else{
126966127098
/* If opening an attached database, the encoding much match ENC(db) */
126967
- if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
127099
+ if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
126968127100
sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
126969127101
" text encoding as main database");
126970127102
rc = SQLITE_ERROR;
126971127103
goto initone_error_out;
126972127104
}
126973127105
}
126974
- }else{
126975
- DbSetProperty(db, iDb, DB_Empty);
126976127106
}
126977127107
pDb->pSchema->enc = ENC(db);
126978127108
126979127109
if( pDb->pSchema->cache_size==0 ){
126980127110
#ifndef SQLITE_OMIT_DEPRECATED
@@ -127082,12 +127212,11 @@
127082127212
** used to store temporary tables, and any additional database files
127083127213
** created using ATTACH statements. Return a success code. If an
127084127214
** error occurs, write an error message into *pzErrMsg.
127085127215
**
127086127216
** After a database is initialized, the DB_SchemaLoaded bit is set
127087
-** bit is set in the flags field of the Db structure. If the database
127088
-** file was of zero-length, then the DB_Empty flag is also set.
127217
+** bit is set in the flags field of the Db structure.
127089127218
*/
127090127219
SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
127091127220
int i, rc;
127092127221
int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
127093127222
@@ -127719,11 +127848,10 @@
127719127848
sqlite3ExprDelete(db, p->pLimit);
127720127849
#ifndef SQLITE_OMIT_WINDOWFUNC
127721127850
if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
127722127851
sqlite3WindowListDelete(db, p->pWinDefn);
127723127852
}
127724
- assert( p->pWin==0 );
127725127853
#endif
127726127854
if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
127727127855
if( bFree ) sqlite3DbFreeNN(db, p);
127728127856
p = pPrior;
127729127857
bFree = 1;
@@ -131197,10 +131325,42 @@
131197131325
}
131198131326
}while( doPrior && (p = p->pPrior)!=0 );
131199131327
}
131200131328
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
131201131329
131330
+#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
131331
+/*
131332
+** pSelect is a SELECT statement and pSrcItem is one item in the FROM
131333
+** clause of that SELECT.
131334
+**
131335
+** This routine scans the entire SELECT statement and recomputes the
131336
+** pSrcItem->colUsed mask.
131337
+*/
131338
+static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
131339
+ struct SrcList_item *pItem;
131340
+ if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
131341
+ pItem = pWalker->u.pSrcItem;
131342
+ if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
131343
+ if( pExpr->iColumn<0 ) return WRC_Continue;
131344
+ pItem->colUsed |= sqlite3ExprColUsed(pExpr);
131345
+ return WRC_Continue;
131346
+}
131347
+static void recomputeColumnsUsed(
131348
+ Select *pSelect, /* The complete SELECT statement */
131349
+ struct SrcList_item *pSrcItem /* Which FROM clause item to recompute */
131350
+){
131351
+ Walker w;
131352
+ if( NEVER(pSrcItem->pTab==0) ) return;
131353
+ memset(&w, 0, sizeof(w));
131354
+ w.xExprCallback = recomputeColumnsUsedExpr;
131355
+ w.xSelectCallback = sqlite3SelectWalkNoop;
131356
+ w.u.pSrcItem = pSrcItem;
131357
+ pSrcItem->colUsed = 0;
131358
+ sqlite3WalkSelect(&w, pSelect);
131359
+}
131360
+#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
131361
+
131202131362
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
131203131363
/*
131204131364
** This routine attempts to flatten subqueries as a performance optimization.
131205131365
** This routine returns 1 if it makes changes and 0 if no flattening occurs.
131206131366
**
@@ -131735,10 +131895,16 @@
131735131895
*/
131736131896
if( pSub->pLimit ){
131737131897
pParent->pLimit = pSub->pLimit;
131738131898
pSub->pLimit = 0;
131739131899
}
131900
+
131901
+ /* Recompute the SrcList_item.colUsed masks for the flattened
131902
+ ** tables. */
131903
+ for(i=0; i<nSubSrc; i++){
131904
+ recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
131905
+ }
131740131906
}
131741131907
131742131908
/* Finially, delete what is left of the subquery and return
131743131909
** success.
131744131910
*/
@@ -135184,11 +135350,11 @@
135184135350
zDb = pName->a[0].zDatabase;
135185135351
zName = pName->a[0].zName;
135186135352
assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
135187135353
for(i=OMIT_TEMPDB; i<db->nDb; i++){
135188135354
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
135189
- if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
135355
+ if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
135190135356
assert( sqlite3SchemaMutexHeld(db, j, 0) );
135191135357
pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
135192135358
if( pTrigger ) break;
135193135359
}
135194135360
if( !pTrigger ){
@@ -141206,10 +141372,13 @@
141206141372
assert( pRangeEnd==0 && pRangeStart==0 );
141207141373
testcase( pLoop->nSkip>0 );
141208141374
nExtraReg = 1;
141209141375
bSeekPastNull = 1;
141210141376
pLevel->regBignull = regBignull = ++pParse->nMem;
141377
+ if( pLevel->iLeftJoin ){
141378
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull);
141379
+ }
141211141380
pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
141212141381
}
141213141382
141214141383
/* If we are doing a reverse order scan on an ascending index, or
141215141384
** a forward order scan on a descending index, interchange the
@@ -148759,11 +148928,11 @@
148759148928
&& (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
148760148929
&& (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
148761148930
&& (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
148762148931
&& pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
148763148932
){
148764
- sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
148933
+ sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ);
148765148934
}
148766148935
VdbeComment((v, "%s", pIx->zName));
148767148936
#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
148768148937
{
148769148938
u64 colUsed = 0;
@@ -148917,16 +149086,10 @@
148917149086
for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
148918149087
sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
148919149088
if( pIn->eEndLoopOp!=OP_Noop ){
148920149089
if( pIn->nPrefix ){
148921149090
assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
148922
- if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
148923
- sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
148924
- sqlite3VdbeCurrentAddr(v)+2+(pLevel->iLeftJoin!=0),
148925
- pIn->iBase, pIn->nPrefix);
148926
- VdbeCoverage(v);
148927
- }
148928149091
if( pLevel->iLeftJoin ){
148929149092
/* For LEFT JOIN queries, cursor pIn->iCur may not have been
148930149093
** opened yet. This occurs for WHERE clauses such as
148931149094
** "a = ? AND b IN (...)", where the index is on (a, b). If
148932149095
** the RHS of the (a=?) is NULL, then the "b IN (...)" may
@@ -148933,13 +149096,20 @@
148933149096
** never have been coded, but the body of the loop run to
148934149097
** return the null-row. So, if the cursor is not open yet,
148935149098
** jump over the OP_Next or OP_Prev instruction about to
148936149099
** be coded. */
148937149100
sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
148938
- sqlite3VdbeCurrentAddr(v) + 2
149101
+ sqlite3VdbeCurrentAddr(v) + 2 +
149102
+ ((pLoop->wsFlags & WHERE_VIRTUALTABLE)==0)
148939149103
);
148940149104
VdbeCoverage(v);
149105
+ }
149106
+ if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
149107
+ sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
149108
+ sqlite3VdbeCurrentAddr(v)+2,
149109
+ pIn->iBase, pIn->nPrefix);
149110
+ VdbeCoverage(v);
148941149111
}
148942149112
}
148943149113
sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
148944149114
VdbeCoverage(v);
148945149115
VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
@@ -150053,10 +150223,11 @@
150053150223
150054150224
ExprList *pSublist = 0; /* Expression list for sub-query */
150055150225
Window *pMWin = p->pWin; /* Master window object */
150056150226
Window *pWin; /* Window object iterator */
150057150227
Table *pTab;
150228
+ u32 selFlags = p->selFlags;
150058150229
150059150230
pTab = sqlite3DbMallocZero(db, sizeof(Table));
150060150231
if( pTab==0 ){
150061150232
return sqlite3ErrorToParser(db, SQLITE_NOMEM);
150062150233
}
@@ -150142,10 +150313,11 @@
150142150313
Table *pTab2;
150143150314
p->pSrc->a[0].pSelect = pSub;
150144150315
sqlite3SrcListAssignCursors(pParse, p->pSrc);
150145150316
pSub->selFlags |= SF_Expanded;
150146150317
pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
150318
+ pSub->selFlags |= (selFlags & SF_Aggregate);
150147150319
if( pTab2==0 ){
150148150320
/* Might actually be some other kind of error, but in that case
150149150321
** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
150150150322
** the correct error message regardless. */
150151150323
rc = SQLITE_NOMEM;
@@ -151030,10 +151202,11 @@
151030151202
int regArg;
151031151203
int nArg = 0;
151032151204
Window *pWin;
151033151205
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
151034151206
FuncDef *pFunc = pWin->pFunc;
151207
+ assert( pWin->regAccum );
151035151208
sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
151036151209
nArg = MAX(nArg, windowArgCount(pWin));
151037151210
if( pMWin->regStartRowid==0 ){
151038151211
if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
151039151212
sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
@@ -151408,10 +151581,14 @@
151408151581
pNew->eFrmType = p->eFrmType;
151409151582
pNew->eEnd = p->eEnd;
151410151583
pNew->eStart = p->eStart;
151411151584
pNew->eExclude = p->eExclude;
151412151585
pNew->regResult = p->regResult;
151586
+ pNew->regAccum = p->regAccum;
151587
+ pNew->iArgCol = p->iArgCol;
151588
+ pNew->iEphCsr = p->iEphCsr;
151589
+ pNew->bExprArgs = p->bExprArgs;
151413151590
pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
151414151591
pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
151415151592
pNew->pOwner = pOwner;
151416151593
pNew->bImplicitFrame = p->bImplicitFrame;
151417151594
}
@@ -152245,10 +152422,11 @@
152245152422
if( p ){
152246152423
/* memset(p, 0, sizeof(Expr)); */
152247152424
p->op = (u8)op;
152248152425
p->affExpr = 0;
152249152426
p->flags = EP_Leaf;
152427
+ ExprClearVVAProperties(p);
152250152428
p->iAgg = -1;
152251152429
p->pLeft = p->pRight = 0;
152252152430
p->x.pList = 0;
152253152431
p->pAggInfo = 0;
152254152432
p->y.pTab = 0;
@@ -162084,15 +162262,10 @@
162084162262
createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
162085162263
createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
162086162264
if( db->mallocFailed ){
162087162265
goto opendb_out;
162088162266
}
162089
- /* EVIDENCE-OF: R-08308-17224 The default collating function for all
162090
- ** strings is BINARY.
162091
- */
162092
- db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
162093
- assert( db->pDfltColl!=0 );
162094162267
162095162268
/* Parse the filename/URI argument
162096162269
**
162097162270
** Only allow sensible combinations of bits in the flags argument.
162098162271
** Throw an error if any non-sense combination is used. If we
@@ -162133,11 +162306,13 @@
162133162306
sqlite3Error(db, rc);
162134162307
goto opendb_out;
162135162308
}
162136162309
sqlite3BtreeEnter(db->aDb[0].pBt);
162137162310
db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
162138
- if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
162311
+ if( !db->mallocFailed ){
162312
+ sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
162313
+ }
162139162314
sqlite3BtreeLeave(db->aDb[0].pBt);
162140162315
db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
162141162316
162142162317
/* The default safety_level for the main database is FULL; for the temp
162143162318
** database it is OFF. This matches the pager layer defaults.
@@ -166664,10 +166839,11 @@
166664166839
const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
166665166840
char *zBuffer = 0; /* Buffer to load terms into */
166666166841
i64 nAlloc = 0; /* Size of allocated buffer */
166667166842
int isFirstTerm = 1; /* True when processing first term on page */
166668166843
sqlite3_int64 iChild; /* Block id of child node to descend to */
166844
+ int nBuffer = 0; /* Total term size */
166669166845
166670166846
/* Skip over the 'height' varint that occurs at the start of every
166671166847
** interior node. Then load the blockid of the left-child of the b-tree
166672166848
** node into variable iChild.
166673166849
**
@@ -166688,16 +166864,19 @@
166688166864
166689166865
while( zCsr<zEnd && (piFirst || piLast) ){
166690166866
int cmp; /* memcmp() result */
166691166867
int nSuffix; /* Size of term suffix */
166692166868
int nPrefix = 0; /* Size of term prefix */
166693
- int nBuffer; /* Total term size */
166694166869
166695166870
/* Load the next term on the node into zBuffer. Use realloc() to expand
166696166871
** the size of zBuffer if required. */
166697166872
if( !isFirstTerm ){
166698166873
zCsr += fts3GetVarint32(zCsr, &nPrefix);
166874
+ if( nPrefix>nBuffer ){
166875
+ rc = FTS_CORRUPT_VTAB;
166876
+ goto finish_scan;
166877
+ }
166699166878
}
166700166879
isFirstTerm = 0;
166701166880
zCsr += fts3GetVarint32(zCsr, &nSuffix);
166702166881
166703166882
assert( nPrefix>=0 && nSuffix>=0 );
@@ -179916,10 +180095,16 @@
179916180095
179917180096
/* If nSeg is less that zero, then there is no level with at least
179918180097
** nMin segments and no hint in the %_stat table. No work to do.
179919180098
** Exit early in this case. */
179920180099
if( nSeg<=0 ) break;
180100
+
180101
+ assert( nMod<=0x7FFFFFFF );
180102
+ if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
180103
+ rc = FTS_CORRUPT_VTAB;
180104
+ break;
180105
+ }
179921180106
179922180107
/* Open a cursor to iterate through the contents of the oldest nSeg
179923180108
** indexes of absolute level iAbsLevel. If this cursor is opened using
179924180109
** the 'hint' parameters, it is possible that there are less than nSeg
179925180110
** segments available in level iAbsLevel. In this case, no work is
@@ -189652,12 +189837,14 @@
189652189837
pRtree->nAux++;
189653189838
sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
189654189839
}else if( pRtree->nAux>0 ){
189655189840
break;
189656189841
}else{
189842
+ static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
189657189843
pRtree->nDim2++;
189658
- sqlite3_str_appendf(pSql, ",%.*s NUM", rtreeTokenLength(zArg), zArg);
189844
+ sqlite3_str_appendf(pSql, azFormat[eCoordType],
189845
+ rtreeTokenLength(zArg), zArg);
189659189846
}
189660189847
}
189661189848
sqlite3_str_appendf(pSql, ");");
189662189849
zSql = sqlite3_str_finish(pSql);
189663189850
if( !zSql ){
@@ -192389,11 +192576,11 @@
192389192576
** 1. uPattern is an unescaped match-all character "%",
192390192577
** 2. uPattern is an unescaped match-one character "_",
192391192578
** 3. uPattern is an unescaped escape character, or
192392192579
** 4. uPattern is to be handled as an ordinary character
192393192580
*/
192394
- if( !prevEscape && uPattern==MATCH_ALL ){
192581
+ if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
192395192582
/* Case 1. */
192396192583
uint8_t c;
192397192584
192398192585
/* Skip any MATCH_ALL or MATCH_ONE characters that follow a
192399192586
** MATCH_ALL. For each MATCH_ONE, skip one character in the
@@ -192415,16 +192602,16 @@
192415192602
}
192416192603
SQLITE_ICU_SKIP_UTF8(zString);
192417192604
}
192418192605
return 0;
192419192606
192420
- }else if( !prevEscape && uPattern==MATCH_ONE ){
192607
+ }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
192421192608
/* Case 2. */
192422192609
if( *zString==0 ) return 0;
192423192610
SQLITE_ICU_SKIP_UTF8(zString);
192424192611
192425
- }else if( !prevEscape && uPattern==(uint32_t)uEsc){
192612
+ }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
192426192613
/* Case 3. */
192427192614
prevEscape = 1;
192428192615
192429192616
}else{
192430192617
/* Case 4. */
@@ -199222,10 +199409,11 @@
199222199409
}
199223199410
}
199224199411
i = 0;
199225199412
if( iSchema>=0 ){
199226199413
pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
199414
+ pIdxInfo->aConstraintUsage[iSchema].omit = 1;
199227199415
pIdxInfo->idxNum |= 0x01;
199228199416
}
199229199417
if( iName>=0 ){
199230199418
pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
199231199419
pIdxInfo->idxNum |= 0x02;
@@ -199436,11 +199624,13 @@
199436199624
assert( nPayload>=(u32)nLocal );
199437199625
assert( nLocal<=(nUsable-35) );
199438199626
if( nPayload>(u32)nLocal ){
199439199627
int j;
199440199628
int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
199441
- if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
199629
+ if( iOff+nLocal>nUsable || nPayload>0x7fffffff ){
199630
+ goto statPageIsCorrupt;
199631
+ }
199442199632
pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
199443199633
pCell->nOvfl = nOvfl;
199444199634
pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
199445199635
if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
199446199636
pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
@@ -203769,11 +203959,11 @@
203769203959
const char *zSep = "";
203770203960
int rc = SQLITE_OK;
203771203961
SessionBuffer buf = {0, 0, 0};
203772203962
int nPk = 0;
203773203963
203774
- sessionAppendStr(&buf, "DELETE FROM ", &rc);
203964
+ sessionAppendStr(&buf, "DELETE FROM main.", &rc);
203775203965
sessionAppendIdent(&buf, zTab, &rc);
203776203966
sessionAppendStr(&buf, " WHERE ", &rc);
203777203967
203778203968
for(i=0; i<p->nCol; i++){
203779203969
if( p->abPK[i] ){
@@ -203852,11 +204042,11 @@
203852204042
int i;
203853204043
const char *zSep = "";
203854204044
SessionBuffer buf = {0, 0, 0};
203855204045
203856204046
/* Append "UPDATE tbl SET " */
203857
- sessionAppendStr(&buf, "UPDATE ", &rc);
204047
+ sessionAppendStr(&buf, "UPDATE main.", &rc);
203858204048
sessionAppendIdent(&buf, zTab, &rc);
203859204049
sessionAppendStr(&buf, " SET ", &rc);
203860204050
203861204051
/* Append the assignments */
203862204052
for(i=0; i<p->nCol; i++){
@@ -223538,11 +223728,11 @@
223538223728
int nArg, /* Number of args */
223539223729
sqlite3_value **apUnused /* Function arguments */
223540223730
){
223541223731
assert( nArg==0 );
223542223732
UNUSED_PARAM2(nArg, apUnused);
223543
- sqlite3_result_text(pCtx, "fts5: 2020-02-27 11:32:14 bfb09371d452d5d4dacab2ec476880bc729952f44ac0e5de90ea7ba203243c8c", -1, SQLITE_TRANSIENT);
223733
+ sqlite3_result_text(pCtx, "fts5: 2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84f525", -1, SQLITE_TRANSIENT);
223544223734
}
223545223735
223546223736
/*
223547223737
** Return true if zName is the extension on one of the shadow tables used
223548223738
** by this module.
@@ -228320,12 +228510,12 @@
228320228510
}
228321228511
#endif /* SQLITE_CORE */
228322228512
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
228323228513
228324228514
/************** End of stmt.c ************************************************/
228325
-#if __LINE__!=228325
228515
+#if __LINE__!=228515
228326228516
#undef SQLITE_SOURCE_ID
228327
-#define SQLITE_SOURCE_ID "2020-02-27 16:21:39 951b39ca74c9bd933139e099d5555283278db475f410f202c162e5d1e6aealt2"
228517
+#define SQLITE_SOURCE_ID "2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84alt2"
228328228518
#endif
228329228519
/* Return the source-id for this library */
228330228520
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
228331228521
/************************** End of sqlite3.c ******************************/
228332228522
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1162,11 +1162,11 @@
1162 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1163 ** [sqlite_version()] and [sqlite_source_id()].
1164 */
1165 #define SQLITE_VERSION "3.32.0"
1166 #define SQLITE_VERSION_NUMBER 3032000
1167 #define SQLITE_SOURCE_ID "2020-02-27 16:21:39 951b39ca74c9bd933139e099d5555283278db475f410f202c162e5d1e6aef933"
1168
1169 /*
1170 ** CAPI3REF: Run-Time Library Version Numbers
1171 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1172 **
@@ -16539,11 +16539,10 @@
16539 ** have been filled out. If the schema changes, these column names might
16540 ** changes and so the view will need to be reset.
16541 */
16542 #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
16543 #define DB_UnresetViews 0x0002 /* Some views have defined column names */
16544 #define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
16545 #define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
16546
16547 /*
16548 ** The number of different kinds of things that can be limited
16549 ** using the sqlite3_limit() interface.
@@ -16697,11 +16696,11 @@
16697 ** Each database connection is an instance of the following structure.
16698 */
16699 struct sqlite3 {
16700 sqlite3_vfs *pVfs; /* OS Interface */
16701 struct Vdbe *pVdbe; /* List of active virtual machines */
16702 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
16703 sqlite3_mutex *mutex; /* Connection mutex */
16704 Db *aDb; /* All backends */
16705 int nDb; /* Number of backends currently in use */
16706 u32 mDbFlags; /* flags recording internal state */
16707 u64 flags; /* flags settable by pragmas. See below */
@@ -16906,10 +16905,11 @@
16906 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
16907 #define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
16908 #define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
16909 #define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
16910 #define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
 
16911
16912 /*
16913 ** Bits of the sqlite3.dbOptFlags field that are used by the
16914 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
16915 ** selectively disable various optimizations.
@@ -17869,10 +17869,13 @@
17869 char affExpr; /* affinity, or RAISE type */
17870 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
17871 ** TK_COLUMN: the value of p5 for OP_Column
17872 ** TK_AGG_FUNCTION: nesting depth
17873 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
 
 
 
17874 u32 flags; /* Various flags. EP_* See below */
17875 union {
17876 char *zToken; /* Token value. Zero terminated and dequoted */
17877 int iValue; /* Non-negative integer value if EP_IntValue */
17878 } u;
@@ -17943,11 +17946,11 @@
17943 #define EP_Skip 0x001000 /* Operator does not contribute to affinity */
17944 #define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
17945 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17946 #define EP_Win 0x008000 /* Contains window functions */
17947 #define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17948 #define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
17949 #define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
17950 #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
17951 #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
17952 #define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
17953 #define EP_Alias 0x400000 /* Is an alias for a result set column */
@@ -17957,10 +17960,11 @@
17957 #define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
17958 #define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
17959 #define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
17960 #define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
17961 #define EP_FromDDL 0x40000000 /* Originates from sqlite_master */
 
17962
17963 /*
17964 ** The EP_Propagate mask is a set of properties that automatically propagate
17965 ** upwards into parent nodes.
17966 */
@@ -17975,18 +17979,28 @@
17975 #define ExprSetProperty(E,P) (E)->flags|=(P)
17976 #define ExprClearProperty(E,P) (E)->flags&=~(P)
17977 #define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
17978 #define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
17979
 
 
 
 
 
 
17980 /* The ExprSetVVAProperty() macro is used for Verification, Validation,
17981 ** and Accreditation only. It works like ExprSetProperty() during VVA
17982 ** processes but is a no-op for delivery.
17983 */
17984 #ifdef SQLITE_DEBUG
17985 # define ExprSetVVAProperty(E,P) (E)->flags|=(P)
 
 
17986 #else
17987 # define ExprSetVVAProperty(E,P)
 
 
17988 #endif
17989
17990 /*
17991 ** Macros to determine the number of bytes required by a normal Expr
17992 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
@@ -18956,10 +18970,11 @@
18956 Select *pSelect; /* HAVING to WHERE clause ctx */
18957 struct WindowRewrite *pRewrite; /* Window rewrite context */
18958 struct WhereConst *pConst; /* WHERE clause constants */
18959 struct RenameCtx *pRename; /* RENAME COLUMN context */
18960 struct Table *pTab; /* Table of generated column */
 
18961 } u;
18962 };
18963
18964 /* Forward declarations */
18965 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -19500,11 +19515,11 @@
19500 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
19501 SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
19502 #endif
19503 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
19504 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
19505 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
19506 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
19507 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
19508 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
19509 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
19510 #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
@@ -19655,10 +19670,11 @@
19655 # define sqlite3AuthRead(a,b,c,d)
19656 # define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
19657 # define sqlite3AuthContextPush(a,b,c)
19658 # define sqlite3AuthContextPop(a) ((void)(a))
19659 #endif
 
19660 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
19661 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
19662 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
19663 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
19664 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
@@ -19714,14 +19730,14 @@
19714 #define putVarint sqlite3PutVarint
19715
19716
19717 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19718 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19719 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
19720 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
19721 SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
19722 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
19723 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
19724 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
19725 SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19726 SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
19727 SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
@@ -19740,13 +19756,14 @@
19740 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
19741 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
19742 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19743 SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
19744 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19745 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
19746 SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
19747 SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
 
19748 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
19749 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
19750 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
19751 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
19752 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
@@ -19809,10 +19826,11 @@
19809 const struct ExprList_item*,
19810 const char*,
19811 const char*,
19812 const char*
19813 );
 
19814 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
19815 SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19816 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19817 SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19818 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
@@ -19973,12 +19991,12 @@
19973 #ifdef SQLITE_ENABLE_NORMALIZE
19974 SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
19975 #endif
19976 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
19977 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
19978 SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,Expr*);
19979 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
19980 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
19981 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
19982 #ifndef SQLITE_OMIT_WAL
19983 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
19984 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
@@ -20947,13 +20965,13 @@
20947 #endif
20948 u16 nResColumn; /* Number of columns in one row of the result set */
20949 u8 errorAction; /* Recovery action to do in case of an error */
20950 u8 minWriteFileFormat; /* Minimum file format for writable database files */
20951 u8 prepFlags; /* SQLITE_PREPARE_* flags */
 
20952 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
20953 bft explain:2; /* True if EXPLAIN present on SQL command */
20954 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
20955 bft changeCntOn:1; /* True to update the change-counter */
20956 bft runOnlyOnce:1; /* Automatically expire on reset */
20957 bft usesStmtJournal:1; /* True if uses a statement journal */
20958 bft readOnly:1; /* True for statements that do not write */
20959 bft bIsReader:1; /* True for statements that read */
@@ -29390,12 +29408,12 @@
29390 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
29391 }else if( pItem->zName ){
29392 sqlite3_str_appendf(&x, " %s", pItem->zName);
29393 }
29394 if( pItem->pTab ){
29395 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p",
29396 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab);
29397 }
29398 if( pItem->zAlias ){
29399 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
29400 }
29401 if( pItem->fg.jointype & JT_LEFT ){
@@ -29650,27 +29668,30 @@
29650 ** Generate a human-readable explanation of an expression tree.
29651 */
29652 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
29653 const char *zBinOp = 0; /* Binary operator */
29654 const char *zUniOp = 0; /* Unary operator */
29655 char zFlgs[60];
29656 pView = sqlite3TreeViewPush(pView, moreToFollow);
29657 if( pExpr==0 ){
29658 sqlite3TreeViewLine(pView, "nil");
29659 sqlite3TreeViewPop(pView);
29660 return;
29661 }
29662 if( pExpr->flags || pExpr->affExpr ){
29663 StrAccum x;
29664 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
29665 sqlite3_str_appendf(&x, " fg.af=%x.%c",
29666 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
29667 if( ExprHasProperty(pExpr, EP_FromJoin) ){
29668 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
29669 }
29670 if( ExprHasProperty(pExpr, EP_FromDDL) ){
29671 sqlite3_str_appendf(&x, " DDL");
 
 
 
29672 }
29673 sqlite3StrAccumFinish(&x);
29674 }else{
29675 zFlgs[0] = 0;
29676 }
@@ -29774,10 +29795,11 @@
29774 case TK_SLASH: zBinOp = "DIV"; break;
29775 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
29776 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
29777 case TK_CONCAT: zBinOp = "CONCAT"; break;
29778 case TK_DOT: zBinOp = "DOT"; break;
 
29779
29780 case TK_UMINUS: zUniOp = "UMINUS"; break;
29781 case TK_UPLUS: zUniOp = "UPLUS"; break;
29782 case TK_BITNOT: zUniOp = "BITNOT"; break;
29783 case TK_NOT: zUniOp = "NOT"; break;
@@ -50895,11 +50917,11 @@
50895 }
50896
50897 /*
50898 ** Allocate a new RowSetEntry object that is associated with the
50899 ** given RowSet. Return a pointer to the new and completely uninitialized
50900 ** objected.
50901 **
50902 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
50903 ** routine returns NULL.
50904 */
50905 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
@@ -51171,11 +51193,11 @@
51171 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
51172 p = pRowSet->pEntry;
51173 if( p ){
51174 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
51175 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51176 /* Only sort the current set of entiries if they need it */
51177 p = rowSetEntrySort(p);
51178 }
51179 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51180 ppPrevTree = &pTree->pRight;
51181 if( pTree->pLeft==0 ){
@@ -64536,11 +64558,11 @@
64536 ** free-list for reuse. It returns false if it is safe to retrieve the
64537 ** page from the pager layer with the 'no-content' flag set. True otherwise.
64538 */
64539 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
64540 Bitvec *p = pBt->pHasContent;
64541 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
64542 }
64543
64544 /*
64545 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
64546 ** invoked at the conclusion of each write-transaction.
@@ -76180,23 +76202,18 @@
76180 u16 mFlags;
76181 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
76182 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
76183 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
76184 }
76185 /* If pX is marked as a shallow copy of pMem, then verify that
76186 ** no significant changes have been made to pX since the OP_SCopy.
76187 ** A significant change would indicated a missed call to this
76188 ** function for pX. Minor changes, such as adding or removing a
76189 ** dual type, are allowed, as long as the underlying value is the
76190 ** same. */
76191 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
76192 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
76193 /* assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r ); */
76194 /* ^^ */
76195 /* Cannot reliably compare doubles for equality */
76196 assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) );
76197 assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 );
76198
76199 /* pMem is the register that is changing. But also mark pX as
76200 ** undefined so that we can quickly detect the shallow-copy error */
76201 pX->flags = MEM_Undefined;
76202 pX->pScopyFrom = 0;
@@ -77547,11 +77564,11 @@
77547 (void)z2;
77548 }
77549 #endif
77550
77551 /*
77552 ** Add a new OP_ opcode.
77553 **
77554 ** If the bPush flag is true, then make this opcode the parent for
77555 ** subsequent Explains until sqlite3VdbeExplainPop() is called.
77556 */
77557 SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
@@ -78781,12 +78798,15 @@
78781 displayP4Expr(&x, pOp->p4.pExpr);
78782 break;
78783 }
78784 #endif
78785 case P4_COLLSEQ: {
 
78786 CollSeq *pColl = pOp->p4.pColl;
78787 sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
 
 
78788 break;
78789 }
78790 case P4_FUNCDEF: {
78791 FuncDef *pDef = pOp->p4.pFunc;
78792 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
@@ -79495,10 +79515,11 @@
79495 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
79496 "id", "parent", "notused", "detail"
79497 };
79498 int iFirst, mx, i;
79499 if( nMem<10 ) nMem = 10;
 
79500 if( pParse->explain==2 ){
79501 sqlite3VdbeSetNumCols(p, 4);
79502 iFirst = 8;
79503 mx = 12;
79504 }else{
@@ -79545,11 +79566,10 @@
79545 }
79546 }
79547
79548 p->pVList = pParse->pVList;
79549 pParse->pVList = 0;
79550 p->explain = pParse->explain;
79551 if( db->mallocFailed ){
79552 p->nVar = 0;
79553 p->nCursor = 0;
79554 p->nMem = 0;
79555 }else{
@@ -86230,11 +86250,10 @@
86230 u16 flags2; /* Initial flags for P2 */
86231
86232 pIn1 = &aMem[pOp->p1];
86233 pIn2 = &aMem[pOp->p2];
86234 pOut = &aMem[pOp->p3];
86235 testcase( pIn1==pIn2 );
86236 testcase( pOut==pIn2 );
86237 assert( pIn1!=pOut );
86238 flags1 = pIn1->flags;
86239 testcase( flags1 & MEM_Null );
86240 testcase( pIn2->flags & MEM_Null );
@@ -88351,11 +88370,11 @@
88351 **
88352 ** Allowed P5 bits:
88353 ** <ul>
88354 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88355 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88356 ** of OP_SeekLE/OP_IdxGT)
88357 ** </ul>
88358 **
88359 ** The P4 value may be either an integer (P4_INT32) or a pointer to
88360 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
88361 ** object, then table being opened must be an [index b-tree] where the
@@ -88381,11 +88400,11 @@
88381 **
88382 ** Allowed P5 bits:
88383 ** <ul>
88384 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88385 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88386 ** of OP_SeekLE/OP_IdxGT)
88387 ** </ul>
88388 **
88389 ** See also: OP_OpenRead, OP_OpenWrite
88390 */
88391 /* Opcode: OpenWrite P1 P2 P3 P4 P5
@@ -88405,11 +88424,11 @@
88405 **
88406 ** Allowed P5 bits:
88407 ** <ul>
88408 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88409 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88410 ** of OP_SeekLE/OP_IdxGT)
88411 ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
88412 ** and subsequently delete entries in an index btree. This is a
88413 ** hint to the storage engine that the storage engine is allowed to
88414 ** ignore. The hint is not used by the official SQLite b*tree storage
88415 ** engine, but is used by COMDB2.
@@ -88517,13 +88536,11 @@
88517
88518 open_cursor_set_hints:
88519 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
88520 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
88521 testcase( pOp->p5 & OPFLAG_BULKCSR );
88522 #ifdef SQLITE_ENABLE_CURSOR_HINTS
88523 testcase( pOp->p2 & OPFLAG_SEEKEQ );
88524 #endif
88525 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
88526 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
88527 if( rc ) goto abort_due_to_error;
88528 break;
88529 }
@@ -88775,15 +88792,17 @@
88775 ** Reposition cursor P1 so that it points to the smallest entry that
88776 ** is greater than or equal to the key value. If there are no records
88777 ** greater than or equal to the key and P2 is not zero, then jump to P2.
88778 **
88779 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
88780 ** opcode will always land on a record that equally equals the key, or
88781 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
88782 ** opcode must be followed by an IdxLE opcode with the same arguments.
88783 ** The IdxLE opcode will be skipped if this opcode succeeds, but the
88784 ** IdxLE opcode will be used on subsequent loop iterations.
 
 
88785 **
88786 ** This opcode leaves the cursor configured to move in forward order,
88787 ** from the beginning toward the end. In other words, the cursor is
88788 ** configured to use Next, not Prev.
88789 **
@@ -88795,11 +88814,11 @@
88795 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
88796 ** use the value in register P3 as a key. If cursor P1 refers
88797 ** to an SQL index, then P3 is the first in an array of P4 registers
88798 ** that are used as an unpacked index key.
88799 **
88800 ** Reposition cursor P1 so that it points to the smallest entry that
88801 ** is greater than the key value. If there are no records greater than
88802 ** the key and P2 is not zero, then jump to P2.
88803 **
88804 ** This opcode leaves the cursor configured to move in forward order,
88805 ** from the beginning toward the end. In other words, the cursor is
@@ -88840,15 +88859,17 @@
88840 ** This opcode leaves the cursor configured to move in reverse order,
88841 ** from the end toward the beginning. In other words, the cursor is
88842 ** configured to use Prev, not Next.
88843 **
88844 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
88845 ** opcode will always land on a record that equally equals the key, or
88846 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
88847 ** opcode must be followed by an IdxGE opcode with the same arguments.
88848 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
88849 ** IdxGE opcode will be used on subsequent loop iterations.
 
 
88850 **
88851 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
88852 */
88853 case OP_SeekLT: /* jump, in3, group */
88854 case OP_SeekLE: /* jump, in3, group */
@@ -88881,11 +88902,11 @@
88881
88882 pC->deferredMoveto = 0;
88883 pC->cacheStatus = CACHE_STALE;
88884 if( pC->isTable ){
88885 u16 flags3, newType;
88886 /* The BTREE_SEEK_EQ flag is only set on index cursors */
88887 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
88888 || CORRUPT_DB );
88889
88890 /* The input value in P3 might be of any type: integer, real, string,
88891 ** blob, or NULL. But it needs to be an integer before we can do
@@ -88940,18 +88961,21 @@
88940 pC->movetoTarget = iKey; /* Used by OP_Delete */
88941 if( rc!=SQLITE_OK ){
88942 goto abort_due_to_error;
88943 }
88944 }else{
88945 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
88946 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
88947 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
 
88948 */
88949 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
88950 eqOnly = 1;
88951 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
88952 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
 
 
88953 assert( pOp[1].p1==pOp[0].p1 );
88954 assert( pOp[1].p2==pOp[0].p2 );
88955 assert( pOp[1].p3==pOp[0].p3 );
88956 assert( pOp[1].p4.i==pOp[0].p4.i );
88957 }
@@ -91162,11 +91186,11 @@
91162 ** try to reuse register values from the first use. */
91163 {
91164 int i;
91165 for(i=0; i<p->nMem; i++){
91166 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
91167 aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
91168 }
91169 }
91170 #endif
91171 pOp = &aOp[-1];
91172 goto check_for_interrupt;
@@ -96555,19 +96579,20 @@
96555 SrcList *pSrc;
96556 int i;
96557 struct SrcList_item *pItem;
96558
96559 pSrc = p->pSrc;
96560 assert( pSrc!=0 );
96561 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
96562 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
96563 return WRC_Abort;
96564 }
96565 if( pItem->fg.isTabFunc
96566 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
96567 ){
96568 return WRC_Abort;
 
96569 }
96570 }
96571 return WRC_Continue;
96572 }
96573
@@ -96784,10 +96809,35 @@
96784 }else{
96785 /* Currently parsing a DML statement */
96786 return (db->flags & SQLITE_DqsDML)!=0;
96787 }
96788 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96789
96790 /*
96791 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
96792 ** that name in the set of source tables in pSrcList and make the pExpr
96793 ** expression node refer back to that source column. The following changes
@@ -96861,10 +96911,16 @@
96861 assert( db->aDb[i].zDbSName );
96862 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
96863 pSchema = db->aDb[i].pSchema;
96864 break;
96865 }
 
 
 
 
 
 
96866 }
96867 }
96868 }
96869
96870 /* Start at the inner-most context and move outward until a match is found */
@@ -97181,26 +97237,11 @@
97181 **
97182 ** If a generated column is referenced, set bits for every column
97183 ** of the table.
97184 */
97185 if( pExpr->iColumn>=0 && pMatch!=0 ){
97186 int n = pExpr->iColumn;
97187 Table *pExTab = pExpr->y.pTab;
97188 assert( pExTab!=0 );
97189 assert( pMatch->iCursor==pExpr->iTable );
97190 if( (pExTab->tabFlags & TF_HasGenerated)!=0
97191 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
97192 ){
97193 testcase( pExTab->nCol==BMS-1 );
97194 testcase( pExTab->nCol==BMS );
97195 pMatch->colUsed = pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
97196 }else{
97197 testcase( n==BMS-1 );
97198 testcase( n==BMS );
97199 if( n>=BMS ) n = BMS-1;
97200 pMatch->colUsed |= ((Bitmask)1)<<n;
97201 }
97202 }
97203
97204 /* Clean up and return
97205 */
97206 sqlite3ExprDelete(db, pExpr->pLeft);
@@ -98557,11 +98598,11 @@
98557 ** CREATE TABLE t1(a);
98558 ** SELECT * FROM t1 WHERE a;
98559 ** SELECT a AS b FROM t1 WHERE b;
98560 ** SELECT * FROM t1 WHERE (select a from t1);
98561 */
98562 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
98563 int op;
98564 while( ExprHasProperty(pExpr, EP_Skip) ){
98565 assert( pExpr->op==TK_COLLATE );
98566 pExpr = pExpr->pLeft;
98567 assert( pExpr!=0 );
@@ -98667,14 +98708,14 @@
98667 ** The collating sequence might be determined by a COLLATE operator
98668 ** or by the presence of a column with a defined collating sequence.
98669 ** COLLATE operators take first precedence. Left operands take
98670 ** precedence over right operands.
98671 */
98672 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
98673 sqlite3 *db = pParse->db;
98674 CollSeq *pColl = 0;
98675 Expr *p = pExpr;
98676 while( p ){
98677 int op = p->op;
98678 if( op==TK_REGISTER ) op = p->op2;
98679 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
98680 && p->y.pTab!=0
@@ -98739,21 +98780,21 @@
98739 ** See also: sqlite3ExprCollSeq()
98740 **
98741 ** The sqlite3ExprCollSeq() routine works the same except that it
98742 ** returns NULL if there is no defined collation.
98743 */
98744 SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
98745 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
98746 if( p==0 ) p = pParse->db->pDfltColl;
98747 assert( p!=0 );
98748 return p;
98749 }
98750
98751 /*
98752 ** Return TRUE if the two expressions have equivalent collating sequences.
98753 */
98754 SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
98755 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
98756 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
98757 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
98758 }
98759
@@ -98760,11 +98801,11 @@
98760 /*
98761 ** pExpr is an operand of a comparison operator. aff2 is the
98762 ** type affinity of the other operand. This routine returns the
98763 ** type affinity that should be used for the comparison operator.
98764 */
98765 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
98766 char aff1 = sqlite3ExprAffinity(pExpr);
98767 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
98768 /* Both sides of the comparison are columns. If one has numeric
98769 ** affinity, use that. Otherwise use no affinity.
98770 */
@@ -98782,11 +98823,11 @@
98782
98783 /*
98784 ** pExpr is a comparison operator. Return the type affinity that should
98785 ** be applied to both operands prior to doing the comparison.
98786 */
98787 static char comparisonAffinity(Expr *pExpr){
98788 char aff;
98789 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
98790 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
98791 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
98792 assert( pExpr->pLeft );
@@ -98805,11 +98846,11 @@
98805 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
98806 ** idx_affinity is the affinity of an indexed column. Return true
98807 ** if the index with affinity idx_affinity may be used to implement
98808 ** the comparison in pExpr.
98809 */
98810 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
98811 char aff = comparisonAffinity(pExpr);
98812 if( aff<SQLITE_AFF_TEXT ){
98813 return 1;
98814 }
98815 if( aff==SQLITE_AFF_TEXT ){
@@ -98820,11 +98861,15 @@
98820
98821 /*
98822 ** Return the P5 value that should be used for a binary comparison
98823 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
98824 */
98825 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
 
 
 
 
98826 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
98827 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
98828 return aff;
98829 }
98830
@@ -98840,12 +98885,12 @@
98840 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
98841 ** it is not considered.
98842 */
98843 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
98844 Parse *pParse,
98845 Expr *pLeft,
98846 Expr *pRight
98847 ){
98848 CollSeq *pColl;
98849 assert( pLeft );
98850 if( pLeft->flags & EP_Collate ){
98851 pColl = sqlite3ExprCollSeq(pParse, pLeft);
@@ -98866,11 +98911,11 @@
98866 ** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
98867 ** However, if the OP_Commuted flag is set, then the order of the operands
98868 ** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
98869 ** correct collating sequence is found.
98870 */
98871 SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, Expr *p){
98872 if( ExprHasProperty(p, EP_Commuted) ){
98873 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
98874 }else{
98875 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
98876 }
@@ -99109,10 +99154,11 @@
99109 int regRight = 0;
99110 u8 opx = op;
99111 int addrDone = sqlite3VdbeMakeLabel(pParse);
99112 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
99113
 
99114 if( pParse->nErr ) return;
99115 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
99116 sqlite3ErrorMsg(pParse, "row value misused");
99117 return;
99118 }
@@ -99721,11 +99767,11 @@
99721 nSize = EXPR_FULLSIZE;
99722 }else{
99723 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
99724 assert( !ExprHasProperty(p, EP_FromJoin) );
99725 assert( !ExprHasProperty(p, EP_MemToken) );
99726 assert( !ExprHasProperty(p, EP_NoReduce) );
99727 if( p->pLeft || p->x.pList ){
99728 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
99729 }else{
99730 assert( p->pRight==0 );
99731 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
@@ -99826,10 +99872,14 @@
99826
99827 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
99828 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
99829 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
99830 pNew->flags |= staticFlag;
 
 
 
 
99831
99832 /* Copy the p->u.zToken string, if any. */
99833 if( nToken ){
99834 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
99835 memcpy(zToken, p->u.zToken, nToken);
@@ -100602,11 +100652,11 @@
100602 ** (3) the expression does not contain any EP_FixedCol TK_COLUMN
100603 ** operands created by the constant propagation optimization.
100604 **
100605 ** When this routine returns true, it indicates that the expression
100606 ** can be added to the pParse->pConstExpr list and evaluated once when
100607 ** the prepared statement starts up. See sqlite3ExprCodeAtInit().
100608 */
100609 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
100610 return exprIsConst(p, 2, 0);
100611 }
100612
@@ -101365,10 +101415,11 @@
101365 return;
101366 }
101367
101368 /* Begin coding the subroutine */
101369 ExprSetProperty(pExpr, EP_Subrtn);
 
101370 pExpr->y.sub.regReturn = ++pParse->nMem;
101371 pExpr->y.sub.iAddr =
101372 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
101373 VdbeComment((v, "return address"));
101374
@@ -101685,10 +101736,11 @@
101685 int addrTruthOp; /* Address of opcode that determines the IN is true */
101686 int destNotNull; /* Jump here if a comparison is not true in step 6 */
101687 int addrTop; /* Top of the step-6 loop */
101688 int iTab = 0; /* Index to use */
101689
 
101690 pLeft = pExpr->pLeft;
101691 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
101692 zAff = exprINAffinity(pParse, pExpr);
101693 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
101694 aiMap = (int*)sqlite3DbMallocZero(
@@ -102011,11 +102063,11 @@
102011 if( pParse->iSelfTab>0 ){
102012 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
102013 }else{
102014 iAddr = 0;
102015 }
102016 sqlite3ExprCode(pParse, pCol->pDflt, regOut);
102017 if( pCol->affinity>=SQLITE_AFF_TEXT ){
102018 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
102019 }
102020 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
102021 }
@@ -102295,10 +102347,11 @@
102295
102296 expr_code_doover:
102297 if( pExpr==0 ){
102298 op = TK_NULL;
102299 }else{
 
102300 op = pExpr->op;
102301 }
102302 switch( op ){
102303 case TK_AGG_COLUMN: {
102304 AggInfo *pAggInfo = pExpr->pAggInfo;
@@ -102305,12 +102358,21 @@
102305 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
102306 if( !pAggInfo->directMode ){
102307 assert( pCol->iMem>0 );
102308 return pCol->iMem;
102309 }else if( pAggInfo->useSortingIdx ){
 
102310 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
102311 pCol->iSorterColumn, target);
 
 
 
 
 
 
 
 
102312 return target;
102313 }
102314 /* Otherwise, fall thru into the TK_COLUMN case */
102315 }
102316 case TK_COLUMN: {
@@ -102549,10 +102611,11 @@
102549 #endif
102550 }else{
102551 tempX.op = TK_INTEGER;
102552 tempX.flags = EP_IntValue|EP_TokenOnly;
102553 tempX.u.iValue = 0;
 
102554 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
102555 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
102556 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
102557 testcase( regFree2==0 );
102558 }
@@ -102620,20 +102683,17 @@
102620 return pExpr->y.pWin->regResult;
102621 }
102622 #endif
102623
102624 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
102625 /* SQL functions can be expensive. So try to move constant functions
102626 ** out of the inner loop, even if that means an extra OP_Copy. */
102627 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
102628 }
102629 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
102630 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
102631 pFarg = 0;
102632 }else{
102633 pFarg = pExpr->x.pList;
102634 }
102635 nFarg = pFarg ? pFarg->nExpr : 0;
102636 assert( !ExprHasProperty(pExpr, EP_IntValue) );
102637 zId = pExpr->u.zToken;
102638 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
102639 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
@@ -103003,19 +103063,27 @@
103003 sqlite3ReleaseTempReg(pParse, regFree2);
103004 return inReg;
103005 }
103006
103007 /*
103008 ** Factor out the code of the given expression to initialization time.
 
 
 
 
 
 
103009 **
103010 ** If regDest>=0 then the result is always stored in that register and the
103011 ** result is not reusable. If regDest<0 then this routine is free to
103012 ** store the value whereever it wants. The register where the expression
103013 ** is stored is returned. When regDest<0, two identical expressions will
103014 ** code to the same register.
 
 
103015 */
103016 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
103017 Parse *pParse, /* Parsing context */
103018 Expr *pExpr, /* The expression to code when the VDBE initializes */
103019 int regDest /* Store the value in this register */
103020 ){
103021 ExprList *p;
@@ -103029,18 +103097,33 @@
103029 return pItem->u.iConstExprReg;
103030 }
103031 }
103032 }
103033 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
103034 p = sqlite3ExprListAppend(pParse, p, pExpr);
103035 if( p ){
103036 struct ExprList_item *pItem = &p->a[p->nExpr-1];
103037 pItem->reusable = regDest<0;
103038 if( regDest<0 ) regDest = ++pParse->nMem;
103039 pItem->u.iConstExprReg = regDest;
103040 }
103041 pParse->pConstExpr = p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103042 return regDest;
103043 }
103044
103045 /*
103046 ** Generate code to evaluate an expression and store the results
@@ -103061,11 +103144,11 @@
103061 if( ConstFactorOk(pParse)
103062 && pExpr->op!=TK_REGISTER
103063 && sqlite3ExprIsConstantNotJoin(pExpr)
103064 ){
103065 *pReg = 0;
103066 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
103067 }else{
103068 int r1 = sqlite3GetTempReg(pParse);
103069 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
103070 if( r2==r1 ){
103071 *pReg = r1;
@@ -103083,10 +103166,11 @@
103083 ** in register target.
103084 */
103085 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
103086 int inReg;
103087
 
103088 assert( target>0 && target<=pParse->nMem );
103089 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
103090 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
103091 if( inReg!=target && pParse->pVdbe ){
103092 u8 op;
@@ -103117,13 +103201,13 @@
103117 ** in register target. If the expression is constant, then this routine
103118 ** might choose to code the expression at initialization time.
103119 */
103120 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
103121 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
103122 sqlite3ExprCodeAtInit(pParse, pExpr, target);
103123 }else{
103124 sqlite3ExprCode(pParse, pExpr, target);
103125 }
103126 }
103127
103128 /*
103129 ** Generate code that pushes the value of every element of the given
@@ -103177,11 +103261,11 @@
103177 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
103178 }
103179 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
103180 && sqlite3ExprIsConstantNotJoin(pExpr)
103181 ){
103182 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
103183 }else{
103184 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
103185 if( inReg!=target+i ){
103186 VdbeOp *pOp;
103187 if( copyOp==OP_Copy
@@ -103300,10 +103384,11 @@
103300 int r1, r2;
103301
103302 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
103303 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
103304 if( NEVER(pExpr==0) ) return; /* No way this can happen */
 
103305 op = pExpr->op;
103306 switch( op ){
103307 case TK_AND:
103308 case TK_OR: {
103309 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
@@ -103441,10 +103526,11 @@
103441 int r1, r2;
103442
103443 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
103444 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
103445 if( pExpr==0 ) return;
 
103446
103447 /* The value of pExpr->op and op are related as follows:
103448 **
103449 ** pExpr->op op
103450 ** --------- ----------
@@ -103724,36 +103810,22 @@
103724 return 2;
103725 }
103726 }
103727 if( (pA->flags & (EP_Distinct|EP_Commuted))
103728 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
103729 if( (combinedFlags & EP_TokenOnly)==0 ){
103730 if( combinedFlags & EP_xIsSelect ) return 2;
103731 if( (combinedFlags & EP_FixedCol)==0
103732 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
103733 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
103734 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
103735 if( pA->op!=TK_STRING
103736 && pA->op!=TK_TRUEFALSE
103737 && (combinedFlags & EP_Reduced)==0
103738 ){
103739 if( pA->iColumn!=pB->iColumn ) return 2;
103740 if( pA->op2!=pB->op2 ){
103741 if( pA->op==TK_TRUTH ) return 2;
103742 if( pA->op==TK_FUNCTION && iTab<0 ){
103743 /* Ex: CREATE TABLE t1(a CHECK( a<julianday('now') ));
103744 ** INSERT INTO t1(a) VALUES(julianday('now')+10);
103745 ** Without this test, sqlite3ExprCodeAtInit() will run on the
103746 ** the julianday() of INSERT first, and remember that expression.
103747 ** Then sqlite3ExprCodeInit() will see the julianday() in the CHECK
103748 ** constraint as redundant, reusing the one from the INSERT, even
103749 ** though the julianday() in INSERT lacks the critical NC_IsCheck
103750 ** flag. See ticket [830277d9db6c3ba1] (2019-10-30)
103751 */
103752 return 2;
103753 }
103754 }
103755 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
103756 return 2;
103757 }
103758 }
103759 }
@@ -106442,13 +106514,13 @@
106442 /*
106443 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
106444 ** share an instance of the following structure to hold their state
106445 ** information.
106446 */
106447 typedef struct Stat4Accum Stat4Accum;
106448 typedef struct Stat4Sample Stat4Sample;
106449 struct Stat4Sample {
106450 tRowcnt *anEq; /* sqlite_stat4.nEq */
106451 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
106452 #ifdef SQLITE_ENABLE_STAT4
106453 tRowcnt *anLt; /* sqlite_stat4.nLt */
106454 union {
@@ -106459,31 +106531,33 @@
106459 u8 isPSample; /* True if a periodic sample */
106460 int iCol; /* If !isPSample, the reason for inclusion */
106461 u32 iHash; /* Tiebreaker hash */
106462 #endif
106463 };
106464 struct Stat4Accum {
 
106465 tRowcnt nRow; /* Number of rows in the entire table */
106466 tRowcnt nPSample; /* How often to do a periodic sample */
106467 int nCol; /* Number of columns in index + pk/rowid */
106468 int nKeyCol; /* Number of index columns w/o the pk/rowid */
 
 
 
106469 int mxSample; /* Maximum number of samples to accumulate */
106470 Stat4Sample current; /* Current row as a Stat4Sample */
106471 u32 iPrn; /* Pseudo-random number used for sampling */
106472 Stat4Sample *aBest; /* Array of nCol best samples */
106473 int iMin; /* Index in a[] of entry with minimum score */
106474 int nSample; /* Current number of samples */
106475 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
106476 int iGet; /* Index of current sample accessed by stat_get() */
106477 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
106478 sqlite3 *db; /* Database connection, for malloc() */
106479 };
106480
106481 /* Reclaim memory used by a Stat4Sample
106482 */
106483 #ifdef SQLITE_ENABLE_STAT4
106484 static void sampleClear(sqlite3 *db, Stat4Sample *p){
106485 assert( db!=0 );
106486 if( p->nRowid ){
106487 sqlite3DbFree(db, p->u.aRowid);
106488 p->nRowid = 0;
106489 }
@@ -106491,11 +106565,11 @@
106491 #endif
106492
106493 /* Initialize the BLOB value of a ROWID
106494 */
106495 #ifdef SQLITE_ENABLE_STAT4
106496 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
106497 assert( db!=0 );
106498 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
106499 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
106500 if( p->u.aRowid ){
106501 p->nRowid = n;
@@ -106507,11 +106581,11 @@
106507 #endif
106508
106509 /* Initialize the INTEGER value of a ROWID.
106510 */
106511 #ifdef SQLITE_ENABLE_STAT4
106512 static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
106513 assert( db!=0 );
106514 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
106515 p->nRowid = 0;
106516 p->u.iRowid = iRowid;
106517 }
@@ -106520,11 +106594,11 @@
106520
106521 /*
106522 ** Copy the contents of object (*pFrom) into (*pTo).
106523 */
106524 #ifdef SQLITE_ENABLE_STAT4
106525 static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
106526 pTo->isPSample = pFrom->isPSample;
106527 pTo->iCol = pFrom->iCol;
106528 pTo->iHash = pFrom->iHash;
106529 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
106530 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
@@ -106536,14 +106610,14 @@
106536 }
106537 }
106538 #endif
106539
106540 /*
106541 ** Reclaim all memory of a Stat4Accum structure.
106542 */
106543 static void stat4Destructor(void *pOld){
106544 Stat4Accum *p = (Stat4Accum*)pOld;
106545 #ifdef SQLITE_ENABLE_STAT4
106546 int i;
106547 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
106548 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
106549 sampleClear(p->db, &p->current);
@@ -106567,21 +106641,21 @@
106567 ** For indexes on ordinary rowid tables, N==K+1. But for indexes on
106568 ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
106569 ** PRIMARY KEY of the table. The covering index that implements the
106570 ** original WITHOUT ROWID table as N==K as a special case.
106571 **
106572 ** This routine allocates the Stat4Accum object in heap memory. The return
106573 ** value is a pointer to the Stat4Accum object. The datatype of the
106574 ** return value is BLOB, but it is really just a pointer to the Stat4Accum
106575 ** object.
106576 */
106577 static void statInit(
106578 sqlite3_context *context,
106579 int argc,
106580 sqlite3_value **argv
106581 ){
106582 Stat4Accum *p;
106583 int nCol; /* Number of columns in index being sampled */
106584 int nKeyCol; /* Number of key columns */
106585 int nColUp; /* nCol rounded up for alignment */
106586 int n; /* Bytes of space to allocate */
106587 sqlite3 *db; /* Database connection */
@@ -106596,17 +106670,17 @@
106596 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
106597 nKeyCol = sqlite3_value_int(argv[1]);
106598 assert( nKeyCol<=nCol );
106599 assert( nKeyCol>0 );
106600
106601 /* Allocate the space required for the Stat4Accum object */
106602 n = sizeof(*p)
106603 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
106604 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
106605 #ifdef SQLITE_ENABLE_STAT4
106606 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
106607 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
106608 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
106609 #endif
106610 ;
106611 db = sqlite3_context_db_handle(context);
106612 p = sqlite3DbMallocZero(db, n);
@@ -106631,12 +106705,12 @@
106631 p->mxSample = mxSample;
106632 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
106633 p->current.anLt = &p->current.anEq[nColUp];
106634 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
106635
106636 /* Set up the Stat4Accum.a[] and aBest[] arrays */
106637 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
106638 p->aBest = &p->a[mxSample];
106639 pSpace = (u8*)(&p->a[mxSample+nCol]);
106640 for(i=0; i<(mxSample+nCol); i++){
106641 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
106642 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
@@ -106652,11 +106726,11 @@
106652
106653 /* Return a pointer to the allocated object to the caller. Note that
106654 ** only the pointer (the 2nd parameter) matters. The size of the object
106655 ** (given by the 3rd parameter) is never used and can be any positive
106656 ** value. */
106657 sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
106658 }
106659 static const FuncDef statInitFuncdef = {
106660 2+IsStat4, /* nArg */
106661 SQLITE_UTF8, /* funcFlags */
106662 0, /* pUserData */
@@ -106679,13 +106753,13 @@
106679 **
106680 ** This function assumes that for each argument sample, the contents of
106681 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
106682 */
106683 static int sampleIsBetterPost(
106684 Stat4Accum *pAccum,
106685 Stat4Sample *pNew,
106686 Stat4Sample *pOld
106687 ){
106688 int nCol = pAccum->nCol;
106689 int i;
106690 assert( pNew->iCol==pOld->iCol );
106691 for(i=pNew->iCol+1; i<nCol; i++){
@@ -106703,13 +106777,13 @@
106703 **
106704 ** This function assumes that for each argument sample, the contents of
106705 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
106706 */
106707 static int sampleIsBetter(
106708 Stat4Accum *pAccum,
106709 Stat4Sample *pNew,
106710 Stat4Sample *pOld
106711 ){
106712 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
106713 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
106714
106715 assert( pOld->isPSample==0 && pNew->isPSample==0 );
@@ -106725,34 +106799,34 @@
106725
106726 /*
106727 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
106728 ** remove the least desirable sample from p->a[] to make room.
106729 */
106730 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
106731 Stat4Sample *pSample = 0;
106732 int i;
106733
106734 assert( IsStat4 || nEqZero==0 );
106735
106736 /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
106737 ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
106738 ** other words, if nMaxEqZero is n, then it is guaranteed that there
106739 ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
106740 if( nEqZero>p->nMaxEqZero ){
106741 p->nMaxEqZero = nEqZero;
106742 }
106743 if( pNew->isPSample==0 ){
106744 Stat4Sample *pUpgrade = 0;
106745 assert( pNew->anEq[pNew->iCol]>0 );
106746
106747 /* This sample is being added because the prefix that ends in column
106748 ** iCol occurs many times in the table. However, if we have already
106749 ** added a sample that shares this prefix, there is no need to add
106750 ** this one. Instead, upgrade the priority of the highest priority
106751 ** existing sample that shares this prefix. */
106752 for(i=p->nSample-1; i>=0; i--){
106753 Stat4Sample *pOld = &p->a[i];
106754 if( pOld->anEq[pNew->iCol]==0 ){
106755 if( pOld->isPSample ) return;
106756 assert( pOld->iCol>pNew->iCol );
106757 assert( sampleIsBetter(p, pNew, pOld) );
106758 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
@@ -106767,11 +106841,11 @@
106767 }
106768 }
106769
106770 /* If necessary, remove sample iMin to make room for the new sample. */
106771 if( p->nSample>=p->mxSample ){
106772 Stat4Sample *pMin = &p->a[p->iMin];
106773 tRowcnt *anEq = pMin->anEq;
106774 tRowcnt *anLt = pMin->anLt;
106775 tRowcnt *anDLt = pMin->anDLt;
106776 sampleClear(p->db, pMin);
106777 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
@@ -106810,24 +106884,24 @@
106810 p->iMin = iMin;
106811 }
106812 }
106813 #endif /* SQLITE_ENABLE_STAT4 */
106814
 
106815 /*
106816 ** Field iChng of the index being scanned has changed. So at this point
106817 ** p->current contains a sample that reflects the previous row of the
106818 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
106819 ** correct at this point.
106820 */
106821 static void samplePushPrevious(Stat4Accum *p, int iChng){
106822 #ifdef SQLITE_ENABLE_STAT4
106823 int i;
106824
106825 /* Check if any samples from the aBest[] array should be pushed
106826 ** into IndexSample.a[] at this point. */
106827 for(i=(p->nCol-2); i>=iChng; i--){
106828 Stat4Sample *pBest = &p->aBest[i];
106829 pBest->anEq[i] = p->current.anEq[i];
106830 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
106831 sampleInsert(p, pBest, i);
106832 }
106833 }
@@ -106847,29 +106921,24 @@
106847 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
106848 }
106849 }
106850 p->nMaxEqZero = iChng;
106851 }
106852 #endif
106853
106854 #ifndef SQLITE_ENABLE_STAT4
106855 UNUSED_PARAMETER( p );
106856 UNUSED_PARAMETER( iChng );
106857 #endif
106858 }
 
106859
106860 /*
106861 ** Implementation of the stat_push SQL function: stat_push(P,C,R)
106862 ** Arguments:
106863 **
106864 ** P Pointer to the Stat4Accum object created by stat_init()
106865 ** C Index of left-most column to differ from previous row
106866 ** R Rowid for the current row. Might be a key record for
106867 ** WITHOUT ROWID tables.
106868 **
106869 ** This SQL function always returns NULL. It's purpose it to accumulate
106870 ** statistical data and/or samples in the Stat4Accum object about the
106871 ** index being analyzed. The stat_get() SQL function will later be used to
106872 ** extract relevant information for constructing the sqlite_statN tables.
106873 **
106874 ** The R parameter is only used for STAT4
106875 */
@@ -106879,11 +106948,11 @@
106879 sqlite3_value **argv
106880 ){
106881 int i;
106882
106883 /* The three function arguments */
106884 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
106885 int iChng = sqlite3_value_int(argv[1]);
106886
106887 UNUSED_PARAMETER( argc );
106888 UNUSED_PARAMETER( context );
106889 assert( p->nCol>0 );
@@ -106892,11 +106961,13 @@
106892 if( p->nRow==0 ){
106893 /* This is the first call to this function. Do initialization. */
106894 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
106895 }else{
106896 /* Second and subsequent calls get processed here */
 
106897 samplePushPrevious(p, iChng);
 
106898
106899 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
106900 ** to the current row of the index. */
106901 for(i=0; i<iChng; i++){
106902 p->current.anEq[i]++;
@@ -106961,19 +107032,19 @@
106961 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
106962
106963 /*
106964 ** Implementation of the stat_get(P,J) SQL function. This routine is
106965 ** used to query statistical information that has been gathered into
106966 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
106967 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
106968 ** The content to returned is determined by the parameter J
106969 ** which is one of the STAT_GET_xxxx values defined above.
106970 **
106971 ** The stat_get(P,J) function is not available to generic SQL. It is
106972 ** inserted as part of a manually constructed bytecode program. (See
106973 ** the callStatGet() routine below.) It is guaranteed that the P
106974 ** parameter will always be a poiner to a Stat4Accum object, never a
106975 ** NULL.
106976 **
106977 ** If STAT4 is not enabled, then J is always
106978 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
106979 ** a one-parameter function, stat_get(P), that always returns the
@@ -106982,11 +107053,11 @@
106982 static void statGet(
106983 sqlite3_context *context,
106984 int argc,
106985 sqlite3_value **argv
106986 ){
106987 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
106988 #ifdef SQLITE_ENABLE_STAT4
106989 /* STAT4 has a parameter on this routine. */
106990 int eCall = sqlite3_value_int(argv[1]);
106991 assert( argc==2 );
106992 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
@@ -107003,11 +107074,11 @@
107003 **
107004 ** The value is a string composed of a list of integers describing
107005 ** the index. The first integer in the list is the total number of
107006 ** entries in the index. There is one additional integer in the list
107007 ** for each indexed column. This additional integer is an estimate of
107008 ** the number of rows matched by a stabbing query on the index using
107009 ** a key with the corresponding number of fields. In other words,
107010 ** if the index is on columns (a,b) and the sqlite_stat1 value is
107011 ** "100 10 2", then SQLite estimates that:
107012 **
107013 ** * the index contains 100 rows,
@@ -107046,11 +107117,11 @@
107046 if( p->iGet<0 ){
107047 samplePushPrevious(p, 0);
107048 p->iGet = 0;
107049 }
107050 if( p->iGet<p->nSample ){
107051 Stat4Sample *pS = p->a + p->iGet;
107052 if( pS->nRowid==0 ){
107053 sqlite3_result_int64(context, pS->u.iRowid);
107054 }else{
107055 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
107056 SQLITE_TRANSIENT);
@@ -107137,11 +107208,11 @@
107137 int i; /* Loop counter */
107138 int jZeroRows = -1; /* Jump from here if number of rows is zero */
107139 int iDb; /* Index of database containing pTab */
107140 u8 needTableCnt = 1; /* True to count the table */
107141 int regNewRowid = iMem++; /* Rowid for the inserted record */
107142 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
107143 int regChng = iMem++; /* Index of changed index field */
107144 #ifdef SQLITE_ENABLE_STAT4
107145 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
107146 #endif
107147 int regTemp = iMem++; /* Temporary use register */
@@ -108105,10 +108176,21 @@
108105 pExpr->op = TK_STRING;
108106 }
108107 }
108108 return rc;
108109 }
 
 
 
 
 
 
 
 
 
 
 
108110
108111 /*
108112 ** An SQL user-function registered to do the work of an ATTACH statement. The
108113 ** three arguments to the function come directly from an attach statement:
108114 **
@@ -108178,13 +108260,12 @@
108178 db->aLimit[SQLITE_LIMIT_ATTACHED]
108179 );
108180 goto attach_error;
108181 }
108182 for(i=0; i<db->nDb; i++){
108183 char *z = db->aDb[i].zDbSName;
108184 assert( z && zName );
108185 if( sqlite3StrICmp(z, zName)==0 ){
108186 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
108187 goto attach_error;
108188 }
108189 }
108190
@@ -108333,11 +108414,11 @@
108333
108334 if( zName==0 ) zName = "";
108335 for(i=0; i<db->nDb; i++){
108336 pDb = &db->aDb[i];
108337 if( pDb->pBt==0 ) continue;
108338 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
108339 }
108340
108341 if( i>=db->nDb ){
108342 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
108343 goto detach_error;
@@ -108524,24 +108605,25 @@
108524 SQLITE_PRIVATE int sqlite3FixSrcList(
108525 DbFixer *pFix, /* Context of the fixation */
108526 SrcList *pList /* The Source list to check and modify */
108527 ){
108528 int i;
108529 const char *zDb;
108530 struct SrcList_item *pItem;
 
 
108531
108532 if( NEVER(pList==0) ) return 0;
108533 zDb = pFix->zDb;
108534 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
108535 if( pFix->bTemp==0 ){
108536 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
108537 sqlite3ErrorMsg(pFix->pParse,
108538 "%s %T cannot reference objects in database %s",
108539 pFix->zType, pFix->pName, pItem->zDatabase);
108540 return 1;
108541 }
108542 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
108543 pItem->zDatabase = 0;
108544 pItem->pSchema = pFix->pSchema;
108545 pItem->fg.fromDDL = 1;
108546 }
108547 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
@@ -109262,11 +109344,11 @@
109262 }
109263 #endif
109264 while(1){
109265 for(i=OMIT_TEMPDB; i<db->nDb; i++){
109266 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
109267 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
109268 assert( sqlite3SchemaMutexHeld(db, j, 0) );
109269 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
109270 if( p ) return p;
109271 }
109272 }
@@ -109384,11 +109466,11 @@
109384 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
109385 for(i=OMIT_TEMPDB; i<db->nDb; i++){
109386 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
109387 Schema *pSchema = db->aDb[j].pSchema;
109388 assert( pSchema );
109389 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
109390 assert( sqlite3SchemaMutexHeld(db, j, 0) );
109391 p = sqlite3HashFind(&pSchema->idxHash, zName);
109392 if( p ) break;
109393 }
109394 return p;
@@ -111103,10 +111185,36 @@
111103 if( pMod->pModule->iVersion<3 ) return 0;
111104 if( pMod->pModule->xShadowName==0 ) return 0;
111105 return pMod->pModule->xShadowName(zTail+1);
111106 }
111107 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111108
111109 /*
111110 ** This routine is called to report the final ")" that terminates
111111 ** a CREATE TABLE statement.
111112 **
@@ -111196,10 +111304,12 @@
111196 if( pParse->nErr ){
111197 /* If errors are seen, delete the CHECK constraints now, else they might
111198 ** actually be used if PRAGMA writable_schema=ON is set. */
111199 sqlite3ExprListDelete(db, p->pCheck);
111200 p->pCheck = 0;
 
 
111201 }
111202 }
111203 #endif /* !defined(SQLITE_OMIT_CHECK) */
111204 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
111205 if( p->tabFlags & TF_HasGenerated ){
@@ -114137,20 +114247,33 @@
114137 u8 enc, /* Desired text encoding */
114138 const char *zName, /* Name of the collating sequence. Might be NULL */
114139 int create /* True to create CollSeq if doesn't already exist */
114140 ){
114141 CollSeq *pColl;
 
 
114142 if( zName ){
114143 pColl = findCollSeqEntry(db, zName, create);
 
114144 }else{
114145 pColl = db->pDfltColl;
114146 }
114147 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
114148 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
114149 if( pColl ) pColl += enc-1;
114150 return pColl;
114151 }
 
 
 
 
 
 
 
 
 
 
 
 
 
114152
114153 /*
114154 ** This function is responsible for invoking the collation factory callback
114155 ** or substituting a collation sequence of a different encoding when the
114156 ** requested collation sequence is not available in the desired encoding.
@@ -116321,10 +116444,11 @@
116321 const unsigned char *zA, *zB;
116322 u32 escape;
116323 int nPat;
116324 sqlite3 *db = sqlite3_context_db_handle(context);
116325 struct compareInfo *pInfo = sqlite3_user_data(context);
 
116326
116327 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
116328 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
116329 || sqlite3_value_type(argv[1])==SQLITE_BLOB
116330 ){
@@ -116356,10 +116480,16 @@
116356 sqlite3_result_error(context,
116357 "ESCAPE expression must be a single character", -1);
116358 return;
116359 }
116360 escape = sqlite3Utf8Read(&zEsc);
 
 
 
 
 
 
116361 }else{
116362 escape = pInfo->matchSet;
116363 }
116364 zB = sqlite3_value_text(argv[0]);
116365 zA = sqlite3_value_text(argv[1]);
@@ -117338,29 +117468,33 @@
117338 if( pDef==0 ) return 0;
117339 #endif
117340 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
117341 return 0;
117342 }
117343 if( nExpr<3 ){
117344 aWc[3] = 0;
117345 }else{
117346 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
117347 char *zEscape;
117348 if( pEscape->op!=TK_STRING ) return 0;
117349 zEscape = pEscape->u.zToken;
117350 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
117351 aWc[3] = zEscape[0];
117352 }
117353
117354 /* The memcpy() statement assumes that the wildcard characters are
117355 ** the first three statements in the compareInfo structure. The
117356 ** asserts() that follow verify that assumption
117357 */
117358 memcpy(aWc, pDef->pUserData, 3);
117359 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
117360 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
117361 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117362 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
117363 return 1;
117364 }
117365
117366 /*
@@ -120564,11 +120698,11 @@
120564 case OE_Replace: {
120565 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
120566 VdbeCoverage(v);
120567 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
120568 nSeenReplace++;
120569 sqlite3ExprCode(pParse, pCol->pDflt, iReg);
120570 sqlite3VdbeJumpHere(v, addr1);
120571 break;
120572 }
120573 case OE_Abort:
120574 sqlite3MayAbort(pParse);
@@ -120619,10 +120753,11 @@
120619 ExprList *pCheck = pTab->pCheck;
120620 pParse->iSelfTab = -(regNewData+1);
120621 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
120622 for(i=0; i<pCheck->nExpr; i++){
120623 int allOk;
 
120624 Expr *pExpr = pCheck->a[i].pExpr;
120625 if( aiChng
120626 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
120627 ){
120628 /* The check constraints do not reference any of the columns being
@@ -120633,11 +120768,15 @@
120633 sqlite3TableAffinity(v, pTab, regNewData+1);
120634 bAffinityDone = 1;
120635 }
120636 allOk = sqlite3VdbeMakeLabel(pParse);
120637 sqlite3VdbeVerifyAbortable(v, onError);
120638 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
 
 
 
 
120639 if( onError==OE_Ignore ){
120640 sqlite3VdbeGoto(v, ignoreDest);
120641 }else{
120642 char *zName = pCheck->a[i].zEName;
120643 if( zName==0 ) zName = pTab->zName;
@@ -125952,25 +126091,16 @@
125952 /* Only change the value of sqlite.enc if the database handle is not
125953 ** initialized. If the main database exists, the new sqlite.enc value
125954 ** will be overwritten when the schema is next loaded. If it does not
125955 ** already exists, it will be created to use the new encoding value.
125956 */
125957 int canChangeEnc = 1; /* True if allowed to change the encoding */
125958 int i; /* For looping over all attached databases */
125959 for(i=0; i<db->nDb; i++){
125960 if( db->aDb[i].pBt!=0
125961 && DbHasProperty(db,i,DB_SchemaLoaded)
125962 && !DbHasProperty(db,i,DB_Empty)
125963 ){
125964 canChangeEnc = 0;
125965 }
125966 }
125967 if( canChangeEnc ){
125968 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
125969 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
125970 SCHEMA_ENC(db) = ENC(db) =
125971 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
 
125972 break;
125973 }
125974 }
125975 if( !pEnc->zName ){
125976 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
@@ -126775,11 +126905,11 @@
126775 int iDb = pData->iDb;
126776
126777 assert( argc==5 );
126778 UNUSED_PARAMETER2(NotUsed, argc);
126779 assert( sqlite3_mutex_held(db->mutex) );
126780 DbClearProperty(db, iDb, DB_Empty);
126781 pData->nInitRow++;
126782 if( db->mallocFailed ){
126783 corruptSchema(pData, argv[1], 0);
126784 return 1;
126785 }
@@ -126863,10 +126993,11 @@
126863 char const *azArg[6];
126864 int meta[5];
126865 InitData initData;
126866 const char *zMasterName;
126867 int openedTransaction = 0;
 
126868
126869 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
126870 assert( iDb>=0 && iDb<db->nDb );
126871 assert( db->aDb[iDb].pSchema );
126872 assert( sqlite3_mutex_held(db->mutex) );
@@ -126891,10 +127022,11 @@
126891 initData.rc = SQLITE_OK;
126892 initData.pzErrMsg = pzErrMsg;
126893 initData.mInitFlags = mFlags;
126894 initData.nInitRow = 0;
126895 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
 
126896 if( initData.rc ){
126897 rc = initData.rc;
126898 goto error_out;
126899 }
126900
@@ -126950,31 +127082,29 @@
126950 ** main database, set sqlite3.enc to the encoding of the main database.
126951 ** For an attached db, it is an error if the encoding is not the same
126952 ** as sqlite3.enc.
126953 */
126954 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
126955 if( iDb==0 ){
126956 #ifndef SQLITE_OMIT_UTF16
126957 u8 encoding;
 
126958 /* If opening the main database, set ENC(db). */
126959 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
126960 if( encoding==0 ) encoding = SQLITE_UTF8;
126961 ENC(db) = encoding;
126962 #else
126963 ENC(db) = SQLITE_UTF8;
126964 #endif
 
126965 }else{
126966 /* If opening an attached database, the encoding much match ENC(db) */
126967 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
126968 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
126969 " text encoding as main database");
126970 rc = SQLITE_ERROR;
126971 goto initone_error_out;
126972 }
126973 }
126974 }else{
126975 DbSetProperty(db, iDb, DB_Empty);
126976 }
126977 pDb->pSchema->enc = ENC(db);
126978
126979 if( pDb->pSchema->cache_size==0 ){
126980 #ifndef SQLITE_OMIT_DEPRECATED
@@ -127082,12 +127212,11 @@
127082 ** used to store temporary tables, and any additional database files
127083 ** created using ATTACH statements. Return a success code. If an
127084 ** error occurs, write an error message into *pzErrMsg.
127085 **
127086 ** After a database is initialized, the DB_SchemaLoaded bit is set
127087 ** bit is set in the flags field of the Db structure. If the database
127088 ** file was of zero-length, then the DB_Empty flag is also set.
127089 */
127090 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
127091 int i, rc;
127092 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
127093
@@ -127719,11 +127848,10 @@
127719 sqlite3ExprDelete(db, p->pLimit);
127720 #ifndef SQLITE_OMIT_WINDOWFUNC
127721 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
127722 sqlite3WindowListDelete(db, p->pWinDefn);
127723 }
127724 assert( p->pWin==0 );
127725 #endif
127726 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
127727 if( bFree ) sqlite3DbFreeNN(db, p);
127728 p = pPrior;
127729 bFree = 1;
@@ -131197,10 +131325,42 @@
131197 }
131198 }while( doPrior && (p = p->pPrior)!=0 );
131199 }
131200 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
131201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131202 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
131203 /*
131204 ** This routine attempts to flatten subqueries as a performance optimization.
131205 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
131206 **
@@ -131735,10 +131895,16 @@
131735 */
131736 if( pSub->pLimit ){
131737 pParent->pLimit = pSub->pLimit;
131738 pSub->pLimit = 0;
131739 }
 
 
 
 
 
 
131740 }
131741
131742 /* Finially, delete what is left of the subquery and return
131743 ** success.
131744 */
@@ -135184,11 +135350,11 @@
135184 zDb = pName->a[0].zDatabase;
135185 zName = pName->a[0].zName;
135186 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
135187 for(i=OMIT_TEMPDB; i<db->nDb; i++){
135188 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
135189 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
135190 assert( sqlite3SchemaMutexHeld(db, j, 0) );
135191 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
135192 if( pTrigger ) break;
135193 }
135194 if( !pTrigger ){
@@ -141206,10 +141372,13 @@
141206 assert( pRangeEnd==0 && pRangeStart==0 );
141207 testcase( pLoop->nSkip>0 );
141208 nExtraReg = 1;
141209 bSeekPastNull = 1;
141210 pLevel->regBignull = regBignull = ++pParse->nMem;
 
 
 
141211 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
141212 }
141213
141214 /* If we are doing a reverse order scan on an ascending index, or
141215 ** a forward order scan on a descending index, interchange the
@@ -148759,11 +148928,11 @@
148759 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
148760 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
148761 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
148762 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
148763 ){
148764 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
148765 }
148766 VdbeComment((v, "%s", pIx->zName));
148767 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
148768 {
148769 u64 colUsed = 0;
@@ -148917,16 +149086,10 @@
148917 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
148918 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
148919 if( pIn->eEndLoopOp!=OP_Noop ){
148920 if( pIn->nPrefix ){
148921 assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
148922 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
148923 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
148924 sqlite3VdbeCurrentAddr(v)+2+(pLevel->iLeftJoin!=0),
148925 pIn->iBase, pIn->nPrefix);
148926 VdbeCoverage(v);
148927 }
148928 if( pLevel->iLeftJoin ){
148929 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
148930 ** opened yet. This occurs for WHERE clauses such as
148931 ** "a = ? AND b IN (...)", where the index is on (a, b). If
148932 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
@@ -148933,13 +149096,20 @@
148933 ** never have been coded, but the body of the loop run to
148934 ** return the null-row. So, if the cursor is not open yet,
148935 ** jump over the OP_Next or OP_Prev instruction about to
148936 ** be coded. */
148937 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
148938 sqlite3VdbeCurrentAddr(v) + 2
 
148939 );
148940 VdbeCoverage(v);
 
 
 
 
 
 
148941 }
148942 }
148943 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
148944 VdbeCoverage(v);
148945 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
@@ -150053,10 +150223,11 @@
150053
150054 ExprList *pSublist = 0; /* Expression list for sub-query */
150055 Window *pMWin = p->pWin; /* Master window object */
150056 Window *pWin; /* Window object iterator */
150057 Table *pTab;
 
150058
150059 pTab = sqlite3DbMallocZero(db, sizeof(Table));
150060 if( pTab==0 ){
150061 return sqlite3ErrorToParser(db, SQLITE_NOMEM);
150062 }
@@ -150142,10 +150313,11 @@
150142 Table *pTab2;
150143 p->pSrc->a[0].pSelect = pSub;
150144 sqlite3SrcListAssignCursors(pParse, p->pSrc);
150145 pSub->selFlags |= SF_Expanded;
150146 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
 
150147 if( pTab2==0 ){
150148 /* Might actually be some other kind of error, but in that case
150149 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
150150 ** the correct error message regardless. */
150151 rc = SQLITE_NOMEM;
@@ -151030,10 +151202,11 @@
151030 int regArg;
151031 int nArg = 0;
151032 Window *pWin;
151033 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
151034 FuncDef *pFunc = pWin->pFunc;
 
151035 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
151036 nArg = MAX(nArg, windowArgCount(pWin));
151037 if( pMWin->regStartRowid==0 ){
151038 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
151039 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
@@ -151408,10 +151581,14 @@
151408 pNew->eFrmType = p->eFrmType;
151409 pNew->eEnd = p->eEnd;
151410 pNew->eStart = p->eStart;
151411 pNew->eExclude = p->eExclude;
151412 pNew->regResult = p->regResult;
 
 
 
 
151413 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
151414 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
151415 pNew->pOwner = pOwner;
151416 pNew->bImplicitFrame = p->bImplicitFrame;
151417 }
@@ -152245,10 +152422,11 @@
152245 if( p ){
152246 /* memset(p, 0, sizeof(Expr)); */
152247 p->op = (u8)op;
152248 p->affExpr = 0;
152249 p->flags = EP_Leaf;
 
152250 p->iAgg = -1;
152251 p->pLeft = p->pRight = 0;
152252 p->x.pList = 0;
152253 p->pAggInfo = 0;
152254 p->y.pTab = 0;
@@ -162084,15 +162262,10 @@
162084 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
162085 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
162086 if( db->mallocFailed ){
162087 goto opendb_out;
162088 }
162089 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
162090 ** strings is BINARY.
162091 */
162092 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
162093 assert( db->pDfltColl!=0 );
162094
162095 /* Parse the filename/URI argument
162096 **
162097 ** Only allow sensible combinations of bits in the flags argument.
162098 ** Throw an error if any non-sense combination is used. If we
@@ -162133,11 +162306,13 @@
162133 sqlite3Error(db, rc);
162134 goto opendb_out;
162135 }
162136 sqlite3BtreeEnter(db->aDb[0].pBt);
162137 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
162138 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
 
 
162139 sqlite3BtreeLeave(db->aDb[0].pBt);
162140 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
162141
162142 /* The default safety_level for the main database is FULL; for the temp
162143 ** database it is OFF. This matches the pager layer defaults.
@@ -166664,10 +166839,11 @@
166664 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
166665 char *zBuffer = 0; /* Buffer to load terms into */
166666 i64 nAlloc = 0; /* Size of allocated buffer */
166667 int isFirstTerm = 1; /* True when processing first term on page */
166668 sqlite3_int64 iChild; /* Block id of child node to descend to */
 
166669
166670 /* Skip over the 'height' varint that occurs at the start of every
166671 ** interior node. Then load the blockid of the left-child of the b-tree
166672 ** node into variable iChild.
166673 **
@@ -166688,16 +166864,19 @@
166688
166689 while( zCsr<zEnd && (piFirst || piLast) ){
166690 int cmp; /* memcmp() result */
166691 int nSuffix; /* Size of term suffix */
166692 int nPrefix = 0; /* Size of term prefix */
166693 int nBuffer; /* Total term size */
166694
166695 /* Load the next term on the node into zBuffer. Use realloc() to expand
166696 ** the size of zBuffer if required. */
166697 if( !isFirstTerm ){
166698 zCsr += fts3GetVarint32(zCsr, &nPrefix);
 
 
 
 
166699 }
166700 isFirstTerm = 0;
166701 zCsr += fts3GetVarint32(zCsr, &nSuffix);
166702
166703 assert( nPrefix>=0 && nSuffix>=0 );
@@ -179916,10 +180095,16 @@
179916
179917 /* If nSeg is less that zero, then there is no level with at least
179918 ** nMin segments and no hint in the %_stat table. No work to do.
179919 ** Exit early in this case. */
179920 if( nSeg<=0 ) break;
 
 
 
 
 
 
179921
179922 /* Open a cursor to iterate through the contents of the oldest nSeg
179923 ** indexes of absolute level iAbsLevel. If this cursor is opened using
179924 ** the 'hint' parameters, it is possible that there are less than nSeg
179925 ** segments available in level iAbsLevel. In this case, no work is
@@ -189652,12 +189837,14 @@
189652 pRtree->nAux++;
189653 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
189654 }else if( pRtree->nAux>0 ){
189655 break;
189656 }else{
 
189657 pRtree->nDim2++;
189658 sqlite3_str_appendf(pSql, ",%.*s NUM", rtreeTokenLength(zArg), zArg);
 
189659 }
189660 }
189661 sqlite3_str_appendf(pSql, ");");
189662 zSql = sqlite3_str_finish(pSql);
189663 if( !zSql ){
@@ -192389,11 +192576,11 @@
192389 ** 1. uPattern is an unescaped match-all character "%",
192390 ** 2. uPattern is an unescaped match-one character "_",
192391 ** 3. uPattern is an unescaped escape character, or
192392 ** 4. uPattern is to be handled as an ordinary character
192393 */
192394 if( !prevEscape && uPattern==MATCH_ALL ){
192395 /* Case 1. */
192396 uint8_t c;
192397
192398 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
192399 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
@@ -192415,16 +192602,16 @@
192415 }
192416 SQLITE_ICU_SKIP_UTF8(zString);
192417 }
192418 return 0;
192419
192420 }else if( !prevEscape && uPattern==MATCH_ONE ){
192421 /* Case 2. */
192422 if( *zString==0 ) return 0;
192423 SQLITE_ICU_SKIP_UTF8(zString);
192424
192425 }else if( !prevEscape && uPattern==(uint32_t)uEsc){
192426 /* Case 3. */
192427 prevEscape = 1;
192428
192429 }else{
192430 /* Case 4. */
@@ -199222,10 +199409,11 @@
199222 }
199223 }
199224 i = 0;
199225 if( iSchema>=0 ){
199226 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
 
199227 pIdxInfo->idxNum |= 0x01;
199228 }
199229 if( iName>=0 ){
199230 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
199231 pIdxInfo->idxNum |= 0x02;
@@ -199436,11 +199624,13 @@
199436 assert( nPayload>=(u32)nLocal );
199437 assert( nLocal<=(nUsable-35) );
199438 if( nPayload>(u32)nLocal ){
199439 int j;
199440 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
199441 if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
 
 
199442 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
199443 pCell->nOvfl = nOvfl;
199444 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
199445 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
199446 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
@@ -203769,11 +203959,11 @@
203769 const char *zSep = "";
203770 int rc = SQLITE_OK;
203771 SessionBuffer buf = {0, 0, 0};
203772 int nPk = 0;
203773
203774 sessionAppendStr(&buf, "DELETE FROM ", &rc);
203775 sessionAppendIdent(&buf, zTab, &rc);
203776 sessionAppendStr(&buf, " WHERE ", &rc);
203777
203778 for(i=0; i<p->nCol; i++){
203779 if( p->abPK[i] ){
@@ -203852,11 +204042,11 @@
203852 int i;
203853 const char *zSep = "";
203854 SessionBuffer buf = {0, 0, 0};
203855
203856 /* Append "UPDATE tbl SET " */
203857 sessionAppendStr(&buf, "UPDATE ", &rc);
203858 sessionAppendIdent(&buf, zTab, &rc);
203859 sessionAppendStr(&buf, " SET ", &rc);
203860
203861 /* Append the assignments */
203862 for(i=0; i<p->nCol; i++){
@@ -223538,11 +223728,11 @@
223538 int nArg, /* Number of args */
223539 sqlite3_value **apUnused /* Function arguments */
223540 ){
223541 assert( nArg==0 );
223542 UNUSED_PARAM2(nArg, apUnused);
223543 sqlite3_result_text(pCtx, "fts5: 2020-02-27 11:32:14 bfb09371d452d5d4dacab2ec476880bc729952f44ac0e5de90ea7ba203243c8c", -1, SQLITE_TRANSIENT);
223544 }
223545
223546 /*
223547 ** Return true if zName is the extension on one of the shadow tables used
223548 ** by this module.
@@ -228320,12 +228510,12 @@
228320 }
228321 #endif /* SQLITE_CORE */
228322 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
228323
228324 /************** End of stmt.c ************************************************/
228325 #if __LINE__!=228325
228326 #undef SQLITE_SOURCE_ID
228327 #define SQLITE_SOURCE_ID "2020-02-27 16:21:39 951b39ca74c9bd933139e099d5555283278db475f410f202c162e5d1e6aealt2"
228328 #endif
228329 /* Return the source-id for this library */
228330 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
228331 /************************** End of sqlite3.c ******************************/
228332
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1162,11 +1162,11 @@
1162 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1163 ** [sqlite_version()] and [sqlite_source_id()].
1164 */
1165 #define SQLITE_VERSION "3.32.0"
1166 #define SQLITE_VERSION_NUMBER 3032000
1167 #define SQLITE_SOURCE_ID "2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84f525"
1168
1169 /*
1170 ** CAPI3REF: Run-Time Library Version Numbers
1171 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1172 **
@@ -16539,11 +16539,10 @@
16539 ** have been filled out. If the schema changes, these column names might
16540 ** changes and so the view will need to be reset.
16541 */
16542 #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
16543 #define DB_UnresetViews 0x0002 /* Some views have defined column names */
 
16544 #define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
16545
16546 /*
16547 ** The number of different kinds of things that can be limited
16548 ** using the sqlite3_limit() interface.
@@ -16697,11 +16696,11 @@
16696 ** Each database connection is an instance of the following structure.
16697 */
16698 struct sqlite3 {
16699 sqlite3_vfs *pVfs; /* OS Interface */
16700 struct Vdbe *pVdbe; /* List of active virtual machines */
16701 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
16702 sqlite3_mutex *mutex; /* Connection mutex */
16703 Db *aDb; /* All backends */
16704 int nDb; /* Number of backends currently in use */
16705 u32 mDbFlags; /* flags recording internal state */
16706 u64 flags; /* flags settable by pragmas. See below */
@@ -16906,10 +16905,11 @@
16905 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
16906 #define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
16907 #define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
16908 #define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
16909 #define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
16910 #define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
16911
16912 /*
16913 ** Bits of the sqlite3.dbOptFlags field that are used by the
16914 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
16915 ** selectively disable various optimizations.
@@ -17869,10 +17869,13 @@
17869 char affExpr; /* affinity, or RAISE type */
17870 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
17871 ** TK_COLUMN: the value of p5 for OP_Column
17872 ** TK_AGG_FUNCTION: nesting depth
17873 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
17874 #ifdef SQLITE_DEBUG
17875 u8 vvaFlags; /* Verification flags. */
17876 #endif
17877 u32 flags; /* Various flags. EP_* See below */
17878 union {
17879 char *zToken; /* Token value. Zero terminated and dequoted */
17880 int iValue; /* Non-negative integer value if EP_IntValue */
17881 } u;
@@ -17943,11 +17946,11 @@
17946 #define EP_Skip 0x001000 /* Operator does not contribute to affinity */
17947 #define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
17948 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17949 #define EP_Win 0x008000 /* Contains window functions */
17950 #define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17951 /* 0x020000 // available for reuse */
17952 #define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
17953 #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
17954 #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
17955 #define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
17956 #define EP_Alias 0x400000 /* Is an alias for a result set column */
@@ -17957,10 +17960,11 @@
17960 #define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
17961 #define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
17962 #define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
17963 #define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
17964 #define EP_FromDDL 0x40000000 /* Originates from sqlite_master */
17965 /* 0x80000000 // Available */
17966
17967 /*
17968 ** The EP_Propagate mask is a set of properties that automatically propagate
17969 ** upwards into parent nodes.
17970 */
@@ -17975,18 +17979,28 @@
17979 #define ExprSetProperty(E,P) (E)->flags|=(P)
17980 #define ExprClearProperty(E,P) (E)->flags&=~(P)
17981 #define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
17982 #define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
17983
17984
17985 /* Flags for use with Expr.vvaFlags
17986 */
17987 #define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
17988 #define EP_Immutable 0x02 /* Do not change this Expr node */
17989
17990 /* The ExprSetVVAProperty() macro is used for Verification, Validation,
17991 ** and Accreditation only. It works like ExprSetProperty() during VVA
17992 ** processes but is a no-op for delivery.
17993 */
17994 #ifdef SQLITE_DEBUG
17995 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
17996 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
17997 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
17998 #else
17999 # define ExprSetVVAProperty(E,P)
18000 # define ExprHasVVAProperty(E,P) 0
18001 # define ExprClearVVAProperties(E)
18002 #endif
18003
18004 /*
18005 ** Macros to determine the number of bytes required by a normal Expr
18006 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
@@ -18956,10 +18970,11 @@
18970 Select *pSelect; /* HAVING to WHERE clause ctx */
18971 struct WindowRewrite *pRewrite; /* Window rewrite context */
18972 struct WhereConst *pConst; /* WHERE clause constants */
18973 struct RenameCtx *pRename; /* RENAME COLUMN context */
18974 struct Table *pTab; /* Table of generated column */
18975 struct SrcList_item *pSrcItem; /* A single FROM clause item */
18976 } u;
18977 };
18978
18979 /* Forward declarations */
18980 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -19500,11 +19515,11 @@
19515 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
19516 SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
19517 #endif
19518 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
19519 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
19520 SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
19521 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
19522 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
19523 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
19524 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
19525 #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
@@ -19655,10 +19670,11 @@
19670 # define sqlite3AuthRead(a,b,c,d)
19671 # define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
19672 # define sqlite3AuthContextPush(a,b,c)
19673 # define sqlite3AuthContextPop(a) ((void)(a))
19674 #endif
19675 SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
19676 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
19677 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
19678 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
19679 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
19680 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
@@ -19714,14 +19730,14 @@
19730 #define putVarint sqlite3PutVarint
19731
19732
19733 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19734 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19735 SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
19736 SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
19737 SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
19738 SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
19739 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
19740 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
19741 SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19742 SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
19743 SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
@@ -19740,13 +19756,14 @@
19756 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
19757 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
19758 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19759 SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
19760 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19761 SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8);
19762 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
19763 SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
19764 SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
19765 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
19766 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
19767 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
19768 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
19769 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
@@ -19809,10 +19826,11 @@
19826 const struct ExprList_item*,
19827 const char*,
19828 const char*,
19829 const char*
19830 );
19831 SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
19832 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
19833 SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19834 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19835 SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19836 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
@@ -19973,12 +19991,12 @@
19991 #ifdef SQLITE_ENABLE_NORMALIZE
19992 SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
19993 #endif
19994 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
19995 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
19996 SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
19997 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
19998 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
19999 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
20000 #ifndef SQLITE_OMIT_WAL
20001 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
20002 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
@@ -20947,13 +20965,13 @@
20965 #endif
20966 u16 nResColumn; /* Number of columns in one row of the result set */
20967 u8 errorAction; /* Recovery action to do in case of an error */
20968 u8 minWriteFileFormat; /* Minimum file format for writable database files */
20969 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20970 u8 doingRerun; /* True if rerunning after an auto-reprepare */
20971 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
20972 bft explain:2; /* True if EXPLAIN present on SQL command */
 
20973 bft changeCntOn:1; /* True to update the change-counter */
20974 bft runOnlyOnce:1; /* Automatically expire on reset */
20975 bft usesStmtJournal:1; /* True if uses a statement journal */
20976 bft readOnly:1; /* True for statements that do not write */
20977 bft bIsReader:1; /* True for statements that read */
@@ -29390,12 +29408,12 @@
29408 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
29409 }else if( pItem->zName ){
29410 sqlite3_str_appendf(&x, " %s", pItem->zName);
29411 }
29412 if( pItem->pTab ){
29413 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
29414 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
29415 }
29416 if( pItem->zAlias ){
29417 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
29418 }
29419 if( pItem->fg.jointype & JT_LEFT ){
@@ -29650,27 +29668,30 @@
29668 ** Generate a human-readable explanation of an expression tree.
29669 */
29670 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
29671 const char *zBinOp = 0; /* Binary operator */
29672 const char *zUniOp = 0; /* Unary operator */
29673 char zFlgs[200];
29674 pView = sqlite3TreeViewPush(pView, moreToFollow);
29675 if( pExpr==0 ){
29676 sqlite3TreeViewLine(pView, "nil");
29677 sqlite3TreeViewPop(pView);
29678 return;
29679 }
29680 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
29681 StrAccum x;
29682 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
29683 sqlite3_str_appendf(&x, " fg.af=%x.%c",
29684 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
29685 if( ExprHasProperty(pExpr, EP_FromJoin) ){
29686 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
29687 }
29688 if( ExprHasProperty(pExpr, EP_FromDDL) ){
29689 sqlite3_str_appendf(&x, " DDL");
29690 }
29691 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
29692 sqlite3_str_appendf(&x, " IMMUTABLE");
29693 }
29694 sqlite3StrAccumFinish(&x);
29695 }else{
29696 zFlgs[0] = 0;
29697 }
@@ -29774,10 +29795,11 @@
29795 case TK_SLASH: zBinOp = "DIV"; break;
29796 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
29797 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
29798 case TK_CONCAT: zBinOp = "CONCAT"; break;
29799 case TK_DOT: zBinOp = "DOT"; break;
29800 case TK_LIMIT: zBinOp = "LIMIT"; break;
29801
29802 case TK_UMINUS: zUniOp = "UMINUS"; break;
29803 case TK_UPLUS: zUniOp = "UPLUS"; break;
29804 case TK_BITNOT: zUniOp = "BITNOT"; break;
29805 case TK_NOT: zUniOp = "NOT"; break;
@@ -50895,11 +50917,11 @@
50917 }
50918
50919 /*
50920 ** Allocate a new RowSetEntry object that is associated with the
50921 ** given RowSet. Return a pointer to the new and completely uninitialized
50922 ** object.
50923 **
50924 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
50925 ** routine returns NULL.
50926 */
50927 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
@@ -51171,11 +51193,11 @@
51193 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
51194 p = pRowSet->pEntry;
51195 if( p ){
51196 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
51197 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51198 /* Only sort the current set of entries if they need it */
51199 p = rowSetEntrySort(p);
51200 }
51201 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51202 ppPrevTree = &pTree->pRight;
51203 if( pTree->pLeft==0 ){
@@ -64536,11 +64558,11 @@
64558 ** free-list for reuse. It returns false if it is safe to retrieve the
64559 ** page from the pager layer with the 'no-content' flag set. True otherwise.
64560 */
64561 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
64562 Bitvec *p = pBt->pHasContent;
64563 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
64564 }
64565
64566 /*
64567 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
64568 ** invoked at the conclusion of each write-transaction.
@@ -76180,23 +76202,18 @@
76202 u16 mFlags;
76203 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
76204 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
76205 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
76206 }
76207 /* If pX is marked as a shallow copy of pMem, then try to verify that
76208 ** no significant changes have been made to pX since the OP_SCopy.
76209 ** A significant change would indicated a missed call to this
76210 ** function for pX. Minor changes, such as adding or removing a
76211 ** dual type, are allowed, as long as the underlying value is the
76212 ** same. */
76213 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
76214 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
 
 
 
 
 
76215
76216 /* pMem is the register that is changing. But also mark pX as
76217 ** undefined so that we can quickly detect the shallow-copy error */
76218 pX->flags = MEM_Undefined;
76219 pX->pScopyFrom = 0;
@@ -77547,11 +77564,11 @@
77564 (void)z2;
77565 }
77566 #endif
77567
77568 /*
77569 ** Add a new OP_Explain opcode.
77570 **
77571 ** If the bPush flag is true, then make this opcode the parent for
77572 ** subsequent Explains until sqlite3VdbeExplainPop() is called.
77573 */
77574 SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
@@ -78781,12 +78798,15 @@
78798 displayP4Expr(&x, pOp->p4.pExpr);
78799 break;
78800 }
78801 #endif
78802 case P4_COLLSEQ: {
78803 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
78804 CollSeq *pColl = pOp->p4.pColl;
78805 assert( pColl->enc>=0 && pColl->enc<4 );
78806 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
78807 encnames[pColl->enc]);
78808 break;
78809 }
78810 case P4_FUNCDEF: {
78811 FuncDef *pDef = pOp->p4.pFunc;
78812 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
@@ -79495,10 +79515,11 @@
79515 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
79516 "id", "parent", "notused", "detail"
79517 };
79518 int iFirst, mx, i;
79519 if( nMem<10 ) nMem = 10;
79520 p->explain = pParse->explain;
79521 if( pParse->explain==2 ){
79522 sqlite3VdbeSetNumCols(p, 4);
79523 iFirst = 8;
79524 mx = 12;
79525 }else{
@@ -79545,11 +79566,10 @@
79566 }
79567 }
79568
79569 p->pVList = pParse->pVList;
79570 pParse->pVList = 0;
 
79571 if( db->mallocFailed ){
79572 p->nVar = 0;
79573 p->nCursor = 0;
79574 p->nMem = 0;
79575 }else{
@@ -86230,11 +86250,10 @@
86250 u16 flags2; /* Initial flags for P2 */
86251
86252 pIn1 = &aMem[pOp->p1];
86253 pIn2 = &aMem[pOp->p2];
86254 pOut = &aMem[pOp->p3];
 
86255 testcase( pOut==pIn2 );
86256 assert( pIn1!=pOut );
86257 flags1 = pIn1->flags;
86258 testcase( flags1 & MEM_Null );
86259 testcase( pIn2->flags & MEM_Null );
@@ -88351,11 +88370,11 @@
88370 **
88371 ** Allowed P5 bits:
88372 ** <ul>
88373 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88374 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88375 ** of OP_SeekLE/OP_IdxLT)
88376 ** </ul>
88377 **
88378 ** The P4 value may be either an integer (P4_INT32) or a pointer to
88379 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
88380 ** object, then table being opened must be an [index b-tree] where the
@@ -88381,11 +88400,11 @@
88400 **
88401 ** Allowed P5 bits:
88402 ** <ul>
88403 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88404 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88405 ** of OP_SeekLE/OP_IdxLT)
88406 ** </ul>
88407 **
88408 ** See also: OP_OpenRead, OP_OpenWrite
88409 */
88410 /* Opcode: OpenWrite P1 P2 P3 P4 P5
@@ -88405,11 +88424,11 @@
88424 **
88425 ** Allowed P5 bits:
88426 ** <ul>
88427 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88428 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88429 ** of OP_SeekLE/OP_IdxLT)
88430 ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
88431 ** and subsequently delete entries in an index btree. This is a
88432 ** hint to the storage engine that the storage engine is allowed to
88433 ** ignore. The hint is not used by the official SQLite b*tree storage
88434 ** engine, but is used by COMDB2.
@@ -88517,13 +88536,11 @@
88536
88537 open_cursor_set_hints:
88538 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
88539 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
88540 testcase( pOp->p5 & OPFLAG_BULKCSR );
 
88541 testcase( pOp->p2 & OPFLAG_SEEKEQ );
 
88542 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
88543 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
88544 if( rc ) goto abort_due_to_error;
88545 break;
88546 }
@@ -88775,15 +88792,17 @@
88792 ** Reposition cursor P1 so that it points to the smallest entry that
88793 ** is greater than or equal to the key value. If there are no records
88794 ** greater than or equal to the key and P2 is not zero, then jump to P2.
88795 **
88796 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
88797 ** opcode will either land on a record that exactly matches the key, or
88798 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
88799 ** this opcode must be followed by an IdxLE opcode with the same arguments.
88800 ** The IdxGT opcode will be skipped if this opcode succeeds, but the
88801 ** IdxGT opcode will be used on subsequent loop iterations. The
88802 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
88803 ** is an equality search.
88804 **
88805 ** This opcode leaves the cursor configured to move in forward order,
88806 ** from the beginning toward the end. In other words, the cursor is
88807 ** configured to use Next, not Prev.
88808 **
@@ -88795,11 +88814,11 @@
88814 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
88815 ** use the value in register P3 as a key. If cursor P1 refers
88816 ** to an SQL index, then P3 is the first in an array of P4 registers
88817 ** that are used as an unpacked index key.
88818 **
88819 ** Reposition cursor P1 so that it points to the smallest entry that
88820 ** is greater than the key value. If there are no records greater than
88821 ** the key and P2 is not zero, then jump to P2.
88822 **
88823 ** This opcode leaves the cursor configured to move in forward order,
88824 ** from the beginning toward the end. In other words, the cursor is
@@ -88840,15 +88859,17 @@
88859 ** This opcode leaves the cursor configured to move in reverse order,
88860 ** from the end toward the beginning. In other words, the cursor is
88861 ** configured to use Prev, not Next.
88862 **
88863 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
88864 ** opcode will either land on a record that exactly matches the key, or
88865 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
88866 ** this opcode must be followed by an IdxLE opcode with the same arguments.
88867 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
88868 ** IdxGE opcode will be used on subsequent loop iterations. The
88869 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
88870 ** is an equality search.
88871 **
88872 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
88873 */
88874 case OP_SeekLT: /* jump, in3, group */
88875 case OP_SeekLE: /* jump, in3, group */
@@ -88881,11 +88902,11 @@
88902
88903 pC->deferredMoveto = 0;
88904 pC->cacheStatus = CACHE_STALE;
88905 if( pC->isTable ){
88906 u16 flags3, newType;
88907 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
88908 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
88909 || CORRUPT_DB );
88910
88911 /* The input value in P3 might be of any type: integer, real, string,
88912 ** blob, or NULL. But it needs to be an integer before we can do
@@ -88940,18 +88961,21 @@
88961 pC->movetoTarget = iKey; /* Used by OP_Delete */
88962 if( rc!=SQLITE_OK ){
88963 goto abort_due_to_error;
88964 }
88965 }else{
88966 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
88967 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
88968 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
88969 ** with the same key.
88970 */
88971 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
88972 eqOnly = 1;
88973 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
88974 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
88975 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
88976 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
88977 assert( pOp[1].p1==pOp[0].p1 );
88978 assert( pOp[1].p2==pOp[0].p2 );
88979 assert( pOp[1].p3==pOp[0].p3 );
88980 assert( pOp[1].p4.i==pOp[0].p4.i );
88981 }
@@ -91162,11 +91186,11 @@
91186 ** try to reuse register values from the first use. */
91187 {
91188 int i;
91189 for(i=0; i<p->nMem; i++){
91190 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
91191 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
91192 }
91193 }
91194 #endif
91195 pOp = &aOp[-1];
91196 goto check_for_interrupt;
@@ -96555,19 +96579,20 @@
96579 SrcList *pSrc;
96580 int i;
96581 struct SrcList_item *pItem;
96582
96583 pSrc = p->pSrc;
96584 if( pSrc ){
96585 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
96586 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
96587 return WRC_Abort;
96588 }
96589 if( pItem->fg.isTabFunc
96590 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
96591 ){
96592 return WRC_Abort;
96593 }
96594 }
96595 }
96596 return WRC_Continue;
96597 }
96598
@@ -96784,10 +96809,35 @@
96809 }else{
96810 /* Currently parsing a DML statement */
96811 return (db->flags & SQLITE_DqsDML)!=0;
96812 }
96813 }
96814
96815 /*
96816 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
96817 ** return the appropriate colUsed mask.
96818 */
96819 SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr *pExpr){
96820 int n;
96821 Table *pExTab;
96822
96823 n = pExpr->iColumn;
96824 pExTab = pExpr->y.pTab;
96825 assert( pExTab!=0 );
96826 if( (pExTab->tabFlags & TF_HasGenerated)!=0
96827 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
96828 ){
96829 testcase( pExTab->nCol==BMS-1 );
96830 testcase( pExTab->nCol==BMS );
96831 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
96832 }else{
96833 testcase( n==BMS-1 );
96834 testcase( n==BMS );
96835 if( n>=BMS ) n = BMS-1;
96836 return ((Bitmask)1)<<n;
96837 }
96838 }
96839
96840 /*
96841 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
96842 ** that name in the set of source tables in pSrcList and make the pExpr
96843 ** expression node refer back to that source column. The following changes
@@ -96861,10 +96911,16 @@
96911 assert( db->aDb[i].zDbSName );
96912 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
96913 pSchema = db->aDb[i].pSchema;
96914 break;
96915 }
96916 }
96917 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
96918 /* This branch is taken when the main database has been renamed
96919 ** using SQLITE_DBCONFIG_MAINDBNAME. */
96920 pSchema = db->aDb[0].pSchema;
96921 zDb = db->aDb[0].zDbSName;
96922 }
96923 }
96924 }
96925
96926 /* Start at the inner-most context and move outward until a match is found */
@@ -97181,26 +97237,11 @@
97237 **
97238 ** If a generated column is referenced, set bits for every column
97239 ** of the table.
97240 */
97241 if( pExpr->iColumn>=0 && pMatch!=0 ){
97242 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97243 }
97244
97245 /* Clean up and return
97246 */
97247 sqlite3ExprDelete(db, pExpr->pLeft);
@@ -98557,11 +98598,11 @@
98598 ** CREATE TABLE t1(a);
98599 ** SELECT * FROM t1 WHERE a;
98600 ** SELECT a AS b FROM t1 WHERE b;
98601 ** SELECT * FROM t1 WHERE (select a from t1);
98602 */
98603 SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
98604 int op;
98605 while( ExprHasProperty(pExpr, EP_Skip) ){
98606 assert( pExpr->op==TK_COLLATE );
98607 pExpr = pExpr->pLeft;
98608 assert( pExpr!=0 );
@@ -98667,14 +98708,14 @@
98708 ** The collating sequence might be determined by a COLLATE operator
98709 ** or by the presence of a column with a defined collating sequence.
98710 ** COLLATE operators take first precedence. Left operands take
98711 ** precedence over right operands.
98712 */
98713 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
98714 sqlite3 *db = pParse->db;
98715 CollSeq *pColl = 0;
98716 const Expr *p = pExpr;
98717 while( p ){
98718 int op = p->op;
98719 if( op==TK_REGISTER ) op = p->op2;
98720 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
98721 && p->y.pTab!=0
@@ -98739,21 +98780,21 @@
98780 ** See also: sqlite3ExprCollSeq()
98781 **
98782 ** The sqlite3ExprCollSeq() routine works the same except that it
98783 ** returns NULL if there is no defined collation.
98784 */
98785 SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
98786 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
98787 if( p==0 ) p = pParse->db->pDfltColl;
98788 assert( p!=0 );
98789 return p;
98790 }
98791
98792 /*
98793 ** Return TRUE if the two expressions have equivalent collating sequences.
98794 */
98795 SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
98796 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
98797 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
98798 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
98799 }
98800
@@ -98760,11 +98801,11 @@
98801 /*
98802 ** pExpr is an operand of a comparison operator. aff2 is the
98803 ** type affinity of the other operand. This routine returns the
98804 ** type affinity that should be used for the comparison operator.
98805 */
98806 SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
98807 char aff1 = sqlite3ExprAffinity(pExpr);
98808 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
98809 /* Both sides of the comparison are columns. If one has numeric
98810 ** affinity, use that. Otherwise use no affinity.
98811 */
@@ -98782,11 +98823,11 @@
98823
98824 /*
98825 ** pExpr is a comparison operator. Return the type affinity that should
98826 ** be applied to both operands prior to doing the comparison.
98827 */
98828 static char comparisonAffinity(const Expr *pExpr){
98829 char aff;
98830 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
98831 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
98832 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
98833 assert( pExpr->pLeft );
@@ -98805,11 +98846,11 @@
98846 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
98847 ** idx_affinity is the affinity of an indexed column. Return true
98848 ** if the index with affinity idx_affinity may be used to implement
98849 ** the comparison in pExpr.
98850 */
98851 SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
98852 char aff = comparisonAffinity(pExpr);
98853 if( aff<SQLITE_AFF_TEXT ){
98854 return 1;
98855 }
98856 if( aff==SQLITE_AFF_TEXT ){
@@ -98820,11 +98861,15 @@
98861
98862 /*
98863 ** Return the P5 value that should be used for a binary comparison
98864 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
98865 */
98866 static u8 binaryCompareP5(
98867 const Expr *pExpr1, /* Left operand */
98868 const Expr *pExpr2, /* Right operand */
98869 int jumpIfNull /* Extra flags added to P5 */
98870 ){
98871 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
98872 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
98873 return aff;
98874 }
98875
@@ -98840,12 +98885,12 @@
98885 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
98886 ** it is not considered.
98887 */
98888 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
98889 Parse *pParse,
98890 const Expr *pLeft,
98891 const Expr *pRight
98892 ){
98893 CollSeq *pColl;
98894 assert( pLeft );
98895 if( pLeft->flags & EP_Collate ){
98896 pColl = sqlite3ExprCollSeq(pParse, pLeft);
@@ -98866,11 +98911,11 @@
98911 ** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
98912 ** However, if the OP_Commuted flag is set, then the order of the operands
98913 ** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
98914 ** correct collating sequence is found.
98915 */
98916 SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
98917 if( ExprHasProperty(p, EP_Commuted) ){
98918 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
98919 }else{
98920 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
98921 }
@@ -99109,10 +99154,11 @@
99154 int regRight = 0;
99155 u8 opx = op;
99156 int addrDone = sqlite3VdbeMakeLabel(pParse);
99157 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
99158
99159 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
99160 if( pParse->nErr ) return;
99161 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
99162 sqlite3ErrorMsg(pParse, "row value misused");
99163 return;
99164 }
@@ -99721,11 +99767,11 @@
99767 nSize = EXPR_FULLSIZE;
99768 }else{
99769 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
99770 assert( !ExprHasProperty(p, EP_FromJoin) );
99771 assert( !ExprHasProperty(p, EP_MemToken) );
99772 assert( !ExprHasVVAProperty(p, EP_NoReduce) );
99773 if( p->pLeft || p->x.pList ){
99774 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
99775 }else{
99776 assert( p->pRight==0 );
99777 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
@@ -99826,10 +99872,14 @@
99872
99873 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
99874 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
99875 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
99876 pNew->flags |= staticFlag;
99877 ExprClearVVAProperties(pNew);
99878 if( dupFlags ){
99879 ExprSetVVAProperty(pNew, EP_Immutable);
99880 }
99881
99882 /* Copy the p->u.zToken string, if any. */
99883 if( nToken ){
99884 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
99885 memcpy(zToken, p->u.zToken, nToken);
@@ -100602,11 +100652,11 @@
100652 ** (3) the expression does not contain any EP_FixedCol TK_COLUMN
100653 ** operands created by the constant propagation optimization.
100654 **
100655 ** When this routine returns true, it indicates that the expression
100656 ** can be added to the pParse->pConstExpr list and evaluated once when
100657 ** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
100658 */
100659 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
100660 return exprIsConst(p, 2, 0);
100661 }
100662
@@ -101365,10 +101415,11 @@
101415 return;
101416 }
101417
101418 /* Begin coding the subroutine */
101419 ExprSetProperty(pExpr, EP_Subrtn);
101420 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
101421 pExpr->y.sub.regReturn = ++pParse->nMem;
101422 pExpr->y.sub.iAddr =
101423 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
101424 VdbeComment((v, "return address"));
101425
@@ -101685,10 +101736,11 @@
101736 int addrTruthOp; /* Address of opcode that determines the IN is true */
101737 int destNotNull; /* Jump here if a comparison is not true in step 6 */
101738 int addrTop; /* Top of the step-6 loop */
101739 int iTab = 0; /* Index to use */
101740
101741 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
101742 pLeft = pExpr->pLeft;
101743 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
101744 zAff = exprINAffinity(pParse, pExpr);
101745 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
101746 aiMap = (int*)sqlite3DbMallocZero(
@@ -102011,11 +102063,11 @@
102063 if( pParse->iSelfTab>0 ){
102064 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
102065 }else{
102066 iAddr = 0;
102067 }
102068 sqlite3ExprCodeCopy(pParse, pCol->pDflt, regOut);
102069 if( pCol->affinity>=SQLITE_AFF_TEXT ){
102070 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
102071 }
102072 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
102073 }
@@ -102295,10 +102347,11 @@
102347
102348 expr_code_doover:
102349 if( pExpr==0 ){
102350 op = TK_NULL;
102351 }else{
102352 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
102353 op = pExpr->op;
102354 }
102355 switch( op ){
102356 case TK_AGG_COLUMN: {
102357 AggInfo *pAggInfo = pExpr->pAggInfo;
@@ -102305,12 +102358,21 @@
102358 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
102359 if( !pAggInfo->directMode ){
102360 assert( pCol->iMem>0 );
102361 return pCol->iMem;
102362 }else if( pAggInfo->useSortingIdx ){
102363 Table *pTab = pCol->pTab;
102364 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
102365 pCol->iSorterColumn, target);
102366 if( pCol->iColumn<0 ){
102367 VdbeComment((v,"%s.rowid",pTab->zName));
102368 }else{
102369 VdbeComment((v,"%s.%s",pTab->zName,pTab->aCol[pCol->iColumn].zName));
102370 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
102371 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
102372 }
102373 }
102374 return target;
102375 }
102376 /* Otherwise, fall thru into the TK_COLUMN case */
102377 }
102378 case TK_COLUMN: {
@@ -102549,10 +102611,11 @@
102611 #endif
102612 }else{
102613 tempX.op = TK_INTEGER;
102614 tempX.flags = EP_IntValue|EP_TokenOnly;
102615 tempX.u.iValue = 0;
102616 ExprClearVVAProperties(&tempX);
102617 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
102618 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
102619 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
102620 testcase( regFree2==0 );
102621 }
@@ -102620,20 +102683,17 @@
102683 return pExpr->y.pWin->regResult;
102684 }
102685 #endif
102686
102687 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
102688 /* SQL functions can be expensive. So try to avoid running them
102689 ** multiple times if we know they always give the same result */
102690 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
102691 }
102692 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
102693 assert( !ExprHasProperty(pExpr, EP_TokenOnly) );
102694 pFarg = pExpr->x.pList;
 
 
 
102695 nFarg = pFarg ? pFarg->nExpr : 0;
102696 assert( !ExprHasProperty(pExpr, EP_IntValue) );
102697 zId = pExpr->u.zToken;
102698 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
102699 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
@@ -103003,19 +103063,27 @@
103063 sqlite3ReleaseTempReg(pParse, regFree2);
103064 return inReg;
103065 }
103066
103067 /*
103068 ** Generate code that will evaluate expression pExpr just one time
103069 ** per prepared statement execution.
103070 **
103071 ** If the expression uses functions (that might throw an exception) then
103072 ** guard them with an OP_Once opcode to ensure that the code is only executed
103073 ** once. If no functions are involved, then factor the code out and put it at
103074 ** the end of the prepared statement in the initialization section.
103075 **
103076 ** If regDest>=0 then the result is always stored in that register and the
103077 ** result is not reusable. If regDest<0 then this routine is free to
103078 ** store the value whereever it wants. The register where the expression
103079 ** is stored is returned. When regDest<0, two identical expressions might
103080 ** code to the same register, if they do not contain function calls and hence
103081 ** are factored out into the initialization section at the end of the
103082 ** prepared statement.
103083 */
103084 SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
103085 Parse *pParse, /* Parsing context */
103086 Expr *pExpr, /* The expression to code when the VDBE initializes */
103087 int regDest /* Store the value in this register */
103088 ){
103089 ExprList *p;
@@ -103029,18 +103097,33 @@
103097 return pItem->u.iConstExprReg;
103098 }
103099 }
103100 }
103101 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
103102 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){
103103 Vdbe *v = pParse->pVdbe;
103104 int addr;
103105 assert( v );
103106 addr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
103107 pParse->okConstFactor = 0;
103108 if( !pParse->db->mallocFailed ){
103109 if( regDest<0 ) regDest = ++pParse->nMem;
103110 sqlite3ExprCode(pParse, pExpr, regDest);
103111 }
103112 pParse->okConstFactor = 1;
103113 sqlite3ExprDelete(pParse->db, pExpr);
103114 sqlite3VdbeJumpHere(v, addr);
103115 }else{
103116 p = sqlite3ExprListAppend(pParse, p, pExpr);
103117 if( p ){
103118 struct ExprList_item *pItem = &p->a[p->nExpr-1];
103119 pItem->reusable = regDest<0;
103120 if( regDest<0 ) regDest = ++pParse->nMem;
103121 pItem->u.iConstExprReg = regDest;
103122 }
103123 pParse->pConstExpr = p;
103124 }
103125 return regDest;
103126 }
103127
103128 /*
103129 ** Generate code to evaluate an expression and store the results
@@ -103061,11 +103144,11 @@
103144 if( ConstFactorOk(pParse)
103145 && pExpr->op!=TK_REGISTER
103146 && sqlite3ExprIsConstantNotJoin(pExpr)
103147 ){
103148 *pReg = 0;
103149 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
103150 }else{
103151 int r1 = sqlite3GetTempReg(pParse);
103152 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
103153 if( r2==r1 ){
103154 *pReg = r1;
@@ -103083,10 +103166,11 @@
103166 ** in register target.
103167 */
103168 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
103169 int inReg;
103170
103171 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) );
103172 assert( target>0 && target<=pParse->nMem );
103173 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
103174 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
103175 if( inReg!=target && pParse->pVdbe ){
103176 u8 op;
@@ -103117,13 +103201,13 @@
103201 ** in register target. If the expression is constant, then this routine
103202 ** might choose to code the expression at initialization time.
103203 */
103204 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
103205 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
103206 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
103207 }else{
103208 sqlite3ExprCodeCopy(pParse, pExpr, target);
103209 }
103210 }
103211
103212 /*
103213 ** Generate code that pushes the value of every element of the given
@@ -103177,11 +103261,11 @@
103261 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
103262 }
103263 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
103264 && sqlite3ExprIsConstantNotJoin(pExpr)
103265 ){
103266 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
103267 }else{
103268 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
103269 if( inReg!=target+i ){
103270 VdbeOp *pOp;
103271 if( copyOp==OP_Copy
@@ -103300,10 +103384,11 @@
103384 int r1, r2;
103385
103386 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
103387 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
103388 if( NEVER(pExpr==0) ) return; /* No way this can happen */
103389 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) );
103390 op = pExpr->op;
103391 switch( op ){
103392 case TK_AND:
103393 case TK_OR: {
103394 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
@@ -103441,10 +103526,11 @@
103526 int r1, r2;
103527
103528 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
103529 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
103530 if( pExpr==0 ) return;
103531 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
103532
103533 /* The value of pExpr->op and op are related as follows:
103534 **
103535 ** pExpr->op op
103536 ** --------- ----------
@@ -103724,36 +103810,22 @@
103810 return 2;
103811 }
103812 }
103813 if( (pA->flags & (EP_Distinct|EP_Commuted))
103814 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
103815 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
103816 if( combinedFlags & EP_xIsSelect ) return 2;
103817 if( (combinedFlags & EP_FixedCol)==0
103818 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
103819 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
103820 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
103821 if( pA->op!=TK_STRING
103822 && pA->op!=TK_TRUEFALSE
103823 && ALWAYS((combinedFlags & EP_Reduced)==0)
103824 ){
103825 if( pA->iColumn!=pB->iColumn ) return 2;
103826 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103827 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
103828 return 2;
103829 }
103830 }
103831 }
@@ -106442,13 +106514,13 @@
106514 /*
106515 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
106516 ** share an instance of the following structure to hold their state
106517 ** information.
106518 */
106519 typedef struct StatAccum StatAccum;
106520 typedef struct StatSample StatSample;
106521 struct StatSample {
106522 tRowcnt *anEq; /* sqlite_stat4.nEq */
106523 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
106524 #ifdef SQLITE_ENABLE_STAT4
106525 tRowcnt *anLt; /* sqlite_stat4.nLt */
106526 union {
@@ -106459,31 +106531,33 @@
106531 u8 isPSample; /* True if a periodic sample */
106532 int iCol; /* If !isPSample, the reason for inclusion */
106533 u32 iHash; /* Tiebreaker hash */
106534 #endif
106535 };
106536 struct StatAccum {
106537 sqlite3 *db; /* Database connection, for malloc() */
106538 tRowcnt nRow; /* Number of rows in the entire table */
 
106539 int nCol; /* Number of columns in index + pk/rowid */
106540 int nKeyCol; /* Number of index columns w/o the pk/rowid */
106541 StatSample current; /* Current row as a StatSample */
106542 #ifdef SQLITE_ENABLE_STAT4
106543 tRowcnt nPSample; /* How often to do a periodic sample */
106544 int mxSample; /* Maximum number of samples to accumulate */
 
106545 u32 iPrn; /* Pseudo-random number used for sampling */
106546 StatSample *aBest; /* Array of nCol best samples */
106547 int iMin; /* Index in a[] of entry with minimum score */
106548 int nSample; /* Current number of samples */
106549 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
106550 int iGet; /* Index of current sample accessed by stat_get() */
106551 StatSample *a; /* Array of mxSample StatSample objects */
106552 #endif
106553 };
106554
106555 /* Reclaim memory used by a StatSample
106556 */
106557 #ifdef SQLITE_ENABLE_STAT4
106558 static void sampleClear(sqlite3 *db, StatSample *p){
106559 assert( db!=0 );
106560 if( p->nRowid ){
106561 sqlite3DbFree(db, p->u.aRowid);
106562 p->nRowid = 0;
106563 }
@@ -106491,11 +106565,11 @@
106565 #endif
106566
106567 /* Initialize the BLOB value of a ROWID
106568 */
106569 #ifdef SQLITE_ENABLE_STAT4
106570 static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
106571 assert( db!=0 );
106572 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
106573 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
106574 if( p->u.aRowid ){
106575 p->nRowid = n;
@@ -106507,11 +106581,11 @@
106581 #endif
106582
106583 /* Initialize the INTEGER value of a ROWID.
106584 */
106585 #ifdef SQLITE_ENABLE_STAT4
106586 static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
106587 assert( db!=0 );
106588 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
106589 p->nRowid = 0;
106590 p->u.iRowid = iRowid;
106591 }
@@ -106520,11 +106594,11 @@
106594
106595 /*
106596 ** Copy the contents of object (*pFrom) into (*pTo).
106597 */
106598 #ifdef SQLITE_ENABLE_STAT4
106599 static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
106600 pTo->isPSample = pFrom->isPSample;
106601 pTo->iCol = pFrom->iCol;
106602 pTo->iHash = pFrom->iHash;
106603 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
106604 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
@@ -106536,14 +106610,14 @@
106610 }
106611 }
106612 #endif
106613
106614 /*
106615 ** Reclaim all memory of a StatAccum structure.
106616 */
106617 static void statAccumDestructor(void *pOld){
106618 StatAccum *p = (StatAccum*)pOld;
106619 #ifdef SQLITE_ENABLE_STAT4
106620 int i;
106621 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
106622 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
106623 sampleClear(p->db, &p->current);
@@ -106567,21 +106641,21 @@
106641 ** For indexes on ordinary rowid tables, N==K+1. But for indexes on
106642 ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
106643 ** PRIMARY KEY of the table. The covering index that implements the
106644 ** original WITHOUT ROWID table as N==K as a special case.
106645 **
106646 ** This routine allocates the StatAccum object in heap memory. The return
106647 ** value is a pointer to the StatAccum object. The datatype of the
106648 ** return value is BLOB, but it is really just a pointer to the StatAccum
106649 ** object.
106650 */
106651 static void statInit(
106652 sqlite3_context *context,
106653 int argc,
106654 sqlite3_value **argv
106655 ){
106656 StatAccum *p;
106657 int nCol; /* Number of columns in index being sampled */
106658 int nKeyCol; /* Number of key columns */
106659 int nColUp; /* nCol rounded up for alignment */
106660 int n; /* Bytes of space to allocate */
106661 sqlite3 *db; /* Database connection */
@@ -106596,17 +106670,17 @@
106670 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
106671 nKeyCol = sqlite3_value_int(argv[1]);
106672 assert( nKeyCol<=nCol );
106673 assert( nKeyCol>0 );
106674
106675 /* Allocate the space required for the StatAccum object */
106676 n = sizeof(*p)
106677 + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
106678 + sizeof(tRowcnt)*nColUp /* StatAccum.anDLt */
106679 #ifdef SQLITE_ENABLE_STAT4
106680 + sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
106681 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
106682 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
106683 #endif
106684 ;
106685 db = sqlite3_context_db_handle(context);
106686 p = sqlite3DbMallocZero(db, n);
@@ -106631,12 +106705,12 @@
106705 p->mxSample = mxSample;
106706 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
106707 p->current.anLt = &p->current.anEq[nColUp];
106708 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
106709
106710 /* Set up the StatAccum.a[] and aBest[] arrays */
106711 p->a = (struct StatSample*)&p->current.anLt[nColUp];
106712 p->aBest = &p->a[mxSample];
106713 pSpace = (u8*)(&p->a[mxSample+nCol]);
106714 for(i=0; i<(mxSample+nCol); i++){
106715 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
106716 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
@@ -106652,11 +106726,11 @@
106726
106727 /* Return a pointer to the allocated object to the caller. Note that
106728 ** only the pointer (the 2nd parameter) matters. The size of the object
106729 ** (given by the 3rd parameter) is never used and can be any positive
106730 ** value. */
106731 sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
106732 }
106733 static const FuncDef statInitFuncdef = {
106734 2+IsStat4, /* nArg */
106735 SQLITE_UTF8, /* funcFlags */
106736 0, /* pUserData */
@@ -106679,13 +106753,13 @@
106753 **
106754 ** This function assumes that for each argument sample, the contents of
106755 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
106756 */
106757 static int sampleIsBetterPost(
106758 StatAccum *pAccum,
106759 StatSample *pNew,
106760 StatSample *pOld
106761 ){
106762 int nCol = pAccum->nCol;
106763 int i;
106764 assert( pNew->iCol==pOld->iCol );
106765 for(i=pNew->iCol+1; i<nCol; i++){
@@ -106703,13 +106777,13 @@
106777 **
106778 ** This function assumes that for each argument sample, the contents of
106779 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
106780 */
106781 static int sampleIsBetter(
106782 StatAccum *pAccum,
106783 StatSample *pNew,
106784 StatSample *pOld
106785 ){
106786 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
106787 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
106788
106789 assert( pOld->isPSample==0 && pNew->isPSample==0 );
@@ -106725,34 +106799,34 @@
106799
106800 /*
106801 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
106802 ** remove the least desirable sample from p->a[] to make room.
106803 */
106804 static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
106805 StatSample *pSample = 0;
106806 int i;
106807
106808 assert( IsStat4 || nEqZero==0 );
106809
106810 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
106811 ** values in the anEq[] array of any sample in StatAccum.a[]. In
106812 ** other words, if nMaxEqZero is n, then it is guaranteed that there
106813 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
106814 if( nEqZero>p->nMaxEqZero ){
106815 p->nMaxEqZero = nEqZero;
106816 }
106817 if( pNew->isPSample==0 ){
106818 StatSample *pUpgrade = 0;
106819 assert( pNew->anEq[pNew->iCol]>0 );
106820
106821 /* This sample is being added because the prefix that ends in column
106822 ** iCol occurs many times in the table. However, if we have already
106823 ** added a sample that shares this prefix, there is no need to add
106824 ** this one. Instead, upgrade the priority of the highest priority
106825 ** existing sample that shares this prefix. */
106826 for(i=p->nSample-1; i>=0; i--){
106827 StatSample *pOld = &p->a[i];
106828 if( pOld->anEq[pNew->iCol]==0 ){
106829 if( pOld->isPSample ) return;
106830 assert( pOld->iCol>pNew->iCol );
106831 assert( sampleIsBetter(p, pNew, pOld) );
106832 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
@@ -106767,11 +106841,11 @@
106841 }
106842 }
106843
106844 /* If necessary, remove sample iMin to make room for the new sample. */
106845 if( p->nSample>=p->mxSample ){
106846 StatSample *pMin = &p->a[p->iMin];
106847 tRowcnt *anEq = pMin->anEq;
106848 tRowcnt *anLt = pMin->anLt;
106849 tRowcnt *anDLt = pMin->anDLt;
106850 sampleClear(p->db, pMin);
106851 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
@@ -106810,24 +106884,24 @@
106884 p->iMin = iMin;
106885 }
106886 }
106887 #endif /* SQLITE_ENABLE_STAT4 */
106888
106889 #ifdef SQLITE_ENABLE_STAT4
106890 /*
106891 ** Field iChng of the index being scanned has changed. So at this point
106892 ** p->current contains a sample that reflects the previous row of the
106893 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
106894 ** correct at this point.
106895 */
106896 static void samplePushPrevious(StatAccum *p, int iChng){
 
106897 int i;
106898
106899 /* Check if any samples from the aBest[] array should be pushed
106900 ** into IndexSample.a[] at this point. */
106901 for(i=(p->nCol-2); i>=iChng; i--){
106902 StatSample *pBest = &p->aBest[i];
106903 pBest->anEq[i] = p->current.anEq[i];
106904 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
106905 sampleInsert(p, pBest, i);
106906 }
106907 }
@@ -106847,29 +106921,24 @@
106921 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
106922 }
106923 }
106924 p->nMaxEqZero = iChng;
106925 }
 
 
 
 
 
 
106926 }
106927 #endif /* SQLITE_ENABLE_STAT4 */
106928
106929 /*
106930 ** Implementation of the stat_push SQL function: stat_push(P,C,R)
106931 ** Arguments:
106932 **
106933 ** P Pointer to the StatAccum object created by stat_init()
106934 ** C Index of left-most column to differ from previous row
106935 ** R Rowid for the current row. Might be a key record for
106936 ** WITHOUT ROWID tables.
106937 **
106938 ** This SQL function always returns NULL. It's purpose it to accumulate
106939 ** statistical data and/or samples in the StatAccum object about the
106940 ** index being analyzed. The stat_get() SQL function will later be used to
106941 ** extract relevant information for constructing the sqlite_statN tables.
106942 **
106943 ** The R parameter is only used for STAT4
106944 */
@@ -106879,11 +106948,11 @@
106948 sqlite3_value **argv
106949 ){
106950 int i;
106951
106952 /* The three function arguments */
106953 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
106954 int iChng = sqlite3_value_int(argv[1]);
106955
106956 UNUSED_PARAMETER( argc );
106957 UNUSED_PARAMETER( context );
106958 assert( p->nCol>0 );
@@ -106892,11 +106961,13 @@
106961 if( p->nRow==0 ){
106962 /* This is the first call to this function. Do initialization. */
106963 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
106964 }else{
106965 /* Second and subsequent calls get processed here */
106966 #ifdef SQLITE_ENABLE_STAT4
106967 samplePushPrevious(p, iChng);
106968 #endif
106969
106970 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
106971 ** to the current row of the index. */
106972 for(i=0; i<iChng; i++){
106973 p->current.anEq[i]++;
@@ -106961,19 +107032,19 @@
107032 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
107033
107034 /*
107035 ** Implementation of the stat_get(P,J) SQL function. This routine is
107036 ** used to query statistical information that has been gathered into
107037 ** the StatAccum object by prior calls to stat_push(). The P parameter
107038 ** has type BLOB but it is really just a pointer to the StatAccum object.
107039 ** The content to returned is determined by the parameter J
107040 ** which is one of the STAT_GET_xxxx values defined above.
107041 **
107042 ** The stat_get(P,J) function is not available to generic SQL. It is
107043 ** inserted as part of a manually constructed bytecode program. (See
107044 ** the callStatGet() routine below.) It is guaranteed that the P
107045 ** parameter will always be a pointer to a StatAccum object, never a
107046 ** NULL.
107047 **
107048 ** If STAT4 is not enabled, then J is always
107049 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
107050 ** a one-parameter function, stat_get(P), that always returns the
@@ -106982,11 +107053,11 @@
107053 static void statGet(
107054 sqlite3_context *context,
107055 int argc,
107056 sqlite3_value **argv
107057 ){
107058 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
107059 #ifdef SQLITE_ENABLE_STAT4
107060 /* STAT4 has a parameter on this routine. */
107061 int eCall = sqlite3_value_int(argv[1]);
107062 assert( argc==2 );
107063 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
@@ -107003,11 +107074,11 @@
107074 **
107075 ** The value is a string composed of a list of integers describing
107076 ** the index. The first integer in the list is the total number of
107077 ** entries in the index. There is one additional integer in the list
107078 ** for each indexed column. This additional integer is an estimate of
107079 ** the number of rows matched by a equality query on the index using
107080 ** a key with the corresponding number of fields. In other words,
107081 ** if the index is on columns (a,b) and the sqlite_stat1 value is
107082 ** "100 10 2", then SQLite estimates that:
107083 **
107084 ** * the index contains 100 rows,
@@ -107046,11 +107117,11 @@
107117 if( p->iGet<0 ){
107118 samplePushPrevious(p, 0);
107119 p->iGet = 0;
107120 }
107121 if( p->iGet<p->nSample ){
107122 StatSample *pS = p->a + p->iGet;
107123 if( pS->nRowid==0 ){
107124 sqlite3_result_int64(context, pS->u.iRowid);
107125 }else{
107126 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
107127 SQLITE_TRANSIENT);
@@ -107137,11 +107208,11 @@
107208 int i; /* Loop counter */
107209 int jZeroRows = -1; /* Jump from here if number of rows is zero */
107210 int iDb; /* Index of database containing pTab */
107211 u8 needTableCnt = 1; /* True to count the table */
107212 int regNewRowid = iMem++; /* Rowid for the inserted record */
107213 int regStat4 = iMem++; /* Register to hold StatAccum object */
107214 int regChng = iMem++; /* Index of changed index field */
107215 #ifdef SQLITE_ENABLE_STAT4
107216 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
107217 #endif
107218 int regTemp = iMem++; /* Temporary use register */
@@ -108105,10 +108176,21 @@
108176 pExpr->op = TK_STRING;
108177 }
108178 }
108179 return rc;
108180 }
108181
108182 /*
108183 ** Return true if zName points to a name that may be used to refer to
108184 ** database iDb attached to handle db.
108185 */
108186 SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
108187 return (
108188 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
108189 || (iDb==0 && sqlite3StrICmp("main", zName)==0)
108190 );
108191 }
108192
108193 /*
108194 ** An SQL user-function registered to do the work of an ATTACH statement. The
108195 ** three arguments to the function come directly from an attach statement:
108196 **
@@ -108178,13 +108260,12 @@
108260 db->aLimit[SQLITE_LIMIT_ATTACHED]
108261 );
108262 goto attach_error;
108263 }
108264 for(i=0; i<db->nDb; i++){
108265 assert( zName );
108266 if( sqlite3DbIsNamed(db, i, zName) ){
 
108267 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
108268 goto attach_error;
108269 }
108270 }
108271
@@ -108333,11 +108414,11 @@
108414
108415 if( zName==0 ) zName = "";
108416 for(i=0; i<db->nDb; i++){
108417 pDb = &db->aDb[i];
108418 if( pDb->pBt==0 ) continue;
108419 if( sqlite3DbIsNamed(db, i, zName) ) break;
108420 }
108421
108422 if( i>=db->nDb ){
108423 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
108424 goto detach_error;
@@ -108524,24 +108605,25 @@
108605 SQLITE_PRIVATE int sqlite3FixSrcList(
108606 DbFixer *pFix, /* Context of the fixation */
108607 SrcList *pList /* The Source list to check and modify */
108608 ){
108609 int i;
 
108610 struct SrcList_item *pItem;
108611 sqlite3 *db = pFix->pParse->db;
108612 int iDb = sqlite3FindDbName(db, pFix->zDb);
108613
108614 if( NEVER(pList==0) ) return 0;
108615
108616 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
108617 if( pFix->bTemp==0 ){
108618 if( pItem->zDatabase && iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
108619 sqlite3ErrorMsg(pFix->pParse,
108620 "%s %T cannot reference objects in database %s",
108621 pFix->zType, pFix->pName, pItem->zDatabase);
108622 return 1;
108623 }
108624 sqlite3DbFree(db, pItem->zDatabase);
108625 pItem->zDatabase = 0;
108626 pItem->pSchema = pFix->pSchema;
108627 pItem->fg.fromDDL = 1;
108628 }
108629 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
@@ -109262,11 +109344,11 @@
109344 }
109345 #endif
109346 while(1){
109347 for(i=OMIT_TEMPDB; i<db->nDb; i++){
109348 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
109349 if( zDatabase==0 || sqlite3DbIsNamed(db, j, zDatabase) ){
109350 assert( sqlite3SchemaMutexHeld(db, j, 0) );
109351 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
109352 if( p ) return p;
109353 }
109354 }
@@ -109384,11 +109466,11 @@
109466 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
109467 for(i=OMIT_TEMPDB; i<db->nDb; i++){
109468 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
109469 Schema *pSchema = db->aDb[j].pSchema;
109470 assert( pSchema );
109471 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
109472 assert( sqlite3SchemaMutexHeld(db, j, 0) );
109473 p = sqlite3HashFind(&pSchema->idxHash, zName);
109474 if( p ) break;
109475 }
109476 return p;
@@ -111103,10 +111185,36 @@
111185 if( pMod->pModule->iVersion<3 ) return 0;
111186 if( pMod->pModule->xShadowName==0 ) return 0;
111187 return pMod->pModule->xShadowName(zTail+1);
111188 }
111189 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
111190
111191 #ifdef SQLITE_DEBUG
111192 /*
111193 ** Mark all nodes of an expression as EP_Immutable, indicating that
111194 ** they should not be changed. Expressions attached to a table or
111195 ** index definition are tagged this way to help ensure that we do
111196 ** not pass them into code generator routines by mistake.
111197 */
111198 static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
111199 ExprSetVVAProperty(pExpr, EP_Immutable);
111200 return WRC_Continue;
111201 }
111202 static void markExprListImmutable(ExprList *pList){
111203 if( pList ){
111204 Walker w;
111205 memset(&w, 0, sizeof(w));
111206 w.xExprCallback = markImmutableExprStep;
111207 w.xSelectCallback = sqlite3SelectWalkNoop;
111208 w.xSelectCallback2 = 0;
111209 sqlite3WalkExprList(&w, pList);
111210 }
111211 }
111212 #else
111213 #define markExprListImmutable(X) /* no-op */
111214 #endif /* SQLITE_DEBUG */
111215
111216
111217 /*
111218 ** This routine is called to report the final ")" that terminates
111219 ** a CREATE TABLE statement.
111220 **
@@ -111196,10 +111304,12 @@
111304 if( pParse->nErr ){
111305 /* If errors are seen, delete the CHECK constraints now, else they might
111306 ** actually be used if PRAGMA writable_schema=ON is set. */
111307 sqlite3ExprListDelete(db, p->pCheck);
111308 p->pCheck = 0;
111309 }else{
111310 markExprListImmutable(p->pCheck);
111311 }
111312 }
111313 #endif /* !defined(SQLITE_OMIT_CHECK) */
111314 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
111315 if( p->tabFlags & TF_HasGenerated ){
@@ -114137,20 +114247,33 @@
114247 u8 enc, /* Desired text encoding */
114248 const char *zName, /* Name of the collating sequence. Might be NULL */
114249 int create /* True to create CollSeq if doesn't already exist */
114250 ){
114251 CollSeq *pColl;
114252 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
114253 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
114254 if( zName ){
114255 pColl = findCollSeqEntry(db, zName, create);
114256 if( pColl ) pColl += enc-1;
114257 }else{
114258 pColl = db->pDfltColl;
114259 }
 
 
 
114260 return pColl;
114261 }
114262
114263 /*
114264 ** Change the text encoding for a database connection. This means that
114265 ** the pDfltColl must change as well.
114266 */
114267 SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
114268 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
114269 db->enc = enc;
114270 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
114271 ** strings is BINARY.
114272 */
114273 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
114274 }
114275
114276 /*
114277 ** This function is responsible for invoking the collation factory callback
114278 ** or substituting a collation sequence of a different encoding when the
114279 ** requested collation sequence is not available in the desired encoding.
@@ -116321,10 +116444,11 @@
116444 const unsigned char *zA, *zB;
116445 u32 escape;
116446 int nPat;
116447 sqlite3 *db = sqlite3_context_db_handle(context);
116448 struct compareInfo *pInfo = sqlite3_user_data(context);
116449 struct compareInfo backupInfo;
116450
116451 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
116452 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
116453 || sqlite3_value_type(argv[1])==SQLITE_BLOB
116454 ){
@@ -116356,10 +116480,16 @@
116480 sqlite3_result_error(context,
116481 "ESCAPE expression must be a single character", -1);
116482 return;
116483 }
116484 escape = sqlite3Utf8Read(&zEsc);
116485 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
116486 memcpy(&backupInfo, pInfo, sizeof(backupInfo));
116487 pInfo = &backupInfo;
116488 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
116489 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
116490 }
116491 }else{
116492 escape = pInfo->matchSet;
116493 }
116494 zB = sqlite3_value_text(argv[0]);
116495 zA = sqlite3_value_text(argv[1]);
@@ -117338,29 +117468,33 @@
117468 if( pDef==0 ) return 0;
117469 #endif
117470 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
117471 return 0;
117472 }
 
 
 
 
 
 
 
 
 
 
117473
117474 /* The memcpy() statement assumes that the wildcard characters are
117475 ** the first three statements in the compareInfo structure. The
117476 ** asserts() that follow verify that assumption
117477 */
117478 memcpy(aWc, pDef->pUserData, 3);
117479 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
117480 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
117481 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
117482
117483 if( nExpr<3 ){
117484 aWc[3] = 0;
117485 }else{
117486 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
117487 char *zEscape;
117488 if( pEscape->op!=TK_STRING ) return 0;
117489 zEscape = pEscape->u.zToken;
117490 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
117491 if( zEscape[0]==aWc[0] ) return 0;
117492 if( zEscape[0]==aWc[1] ) return 0;
117493 aWc[3] = zEscape[0];
117494 }
117495
117496 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
117497 return 1;
117498 }
117499
117500 /*
@@ -120564,11 +120698,11 @@
120698 case OE_Replace: {
120699 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
120700 VdbeCoverage(v);
120701 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
120702 nSeenReplace++;
120703 sqlite3ExprCodeCopy(pParse, pCol->pDflt, iReg);
120704 sqlite3VdbeJumpHere(v, addr1);
120705 break;
120706 }
120707 case OE_Abort:
120708 sqlite3MayAbort(pParse);
@@ -120619,10 +120753,11 @@
120753 ExprList *pCheck = pTab->pCheck;
120754 pParse->iSelfTab = -(regNewData+1);
120755 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
120756 for(i=0; i<pCheck->nExpr; i++){
120757 int allOk;
120758 Expr *pCopy;
120759 Expr *pExpr = pCheck->a[i].pExpr;
120760 if( aiChng
120761 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
120762 ){
120763 /* The check constraints do not reference any of the columns being
@@ -120633,11 +120768,15 @@
120768 sqlite3TableAffinity(v, pTab, regNewData+1);
120769 bAffinityDone = 1;
120770 }
120771 allOk = sqlite3VdbeMakeLabel(pParse);
120772 sqlite3VdbeVerifyAbortable(v, onError);
120773 pCopy = sqlite3ExprDup(db, pExpr, 0);
120774 if( !db->mallocFailed ){
120775 sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL);
120776 }
120777 sqlite3ExprDelete(db, pCopy);
120778 if( onError==OE_Ignore ){
120779 sqlite3VdbeGoto(v, ignoreDest);
120780 }else{
120781 char *zName = pCheck->a[i].zEName;
120782 if( zName==0 ) zName = pTab->zName;
@@ -125952,25 +126091,16 @@
126091 /* Only change the value of sqlite.enc if the database handle is not
126092 ** initialized. If the main database exists, the new sqlite.enc value
126093 ** will be overwritten when the schema is next loaded. If it does not
126094 ** already exists, it will be created to use the new encoding value.
126095 */
126096 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
 
 
 
 
 
 
 
 
 
 
126097 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
126098 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
126099 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
126100 SCHEMA_ENC(db) = enc;
126101 sqlite3SetTextEncoding(db, enc);
126102 break;
126103 }
126104 }
126105 if( !pEnc->zName ){
126106 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
@@ -126775,11 +126905,11 @@
126905 int iDb = pData->iDb;
126906
126907 assert( argc==5 );
126908 UNUSED_PARAMETER2(NotUsed, argc);
126909 assert( sqlite3_mutex_held(db->mutex) );
126910 db->mDbFlags |= DBFLAG_EncodingFixed;
126911 pData->nInitRow++;
126912 if( db->mallocFailed ){
126913 corruptSchema(pData, argv[1], 0);
126914 return 1;
126915 }
@@ -126863,10 +126993,11 @@
126993 char const *azArg[6];
126994 int meta[5];
126995 InitData initData;
126996 const char *zMasterName;
126997 int openedTransaction = 0;
126998 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
126999
127000 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
127001 assert( iDb>=0 && iDb<db->nDb );
127002 assert( db->aDb[iDb].pSchema );
127003 assert( sqlite3_mutex_held(db->mutex) );
@@ -126891,10 +127022,11 @@
127022 initData.rc = SQLITE_OK;
127023 initData.pzErrMsg = pzErrMsg;
127024 initData.mInitFlags = mFlags;
127025 initData.nInitRow = 0;
127026 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
127027 db->mDbFlags &= mask;
127028 if( initData.rc ){
127029 rc = initData.rc;
127030 goto error_out;
127031 }
127032
@@ -126950,31 +127082,29 @@
127082 ** main database, set sqlite3.enc to the encoding of the main database.
127083 ** For an attached db, it is an error if the encoding is not the same
127084 ** as sqlite3.enc.
127085 */
127086 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
127087 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
 
127088 u8 encoding;
127089 #ifndef SQLITE_OMIT_UTF16
127090 /* If opening the main database, set ENC(db). */
127091 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
127092 if( encoding==0 ) encoding = SQLITE_UTF8;
 
127093 #else
127094 encoding = SQLITE_UTF8;
127095 #endif
127096 sqlite3SetTextEncoding(db, encoding);
127097 }else{
127098 /* If opening an attached database, the encoding much match ENC(db) */
127099 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
127100 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
127101 " text encoding as main database");
127102 rc = SQLITE_ERROR;
127103 goto initone_error_out;
127104 }
127105 }
 
 
127106 }
127107 pDb->pSchema->enc = ENC(db);
127108
127109 if( pDb->pSchema->cache_size==0 ){
127110 #ifndef SQLITE_OMIT_DEPRECATED
@@ -127082,12 +127212,11 @@
127212 ** used to store temporary tables, and any additional database files
127213 ** created using ATTACH statements. Return a success code. If an
127214 ** error occurs, write an error message into *pzErrMsg.
127215 **
127216 ** After a database is initialized, the DB_SchemaLoaded bit is set
127217 ** bit is set in the flags field of the Db structure.
 
127218 */
127219 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
127220 int i, rc;
127221 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
127222
@@ -127719,11 +127848,10 @@
127848 sqlite3ExprDelete(db, p->pLimit);
127849 #ifndef SQLITE_OMIT_WINDOWFUNC
127850 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
127851 sqlite3WindowListDelete(db, p->pWinDefn);
127852 }
 
127853 #endif
127854 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
127855 if( bFree ) sqlite3DbFreeNN(db, p);
127856 p = pPrior;
127857 bFree = 1;
@@ -131197,10 +131325,42 @@
131325 }
131326 }while( doPrior && (p = p->pPrior)!=0 );
131327 }
131328 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
131329
131330 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
131331 /*
131332 ** pSelect is a SELECT statement and pSrcItem is one item in the FROM
131333 ** clause of that SELECT.
131334 **
131335 ** This routine scans the entire SELECT statement and recomputes the
131336 ** pSrcItem->colUsed mask.
131337 */
131338 static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
131339 struct SrcList_item *pItem;
131340 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
131341 pItem = pWalker->u.pSrcItem;
131342 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
131343 if( pExpr->iColumn<0 ) return WRC_Continue;
131344 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
131345 return WRC_Continue;
131346 }
131347 static void recomputeColumnsUsed(
131348 Select *pSelect, /* The complete SELECT statement */
131349 struct SrcList_item *pSrcItem /* Which FROM clause item to recompute */
131350 ){
131351 Walker w;
131352 if( NEVER(pSrcItem->pTab==0) ) return;
131353 memset(&w, 0, sizeof(w));
131354 w.xExprCallback = recomputeColumnsUsedExpr;
131355 w.xSelectCallback = sqlite3SelectWalkNoop;
131356 w.u.pSrcItem = pSrcItem;
131357 pSrcItem->colUsed = 0;
131358 sqlite3WalkSelect(&w, pSelect);
131359 }
131360 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
131361
131362 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
131363 /*
131364 ** This routine attempts to flatten subqueries as a performance optimization.
131365 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
131366 **
@@ -131735,10 +131895,16 @@
131895 */
131896 if( pSub->pLimit ){
131897 pParent->pLimit = pSub->pLimit;
131898 pSub->pLimit = 0;
131899 }
131900
131901 /* Recompute the SrcList_item.colUsed masks for the flattened
131902 ** tables. */
131903 for(i=0; i<nSubSrc; i++){
131904 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
131905 }
131906 }
131907
131908 /* Finially, delete what is left of the subquery and return
131909 ** success.
131910 */
@@ -135184,11 +135350,11 @@
135350 zDb = pName->a[0].zDatabase;
135351 zName = pName->a[0].zName;
135352 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
135353 for(i=OMIT_TEMPDB; i<db->nDb; i++){
135354 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
135355 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
135356 assert( sqlite3SchemaMutexHeld(db, j, 0) );
135357 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
135358 if( pTrigger ) break;
135359 }
135360 if( !pTrigger ){
@@ -141206,10 +141372,13 @@
141372 assert( pRangeEnd==0 && pRangeStart==0 );
141373 testcase( pLoop->nSkip>0 );
141374 nExtraReg = 1;
141375 bSeekPastNull = 1;
141376 pLevel->regBignull = regBignull = ++pParse->nMem;
141377 if( pLevel->iLeftJoin ){
141378 sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull);
141379 }
141380 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
141381 }
141382
141383 /* If we are doing a reverse order scan on an ascending index, or
141384 ** a forward order scan on a descending index, interchange the
@@ -148759,11 +148928,11 @@
148928 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
148929 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
148930 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
148931 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
148932 ){
148933 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ);
148934 }
148935 VdbeComment((v, "%s", pIx->zName));
148936 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
148937 {
148938 u64 colUsed = 0;
@@ -148917,16 +149086,10 @@
149086 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
149087 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
149088 if( pIn->eEndLoopOp!=OP_Noop ){
149089 if( pIn->nPrefix ){
149090 assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
 
 
 
 
 
 
149091 if( pLevel->iLeftJoin ){
149092 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
149093 ** opened yet. This occurs for WHERE clauses such as
149094 ** "a = ? AND b IN (...)", where the index is on (a, b). If
149095 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
@@ -148933,13 +149096,20 @@
149096 ** never have been coded, but the body of the loop run to
149097 ** return the null-row. So, if the cursor is not open yet,
149098 ** jump over the OP_Next or OP_Prev instruction about to
149099 ** be coded. */
149100 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
149101 sqlite3VdbeCurrentAddr(v) + 2 +
149102 ((pLoop->wsFlags & WHERE_VIRTUALTABLE)==0)
149103 );
149104 VdbeCoverage(v);
149105 }
149106 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
149107 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
149108 sqlite3VdbeCurrentAddr(v)+2,
149109 pIn->iBase, pIn->nPrefix);
149110 VdbeCoverage(v);
149111 }
149112 }
149113 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
149114 VdbeCoverage(v);
149115 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
@@ -150053,10 +150223,11 @@
150223
150224 ExprList *pSublist = 0; /* Expression list for sub-query */
150225 Window *pMWin = p->pWin; /* Master window object */
150226 Window *pWin; /* Window object iterator */
150227 Table *pTab;
150228 u32 selFlags = p->selFlags;
150229
150230 pTab = sqlite3DbMallocZero(db, sizeof(Table));
150231 if( pTab==0 ){
150232 return sqlite3ErrorToParser(db, SQLITE_NOMEM);
150233 }
@@ -150142,10 +150313,11 @@
150313 Table *pTab2;
150314 p->pSrc->a[0].pSelect = pSub;
150315 sqlite3SrcListAssignCursors(pParse, p->pSrc);
150316 pSub->selFlags |= SF_Expanded;
150317 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
150318 pSub->selFlags |= (selFlags & SF_Aggregate);
150319 if( pTab2==0 ){
150320 /* Might actually be some other kind of error, but in that case
150321 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
150322 ** the correct error message regardless. */
150323 rc = SQLITE_NOMEM;
@@ -151030,10 +151202,11 @@
151202 int regArg;
151203 int nArg = 0;
151204 Window *pWin;
151205 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
151206 FuncDef *pFunc = pWin->pFunc;
151207 assert( pWin->regAccum );
151208 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
151209 nArg = MAX(nArg, windowArgCount(pWin));
151210 if( pMWin->regStartRowid==0 ){
151211 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
151212 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
@@ -151408,10 +151581,14 @@
151581 pNew->eFrmType = p->eFrmType;
151582 pNew->eEnd = p->eEnd;
151583 pNew->eStart = p->eStart;
151584 pNew->eExclude = p->eExclude;
151585 pNew->regResult = p->regResult;
151586 pNew->regAccum = p->regAccum;
151587 pNew->iArgCol = p->iArgCol;
151588 pNew->iEphCsr = p->iEphCsr;
151589 pNew->bExprArgs = p->bExprArgs;
151590 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
151591 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
151592 pNew->pOwner = pOwner;
151593 pNew->bImplicitFrame = p->bImplicitFrame;
151594 }
@@ -152245,10 +152422,11 @@
152422 if( p ){
152423 /* memset(p, 0, sizeof(Expr)); */
152424 p->op = (u8)op;
152425 p->affExpr = 0;
152426 p->flags = EP_Leaf;
152427 ExprClearVVAProperties(p);
152428 p->iAgg = -1;
152429 p->pLeft = p->pRight = 0;
152430 p->x.pList = 0;
152431 p->pAggInfo = 0;
152432 p->y.pTab = 0;
@@ -162084,15 +162262,10 @@
162262 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
162263 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
162264 if( db->mallocFailed ){
162265 goto opendb_out;
162266 }
 
 
 
 
 
162267
162268 /* Parse the filename/URI argument
162269 **
162270 ** Only allow sensible combinations of bits in the flags argument.
162271 ** Throw an error if any non-sense combination is used. If we
@@ -162133,11 +162306,13 @@
162306 sqlite3Error(db, rc);
162307 goto opendb_out;
162308 }
162309 sqlite3BtreeEnter(db->aDb[0].pBt);
162310 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
162311 if( !db->mallocFailed ){
162312 sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
162313 }
162314 sqlite3BtreeLeave(db->aDb[0].pBt);
162315 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
162316
162317 /* The default safety_level for the main database is FULL; for the temp
162318 ** database it is OFF. This matches the pager layer defaults.
@@ -166664,10 +166839,11 @@
166839 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
166840 char *zBuffer = 0; /* Buffer to load terms into */
166841 i64 nAlloc = 0; /* Size of allocated buffer */
166842 int isFirstTerm = 1; /* True when processing first term on page */
166843 sqlite3_int64 iChild; /* Block id of child node to descend to */
166844 int nBuffer = 0; /* Total term size */
166845
166846 /* Skip over the 'height' varint that occurs at the start of every
166847 ** interior node. Then load the blockid of the left-child of the b-tree
166848 ** node into variable iChild.
166849 **
@@ -166688,16 +166864,19 @@
166864
166865 while( zCsr<zEnd && (piFirst || piLast) ){
166866 int cmp; /* memcmp() result */
166867 int nSuffix; /* Size of term suffix */
166868 int nPrefix = 0; /* Size of term prefix */
 
166869
166870 /* Load the next term on the node into zBuffer. Use realloc() to expand
166871 ** the size of zBuffer if required. */
166872 if( !isFirstTerm ){
166873 zCsr += fts3GetVarint32(zCsr, &nPrefix);
166874 if( nPrefix>nBuffer ){
166875 rc = FTS_CORRUPT_VTAB;
166876 goto finish_scan;
166877 }
166878 }
166879 isFirstTerm = 0;
166880 zCsr += fts3GetVarint32(zCsr, &nSuffix);
166881
166882 assert( nPrefix>=0 && nSuffix>=0 );
@@ -179916,10 +180095,16 @@
180095
180096 /* If nSeg is less that zero, then there is no level with at least
180097 ** nMin segments and no hint in the %_stat table. No work to do.
180098 ** Exit early in this case. */
180099 if( nSeg<=0 ) break;
180100
180101 assert( nMod<=0x7FFFFFFF );
180102 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
180103 rc = FTS_CORRUPT_VTAB;
180104 break;
180105 }
180106
180107 /* Open a cursor to iterate through the contents of the oldest nSeg
180108 ** indexes of absolute level iAbsLevel. If this cursor is opened using
180109 ** the 'hint' parameters, it is possible that there are less than nSeg
180110 ** segments available in level iAbsLevel. In this case, no work is
@@ -189652,12 +189837,14 @@
189837 pRtree->nAux++;
189838 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
189839 }else if( pRtree->nAux>0 ){
189840 break;
189841 }else{
189842 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
189843 pRtree->nDim2++;
189844 sqlite3_str_appendf(pSql, azFormat[eCoordType],
189845 rtreeTokenLength(zArg), zArg);
189846 }
189847 }
189848 sqlite3_str_appendf(pSql, ");");
189849 zSql = sqlite3_str_finish(pSql);
189850 if( !zSql ){
@@ -192389,11 +192576,11 @@
192576 ** 1. uPattern is an unescaped match-all character "%",
192577 ** 2. uPattern is an unescaped match-one character "_",
192578 ** 3. uPattern is an unescaped escape character, or
192579 ** 4. uPattern is to be handled as an ordinary character
192580 */
192581 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
192582 /* Case 1. */
192583 uint8_t c;
192584
192585 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
192586 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
@@ -192415,16 +192602,16 @@
192602 }
192603 SQLITE_ICU_SKIP_UTF8(zString);
192604 }
192605 return 0;
192606
192607 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
192608 /* Case 2. */
192609 if( *zString==0 ) return 0;
192610 SQLITE_ICU_SKIP_UTF8(zString);
192611
192612 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
192613 /* Case 3. */
192614 prevEscape = 1;
192615
192616 }else{
192617 /* Case 4. */
@@ -199222,10 +199409,11 @@
199409 }
199410 }
199411 i = 0;
199412 if( iSchema>=0 ){
199413 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
199414 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
199415 pIdxInfo->idxNum |= 0x01;
199416 }
199417 if( iName>=0 ){
199418 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
199419 pIdxInfo->idxNum |= 0x02;
@@ -199436,11 +199624,13 @@
199624 assert( nPayload>=(u32)nLocal );
199625 assert( nLocal<=(nUsable-35) );
199626 if( nPayload>(u32)nLocal ){
199627 int j;
199628 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
199629 if( iOff+nLocal>nUsable || nPayload>0x7fffffff ){
199630 goto statPageIsCorrupt;
199631 }
199632 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
199633 pCell->nOvfl = nOvfl;
199634 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
199635 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
199636 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
@@ -203769,11 +203959,11 @@
203959 const char *zSep = "";
203960 int rc = SQLITE_OK;
203961 SessionBuffer buf = {0, 0, 0};
203962 int nPk = 0;
203963
203964 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
203965 sessionAppendIdent(&buf, zTab, &rc);
203966 sessionAppendStr(&buf, " WHERE ", &rc);
203967
203968 for(i=0; i<p->nCol; i++){
203969 if( p->abPK[i] ){
@@ -203852,11 +204042,11 @@
204042 int i;
204043 const char *zSep = "";
204044 SessionBuffer buf = {0, 0, 0};
204045
204046 /* Append "UPDATE tbl SET " */
204047 sessionAppendStr(&buf, "UPDATE main.", &rc);
204048 sessionAppendIdent(&buf, zTab, &rc);
204049 sessionAppendStr(&buf, " SET ", &rc);
204050
204051 /* Append the assignments */
204052 for(i=0; i<p->nCol; i++){
@@ -223538,11 +223728,11 @@
223728 int nArg, /* Number of args */
223729 sqlite3_value **apUnused /* Function arguments */
223730 ){
223731 assert( nArg==0 );
223732 UNUSED_PARAM2(nArg, apUnused);
223733 sqlite3_result_text(pCtx, "fts5: 2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84f525", -1, SQLITE_TRANSIENT);
223734 }
223735
223736 /*
223737 ** Return true if zName is the extension on one of the shadow tables used
223738 ** by this module.
@@ -228320,12 +228510,12 @@
228510 }
228511 #endif /* SQLITE_CORE */
228512 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
228513
228514 /************** End of stmt.c ************************************************/
228515 #if __LINE__!=228515
228516 #undef SQLITE_SOURCE_ID
228517 #define SQLITE_SOURCE_ID "2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84alt2"
228518 #endif
228519 /* Return the source-id for this library */
228520 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
228521 /************************** End of sqlite3.c ******************************/
228522
+1 -1
--- 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.32.0"
127127
#define SQLITE_VERSION_NUMBER 3032000
128
-#define SQLITE_SOURCE_ID "2020-02-27 16:21:39 951b39ca74c9bd933139e099d5555283278db475f410f202c162e5d1e6aef933"
128
+#define SQLITE_SOURCE_ID "2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84f525"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
134134
--- 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.32.0"
127 #define SQLITE_VERSION_NUMBER 3032000
128 #define SQLITE_SOURCE_ID "2020-02-27 16:21:39 951b39ca74c9bd933139e099d5555283278db475f410f202c162e5d1e6aef933"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
134
--- 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.32.0"
127 #define SQLITE_VERSION_NUMBER 3032000
128 #define SQLITE_SOURCE_ID "2020-03-21 23:10:38 5d14a1c4f2fc17de98ad685ad1422cdfda89dfccb00afcaf32ee416b6f84f525"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
134

Keyboard Shortcuts

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