Fossil SCM

(cherry-pick): Update the built-in SQLite to version 3.28.0.

jan.nijtmans 2019-04-17 22:23 UTC branch-2.8
Commit 96c35627331277f74f0ae62803eefdfcd96f69243a3e061cdb8c19753be50902
5 files changed +4 -4 +4 -4 +252 -37 +4854 -3815 +48 -11
+4 -4
--- auto.def
+++ auto.def
@@ -126,17 +126,17 @@
126126
# the code below will append -ldl to LIBS.
127127
#
128128
foreach extralibs {{} {-ldl}} {
129129
130130
# Locate the system SQLite by searching for sqlite3_open(). Then check
131
- # if sqlite3_create_window_function can be found as well. If we can find open() but
132
- # not create_window_function(), then the system SQLite is too old to link against
131
+ # if sqlite3_stmt_isexplain can be found as well. If we can find open() but
132
+ # not stmt_isexplain(), then the system SQLite is too old to link against
133133
# fossil.
134134
#
135135
if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
136
- if {![check-function-in-lib sqlite3_create_window_function sqlite3 $extralibs]} {
137
- user-error "system sqlite3 too old (require >= 3.25.0)"
136
+ if {![check-function-in-lib sqlite3_stmt_isexplain sqlite3 $extralibs]} {
137
+ user-error "system sqlite3 too old (require >= 3.28.0)"
138138
}
139139
140140
# Success. Update symbols and return.
141141
#
142142
define USE_SYSTEM_SQLITE 1
143143
--- auto.def
+++ auto.def
@@ -126,17 +126,17 @@
126 # the code below will append -ldl to LIBS.
127 #
128 foreach extralibs {{} {-ldl}} {
129
130 # Locate the system SQLite by searching for sqlite3_open(). Then check
131 # if sqlite3_create_window_function can be found as well. If we can find open() but
132 # not create_window_function(), then the system SQLite is too old to link against
133 # fossil.
134 #
135 if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
136 if {![check-function-in-lib sqlite3_create_window_function sqlite3 $extralibs]} {
137 user-error "system sqlite3 too old (require >= 3.25.0)"
138 }
139
140 # Success. Update symbols and return.
141 #
142 define USE_SYSTEM_SQLITE 1
143
--- auto.def
+++ auto.def
@@ -126,17 +126,17 @@
126 # the code below will append -ldl to LIBS.
127 #
128 foreach extralibs {{} {-ldl}} {
129
130 # Locate the system SQLite by searching for sqlite3_open(). Then check
131 # if sqlite3_stmt_isexplain can be found as well. If we can find open() but
132 # not stmt_isexplain(), then the system SQLite is too old to link against
133 # fossil.
134 #
135 if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
136 if {![check-function-in-lib sqlite3_stmt_isexplain sqlite3 $extralibs]} {
137 user-error "system sqlite3 too old (require >= 3.28.0)"
138 }
139
140 # Success. Update symbols and return.
141 #
142 define USE_SYSTEM_SQLITE 1
143
+4 -4
--- auto.def
+++ auto.def
@@ -126,17 +126,17 @@
126126
# the code below will append -ldl to LIBS.
127127
#
128128
foreach extralibs {{} {-ldl}} {
129129
130130
# Locate the system SQLite by searching for sqlite3_open(). Then check
131
- # if sqlite3_create_window_function can be found as well. If we can find open() but
132
- # not create_window_function(), then the system SQLite is too old to link against
131
+ # if sqlite3_stmt_isexplain can be found as well. If we can find open() but
132
+ # not stmt_isexplain(), then the system SQLite is too old to link against
133133
# fossil.
134134
#
135135
if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
136
- if {![check-function-in-lib sqlite3_create_window_function sqlite3 $extralibs]} {
137
- user-error "system sqlite3 too old (require >= 3.25.0)"
136
+ if {![check-function-in-lib sqlite3_stmt_isexplain sqlite3 $extralibs]} {
137
+ user-error "system sqlite3 too old (require >= 3.28.0)"
138138
}
139139
140140
# Success. Update symbols and return.
141141
#
142142
define USE_SYSTEM_SQLITE 1
143143
--- auto.def
+++ auto.def
@@ -126,17 +126,17 @@
126 # the code below will append -ldl to LIBS.
127 #
128 foreach extralibs {{} {-ldl}} {
129
130 # Locate the system SQLite by searching for sqlite3_open(). Then check
131 # if sqlite3_create_window_function can be found as well. If we can find open() but
132 # not create_window_function(), then the system SQLite is too old to link against
133 # fossil.
134 #
135 if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
136 if {![check-function-in-lib sqlite3_create_window_function sqlite3 $extralibs]} {
137 user-error "system sqlite3 too old (require >= 3.25.0)"
138 }
139
140 # Success. Update symbols and return.
141 #
142 define USE_SYSTEM_SQLITE 1
143
--- auto.def
+++ auto.def
@@ -126,17 +126,17 @@
126 # the code below will append -ldl to LIBS.
127 #
128 foreach extralibs {{} {-ldl}} {
129
130 # Locate the system SQLite by searching for sqlite3_open(). Then check
131 # if sqlite3_stmt_isexplain can be found as well. If we can find open() but
132 # not stmt_isexplain(), then the system SQLite is too old to link against
133 # fossil.
134 #
135 if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
136 if {![check-function-in-lib sqlite3_stmt_isexplain sqlite3 $extralibs]} {
137 user-error "system sqlite3 too old (require >= 3.28.0)"
138 }
139
140 # Success. Update symbols and return.
141 #
142 define USE_SYSTEM_SQLITE 1
143
+252 -37
--- src/shell.c
+++ src/shell.c
@@ -2175,17 +2175,17 @@
21752175
if( nIn>mxBlob ){
21762176
sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
21772177
fclose(in);
21782178
return;
21792179
}
2180
- pBuf = sqlite3_malloc64( nIn );
2180
+ pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
21812181
if( pBuf==0 ){
21822182
sqlite3_result_error_nomem(ctx);
21832183
fclose(in);
21842184
return;
21852185
}
2186
- if( 1==fread(pBuf, nIn, 1, in) ){
2186
+ if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
21872187
sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
21882188
}else{
21892189
sqlite3_result_error_code(ctx, SQLITE_IOERR);
21902190
sqlite3_free(pBuf);
21912191
}
@@ -2316,19 +2316,19 @@
23162316
23172317
/*
23182318
** Argument zFile is the name of a file that will be created and/or written
23192319
** by SQL function writefile(). This function ensures that the directory
23202320
** zFile will be written to exists, creating it if required. The permissions
2321
-** for any path components created by this function are set to (mode&0777).
2321
+** for any path components created by this function are set in accordance
2322
+** with the current umask.
23222323
**
23232324
** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
23242325
** SQLITE_OK is returned if the directory is successfully created, or
23252326
** SQLITE_ERROR otherwise.
23262327
*/
23272328
static int makeDirectory(
2328
- const char *zFile,
2329
- mode_t mode
2329
+ const char *zFile
23302330
){
23312331
char *zCopy = sqlite3_mprintf("%s", zFile);
23322332
int rc = SQLITE_OK;
23332333
23342334
if( zCopy==0 ){
@@ -2345,11 +2345,11 @@
23452345
if( i==nCopy ) break;
23462346
zCopy[i] = '\0';
23472347
23482348
rc2 = fileStat(zCopy, &sStat);
23492349
if( rc2!=0 ){
2350
- if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2350
+ if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
23512351
}else{
23522352
if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
23532353
}
23542354
zCopy[i] = '/';
23552355
i++;
@@ -2503,11 +2503,11 @@
25032503
mtime = sqlite3_value_int64(argv[3]);
25042504
}
25052505
25062506
res = writeFile(context, zFile, argv[1], mode, mtime);
25072507
if( res==1 && errno==ENOENT ){
2508
- if( makeDirectory(zFile, mode)==SQLITE_OK ){
2508
+ if( makeDirectory(zFile)==SQLITE_OK ){
25092509
res = writeFile(context, zFile, argv[1], mode, mtime);
25102510
}
25112511
}
25122512
25132513
if( argc>2 && res!=0 ){
@@ -10427,10 +10427,70 @@
1042710427
#endif
1042810428
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
1042910429
sqlite3WhereTrace = savedWhereTrace;
1043010430
#endif
1043110431
}
10432
+
10433
+/* Create the TEMP table used to store parameter bindings */
10434
+static void bind_table_init(ShellState *p){
10435
+ int wrSchema = 0;
10436
+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
10437
+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
10438
+ sqlite3_exec(p->db,
10439
+ "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
10440
+ " key TEXT PRIMARY KEY,\n"
10441
+ " value ANY\n"
10442
+ ") WITHOUT ROWID;",
10443
+ 0, 0, 0);
10444
+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
10445
+}
10446
+
10447
+/*
10448
+** Bind parameters on a prepared statement.
10449
+**
10450
+** Parameter bindings are taken from a TEMP table of the form:
10451
+**
10452
+** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
10453
+** WITHOUT ROWID;
10454
+**
10455
+** No bindings occur if this table does not exist. The special character '$'
10456
+** is included in the table name to help prevent collisions with actual tables.
10457
+** The table must be in the TEMP schema.
10458
+*/
10459
+static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
10460
+ int nVar;
10461
+ int i;
10462
+ int rc;
10463
+ sqlite3_stmt *pQ = 0;
10464
+
10465
+ nVar = sqlite3_bind_parameter_count(pStmt);
10466
+ if( nVar==0 ) return; /* Nothing to do */
10467
+ if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
10468
+ "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
10469
+ return; /* Parameter table does not exist */
10470
+ }
10471
+ rc = sqlite3_prepare_v2(pArg->db,
10472
+ "SELECT value FROM temp.sqlite_parameters"
10473
+ " WHERE key=?1", -1, &pQ, 0);
10474
+ if( rc || pQ==0 ) return;
10475
+ for(i=1; i<=nVar; i++){
10476
+ char zNum[30];
10477
+ const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
10478
+ if( zVar==0 ){
10479
+ sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
10480
+ zVar = zNum;
10481
+ }
10482
+ sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
10483
+ if( sqlite3_step(pQ)==SQLITE_ROW ){
10484
+ sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
10485
+ }else{
10486
+ sqlite3_bind_null(pStmt, i);
10487
+ }
10488
+ sqlite3_reset(pQ);
10489
+ }
10490
+ sqlite3_finalize(pQ);
10491
+}
1043210492
1043310493
/*
1043410494
** Run a prepared statement
1043510495
*/
1043610496
static void exec_prepared_stmt(
@@ -10680,11 +10740,11 @@
1068010740
if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
1068110741
utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1068210742
}
1068310743
1068410744
/* Show the EXPLAIN QUERY PLAN if .eqp is on */
10685
- if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10745
+ if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
1068610746
sqlite3_stmt *pExplain;
1068710747
char *zEQP;
1068810748
int triggerEQP = 0;
1068910749
disable_debug_trace_modes();
1069010750
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
@@ -10729,17 +10789,14 @@
1072910789
}
1073010790
1073110791
if( pArg ){
1073210792
pArg->cMode = pArg->mode;
1073310793
if( pArg->autoExplain ){
10734
- if( sqlite3_column_count(pStmt)==8
10735
- && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10736
- ){
10794
+ if( sqlite3_stmt_isexplain(pStmt)==1 ){
1073710795
pArg->cMode = MODE_Explain;
1073810796
}
10739
- if( sqlite3_column_count(pStmt)==4
10740
- && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10797
+ if( sqlite3_stmt_isexplain(pStmt)==2 ){
1074110798
pArg->cMode = MODE_EQP;
1074210799
}
1074310800
}
1074410801
1074510802
/* If the shell is currently in ".explain" mode, gather the extra
@@ -10747,10 +10804,11 @@
1074710804
if( pArg->cMode==MODE_Explain ){
1074810805
explain_data_prepare(pArg, pStmt);
1074910806
}
1075010807
}
1075110808
10809
+ bind_prepared_stmt(pArg, pStmt);
1075210810
exec_prepared_stmt(pArg, pStmt);
1075310811
explain_data_delete(pArg);
1075410812
eqp_render(pArg);
1075510813
1075610814
/* print usage stats if stats on */
@@ -11076,11 +11134,12 @@
1107611134
static const char *(azHelp[]) = {
1107711135
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
1107811136
".archive ... Manage SQL archives",
1107911137
" Each command must have exactly one of the following options:",
1108011138
" -c, --create Create a new archive",
11081
- " -u, --update Update or add files to an existing archive",
11139
+ " -u, --update Add files or update files with changed mtime",
11140
+ " -i, --insert Like -u but always add even if mtime unchanged",
1108211141
" -t, --list List contents of archive",
1108311142
" -x, --extract Extract files from archive",
1108411143
" Optional arguments:",
1108511144
" -v, --verbose Print each filename as it is processed",
1108611145
" -f FILE, --file FILE Operate on archive FILE (default is current db)",
@@ -11178,10 +11237,17 @@
1117811237
" --new Initialize FILE to an empty database",
1117911238
" --readonly Open FILE readonly",
1118011239
" --zip FILE is a ZIP archive",
1118111240
".output ?FILE? Send output to FILE or stdout if FILE is omitted",
1118211241
" If FILE begins with '|' then open it as a pipe.",
11242
+ ".parameter CMD ... Manage SQL parameter bindings",
11243
+ " clear Erase all bindings",
11244
+ " init Initialize the TEMP table that holds bindings",
11245
+ " list List the current parameter bindings",
11246
+ " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
11247
+ " PARAMETER should start with '$', ':', '@', or '?'",
11248
+ " unset PARAMETER Remove PARAMETER from the binding table",
1118311249
".print STRING... Print literal STRING",
1118411250
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1118511251
".progress N Invoke progress handler after every N opcodes",
1118611252
" --limit N Interrupt after N progress callbacks",
1118711253
" --once Do no more than one progress interrupt",
@@ -12390,20 +12456,31 @@
1239012456
{ "number of views:",
1239112457
"SELECT count(*) FROM %s WHERE type='view'" },
1239212458
{ "schema size:",
1239312459
"SELECT total(length(sql)) FROM %s" },
1239412460
};
12395
- int i;
12461
+ int i, rc;
1239612462
unsigned iDataVersion;
1239712463
char *zSchemaTab;
1239812464
char *zDb = nArg>=2 ? azArg[1] : "main";
1239912465
sqlite3_stmt *pStmt = 0;
1240012466
unsigned char aHdr[100];
1240112467
open_db(p, 0);
1240212468
if( p->db==0 ) return 1;
12403
- sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12404
- -1, &pStmt, 0);
12469
+ rc = sqlite3_prepare_v2(p->db,
12470
+ "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12471
+ -1, &pStmt, 0);
12472
+ if( rc ){
12473
+ if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
12474
+ utf8_printf(stderr, "the \".dbinfo\" command requires the "
12475
+ "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
12476
+ }else{
12477
+ utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
12478
+ }
12479
+ sqlite3_finalize(pStmt);
12480
+ return 1;
12481
+ }
1240512482
sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
1240612483
if( sqlite3_step(pStmt)==SQLITE_ROW
1240712484
&& sqlite3_column_bytes(pStmt,0)>100
1240812485
){
1240912486
memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
@@ -12993,30 +13070,32 @@
1299313070
1299413071
/*
1299513072
** Values for ArCommand.eCmd.
1299613073
*/
1299713074
#define AR_CMD_CREATE 1
12998
-#define AR_CMD_EXTRACT 2
12999
-#define AR_CMD_LIST 3
13000
-#define AR_CMD_UPDATE 4
13001
-#define AR_CMD_HELP 5
13075
+#define AR_CMD_UPDATE 2
13076
+#define AR_CMD_INSERT 3
13077
+#define AR_CMD_EXTRACT 4
13078
+#define AR_CMD_LIST 5
13079
+#define AR_CMD_HELP 6
1300213080
1300313081
/*
1300413082
** Other (non-command) switches.
1300513083
*/
13006
-#define AR_SWITCH_VERBOSE 6
13007
-#define AR_SWITCH_FILE 7
13008
-#define AR_SWITCH_DIRECTORY 8
13009
-#define AR_SWITCH_APPEND 9
13010
-#define AR_SWITCH_DRYRUN 10
13084
+#define AR_SWITCH_VERBOSE 7
13085
+#define AR_SWITCH_FILE 8
13086
+#define AR_SWITCH_DIRECTORY 9
13087
+#define AR_SWITCH_APPEND 10
13088
+#define AR_SWITCH_DRYRUN 11
1301113089
1301213090
static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
1301313091
switch( eSwitch ){
1301413092
case AR_CMD_CREATE:
1301513093
case AR_CMD_EXTRACT:
1301613094
case AR_CMD_LIST:
1301713095
case AR_CMD_UPDATE:
13096
+ case AR_CMD_INSERT:
1301813097
case AR_CMD_HELP:
1301913098
if( pAr->eCmd ){
1302013099
return arErrorMsg(pAr, "multiple command options");
1302113100
}
1302213101
pAr->eCmd = eSwitch;
@@ -13059,10 +13138,11 @@
1305913138
u8 eSwitch;
1306013139
u8 bArg;
1306113140
} aSwitch[] = {
1306213141
{ "create", 'c', AR_CMD_CREATE, 0 },
1306313142
{ "extract", 'x', AR_CMD_EXTRACT, 0 },
13143
+ { "insert", 'i', AR_CMD_INSERT, 0 },
1306413144
{ "list", 't', AR_CMD_LIST, 0 },
1306513145
{ "update", 'u', AR_CMD_UPDATE, 0 },
1306613146
{ "help", 'h', AR_CMD_HELP, 0 },
1306713147
{ "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
1306813148
{ "file", 'f', AR_SWITCH_FILE, 1 },
@@ -13394,23 +13474,31 @@
1339413474
return rc;
1339513475
}
1339613476
1339713477
1339813478
/*
13399
-** Implementation of .ar "create" and "update" commands.
13479
+** Implementation of .ar "create", "insert", and "update" commands.
13480
+**
13481
+** create -> Create a new SQL archive
13482
+** insert -> Insert or reinsert all files listed
13483
+** update -> Insert files that have changed or that were not
13484
+** previously in the archive
1340013485
**
1340113486
** Create the "sqlar" table in the database if it does not already exist.
1340213487
** Then add each file in the azFile[] array to the archive. Directories
1340313488
** are added recursively. If argument bVerbose is non-zero, a message is
1340413489
** printed on stdout for each file archived.
1340513490
**
1340613491
** The create command is the same as update, except that it drops
13407
-** any existing "sqlar" table before beginning.
13492
+** any existing "sqlar" table before beginning. The "insert" command
13493
+** always overwrites every file named on the command-line, where as
13494
+** "update" only overwrites if the size or mtime or mode has changed.
1340813495
*/
1340913496
static int arCreateOrUpdateCommand(
1341013497
ArCommand *pAr, /* Command arguments and options */
13411
- int bUpdate /* true for a --create. false for --update */
13498
+ int bUpdate, /* true for a --create. */
13499
+ int bOnlyIfChanged /* Only update if file has changed */
1341213500
){
1341313501
const char *zCreate =
1341413502
"CREATE TABLE IF NOT EXISTS sqlar(\n"
1341513503
" name TEXT PRIMARY KEY, -- name of the file\n"
1341613504
" mode INT, -- access permissions\n"
@@ -13428,26 +13516,28 @@
1342813516
" CASE substr(lsmode(mode),1,1)\n"
1342913517
" WHEN '-' THEN length(data)\n"
1343013518
" WHEN 'd' THEN 0\n"
1343113519
" ELSE -1 END,\n"
1343213520
" sqlar_compress(data)\n"
13433
- " FROM fsdir(%Q,%Q)\n"
13434
- " WHERE lsmode(mode) NOT LIKE '?%%';",
13521
+ " FROM fsdir(%Q,%Q) AS disk\n"
13522
+ " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13523
+ ,
1343513524
"REPLACE INTO %s(name,mode,mtime,data)\n"
1343613525
" SELECT\n"
1343713526
" %s,\n"
1343813527
" mode,\n"
1343913528
" mtime,\n"
1344013529
" data\n"
13441
- " FROM fsdir(%Q,%Q)\n"
13442
- " WHERE lsmode(mode) NOT LIKE '?%%';"
13530
+ " FROM fsdir(%Q,%Q) AS disk\n"
13531
+ " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
1344313532
};
1344413533
int i; /* For iterating through azFile[] */
1344513534
int rc; /* Return code */
1344613535
const char *zTab = 0; /* SQL table into which to insert */
1344713536
char *zSql;
1344813537
char zTemp[50];
13538
+ char *zExists = 0;
1344913539
1345013540
arExecSql(pAr, "PRAGMA page_size=512");
1345113541
rc = arExecSql(pAr, "SAVEPOINT ar;");
1345213542
if( rc!=SQLITE_OK ) return rc;
1345313543
zTemp[0] = 0;
@@ -13474,14 +13564,25 @@
1347413564
rc = arExecSql(pAr, zDrop);
1347513565
if( rc!=SQLITE_OK ) goto end_ar_transaction;
1347613566
}
1347713567
rc = arExecSql(pAr, zCreate);
1347813568
}
13569
+ if( bOnlyIfChanged ){
13570
+ zExists = sqlite3_mprintf(
13571
+ " AND NOT EXISTS("
13572
+ "SELECT 1 FROM %s AS mem"
13573
+ " WHERE mem.name=disk.name"
13574
+ " AND mem.mtime=disk.mtime"
13575
+ " AND mem.mode=disk.mode)", zTab);
13576
+ }else{
13577
+ zExists = sqlite3_mprintf("");
13578
+ }
13579
+ if( zExists==0 ) rc = SQLITE_NOMEM;
1347913580
for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
1348013581
char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
1348113582
pAr->bVerbose ? "shell_putsnl(name)" : "name",
13482
- pAr->azArg[i], pAr->zDir);
13583
+ pAr->azArg[i], pAr->zDir, zExists);
1348313584
rc = arExecSql(pAr, zSql2);
1348413585
sqlite3_free(zSql2);
1348513586
}
1348613587
end_ar_transaction:
1348713588
if( rc!=SQLITE_OK ){
@@ -13492,10 +13593,11 @@
1349213593
zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
1349313594
arExecSql(pAr, zSql);
1349413595
sqlite3_free(zSql);
1349513596
}
1349613597
}
13598
+ sqlite3_free(zExists);
1349713599
return rc;
1349813600
}
1349913601
1350013602
/*
1350113603
** Implementation of ".ar" dot command.
@@ -13530,11 +13632,12 @@
1353013632
}
1353113633
cmd.bZip = 1;
1353213634
}else if( cmd.zFile ){
1353313635
int flags;
1353413636
if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13535
- if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
13637
+ if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
13638
+ || cmd.eCmd==AR_CMD_UPDATE ){
1353613639
flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
1353713640
}else{
1353813641
flags = SQLITE_OPEN_READONLY;
1353913642
}
1354013643
cmd.db = 0;
@@ -13567,11 +13670,11 @@
1356713670
cmd.zSrcTable = sqlite3_mprintf("sqlar");
1356813671
}
1356913672
1357013673
switch( cmd.eCmd ){
1357113674
case AR_CMD_CREATE:
13572
- rc = arCreateOrUpdateCommand(&cmd, 0);
13675
+ rc = arCreateOrUpdateCommand(&cmd, 0, 0);
1357313676
break;
1357413677
1357513678
case AR_CMD_EXTRACT:
1357613679
rc = arExtractCommand(&cmd);
1357713680
break;
@@ -13582,13 +13685,17 @@
1358213685
1358313686
case AR_CMD_HELP:
1358413687
arUsage(pState->out);
1358513688
break;
1358613689
13690
+ case AR_CMD_INSERT:
13691
+ rc = arCreateOrUpdateCommand(&cmd, 1, 0);
13692
+ break;
13693
+
1358713694
default:
1358813695
assert( cmd.eCmd==AR_CMD_UPDATE );
13589
- rc = arCreateOrUpdateCommand(&cmd, 1);
13696
+ rc = arCreateOrUpdateCommand(&cmd, 1, 1);
1359013697
break;
1359113698
}
1359213699
}
1359313700
end_ar_command:
1359413701
if( cmd.db!=pState->db ){
@@ -14708,10 +14815,118 @@
1470814815
} else {
1470914816
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
1471014817
}
1471114818
}
1471214819
}else
14820
+
14821
+ if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
14822
+ open_db(p,0);
14823
+ if( nArg<=1 ) goto parameter_syntax_error;
14824
+
14825
+ /* .parameter clear
14826
+ ** Clear all bind parameters by dropping the TEMP table that holds them.
14827
+ */
14828
+ if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
14829
+ int wrSchema = 0;
14830
+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14831
+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14832
+ sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
14833
+ 0, 0, 0);
14834
+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14835
+ }else
14836
+
14837
+ /* .parameter list
14838
+ ** List all bind parameters.
14839
+ */
14840
+ if( nArg==2 && strcmp(azArg[1],"list")==0 ){
14841
+ sqlite3_stmt *pStmt = 0;
14842
+ int rx;
14843
+ int len = 0;
14844
+ rx = sqlite3_prepare_v2(p->db,
14845
+ "SELECT max(length(key)) "
14846
+ "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14847
+ if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
14848
+ len = sqlite3_column_int(pStmt, 0);
14849
+ if( len>40 ) len = 40;
14850
+ }
14851
+ sqlite3_finalize(pStmt);
14852
+ pStmt = 0;
14853
+ if( len ){
14854
+ rx = sqlite3_prepare_v2(p->db,
14855
+ "SELECT key, quote(value) "
14856
+ "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14857
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
14858
+ utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
14859
+ sqlite3_column_text(pStmt,1));
14860
+ }
14861
+ sqlite3_finalize(pStmt);
14862
+ }
14863
+ }else
14864
+
14865
+ /* .parameter init
14866
+ ** Make sure the TEMP table used to hold bind parameters exists.
14867
+ ** Create it if necessary.
14868
+ */
14869
+ if( nArg==2 && strcmp(azArg[1],"init")==0 ){
14870
+ bind_table_init(p);
14871
+ }else
14872
+
14873
+ /* .parameter set NAME VALUE
14874
+ ** Set or reset a bind parameter. NAME should be the full parameter
14875
+ ** name exactly as it appears in the query. (ex: $abc, @def). The
14876
+ ** VALUE can be in either SQL literal notation, or if not it will be
14877
+ ** understood to be a text string.
14878
+ */
14879
+ if( nArg==4 && strcmp(azArg[1],"set")==0 ){
14880
+ int rx;
14881
+ char *zSql;
14882
+ sqlite3_stmt *pStmt;
14883
+ const char *zKey = azArg[2];
14884
+ const char *zValue = azArg[3];
14885
+ bind_table_init(p);
14886
+ zSql = sqlite3_mprintf(
14887
+ "REPLACE INTO temp.sqlite_parameters(key,value)"
14888
+ "VALUES(%Q,%s);", zKey, zValue);
14889
+ if( zSql==0 ) shell_out_of_memory();
14890
+ pStmt = 0;
14891
+ rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14892
+ sqlite3_free(zSql);
14893
+ if( rx!=SQLITE_OK ){
14894
+ sqlite3_finalize(pStmt);
14895
+ pStmt = 0;
14896
+ zSql = sqlite3_mprintf(
14897
+ "REPLACE INTO temp.sqlite_parameters(key,value)"
14898
+ "VALUES(%Q,%Q);", zKey, zValue);
14899
+ if( zSql==0 ) shell_out_of_memory();
14900
+ rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14901
+ sqlite3_free(zSql);
14902
+ if( rx!=SQLITE_OK ){
14903
+ utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
14904
+ sqlite3_finalize(pStmt);
14905
+ pStmt = 0;
14906
+ rc = 1;
14907
+ }
14908
+ }
14909
+ sqlite3_step(pStmt);
14910
+ sqlite3_finalize(pStmt);
14911
+ }else
14912
+
14913
+ /* .parameter unset NAME
14914
+ ** Remove the NAME binding from the parameter binding table, if it
14915
+ ** exists.
14916
+ */
14917
+ if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
14918
+ char *zSql = sqlite3_mprintf(
14919
+ "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
14920
+ if( zSql==0 ) shell_out_of_memory();
14921
+ sqlite3_exec(p->db, zSql, 0, 0, 0);
14922
+ sqlite3_free(zSql);
14923
+ }else
14924
+ /* If no command name matches, show a syntax error */
14925
+ parameter_syntax_error:
14926
+ showHelp(p->out, "parameter");
14927
+ }else
1471314928
1471414929
if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
1471514930
int i;
1471614931
for(i=1; i<nArg; i++){
1471714932
if( i>1 ) raw_printf(p->out, " ");
1471814933
--- src/shell.c
+++ src/shell.c
@@ -2175,17 +2175,17 @@
2175 if( nIn>mxBlob ){
2176 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2177 fclose(in);
2178 return;
2179 }
2180 pBuf = sqlite3_malloc64( nIn );
2181 if( pBuf==0 ){
2182 sqlite3_result_error_nomem(ctx);
2183 fclose(in);
2184 return;
2185 }
2186 if( 1==fread(pBuf, nIn, 1, in) ){
2187 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2188 }else{
2189 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2190 sqlite3_free(pBuf);
2191 }
@@ -2316,19 +2316,19 @@
2316
2317 /*
2318 ** Argument zFile is the name of a file that will be created and/or written
2319 ** by SQL function writefile(). This function ensures that the directory
2320 ** zFile will be written to exists, creating it if required. The permissions
2321 ** for any path components created by this function are set to (mode&0777).
 
2322 **
2323 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2324 ** SQLITE_OK is returned if the directory is successfully created, or
2325 ** SQLITE_ERROR otherwise.
2326 */
2327 static int makeDirectory(
2328 const char *zFile,
2329 mode_t mode
2330 ){
2331 char *zCopy = sqlite3_mprintf("%s", zFile);
2332 int rc = SQLITE_OK;
2333
2334 if( zCopy==0 ){
@@ -2345,11 +2345,11 @@
2345 if( i==nCopy ) break;
2346 zCopy[i] = '\0';
2347
2348 rc2 = fileStat(zCopy, &sStat);
2349 if( rc2!=0 ){
2350 if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2351 }else{
2352 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2353 }
2354 zCopy[i] = '/';
2355 i++;
@@ -2503,11 +2503,11 @@
2503 mtime = sqlite3_value_int64(argv[3]);
2504 }
2505
2506 res = writeFile(context, zFile, argv[1], mode, mtime);
2507 if( res==1 && errno==ENOENT ){
2508 if( makeDirectory(zFile, mode)==SQLITE_OK ){
2509 res = writeFile(context, zFile, argv[1], mode, mtime);
2510 }
2511 }
2512
2513 if( argc>2 && res!=0 ){
@@ -10427,10 +10427,70 @@
10427 #endif
10428 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10429 sqlite3WhereTrace = savedWhereTrace;
10430 #endif
10431 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10432
10433 /*
10434 ** Run a prepared statement
10435 */
10436 static void exec_prepared_stmt(
@@ -10680,11 +10740,11 @@
10680 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10681 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10682 }
10683
10684 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10685 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10686 sqlite3_stmt *pExplain;
10687 char *zEQP;
10688 int triggerEQP = 0;
10689 disable_debug_trace_modes();
10690 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
@@ -10729,17 +10789,14 @@
10729 }
10730
10731 if( pArg ){
10732 pArg->cMode = pArg->mode;
10733 if( pArg->autoExplain ){
10734 if( sqlite3_column_count(pStmt)==8
10735 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10736 ){
10737 pArg->cMode = MODE_Explain;
10738 }
10739 if( sqlite3_column_count(pStmt)==4
10740 && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10741 pArg->cMode = MODE_EQP;
10742 }
10743 }
10744
10745 /* If the shell is currently in ".explain" mode, gather the extra
@@ -10747,10 +10804,11 @@
10747 if( pArg->cMode==MODE_Explain ){
10748 explain_data_prepare(pArg, pStmt);
10749 }
10750 }
10751
 
10752 exec_prepared_stmt(pArg, pStmt);
10753 explain_data_delete(pArg);
10754 eqp_render(pArg);
10755
10756 /* print usage stats if stats on */
@@ -11076,11 +11134,12 @@
11076 static const char *(azHelp[]) = {
11077 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11078 ".archive ... Manage SQL archives",
11079 " Each command must have exactly one of the following options:",
11080 " -c, --create Create a new archive",
11081 " -u, --update Update or add files to an existing archive",
 
11082 " -t, --list List contents of archive",
11083 " -x, --extract Extract files from archive",
11084 " Optional arguments:",
11085 " -v, --verbose Print each filename as it is processed",
11086 " -f FILE, --file FILE Operate on archive FILE (default is current db)",
@@ -11178,10 +11237,17 @@
11178 " --new Initialize FILE to an empty database",
11179 " --readonly Open FILE readonly",
11180 " --zip FILE is a ZIP archive",
11181 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
11182 " If FILE begins with '|' then open it as a pipe.",
 
 
 
 
 
 
 
11183 ".print STRING... Print literal STRING",
11184 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11185 ".progress N Invoke progress handler after every N opcodes",
11186 " --limit N Interrupt after N progress callbacks",
11187 " --once Do no more than one progress interrupt",
@@ -12390,20 +12456,31 @@
12390 { "number of views:",
12391 "SELECT count(*) FROM %s WHERE type='view'" },
12392 { "schema size:",
12393 "SELECT total(length(sql)) FROM %s" },
12394 };
12395 int i;
12396 unsigned iDataVersion;
12397 char *zSchemaTab;
12398 char *zDb = nArg>=2 ? azArg[1] : "main";
12399 sqlite3_stmt *pStmt = 0;
12400 unsigned char aHdr[100];
12401 open_db(p, 0);
12402 if( p->db==0 ) return 1;
12403 sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12404 -1, &pStmt, 0);
 
 
 
 
 
 
 
 
 
 
 
12405 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12406 if( sqlite3_step(pStmt)==SQLITE_ROW
12407 && sqlite3_column_bytes(pStmt,0)>100
12408 ){
12409 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
@@ -12993,30 +13070,32 @@
12993
12994 /*
12995 ** Values for ArCommand.eCmd.
12996 */
12997 #define AR_CMD_CREATE 1
12998 #define AR_CMD_EXTRACT 2
12999 #define AR_CMD_LIST 3
13000 #define AR_CMD_UPDATE 4
13001 #define AR_CMD_HELP 5
 
13002
13003 /*
13004 ** Other (non-command) switches.
13005 */
13006 #define AR_SWITCH_VERBOSE 6
13007 #define AR_SWITCH_FILE 7
13008 #define AR_SWITCH_DIRECTORY 8
13009 #define AR_SWITCH_APPEND 9
13010 #define AR_SWITCH_DRYRUN 10
13011
13012 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
13013 switch( eSwitch ){
13014 case AR_CMD_CREATE:
13015 case AR_CMD_EXTRACT:
13016 case AR_CMD_LIST:
13017 case AR_CMD_UPDATE:
 
13018 case AR_CMD_HELP:
13019 if( pAr->eCmd ){
13020 return arErrorMsg(pAr, "multiple command options");
13021 }
13022 pAr->eCmd = eSwitch;
@@ -13059,10 +13138,11 @@
13059 u8 eSwitch;
13060 u8 bArg;
13061 } aSwitch[] = {
13062 { "create", 'c', AR_CMD_CREATE, 0 },
13063 { "extract", 'x', AR_CMD_EXTRACT, 0 },
 
13064 { "list", 't', AR_CMD_LIST, 0 },
13065 { "update", 'u', AR_CMD_UPDATE, 0 },
13066 { "help", 'h', AR_CMD_HELP, 0 },
13067 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
13068 { "file", 'f', AR_SWITCH_FILE, 1 },
@@ -13394,23 +13474,31 @@
13394 return rc;
13395 }
13396
13397
13398 /*
13399 ** Implementation of .ar "create" and "update" commands.
 
 
 
 
 
13400 **
13401 ** Create the "sqlar" table in the database if it does not already exist.
13402 ** Then add each file in the azFile[] array to the archive. Directories
13403 ** are added recursively. If argument bVerbose is non-zero, a message is
13404 ** printed on stdout for each file archived.
13405 **
13406 ** The create command is the same as update, except that it drops
13407 ** any existing "sqlar" table before beginning.
 
 
13408 */
13409 static int arCreateOrUpdateCommand(
13410 ArCommand *pAr, /* Command arguments and options */
13411 int bUpdate /* true for a --create. false for --update */
 
13412 ){
13413 const char *zCreate =
13414 "CREATE TABLE IF NOT EXISTS sqlar(\n"
13415 " name TEXT PRIMARY KEY, -- name of the file\n"
13416 " mode INT, -- access permissions\n"
@@ -13428,26 +13516,28 @@
13428 " CASE substr(lsmode(mode),1,1)\n"
13429 " WHEN '-' THEN length(data)\n"
13430 " WHEN 'd' THEN 0\n"
13431 " ELSE -1 END,\n"
13432 " sqlar_compress(data)\n"
13433 " FROM fsdir(%Q,%Q)\n"
13434 " WHERE lsmode(mode) NOT LIKE '?%%';",
 
13435 "REPLACE INTO %s(name,mode,mtime,data)\n"
13436 " SELECT\n"
13437 " %s,\n"
13438 " mode,\n"
13439 " mtime,\n"
13440 " data\n"
13441 " FROM fsdir(%Q,%Q)\n"
13442 " WHERE lsmode(mode) NOT LIKE '?%%';"
13443 };
13444 int i; /* For iterating through azFile[] */
13445 int rc; /* Return code */
13446 const char *zTab = 0; /* SQL table into which to insert */
13447 char *zSql;
13448 char zTemp[50];
 
13449
13450 arExecSql(pAr, "PRAGMA page_size=512");
13451 rc = arExecSql(pAr, "SAVEPOINT ar;");
13452 if( rc!=SQLITE_OK ) return rc;
13453 zTemp[0] = 0;
@@ -13474,14 +13564,25 @@
13474 rc = arExecSql(pAr, zDrop);
13475 if( rc!=SQLITE_OK ) goto end_ar_transaction;
13476 }
13477 rc = arExecSql(pAr, zCreate);
13478 }
 
 
 
 
 
 
 
 
 
 
 
13479 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13480 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13481 pAr->bVerbose ? "shell_putsnl(name)" : "name",
13482 pAr->azArg[i], pAr->zDir);
13483 rc = arExecSql(pAr, zSql2);
13484 sqlite3_free(zSql2);
13485 }
13486 end_ar_transaction:
13487 if( rc!=SQLITE_OK ){
@@ -13492,10 +13593,11 @@
13492 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13493 arExecSql(pAr, zSql);
13494 sqlite3_free(zSql);
13495 }
13496 }
 
13497 return rc;
13498 }
13499
13500 /*
13501 ** Implementation of ".ar" dot command.
@@ -13530,11 +13632,12 @@
13530 }
13531 cmd.bZip = 1;
13532 }else if( cmd.zFile ){
13533 int flags;
13534 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13535 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
 
13536 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13537 }else{
13538 flags = SQLITE_OPEN_READONLY;
13539 }
13540 cmd.db = 0;
@@ -13567,11 +13670,11 @@
13567 cmd.zSrcTable = sqlite3_mprintf("sqlar");
13568 }
13569
13570 switch( cmd.eCmd ){
13571 case AR_CMD_CREATE:
13572 rc = arCreateOrUpdateCommand(&cmd, 0);
13573 break;
13574
13575 case AR_CMD_EXTRACT:
13576 rc = arExtractCommand(&cmd);
13577 break;
@@ -13582,13 +13685,17 @@
13582
13583 case AR_CMD_HELP:
13584 arUsage(pState->out);
13585 break;
13586
 
 
 
 
13587 default:
13588 assert( cmd.eCmd==AR_CMD_UPDATE );
13589 rc = arCreateOrUpdateCommand(&cmd, 1);
13590 break;
13591 }
13592 }
13593 end_ar_command:
13594 if( cmd.db!=pState->db ){
@@ -14708,10 +14815,118 @@
14708 } else {
14709 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14710 }
14711 }
14712 }else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14713
14714 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14715 int i;
14716 for(i=1; i<nArg; i++){
14717 if( i>1 ) raw_printf(p->out, " ");
14718
--- src/shell.c
+++ src/shell.c
@@ -2175,17 +2175,17 @@
2175 if( nIn>mxBlob ){
2176 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2177 fclose(in);
2178 return;
2179 }
2180 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2181 if( pBuf==0 ){
2182 sqlite3_result_error_nomem(ctx);
2183 fclose(in);
2184 return;
2185 }
2186 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2187 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2188 }else{
2189 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2190 sqlite3_free(pBuf);
2191 }
@@ -2316,19 +2316,19 @@
2316
2317 /*
2318 ** Argument zFile is the name of a file that will be created and/or written
2319 ** by SQL function writefile(). This function ensures that the directory
2320 ** zFile will be written to exists, creating it if required. The permissions
2321 ** for any path components created by this function are set in accordance
2322 ** with the current umask.
2323 **
2324 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2325 ** SQLITE_OK is returned if the directory is successfully created, or
2326 ** SQLITE_ERROR otherwise.
2327 */
2328 static int makeDirectory(
2329 const char *zFile
 
2330 ){
2331 char *zCopy = sqlite3_mprintf("%s", zFile);
2332 int rc = SQLITE_OK;
2333
2334 if( zCopy==0 ){
@@ -2345,11 +2345,11 @@
2345 if( i==nCopy ) break;
2346 zCopy[i] = '\0';
2347
2348 rc2 = fileStat(zCopy, &sStat);
2349 if( rc2!=0 ){
2350 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2351 }else{
2352 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2353 }
2354 zCopy[i] = '/';
2355 i++;
@@ -2503,11 +2503,11 @@
2503 mtime = sqlite3_value_int64(argv[3]);
2504 }
2505
2506 res = writeFile(context, zFile, argv[1], mode, mtime);
2507 if( res==1 && errno==ENOENT ){
2508 if( makeDirectory(zFile)==SQLITE_OK ){
2509 res = writeFile(context, zFile, argv[1], mode, mtime);
2510 }
2511 }
2512
2513 if( argc>2 && res!=0 ){
@@ -10427,10 +10427,70 @@
10427 #endif
10428 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10429 sqlite3WhereTrace = savedWhereTrace;
10430 #endif
10431 }
10432
10433 /* Create the TEMP table used to store parameter bindings */
10434 static void bind_table_init(ShellState *p){
10435 int wrSchema = 0;
10436 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
10437 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
10438 sqlite3_exec(p->db,
10439 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
10440 " key TEXT PRIMARY KEY,\n"
10441 " value ANY\n"
10442 ") WITHOUT ROWID;",
10443 0, 0, 0);
10444 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
10445 }
10446
10447 /*
10448 ** Bind parameters on a prepared statement.
10449 **
10450 ** Parameter bindings are taken from a TEMP table of the form:
10451 **
10452 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
10453 ** WITHOUT ROWID;
10454 **
10455 ** No bindings occur if this table does not exist. The special character '$'
10456 ** is included in the table name to help prevent collisions with actual tables.
10457 ** The table must be in the TEMP schema.
10458 */
10459 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
10460 int nVar;
10461 int i;
10462 int rc;
10463 sqlite3_stmt *pQ = 0;
10464
10465 nVar = sqlite3_bind_parameter_count(pStmt);
10466 if( nVar==0 ) return; /* Nothing to do */
10467 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
10468 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
10469 return; /* Parameter table does not exist */
10470 }
10471 rc = sqlite3_prepare_v2(pArg->db,
10472 "SELECT value FROM temp.sqlite_parameters"
10473 " WHERE key=?1", -1, &pQ, 0);
10474 if( rc || pQ==0 ) return;
10475 for(i=1; i<=nVar; i++){
10476 char zNum[30];
10477 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
10478 if( zVar==0 ){
10479 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
10480 zVar = zNum;
10481 }
10482 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
10483 if( sqlite3_step(pQ)==SQLITE_ROW ){
10484 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
10485 }else{
10486 sqlite3_bind_null(pStmt, i);
10487 }
10488 sqlite3_reset(pQ);
10489 }
10490 sqlite3_finalize(pQ);
10491 }
10492
10493 /*
10494 ** Run a prepared statement
10495 */
10496 static void exec_prepared_stmt(
@@ -10680,11 +10740,11 @@
10740 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10741 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10742 }
10743
10744 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10745 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
10746 sqlite3_stmt *pExplain;
10747 char *zEQP;
10748 int triggerEQP = 0;
10749 disable_debug_trace_modes();
10750 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
@@ -10729,17 +10789,14 @@
10789 }
10790
10791 if( pArg ){
10792 pArg->cMode = pArg->mode;
10793 if( pArg->autoExplain ){
10794 if( sqlite3_stmt_isexplain(pStmt)==1 ){
 
 
10795 pArg->cMode = MODE_Explain;
10796 }
10797 if( sqlite3_stmt_isexplain(pStmt)==2 ){
 
10798 pArg->cMode = MODE_EQP;
10799 }
10800 }
10801
10802 /* If the shell is currently in ".explain" mode, gather the extra
@@ -10747,10 +10804,11 @@
10804 if( pArg->cMode==MODE_Explain ){
10805 explain_data_prepare(pArg, pStmt);
10806 }
10807 }
10808
10809 bind_prepared_stmt(pArg, pStmt);
10810 exec_prepared_stmt(pArg, pStmt);
10811 explain_data_delete(pArg);
10812 eqp_render(pArg);
10813
10814 /* print usage stats if stats on */
@@ -11076,11 +11134,12 @@
11134 static const char *(azHelp[]) = {
11135 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11136 ".archive ... Manage SQL archives",
11137 " Each command must have exactly one of the following options:",
11138 " -c, --create Create a new archive",
11139 " -u, --update Add files or update files with changed mtime",
11140 " -i, --insert Like -u but always add even if mtime unchanged",
11141 " -t, --list List contents of archive",
11142 " -x, --extract Extract files from archive",
11143 " Optional arguments:",
11144 " -v, --verbose Print each filename as it is processed",
11145 " -f FILE, --file FILE Operate on archive FILE (default is current db)",
@@ -11178,10 +11237,17 @@
11237 " --new Initialize FILE to an empty database",
11238 " --readonly Open FILE readonly",
11239 " --zip FILE is a ZIP archive",
11240 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
11241 " If FILE begins with '|' then open it as a pipe.",
11242 ".parameter CMD ... Manage SQL parameter bindings",
11243 " clear Erase all bindings",
11244 " init Initialize the TEMP table that holds bindings",
11245 " list List the current parameter bindings",
11246 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
11247 " PARAMETER should start with '$', ':', '@', or '?'",
11248 " unset PARAMETER Remove PARAMETER from the binding table",
11249 ".print STRING... Print literal STRING",
11250 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11251 ".progress N Invoke progress handler after every N opcodes",
11252 " --limit N Interrupt after N progress callbacks",
11253 " --once Do no more than one progress interrupt",
@@ -12390,20 +12456,31 @@
12456 { "number of views:",
12457 "SELECT count(*) FROM %s WHERE type='view'" },
12458 { "schema size:",
12459 "SELECT total(length(sql)) FROM %s" },
12460 };
12461 int i, rc;
12462 unsigned iDataVersion;
12463 char *zSchemaTab;
12464 char *zDb = nArg>=2 ? azArg[1] : "main";
12465 sqlite3_stmt *pStmt = 0;
12466 unsigned char aHdr[100];
12467 open_db(p, 0);
12468 if( p->db==0 ) return 1;
12469 rc = sqlite3_prepare_v2(p->db,
12470 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12471 -1, &pStmt, 0);
12472 if( rc ){
12473 if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
12474 utf8_printf(stderr, "the \".dbinfo\" command requires the "
12475 "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
12476 }else{
12477 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
12478 }
12479 sqlite3_finalize(pStmt);
12480 return 1;
12481 }
12482 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12483 if( sqlite3_step(pStmt)==SQLITE_ROW
12484 && sqlite3_column_bytes(pStmt,0)>100
12485 ){
12486 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
@@ -12993,30 +13070,32 @@
13070
13071 /*
13072 ** Values for ArCommand.eCmd.
13073 */
13074 #define AR_CMD_CREATE 1
13075 #define AR_CMD_UPDATE 2
13076 #define AR_CMD_INSERT 3
13077 #define AR_CMD_EXTRACT 4
13078 #define AR_CMD_LIST 5
13079 #define AR_CMD_HELP 6
13080
13081 /*
13082 ** Other (non-command) switches.
13083 */
13084 #define AR_SWITCH_VERBOSE 7
13085 #define AR_SWITCH_FILE 8
13086 #define AR_SWITCH_DIRECTORY 9
13087 #define AR_SWITCH_APPEND 10
13088 #define AR_SWITCH_DRYRUN 11
13089
13090 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
13091 switch( eSwitch ){
13092 case AR_CMD_CREATE:
13093 case AR_CMD_EXTRACT:
13094 case AR_CMD_LIST:
13095 case AR_CMD_UPDATE:
13096 case AR_CMD_INSERT:
13097 case AR_CMD_HELP:
13098 if( pAr->eCmd ){
13099 return arErrorMsg(pAr, "multiple command options");
13100 }
13101 pAr->eCmd = eSwitch;
@@ -13059,10 +13138,11 @@
13138 u8 eSwitch;
13139 u8 bArg;
13140 } aSwitch[] = {
13141 { "create", 'c', AR_CMD_CREATE, 0 },
13142 { "extract", 'x', AR_CMD_EXTRACT, 0 },
13143 { "insert", 'i', AR_CMD_INSERT, 0 },
13144 { "list", 't', AR_CMD_LIST, 0 },
13145 { "update", 'u', AR_CMD_UPDATE, 0 },
13146 { "help", 'h', AR_CMD_HELP, 0 },
13147 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
13148 { "file", 'f', AR_SWITCH_FILE, 1 },
@@ -13394,23 +13474,31 @@
13474 return rc;
13475 }
13476
13477
13478 /*
13479 ** Implementation of .ar "create", "insert", and "update" commands.
13480 **
13481 ** create -> Create a new SQL archive
13482 ** insert -> Insert or reinsert all files listed
13483 ** update -> Insert files that have changed or that were not
13484 ** previously in the archive
13485 **
13486 ** Create the "sqlar" table in the database if it does not already exist.
13487 ** Then add each file in the azFile[] array to the archive. Directories
13488 ** are added recursively. If argument bVerbose is non-zero, a message is
13489 ** printed on stdout for each file archived.
13490 **
13491 ** The create command is the same as update, except that it drops
13492 ** any existing "sqlar" table before beginning. The "insert" command
13493 ** always overwrites every file named on the command-line, where as
13494 ** "update" only overwrites if the size or mtime or mode has changed.
13495 */
13496 static int arCreateOrUpdateCommand(
13497 ArCommand *pAr, /* Command arguments and options */
13498 int bUpdate, /* true for a --create. */
13499 int bOnlyIfChanged /* Only update if file has changed */
13500 ){
13501 const char *zCreate =
13502 "CREATE TABLE IF NOT EXISTS sqlar(\n"
13503 " name TEXT PRIMARY KEY, -- name of the file\n"
13504 " mode INT, -- access permissions\n"
@@ -13428,26 +13516,28 @@
13516 " CASE substr(lsmode(mode),1,1)\n"
13517 " WHEN '-' THEN length(data)\n"
13518 " WHEN 'd' THEN 0\n"
13519 " ELSE -1 END,\n"
13520 " sqlar_compress(data)\n"
13521 " FROM fsdir(%Q,%Q) AS disk\n"
13522 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13523 ,
13524 "REPLACE INTO %s(name,mode,mtime,data)\n"
13525 " SELECT\n"
13526 " %s,\n"
13527 " mode,\n"
13528 " mtime,\n"
13529 " data\n"
13530 " FROM fsdir(%Q,%Q) AS disk\n"
13531 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13532 };
13533 int i; /* For iterating through azFile[] */
13534 int rc; /* Return code */
13535 const char *zTab = 0; /* SQL table into which to insert */
13536 char *zSql;
13537 char zTemp[50];
13538 char *zExists = 0;
13539
13540 arExecSql(pAr, "PRAGMA page_size=512");
13541 rc = arExecSql(pAr, "SAVEPOINT ar;");
13542 if( rc!=SQLITE_OK ) return rc;
13543 zTemp[0] = 0;
@@ -13474,14 +13564,25 @@
13564 rc = arExecSql(pAr, zDrop);
13565 if( rc!=SQLITE_OK ) goto end_ar_transaction;
13566 }
13567 rc = arExecSql(pAr, zCreate);
13568 }
13569 if( bOnlyIfChanged ){
13570 zExists = sqlite3_mprintf(
13571 " AND NOT EXISTS("
13572 "SELECT 1 FROM %s AS mem"
13573 " WHERE mem.name=disk.name"
13574 " AND mem.mtime=disk.mtime"
13575 " AND mem.mode=disk.mode)", zTab);
13576 }else{
13577 zExists = sqlite3_mprintf("");
13578 }
13579 if( zExists==0 ) rc = SQLITE_NOMEM;
13580 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13581 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13582 pAr->bVerbose ? "shell_putsnl(name)" : "name",
13583 pAr->azArg[i], pAr->zDir, zExists);
13584 rc = arExecSql(pAr, zSql2);
13585 sqlite3_free(zSql2);
13586 }
13587 end_ar_transaction:
13588 if( rc!=SQLITE_OK ){
@@ -13492,10 +13593,11 @@
13593 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13594 arExecSql(pAr, zSql);
13595 sqlite3_free(zSql);
13596 }
13597 }
13598 sqlite3_free(zExists);
13599 return rc;
13600 }
13601
13602 /*
13603 ** Implementation of ".ar" dot command.
@@ -13530,11 +13632,12 @@
13632 }
13633 cmd.bZip = 1;
13634 }else if( cmd.zFile ){
13635 int flags;
13636 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13637 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
13638 || cmd.eCmd==AR_CMD_UPDATE ){
13639 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13640 }else{
13641 flags = SQLITE_OPEN_READONLY;
13642 }
13643 cmd.db = 0;
@@ -13567,11 +13670,11 @@
13670 cmd.zSrcTable = sqlite3_mprintf("sqlar");
13671 }
13672
13673 switch( cmd.eCmd ){
13674 case AR_CMD_CREATE:
13675 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
13676 break;
13677
13678 case AR_CMD_EXTRACT:
13679 rc = arExtractCommand(&cmd);
13680 break;
@@ -13582,13 +13685,17 @@
13685
13686 case AR_CMD_HELP:
13687 arUsage(pState->out);
13688 break;
13689
13690 case AR_CMD_INSERT:
13691 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
13692 break;
13693
13694 default:
13695 assert( cmd.eCmd==AR_CMD_UPDATE );
13696 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
13697 break;
13698 }
13699 }
13700 end_ar_command:
13701 if( cmd.db!=pState->db ){
@@ -14708,10 +14815,118 @@
14815 } else {
14816 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14817 }
14818 }
14819 }else
14820
14821 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
14822 open_db(p,0);
14823 if( nArg<=1 ) goto parameter_syntax_error;
14824
14825 /* .parameter clear
14826 ** Clear all bind parameters by dropping the TEMP table that holds them.
14827 */
14828 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
14829 int wrSchema = 0;
14830 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14831 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14832 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
14833 0, 0, 0);
14834 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14835 }else
14836
14837 /* .parameter list
14838 ** List all bind parameters.
14839 */
14840 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
14841 sqlite3_stmt *pStmt = 0;
14842 int rx;
14843 int len = 0;
14844 rx = sqlite3_prepare_v2(p->db,
14845 "SELECT max(length(key)) "
14846 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14847 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
14848 len = sqlite3_column_int(pStmt, 0);
14849 if( len>40 ) len = 40;
14850 }
14851 sqlite3_finalize(pStmt);
14852 pStmt = 0;
14853 if( len ){
14854 rx = sqlite3_prepare_v2(p->db,
14855 "SELECT key, quote(value) "
14856 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14857 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14858 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
14859 sqlite3_column_text(pStmt,1));
14860 }
14861 sqlite3_finalize(pStmt);
14862 }
14863 }else
14864
14865 /* .parameter init
14866 ** Make sure the TEMP table used to hold bind parameters exists.
14867 ** Create it if necessary.
14868 */
14869 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
14870 bind_table_init(p);
14871 }else
14872
14873 /* .parameter set NAME VALUE
14874 ** Set or reset a bind parameter. NAME should be the full parameter
14875 ** name exactly as it appears in the query. (ex: $abc, @def). The
14876 ** VALUE can be in either SQL literal notation, or if not it will be
14877 ** understood to be a text string.
14878 */
14879 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
14880 int rx;
14881 char *zSql;
14882 sqlite3_stmt *pStmt;
14883 const char *zKey = azArg[2];
14884 const char *zValue = azArg[3];
14885 bind_table_init(p);
14886 zSql = sqlite3_mprintf(
14887 "REPLACE INTO temp.sqlite_parameters(key,value)"
14888 "VALUES(%Q,%s);", zKey, zValue);
14889 if( zSql==0 ) shell_out_of_memory();
14890 pStmt = 0;
14891 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14892 sqlite3_free(zSql);
14893 if( rx!=SQLITE_OK ){
14894 sqlite3_finalize(pStmt);
14895 pStmt = 0;
14896 zSql = sqlite3_mprintf(
14897 "REPLACE INTO temp.sqlite_parameters(key,value)"
14898 "VALUES(%Q,%Q);", zKey, zValue);
14899 if( zSql==0 ) shell_out_of_memory();
14900 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14901 sqlite3_free(zSql);
14902 if( rx!=SQLITE_OK ){
14903 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
14904 sqlite3_finalize(pStmt);
14905 pStmt = 0;
14906 rc = 1;
14907 }
14908 }
14909 sqlite3_step(pStmt);
14910 sqlite3_finalize(pStmt);
14911 }else
14912
14913 /* .parameter unset NAME
14914 ** Remove the NAME binding from the parameter binding table, if it
14915 ** exists.
14916 */
14917 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
14918 char *zSql = sqlite3_mprintf(
14919 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
14920 if( zSql==0 ) shell_out_of_memory();
14921 sqlite3_exec(p->db, zSql, 0, 0, 0);
14922 sqlite3_free(zSql);
14923 }else
14924 /* If no command name matches, show a syntax error */
14925 parameter_syntax_error:
14926 showHelp(p->out, "parameter");
14927 }else
14928
14929 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14930 int i;
14931 for(i=1; i<nArg; i++){
14932 if( i>1 ) raw_printf(p->out, " ");
14933
+4854 -3815
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.27.2. By combining all the individual C code files into this
3
+** version 3.28.0. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -1160,13 +1160,13 @@
11601160
**
11611161
** See also: [sqlite3_libversion()],
11621162
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11631163
** [sqlite_version()] and [sqlite_source_id()].
11641164
*/
1165
-#define SQLITE_VERSION "3.27.2"
1166
-#define SQLITE_VERSION_NUMBER 3027002
1167
-#define SQLITE_SOURCE_ID "2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0f6d7"
1165
+#define SQLITE_VERSION "3.28.0"
1166
+#define SQLITE_VERSION_NUMBER 3028000
1167
+#define SQLITE_SOURCE_ID "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50"
11681168
11691169
/*
11701170
** CAPI3REF: Run-Time Library Version Numbers
11711171
** KEYWORDS: sqlite3_version sqlite3_sourceid
11721172
**
@@ -1226,10 +1226,13 @@
12261226
** [sqlite_compileoption_get()] and the [compile_options pragma].
12271227
*/
12281228
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
12291229
SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
12301230
SQLITE_API const char *sqlite3_compileoption_get(int N);
1231
+#else
1232
+# define sqlite3_compileoption_used(X) 0
1233
+# define sqlite3_compileoption_get(X) ((void*)0)
12311234
#endif
12321235
12331236
/*
12341237
** CAPI3REF: Test To See If The Library Is Threadsafe
12351238
**
@@ -3123,12 +3126,12 @@
31233126
** following this call. The second parameter may be a NULL pointer, in
31243127
** which case the trigger setting is not reported back. </dd>
31253128
**
31263129
** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
31273130
** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
3128
-** <dd> ^This option is used to enable or disable the two-argument
3129
-** version of the [fts3_tokenizer()] function which is part of the
3131
+** <dd> ^This option is used to enable or disable the
3132
+** [fts3_tokenizer()] function which is part of the
31303133
** [FTS3] full-text search engine extension.
31313134
** There should be two additional arguments.
31323135
** The first argument is an integer which is 0 to disable fts3_tokenizer() or
31333136
** positive to enable fts3_tokenizer() or negative to leave the setting
31343137
** unchanged.
@@ -3236,10 +3239,21 @@
32363239
** <li> The [PRAGMA writable_schema=ON] statement.
32373240
** <li> Writes to the [sqlite_dbpage] virtual table.
32383241
** <li> Direct writes to [shadow tables].
32393242
** </ul>
32403243
** </dd>
3244
+**
3245
+** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
3246
+** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
3247
+** "writable_schema" flag. This has the same effect and is logically equivalent
3248
+** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
3249
+** The first argument to this setting is an integer which is 0 to disable
3250
+** the writable_schema, positive to enable writable_schema, or negative to
3251
+** leave the setting unchanged. The second parameter is a pointer to an
3252
+** integer into which is written 0 or 1 to indicate whether the writable_schema
3253
+** is enabled or disabled following this call.
3254
+** </dd>
32413255
** </dl>
32423256
*/
32433257
#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
32443258
#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
32453259
#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
@@ -3249,11 +3263,12 @@
32493263
#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
32503264
#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
32513265
#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
32523266
#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
32533267
#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */
3254
-#define SQLITE_DBCONFIG_MAX 1010 /* Largest DBCONFIG */
3268
+#define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */
3269
+#define SQLITE_DBCONFIG_MAX 1011 /* Largest DBCONFIG */
32553270
32563271
/*
32573272
** CAPI3REF: Enable Or Disable Extended Result Codes
32583273
** METHOD: sqlite3
32593274
**
@@ -4931,10 +4946,22 @@
49314946
** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
49324947
** sqlite3_stmt_readonly() returns false for those commands.
49334948
*/
49344949
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
49354950
4951
+/*
4952
+** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4953
+** METHOD: sqlite3_stmt
4954
+**
4955
+** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4956
+** prepared statement S is an EXPLAIN statement, or 2 if the
4957
+** statement S is an EXPLAIN QUERY PLAN.
4958
+** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4959
+** an ordinary statement or a NULL pointer.
4960
+*/
4961
+SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4962
+
49364963
/*
49374964
** CAPI3REF: Determine If A Prepared Statement Has Been Reset
49384965
** METHOD: sqlite3_stmt
49394966
**
49404967
** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
@@ -5070,11 +5097,13 @@
50705097
** with embedded NULs is undefined.
50715098
**
50725099
** ^The fifth argument to the BLOB and string binding interfaces
50735100
** is a destructor used to dispose of the BLOB or
50745101
** string after SQLite has finished with it. ^The destructor is called
5075
-** to dispose of the BLOB or string even if the call to bind API fails.
5102
+** to dispose of the BLOB or string even if the call to the bind API fails,
5103
+** except the destructor is not called if the third parameter is a NULL
5104
+** pointer or the fourth parameter is negative.
50765105
** ^If the fifth argument is
50775106
** the special value [SQLITE_STATIC], then SQLite assumes that the
50785107
** information is in static, unmanaged space and does not need to be freed.
50795108
** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
50805109
** SQLite makes its own private copy of the data immediately, before
@@ -5987,10 +6016,12 @@
59876016
** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
59886017
** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
59896018
** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
59906019
** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
59916020
** against a virtual table.
6021
+** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
6022
+** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
59926023
** </table></blockquote>
59936024
**
59946025
** <b>Details:</b>
59956026
**
59966027
** These routines extract type, size, and content information from
@@ -6047,10 +6078,15 @@
60476078
** was unchanging). ^Within an [xUpdate] method, any value for which
60486079
** sqlite3_value_nochange(X) is true will in all other respects appear
60496080
** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
60506081
** than within an [xUpdate] method call for an UPDATE statement, then
60516082
** the return value is arbitrary and meaningless.
6083
+**
6084
+** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6085
+** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6086
+** interfaces. ^If X comes from an SQL literal value, or a table column,
6087
+** and expression, then sqlite3_value_frombind(X) returns zero.
60526088
**
60536089
** Please pay particular attention to the fact that the pointer returned
60546090
** from [sqlite3_value_blob()], [sqlite3_value_text()], or
60556091
** [sqlite3_value_text16()] can be invalidated by a subsequent call to
60566092
** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
@@ -6093,10 +6129,11 @@
60936129
SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
60946130
SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
60956131
SQLITE_API int sqlite3_value_type(sqlite3_value*);
60966132
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
60976133
SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6134
+SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
60986135
60996136
/*
61006137
** CAPI3REF: Finding The Subtype Of SQL Values
61016138
** METHOD: sqlite3_value
61026139
**
@@ -6828,11 +6865,11 @@
68286865
**
68296866
** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
68306867
** associated with database N of connection D. ^The main database file
68316868
** has the name "main". If there is no attached database N on the database
68326869
** connection D, or if database N is a temporary or in-memory database, then
6833
-** a NULL pointer is returned.
6870
+** this function will return either a NULL pointer or an empty string.
68346871
**
68356872
** ^The filename returned by this function is the output of the
68366873
** xFullPathname method of the [VFS]. ^In other words, the filename
68376874
** will be an absolute pathname, even if the filename used
68386875
** to open the database originally was a URI or relative pathname.
@@ -11929,11 +11966,11 @@
1192911966
**
1193011967
** Argument pIn must point to a buffer containing a changeset nIn bytes
1193111968
** in size. This function allocates and populates a buffer with a copy
1193211969
** of the changeset rebased rebased according to the configuration of the
1193311970
** rebaser object passed as the first argument. If successful, (*ppOut)
11934
-** is set to point to the new buffer containing the rebased changset and
11971
+** is set to point to the new buffer containing the rebased changeset and
1193511972
** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
1193611973
** responsibility of the caller to eventually free the new buffer using
1193711974
** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
1193811975
** are set to zero and an SQLite error code returned.
1193911976
*/
@@ -12338,11 +12375,11 @@
1233812375
** xSetAuxdata(pFts5, pAux, xDelete)
1233912376
**
1234012377
** Save the pointer passed as the second argument as the extension functions
1234112378
** "auxiliary data". The pointer may then be retrieved by the current or any
1234212379
** future invocation of the same fts5 extension function made as part of
12343
-** of the same MATCH query using the xGetAuxdata() API.
12380
+** the same MATCH query using the xGetAuxdata() API.
1234412381
**
1234512382
** Each extension function is allocated a single auxiliary data slot for
1234612383
** each FTS query (MATCH expression). If the extension function is invoked
1234712384
** more than once for a single FTS query, then all invocations share a
1234812385
** single auxiliary data context.
@@ -12353,11 +12390,11 @@
1235312390
** point.
1235412391
**
1235512392
** The xDelete callback, if one is specified, is also invoked on the
1235612393
** auxiliary data pointer after the FTS5 query has finished.
1235712394
**
12358
-** If an error (e.g. an OOM condition) occurs within this function, an
12395
+** If an error (e.g. an OOM condition) occurs within this function,
1235912396
** the auxiliary data is set to NULL and an error code returned. If the
1236012397
** xDelete parameter was not NULL, it is invoked on the auxiliary data
1236112398
** pointer before returning.
1236212399
**
1236312400
**
@@ -13379,11 +13416,11 @@
1337913416
struct Hash {
1338013417
unsigned int htsize; /* Number of buckets in the hash table */
1338113418
unsigned int count; /* Number of entries in this table */
1338213419
HashElem *first; /* The first element of the array */
1338313420
struct _ht { /* the hash table */
13384
- int count; /* Number of entries with this hash */
13421
+ unsigned int count; /* Number of entries with this hash */
1338513422
HashElem *chain; /* Pointer to first entry with this hash */
1338613423
} *ht;
1338713424
};
1338813425
1338913426
/* Each element in the hash table is an instance of the following
@@ -13520,103 +13557,98 @@
1352013557
#define TK_FOLLOWING 83
1352113558
#define TK_PARTITION 84
1352213559
#define TK_PRECEDING 85
1352313560
#define TK_RANGE 86
1352413561
#define TK_UNBOUNDED 87
13525
-#define TK_REINDEX 88
13526
-#define TK_RENAME 89
13527
-#define TK_CTIME_KW 90
13528
-#define TK_ANY 91
13529
-#define TK_BITAND 92
13530
-#define TK_BITOR 93
13531
-#define TK_LSHIFT 94
13532
-#define TK_RSHIFT 95
13533
-#define TK_PLUS 96
13534
-#define TK_MINUS 97
13535
-#define TK_STAR 98
13536
-#define TK_SLASH 99
13537
-#define TK_REM 100
13538
-#define TK_CONCAT 101
13539
-#define TK_COLLATE 102
13540
-#define TK_BITNOT 103
13541
-#define TK_ON 104
13542
-#define TK_INDEXED 105
13543
-#define TK_STRING 106
13544
-#define TK_JOIN_KW 107
13545
-#define TK_CONSTRAINT 108
13546
-#define TK_DEFAULT 109
13547
-#define TK_NULL 110
13548
-#define TK_PRIMARY 111
13549
-#define TK_UNIQUE 112
13550
-#define TK_CHECK 113
13551
-#define TK_REFERENCES 114
13552
-#define TK_AUTOINCR 115
13553
-#define TK_INSERT 116
13554
-#define TK_DELETE 117
13555
-#define TK_UPDATE 118
13556
-#define TK_SET 119
13557
-#define TK_DEFERRABLE 120
13558
-#define TK_FOREIGN 121
13559
-#define TK_DROP 122
13560
-#define TK_UNION 123
13561
-#define TK_ALL 124
13562
-#define TK_EXCEPT 125
13563
-#define TK_INTERSECT 126
13564
-#define TK_SELECT 127
13565
-#define TK_VALUES 128
13566
-#define TK_DISTINCT 129
13567
-#define TK_DOT 130
13568
-#define TK_FROM 131
13569
-#define TK_JOIN 132
13570
-#define TK_USING 133
13571
-#define TK_ORDER 134
13572
-#define TK_GROUP 135
13573
-#define TK_HAVING 136
13574
-#define TK_LIMIT 137
13575
-#define TK_WHERE 138
13576
-#define TK_INTO 139
13577
-#define TK_NOTHING 140
13578
-#define TK_FLOAT 141
13579
-#define TK_BLOB 142
13580
-#define TK_INTEGER 143
13581
-#define TK_VARIABLE 144
13582
-#define TK_CASE 145
13583
-#define TK_WHEN 146
13584
-#define TK_THEN 147
13585
-#define TK_ELSE 148
13586
-#define TK_INDEX 149
13587
-#define TK_ALTER 150
13588
-#define TK_ADD 151
13589
-#define TK_WINDOW 152
13590
-#define TK_OVER 153
13591
-#define TK_FILTER 154
13592
-#define TK_TRUEFALSE 155
13593
-#define TK_ISNOT 156
13594
-#define TK_FUNCTION 157
13595
-#define TK_COLUMN 158
13596
-#define TK_AGG_FUNCTION 159
13597
-#define TK_AGG_COLUMN 160
13598
-#define TK_UMINUS 161
13599
-#define TK_UPLUS 162
13600
-#define TK_TRUTH 163
13601
-#define TK_REGISTER 164
13602
-#define TK_VECTOR 165
13603
-#define TK_SELECT_COLUMN 166
13604
-#define TK_IF_NULL_ROW 167
13605
-#define TK_ASTERISK 168
13606
-#define TK_SPAN 169
13607
-#define TK_END_OF_FILE 170
13608
-#define TK_UNCLOSED_STRING 171
13609
-#define TK_SPACE 172
13610
-#define TK_ILLEGAL 173
13611
-
13612
-/* The token codes above must all fit in 8 bits */
13613
-#define TKFLG_MASK 0xff
13614
-
13615
-/* Flags that can be added to a token code when it is not
13616
-** being stored in a u8: */
13617
-#define TKFLG_DONTFOLD 0x100 /* Omit constant folding optimizations */
13562
+#define TK_EXCLUDE 88
13563
+#define TK_GROUPS 89
13564
+#define TK_OTHERS 90
13565
+#define TK_TIES 91
13566
+#define TK_REINDEX 92
13567
+#define TK_RENAME 93
13568
+#define TK_CTIME_KW 94
13569
+#define TK_ANY 95
13570
+#define TK_BITAND 96
13571
+#define TK_BITOR 97
13572
+#define TK_LSHIFT 98
13573
+#define TK_RSHIFT 99
13574
+#define TK_PLUS 100
13575
+#define TK_MINUS 101
13576
+#define TK_STAR 102
13577
+#define TK_SLASH 103
13578
+#define TK_REM 104
13579
+#define TK_CONCAT 105
13580
+#define TK_COLLATE 106
13581
+#define TK_BITNOT 107
13582
+#define TK_ON 108
13583
+#define TK_INDEXED 109
13584
+#define TK_STRING 110
13585
+#define TK_JOIN_KW 111
13586
+#define TK_CONSTRAINT 112
13587
+#define TK_DEFAULT 113
13588
+#define TK_NULL 114
13589
+#define TK_PRIMARY 115
13590
+#define TK_UNIQUE 116
13591
+#define TK_CHECK 117
13592
+#define TK_REFERENCES 118
13593
+#define TK_AUTOINCR 119
13594
+#define TK_INSERT 120
13595
+#define TK_DELETE 121
13596
+#define TK_UPDATE 122
13597
+#define TK_SET 123
13598
+#define TK_DEFERRABLE 124
13599
+#define TK_FOREIGN 125
13600
+#define TK_DROP 126
13601
+#define TK_UNION 127
13602
+#define TK_ALL 128
13603
+#define TK_EXCEPT 129
13604
+#define TK_INTERSECT 130
13605
+#define TK_SELECT 131
13606
+#define TK_VALUES 132
13607
+#define TK_DISTINCT 133
13608
+#define TK_DOT 134
13609
+#define TK_FROM 135
13610
+#define TK_JOIN 136
13611
+#define TK_USING 137
13612
+#define TK_ORDER 138
13613
+#define TK_GROUP 139
13614
+#define TK_HAVING 140
13615
+#define TK_LIMIT 141
13616
+#define TK_WHERE 142
13617
+#define TK_INTO 143
13618
+#define TK_NOTHING 144
13619
+#define TK_FLOAT 145
13620
+#define TK_BLOB 146
13621
+#define TK_INTEGER 147
13622
+#define TK_VARIABLE 148
13623
+#define TK_CASE 149
13624
+#define TK_WHEN 150
13625
+#define TK_THEN 151
13626
+#define TK_ELSE 152
13627
+#define TK_INDEX 153
13628
+#define TK_ALTER 154
13629
+#define TK_ADD 155
13630
+#define TK_WINDOW 156
13631
+#define TK_OVER 157
13632
+#define TK_FILTER 158
13633
+#define TK_TRUEFALSE 159
13634
+#define TK_ISNOT 160
13635
+#define TK_FUNCTION 161
13636
+#define TK_COLUMN 162
13637
+#define TK_AGG_FUNCTION 163
13638
+#define TK_AGG_COLUMN 164
13639
+#define TK_UMINUS 165
13640
+#define TK_UPLUS 166
13641
+#define TK_TRUTH 167
13642
+#define TK_REGISTER 168
13643
+#define TK_VECTOR 169
13644
+#define TK_SELECT_COLUMN 170
13645
+#define TK_IF_NULL_ROW 171
13646
+#define TK_ASTERISK 172
13647
+#define TK_SPAN 173
13648
+#define TK_SPACE 174
13649
+#define TK_ILLEGAL 175
1361813650
1361913651
/************** End of parse.h ***********************************************/
1362013652
/************** Continuing where we left off in sqliteInt.h ******************/
1362113653
#include <stdio.h>
1362213654
#include <stdlib.h>
@@ -14544,13 +14576,10 @@
1454414576
};
1454514577
1454614578
SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
1454714579
int flags, int seekResult);
1454814580
SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
14549
-#ifndef SQLITE_OMIT_WINDOWFUNC
14550
-SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
14551
-#endif
1455214581
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
1455314582
SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
1455414583
SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
1455514584
SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
1455614585
SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
@@ -14904,29 +14933,29 @@
1490414933
#define OP_Compare 87 /* synopsis: r[P1@P3] <-> r[P2@P3] */
1490514934
#define OP_IsTrue 88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
1490614935
#define OP_Offset 89 /* synopsis: r[P3] = sqlite_offset(P1) */
1490714936
#define OP_Column 90 /* synopsis: r[P3]=PX */
1490814937
#define OP_Affinity 91 /* synopsis: affinity(r[P1@P2]) */
14909
-#define OP_BitAnd 92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
14910
-#define OP_BitOr 93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
14911
-#define OP_ShiftLeft 94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
14912
-#define OP_ShiftRight 95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
14913
-#define OP_Add 96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
14914
-#define OP_Subtract 97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
14915
-#define OP_Multiply 98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
14916
-#define OP_Divide 99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
14917
-#define OP_Remainder 100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
14918
-#define OP_Concat 101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
14919
-#define OP_MakeRecord 102 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14920
-#define OP_BitNot 103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
14921
-#define OP_Count 104 /* synopsis: r[P2]=count() */
14922
-#define OP_ReadCookie 105
14923
-#define OP_String8 106 /* same as TK_STRING, synopsis: r[P2]='P4' */
14924
-#define OP_SetCookie 107
14925
-#define OP_ReopenIdx 108 /* synopsis: root=P2 iDb=P3 */
14926
-#define OP_OpenRead 109 /* synopsis: root=P2 iDb=P3 */
14927
-#define OP_OpenWrite 110 /* synopsis: root=P2 iDb=P3 */
14938
+#define OP_MakeRecord 92 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14939
+#define OP_Count 93 /* synopsis: r[P2]=count() */
14940
+#define OP_ReadCookie 94
14941
+#define OP_SetCookie 95
14942
+#define OP_BitAnd 96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
14943
+#define OP_BitOr 97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
14944
+#define OP_ShiftLeft 98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
14945
+#define OP_ShiftRight 99 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
14946
+#define OP_Add 100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
14947
+#define OP_Subtract 101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
14948
+#define OP_Multiply 102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
14949
+#define OP_Divide 103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
14950
+#define OP_Remainder 104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
14951
+#define OP_Concat 105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
14952
+#define OP_ReopenIdx 106 /* synopsis: root=P2 iDb=P3 */
14953
+#define OP_BitNot 107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
14954
+#define OP_OpenRead 108 /* synopsis: root=P2 iDb=P3 */
14955
+#define OP_OpenWrite 109 /* synopsis: root=P2 iDb=P3 */
14956
+#define OP_String8 110 /* same as TK_STRING, synopsis: r[P2]='P4' */
1492814957
#define OP_OpenDup 111
1492914958
#define OP_OpenAutoindex 112 /* synopsis: nColumn=P2 */
1493014959
#define OP_OpenEphemeral 113 /* synopsis: nColumn=P2 */
1493114960
#define OP_SorterOpen 114
1493214961
#define OP_SequenceTest 115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
@@ -14953,15 +14982,15 @@
1495314982
#define OP_Destroy 136
1495414983
#define OP_Clear 137
1495514984
#define OP_ResetSorter 138
1495614985
#define OP_CreateBtree 139 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
1495714986
#define OP_SqlExec 140
14958
-#define OP_Real 141 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
14959
-#define OP_ParseSchema 142
14960
-#define OP_LoadAnalysis 143
14961
-#define OP_DropTable 144
14962
-#define OP_DropIndex 145
14987
+#define OP_ParseSchema 141
14988
+#define OP_LoadAnalysis 142
14989
+#define OP_DropTable 143
14990
+#define OP_DropIndex 144
14991
+#define OP_Real 145 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
1496314992
#define OP_DropTrigger 146
1496414993
#define OP_IntegrityCk 147
1496514994
#define OP_RowSetAdd 148 /* synopsis: rowset(P1)=r[P2] */
1496614995
#define OP_Param 149
1496714996
#define OP_FkCounter 150 /* synopsis: fkctr[P1]+=P2 */
@@ -15008,18 +15037,18 @@
1500815037
/* 48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
1500915038
/* 56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
1501015039
/* 64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
1501115040
/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
1501215041
/* 80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
15013
-/* 88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
15014
-/* 96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
15015
-/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
15042
+/* 88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
15043
+/* 96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
15044
+/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\
1501615045
/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
1501715046
/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
1501815047
/* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
15019
-/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00,\
15020
-/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
15048
+/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
15049
+/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
1502115050
/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
1502215051
/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
1502315052
/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,}
1502415053
1502515054
/* The sqlite3P2Values() routine is able to run faster if it knows
@@ -16323,10 +16352,11 @@
1632316352
int (*xCommitCallback)(void*); /* Invoked at every commit. */
1632416353
void *pRollbackArg; /* Argument to xRollbackCallback() */
1632516354
void (*xRollbackCallback)(void*); /* Invoked at every commit. */
1632616355
void *pUpdateArg;
1632716356
void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16357
+ Parse *pParse; /* Current parse */
1632816358
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1632916359
void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
1633016360
void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
1633116361
void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
1633216362
);
@@ -16456,19 +16486,20 @@
1645616486
** Allowed values for sqlite3.mDbFlags
1645716487
*/
1645816488
#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
1645916489
#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
1646016490
#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
16461
-#define DBFLAG_SchemaKnownOk 0x0008 /* Schema is known to be valid */
16491
+#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
16492
+#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
1646216493
1646316494
/*
1646416495
** Bits of the sqlite3.dbOptFlags field that are used by the
1646516496
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
1646616497
** selectively disable various optimizations.
1646716498
*/
1646816499
#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
16469
- /* 0x0002 available for reuse */
16500
+#define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */
1647016501
#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
1647116502
#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
1647216503
#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
1647316504
#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
1647416505
#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
@@ -16582,11 +16613,10 @@
1658216613
#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
1658316614
** single query - might change over time */
1658416615
#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
1658516616
#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
1658616617
#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
16587
-#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
1658816618
#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
1658916619
1659016620
/*
1659116621
** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
1659216622
** used to create the initializers for the FuncDef structures.
@@ -17388,16 +17418,20 @@
1738817418
} y;
1738917419
};
1739017420
1739117421
/*
1739217422
** The following are the meanings of bits in the Expr.flags field.
17423
+** Value restrictions:
17424
+**
17425
+** EP_Agg == NC_HasAgg == SF_HasAgg
17426
+** EP_Win == NC_HasWin
1739317427
*/
1739417428
#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
17395
-#define EP_Agg 0x000002 /* Contains one or more aggregate functions */
17429
+#define EP_Distinct 0x000002 /* Aggregate function with DISTINCT keyword */
1739617430
#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
1739717431
#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
17398
-#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */
17432
+#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
1739917433
#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
1740017434
#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
1740117435
#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
1740217436
#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
1740317437
#define EP_Generic 0x000200 /* Ignore COLLATE or affinity on this tree */
@@ -17404,11 +17438,11 @@
1740417438
#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
1740517439
#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
1740617440
#define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */
1740717441
#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
1740817442
#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17409
-#define EP_Static 0x008000 /* Held in memory not obtained from malloc() */
17443
+#define EP_Win 0x008000 /* Contains window functions */
1741017444
#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
1741117445
#define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
1741217446
#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
1741317447
#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
1741417448
#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
@@ -17416,10 +17450,11 @@
1741617450
#define EP_Alias 0x400000 /* Is an alias for a result set column */
1741717451
#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
1741817452
#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
1741917453
#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
1742017454
#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
17455
+#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
1742117456
1742217457
/*
1742317458
** The EP_Propagate mask is a set of properties that automatically propagate
1742417459
** upwards into parent nodes.
1742517460
*/
@@ -17655,12 +17690,13 @@
1765517690
1765617691
/*
1765717692
** Allowed values for the NameContext, ncFlags field.
1765817693
**
1765917694
** Value constraints (all checked via assert()):
17660
-** NC_HasAgg == SF_HasAgg
17695
+** NC_HasAgg == SF_HasAgg == EP_Agg
1766117696
** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
17697
+** NC_HasWin == EP_Win
1766217698
**
1766317699
*/
1766417700
#define NC_AllowAgg 0x0001 /* Aggregate functions are allowed here */
1766517701
#define NC_PartIdx 0x0002 /* True if resolving a partial index WHERE */
1766617702
#define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */
@@ -17672,10 +17708,11 @@
1767217708
#define NC_UAggInfo 0x0100 /* True if uNC.pAggInfo is used */
1767317709
#define NC_UUpsert 0x0200 /* True if uNC.pUpsert is used */
1767417710
#define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */
1767517711
#define NC_Complex 0x2000 /* True if a function or subquery seen */
1767617712
#define NC_AllowWin 0x4000 /* Window functions are allowed here */
17713
+#define NC_HasWin 0x8000 /* One or more window functions seen */
1767717714
1767817715
/*
1767917716
** An instance of the following object describes a single ON CONFLICT
1768017717
** clause in an upsert.
1768117718
**
@@ -17986,10 +18023,11 @@
1798618023
TableLock *aTableLock; /* Required table locks for shared-cache mode */
1798718024
#endif
1798818025
AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
1798918026
Parse *pToplevel; /* Parse structure for main program (or NULL) */
1799018027
Table *pTriggerTab; /* Table triggers are being coded for */
18028
+ Parse *pParentParse; /* Parent parser if this parser is nested */
1799118029
int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
1799218030
u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
1799318031
u32 oldmask; /* Mask of old.* columns referenced */
1799418032
u32 newmask; /* Mask of new.* columns referenced */
1799518033
u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
@@ -18426,11 +18464,11 @@
1842618464
u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
1842718465
};
1842818466
#endif /* SQLITE_DEBUG */
1842918467
1843018468
/*
18431
-** This object is used in varioius ways, all related to window functions
18469
+** This object is used in various ways, all related to window functions
1843218470
**
1843318471
** (1) A single instance of this structure is attached to the
1843418472
** the Expr.pWin field for each window function in an expression tree.
1843518473
** This object holds the information contained in the OVER clause,
1843618474
** plus additional fields used during code generation.
@@ -18441,19 +18479,22 @@
1844118479
**
1844218480
** (3) The terms of the WINDOW clause of a SELECT are instances of this
1844318481
** object on a linked list attached to Select.pWinDefn.
1844418482
**
1844518483
** The uses (1) and (2) are really the same Window object that just happens
18446
-** to be accessible in two different ways. Use (3) is are separate objects.
18484
+** to be accessible in two different ways. Use case (3) are separate objects.
1844718485
*/
1844818486
struct Window {
1844918487
char *zName; /* Name of window (may be NULL) */
18488
+ char *zBase; /* Name of base window for chaining (may be NULL) */
1845018489
ExprList *pPartition; /* PARTITION BY clause */
1845118490
ExprList *pOrderBy; /* ORDER BY clause */
18452
- u8 eType; /* TK_RANGE or TK_ROWS */
18491
+ u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
1845318492
u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
1845418493
u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18494
+ u8 bImplicitFrame; /* True if frame was implicitly specified */
18495
+ u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
1845518496
Expr *pStart; /* Expression for "<expr> PRECEDING" */
1845618497
Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
1845718498
Window *pNextWin; /* Next window function belonging to this SELECT */
1845818499
Expr *pFilter; /* The FILTER expression */
1845918500
FuncDef *pFunc; /* The function */
@@ -18460,21 +18501,23 @@
1846018501
int iEphCsr; /* Partition buffer or Peer buffer */
1846118502
int regAccum;
1846218503
int regResult;
1846318504
int csrApp; /* Function cursor (used by min/max) */
1846418505
int regApp; /* Function register (also used by min/max) */
18465
- int regPart; /* First in a set of registers holding PARTITION BY
18466
- ** and ORDER BY values for the window */
18506
+ int regPart; /* Array of registers for PARTITION BY values */
1846718507
Expr *pOwner; /* Expression object this window is attached to */
1846818508
int nBufferCol; /* Number of columns in buffer table */
1846918509
int iArgCol; /* Offset of first argument for this function */
18510
+ int regOne; /* Register containing constant value 1 */
18511
+ int regStartRowid;
18512
+ int regEndRowid;
1847018513
};
1847118514
1847218515
#ifndef SQLITE_OMIT_WINDOWFUNC
1847318516
SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
1847418517
SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
18475
-SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
18518
+SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
1847618519
SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
1847718520
SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
1847818521
SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
1847918522
SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
1848018523
SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
@@ -18481,10 +18524,12 @@
1848118524
SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
1848218525
SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
1848318526
SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
1848418527
SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
1848518528
SQLITE_PRIVATE void sqlite3WindowFunctions(void);
18529
+SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
18530
+SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
1848618531
#else
1848718532
# define sqlite3WindowDelete(a,b)
1848818533
# define sqlite3WindowFunctions()
1848918534
# define sqlite3WindowAttach(a,b,c)
1849018535
#endif
@@ -18710,10 +18755,11 @@
1871018755
#endif
1871118756
1871218757
1871318758
SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
1871418759
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
18760
+SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
1871518761
SQLITE_PRIVATE void sqlite3Dequote(char*);
1871618762
SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
1871718763
SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
1871818764
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
1871918765
SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
@@ -20133,15 +20179,15 @@
2013320179
#define MEM_Str 0x0002 /* Value is a string */
2013420180
#define MEM_Int 0x0004 /* Value is an integer */
2013520181
#define MEM_Real 0x0008 /* Value is a real number */
2013620182
#define MEM_Blob 0x0010 /* Value is a BLOB */
2013720183
#define MEM_AffMask 0x001f /* Mask of affinity bits */
20138
-/* Available 0x0020 */
20184
+#define MEM_FromBind 0x0020 /* Value originates from sqlite3_bind() */
2013920185
/* Available 0x0040 */
2014020186
#define MEM_Undefined 0x0080 /* Value is undefined */
2014120187
#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
20142
-#define MEM_TypeMask 0xc1ff /* Mask of type bits */
20188
+#define MEM_TypeMask 0xc1df /* Mask of type bits */
2014320189
2014420190
2014520191
/* Whenever Mem contains a valid string or blob representation, one of
2014620192
** the following flags must be set to determine the memory management
2014720193
** policy for Mem.z. The MEM_Term flag tells us whether or not the
@@ -20169,10 +20215,16 @@
2016920215
** Clear any existing type flags from a Mem and replace them with f
2017020216
*/
2017120217
#define MemSetTypeFlag(p, f) \
2017220218
((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
2017320219
20220
+/*
20221
+** True if Mem X is a NULL-nochng type.
20222
+*/
20223
+#define MemNullNochng(X) \
20224
+ ((X)->flags==(MEM_Null|MEM_Zero) && (X)->n==0 && (X)->u.nZero==0)
20225
+
2017420226
/*
2017520227
** Return true if a memory cell is not marked as invalid. This macro
2017620228
** is for use inside assert() statements only.
2017720229
*/
2017820230
#ifdef SQLITE_DEBUG
@@ -27118,10 +27170,13 @@
2711827170
db->mallocFailed = 1;
2711927171
if( db->nVdbeExec>0 ){
2712027172
db->u1.isInterrupted = 1;
2712127173
}
2712227174
db->lookaside.bDisable++;
27175
+ if( db->pParse ){
27176
+ db->pParse->rc = SQLITE_NOMEM_BKPT;
27177
+ }
2712327178
}
2712427179
}
2712527180
2712627181
/*
2712727182
** This routine reactivates the memory allocator and clears the
@@ -27311,11 +27366,12 @@
2731127366
** Set the StrAccum object to an error mode.
2731227367
*/
2731327368
static void setStrAccumError(StrAccum *p, u8 eError){
2731427369
assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
2731527370
p->accError = eError;
27316
- p->nAlloc = 0;
27371
+ if( p->mxAlloc ) sqlite3_str_reset(p);
27372
+ if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
2731727373
}
2731827374
2731927375
/*
2732027376
** Extra argument values from a PrintfArguments object
2732127377
*/
@@ -27341,10 +27397,11 @@
2734127397
** SQL from requesting large allocations using the precision or width
2734227398
** field of the printf() function.
2734327399
*/
2734427400
static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
2734527401
char *z;
27402
+ if( pAccum->accError ) return 0;
2734627403
if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
2734727404
setStrAccumError(pAccum, SQLITE_TOOBIG);
2734827405
return 0;
2734927406
}
2735027407
z = sqlite3DbMallocRaw(pAccum->db, n);
@@ -28060,13 +28117,12 @@
2806028117
testcase(p->accError==SQLITE_TOOBIG);
2806128118
testcase(p->accError==SQLITE_NOMEM);
2806228119
return 0;
2806328120
}
2806428121
if( p->mxAlloc==0 ){
28065
- N = p->nAlloc - p->nChar - 1;
2806628122
setStrAccumError(p, SQLITE_TOOBIG);
28067
- return N;
28123
+ return p->nAlloc - p->nChar - 1;
2806828124
}else{
2806928125
char *zOld = isMalloced(p) ? p->zText : 0;
2807028126
i64 szNew = p->nChar;
2807128127
szNew += N + 1;
2807228128
if( szNew+p->nChar<=p->mxAlloc ){
@@ -28134,11 +28190,11 @@
2813428190
*/
2813528191
SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
2813628192
assert( z!=0 || N==0 );
2813728193
assert( p->zText!=0 || p->nChar==0 || p->accError );
2813828194
assert( N>=0 );
28139
- assert( p->accError==0 || p->nAlloc==0 );
28195
+ assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
2814028196
if( p->nChar+N >= p->nAlloc ){
2814128197
enlargeAndAppend(p,z,N);
2814228198
}else if( N ){
2814328199
assert( p->zText );
2814428200
p->nChar += N;
@@ -28767,28 +28823,66 @@
2876728823
#ifndef SQLITE_OMIT_WINDOWFUNC
2876828824
/*
2876928825
** Generate a human-readable explanation for a Window object
2877028826
*/
2877128827
SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
28828
+ int nElement = 0;
28829
+ if( pWin->pFilter ){
28830
+ sqlite3TreeViewItem(pView, "FILTER", 1);
28831
+ sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
28832
+ sqlite3TreeViewPop(pView);
28833
+ }
2877228834
pView = sqlite3TreeViewPush(pView, more);
2877328835
if( pWin->zName ){
28774
- sqlite3TreeViewLine(pView, "OVER %s", pWin->zName);
28836
+ sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
2877528837
}else{
28776
- sqlite3TreeViewLine(pView, "OVER");
28838
+ sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
28839
+ }
28840
+ if( pWin->zBase ) nElement++;
28841
+ if( pWin->pOrderBy ) nElement++;
28842
+ if( pWin->eFrmType ) nElement++;
28843
+ if( pWin->eExclude ) nElement++;
28844
+ if( pWin->zBase ){
28845
+ sqlite3TreeViewPush(pView, (--nElement)>0);
28846
+ sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
28847
+ sqlite3TreeViewPop(pView);
2877728848
}
2877828849
if( pWin->pPartition ){
28779
- sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY");
28850
+ sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
2878028851
}
2878128852
if( pWin->pOrderBy ){
28782
- sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY");
28853
+ sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
2878328854
}
28784
- if( pWin->eType ){
28785
- sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0);
28855
+ if( pWin->eFrmType ){
28856
+ char zBuf[30];
28857
+ const char *zFrmType = "ROWS";
28858
+ if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
28859
+ if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
28860
+ sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
28861
+ pWin->bImplicitFrame ? " (implied)" : "");
28862
+ sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
2878628863
sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
2878728864
sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
2878828865
sqlite3TreeViewPop(pView);
2878928866
}
28867
+ if( pWin->eExclude ){
28868
+ char zBuf[30];
28869
+ const char *zExclude;
28870
+ switch( pWin->eExclude ){
28871
+ case TK_NO: zExclude = "NO OTHERS"; break;
28872
+ case TK_CURRENT: zExclude = "CURRENT ROW"; break;
28873
+ case TK_GROUP: zExclude = "GROUP"; break;
28874
+ case TK_TIES: zExclude = "TIES"; break;
28875
+ default:
28876
+ sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
28877
+ zExclude = zBuf;
28878
+ break;
28879
+ }
28880
+ sqlite3TreeViewPush(pView, 0);
28881
+ sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
28882
+ sqlite3TreeViewPop(pView);
28883
+ }
2879028884
sqlite3TreeViewPop(pView);
2879128885
}
2879228886
#endif /* SQLITE_OMIT_WINDOWFUNC */
2879328887
2879428888
#ifndef SQLITE_OMIT_WINDOWFUNC
@@ -29764,15 +29858,15 @@
2976429858
** This routine transforms the internal text encoding used by pMem to
2976529859
** desiredEnc. It is an error if the string is already of the desired
2976629860
** encoding, or if *pMem does not contain a string value.
2976729861
*/
2976829862
SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
29769
- int len; /* Maximum length of output string in bytes */
29770
- unsigned char *zOut; /* Output buffer */
29771
- unsigned char *zIn; /* Input iterator */
29772
- unsigned char *zTerm; /* End of input */
29773
- unsigned char *z; /* Output iterator */
29863
+ sqlite3_int64 len; /* Maximum length of output string in bytes */
29864
+ unsigned char *zOut; /* Output buffer */
29865
+ unsigned char *zIn; /* Input iterator */
29866
+ unsigned char *zTerm; /* End of input */
29867
+ unsigned char *z; /* Output iterator */
2977429868
unsigned int c;
2977529869
2977629870
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
2977729871
assert( pMem->flags&MEM_Str );
2977829872
assert( pMem->enc!=desiredEnc );
@@ -29817,18 +29911,18 @@
2981729911
** translating a 2-byte character to a 4-byte UTF-8 character.
2981829912
** A single byte is required for the output string
2981929913
** nul-terminator.
2982029914
*/
2982129915
pMem->n &= ~1;
29822
- len = pMem->n * 2 + 1;
29916
+ len = 2 * (sqlite3_int64)pMem->n + 1;
2982329917
}else{
2982429918
/* When converting from UTF-8 to UTF-16 the maximum growth is caused
2982529919
** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
2982629920
** character. Two bytes are required in the output buffer for the
2982729921
** nul-terminator.
2982829922
*/
29829
- len = pMem->n * 2 + 2;
29923
+ len = 2 * (sqlite3_int64)pMem->n + 2;
2983029924
}
2983129925
2983229926
/* Set zIn to point at the start of the input buffer and zTerm to point 1
2983329927
** byte past the end.
2983429928
**
@@ -30131,19 +30225,27 @@
3013130225
dummy += (unsigned)x;
3013230226
}
3013330227
#endif
3013430228
3013530229
/*
30136
-** Give a callback to the test harness that can be used to simulate faults
30137
-** in places where it is difficult or expensive to do so purely by means
30138
-** of inputs.
30139
-**
30140
-** The intent of the integer argument is to let the fault simulator know
30141
-** which of multiple sqlite3FaultSim() calls has been hit.
30142
-**
30143
-** Return whatever integer value the test callback returns, or return
30144
-** SQLITE_OK if no test callback is installed.
30230
+** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
30231
+** or to bypass normal error detection during testing in order to let
30232
+** execute proceed futher downstream.
30233
+**
30234
+** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
30235
+** sqlite3FaultSim() function only returns non-zero during testing.
30236
+**
30237
+** During testing, if the test harness has set a fault-sim callback using
30238
+** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
30239
+** each call to sqlite3FaultSim() is relayed to that application-supplied
30240
+** callback and the integer return value form the application-supplied
30241
+** callback is returned by sqlite3FaultSim().
30242
+**
30243
+** The integer argument to sqlite3FaultSim() is a code to identify which
30244
+** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
30245
+** should have a unique code. To prevent legacy testing applications from
30246
+** breaking, the codes should not be changed or reused.
3014530247
*/
3014630248
#ifndef SQLITE_UNTESTABLE
3014730249
SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
3014830250
int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
3014930251
return xCallback ? xCallback(iTest) : SQLITE_OK;
@@ -30323,10 +30425,23 @@
3032330425
sqlite3DbFree(db, pParse->zErrMsg);
3032430426
pParse->zErrMsg = zMsg;
3032530427
pParse->rc = SQLITE_ERROR;
3032630428
}
3032730429
}
30430
+
30431
+/*
30432
+** If database connection db is currently parsing SQL, then transfer
30433
+** error code errCode to that parser if the parser has not already
30434
+** encountered some other kind of error.
30435
+*/
30436
+SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
30437
+ Parse *pParse;
30438
+ if( db==0 || (pParse = db->pParse)==0 ) return errCode;
30439
+ pParse->rc = errCode;
30440
+ pParse->nErr++;
30441
+ return errCode;
30442
+}
3032830443
3032930444
/*
3033030445
** Convert an SQL-style quoted string into a normal string by removing
3033130446
** the quote characters. The conversion is done in-place. If the
3033230447
** input does not begin with a quote character, then this routine
@@ -31675,11 +31790,11 @@
3167531790
3167631791
nInt = nName/4 + 3;
3167731792
assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
3167831793
if( pIn==0 || pIn[1]+nInt > pIn[0] ){
3167931794
/* Enlarge the allocation */
31680
- int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
31795
+ sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
3168131796
VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
3168231797
if( pOut==0 ) return pIn;
3168331798
if( pIn==0 ) pOut[1] = 2;
3168431799
pIn = pOut;
3168531800
pIn[0] = nAlloc;
@@ -31881,11 +31996,11 @@
3188131996
const Hash *pH, /* The pH to be searched */
3188231997
const char *pKey, /* The key we are searching for */
3188331998
unsigned int *pHash /* Write the hash value here */
3188431999
){
3188532000
HashElem *elem; /* Used to loop thru the element list */
31886
- int count; /* Number of elements left to test */
32001
+ unsigned int count; /* Number of elements left to test */
3188732002
unsigned int h; /* The computed hash */
3188832003
static HashElem nullElement = { 0, 0, 0, 0 };
3188932004
3189032005
if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
3189132006
struct _ht *pEntry;
@@ -31929,12 +32044,12 @@
3192932044
if( pH->ht ){
3193032045
pEntry = &pH->ht[h];
3193132046
if( pEntry->chain==elem ){
3193232047
pEntry->chain = elem->next;
3193332048
}
32049
+ assert( pEntry->count>0 );
3193432050
pEntry->count--;
31935
- assert( pEntry->count>=0 );
3193632051
}
3193732052
sqlite3_free( elem );
3193832053
pH->count--;
3193932054
if( pH->count==0 ){
3194032055
assert( pH->first==0 );
@@ -32105,29 +32220,29 @@
3210532220
/* 87 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
3210632221
/* 88 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
3210732222
/* 89 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
3210832223
/* 90 */ "Column" OpHelp("r[P3]=PX"),
3210932224
/* 91 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
32110
- /* 92 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
32111
- /* 93 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
32112
- /* 94 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
32113
- /* 95 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
32114
- /* 96 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
32115
- /* 97 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
32116
- /* 98 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
32117
- /* 99 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
32118
- /* 100 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
32119
- /* 101 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
32120
- /* 102 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
32121
- /* 103 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
32122
- /* 104 */ "Count" OpHelp("r[P2]=count()"),
32123
- /* 105 */ "ReadCookie" OpHelp(""),
32124
- /* 106 */ "String8" OpHelp("r[P2]='P4'"),
32125
- /* 107 */ "SetCookie" OpHelp(""),
32126
- /* 108 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
32127
- /* 109 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
32128
- /* 110 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
32225
+ /* 92 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
32226
+ /* 93 */ "Count" OpHelp("r[P2]=count()"),
32227
+ /* 94 */ "ReadCookie" OpHelp(""),
32228
+ /* 95 */ "SetCookie" OpHelp(""),
32229
+ /* 96 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
32230
+ /* 97 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
32231
+ /* 98 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
32232
+ /* 99 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
32233
+ /* 100 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
32234
+ /* 101 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
32235
+ /* 102 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
32236
+ /* 103 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
32237
+ /* 104 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
32238
+ /* 105 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
32239
+ /* 106 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
32240
+ /* 107 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
32241
+ /* 108 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
32242
+ /* 109 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
32243
+ /* 110 */ "String8" OpHelp("r[P2]='P4'"),
3212932244
/* 111 */ "OpenDup" OpHelp(""),
3213032245
/* 112 */ "OpenAutoindex" OpHelp("nColumn=P2"),
3213132246
/* 113 */ "OpenEphemeral" OpHelp("nColumn=P2"),
3213232247
/* 114 */ "SorterOpen" OpHelp(""),
3213332248
/* 115 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
@@ -32154,15 +32269,15 @@
3215432269
/* 136 */ "Destroy" OpHelp(""),
3215532270
/* 137 */ "Clear" OpHelp(""),
3215632271
/* 138 */ "ResetSorter" OpHelp(""),
3215732272
/* 139 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
3215832273
/* 140 */ "SqlExec" OpHelp(""),
32159
- /* 141 */ "Real" OpHelp("r[P2]=P4"),
32160
- /* 142 */ "ParseSchema" OpHelp(""),
32161
- /* 143 */ "LoadAnalysis" OpHelp(""),
32162
- /* 144 */ "DropTable" OpHelp(""),
32163
- /* 145 */ "DropIndex" OpHelp(""),
32274
+ /* 141 */ "ParseSchema" OpHelp(""),
32275
+ /* 142 */ "LoadAnalysis" OpHelp(""),
32276
+ /* 143 */ "DropTable" OpHelp(""),
32277
+ /* 144 */ "DropIndex" OpHelp(""),
32278
+ /* 145 */ "Real" OpHelp("r[P2]=P4"),
3216432279
/* 146 */ "DropTrigger" OpHelp(""),
3216532280
/* 147 */ "IntegrityCk" OpHelp(""),
3216632281
/* 148 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
3216732282
/* 149 */ "Param" OpHelp(""),
3216832283
/* 150 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
@@ -49014,13 +49129,11 @@
4901449129
** Malloc function used by SQLite to obtain space from the buffer configured
4901549130
** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
4901649131
** exists, this function falls back to sqlite3Malloc().
4901749132
*/
4901849133
SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
49019
- /* During rebalance operations on a corrupt database file, it is sometimes
49020
- ** (rarely) possible to overread the temporary page buffer by a few bytes.
49021
- ** Enlarge the allocation slightly so that this does not cause problems. */
49134
+ assert( sz<=65536+8 ); /* These allocations are never very large */
4902249135
return pcache1Alloc(sz);
4902349136
}
4902449137
4902549138
/*
4902649139
** Free an allocated buffer obtained from sqlite3PageMalloc().
@@ -51300,10 +51413,13 @@
5130051413
** * the desired page is not currently in the wal file.
5130151414
*/
5130251415
SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
5130351416
if( pPager->fd->pMethods==0 ) return 0;
5130451417
if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
51418
+#ifdef SQLITE_HAS_CODEC
51419
+ if( pPager->xCodec!=0 ) return 0;
51420
+#endif
5130551421
#ifndef SQLITE_OMIT_WAL
5130651422
if( pPager->pWal ){
5130751423
u32 iRead = 0;
5130851424
int rc;
5130951425
rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
@@ -54249,12 +54365,18 @@
5424954365
5425054366
if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
5425154367
rc = sqlite3OsFileSize(pPager->fd, &nByte);
5425254368
}
5425354369
if( rc==SQLITE_OK ){
54254
- pNew = (char *)sqlite3PageMalloc(pageSize);
54255
- if( !pNew ) rc = SQLITE_NOMEM_BKPT;
54370
+ /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
54371
+ * cell header parser will never run off the end of the allocation */
54372
+ pNew = (char *)sqlite3PageMalloc(pageSize+8);
54373
+ if( !pNew ){
54374
+ rc = SQLITE_NOMEM_BKPT;
54375
+ }else{
54376
+ memset(pNew+pageSize, 0, 8);
54377
+ }
5425654378
}
5425754379
5425854380
if( rc==SQLITE_OK ){
5425954381
pager_reset(pPager);
5426054382
rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
@@ -57631,12 +57753,16 @@
5763157753
** page pgno before the 'move' operation, it needs to be retained
5763257754
** for the page moved there.
5763357755
*/
5763457756
pPg->flags &= ~PGHDR_NEED_SYNC;
5763557757
pPgOld = sqlite3PagerLookup(pPager, pgno);
57636
- assert( !pPgOld || pPgOld->nRef==1 );
57758
+ assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
5763757759
if( pPgOld ){
57760
+ if( pPgOld->nRef>1 ){
57761
+ sqlite3PagerUnrefNotNull(pPgOld);
57762
+ return SQLITE_CORRUPT_BKPT;
57763
+ }
5763857764
pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
5763957765
if( pPager->tempFile ){
5764057766
/* Do not discard pages from an in-memory database since we might
5764157767
** need to rollback later. Just move the page out of the way. */
5764257768
sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
@@ -58160,11 +58286,11 @@
5816058286
** Release a lock obtained by an earlier successful call to
5816158287
** sqlite3PagerSnapshotCheck().
5816258288
*/
5816358289
SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
5816458290
assert( pPager->pWal );
58165
- return sqlite3WalSnapshotUnlock(pPager->pWal);
58291
+ sqlite3WalSnapshotUnlock(pPager->pWal);
5816658292
}
5816758293
5816858294
#endif /* SQLITE_ENABLE_SNAPSHOT */
5816958295
#endif /* !SQLITE_OMIT_WAL */
5817058296
@@ -58761,11 +58887,11 @@
5876158887
){
5876258888
int rc = SQLITE_OK;
5876358889
5876458890
/* Enlarge the pWal->apWiData[] array if required */
5876558891
if( pWal->nWiData<=iPage ){
58766
- int nByte = sizeof(u32*)*(iPage+1);
58892
+ sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
5876758893
volatile u32 **apNew;
5876858894
apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
5876958895
if( !apNew ){
5877058896
*ppPage = 0;
5877158897
return SQLITE_NOMEM_BKPT;
@@ -58865,10 +58991,11 @@
5886558991
s1 = s2 = 0;
5886658992
}
5886758993
5886858994
assert( nByte>=8 );
5886958995
assert( (nByte&0x00000007)==0 );
58996
+ assert( nByte<=65536 );
5887058997
5887158998
if( nativeCksum ){
5887258999
do {
5887359000
s1 += *aData++ + s2;
5887459001
s2 += *aData++ + s1;
@@ -59172,10 +59299,11 @@
5917259299
static void walCleanupHash(Wal *pWal){
5917359300
WalHashLoc sLoc; /* Hash table location */
5917459301
int iLimit = 0; /* Zero values greater than this */
5917559302
int nByte; /* Number of bytes to zero in aPgno[] */
5917659303
int i; /* Used to iterate through aHash[] */
59304
+ int rc; /* Return code form walHashGet() */
5917759305
5917859306
assert( pWal->writeLock );
5917959307
testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
5918059308
testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
5918159309
testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
@@ -59182,15 +59310,16 @@
5918259310
5918359311
if( pWal->hdr.mxFrame==0 ) return;
5918459312
5918559313
/* Obtain pointers to the hash-table and page-number array containing
5918659314
** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
59187
- ** that the page said hash-table and array reside on is already mapped.
59315
+ ** that the page said hash-table and array reside on is already mapped.(1)
5918859316
*/
5918959317
assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
5919059318
assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
59191
- walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
59319
+ rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
59320
+ if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
5919259321
5919359322
/* Zero all hash-table entries that correspond to frame numbers greater
5919459323
** than pWal->hdr.mxFrame.
5919559324
*/
5919659325
iLimit = pWal->hdr.mxFrame - sLoc.iZero;
@@ -59800,11 +59929,11 @@
5980059929
*/
5980159930
static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
5980259931
WalIterator *p; /* Return value */
5980359932
int nSegment; /* Number of segments to merge */
5980459933
u32 iLast; /* Last frame in log */
59805
- int nByte; /* Number of bytes to allocate */
59934
+ sqlite3_int64 nByte; /* Number of bytes to allocate */
5980659935
int i; /* Iterator variable */
5980759936
ht_slot *aTmp; /* Temp space used by merge-sort */
5980859937
int rc = SQLITE_OK; /* Return Code */
5980959938
5981059939
/* This routine only runs while holding the checkpoint lock. And
@@ -62336,11 +62465,11 @@
6233662465
u8 max1bytePayload; /* min(maxLocal,127) */
6233762466
u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
6233862467
u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
6233962468
u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
6234062469
u16 cellOffset; /* Index in aData of first cell pointer */
62341
- u16 nFree; /* Number of free bytes on the page */
62470
+ int nFree; /* Number of free bytes on the page. -1 for unknown */
6234262471
u16 nCell; /* Number of cells on this page, local and ovfl */
6234362472
u16 maskPage; /* Mask for page offset */
6234462473
u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
6234562474
** non-overflow cell */
6234662475
u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
@@ -63890,18 +64019,22 @@
6389064019
** at most one effective restoreCursorPosition() call after each
6389164020
** saveCursorPosition().
6389264021
*/
6389364022
static int btreeRestoreCursorPosition(BtCursor *pCur){
6389464023
int rc;
63895
- int skipNext;
64024
+ int skipNext = 0;
6389664025
assert( cursorOwnsBtShared(pCur) );
6389764026
assert( pCur->eState>=CURSOR_REQUIRESEEK );
6389864027
if( pCur->eState==CURSOR_FAULT ){
6389964028
return pCur->skipNext;
6390064029
}
6390164030
pCur->eState = CURSOR_INVALID;
63902
- rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
64031
+ if( sqlite3FaultSim(410) ){
64032
+ rc = SQLITE_IOERR;
64033
+ }else{
64034
+ rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
64035
+ }
6390364036
if( rc==SQLITE_OK ){
6390464037
sqlite3_free(pCur->pKey);
6390564038
pCur->pKey = 0;
6390664039
assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
6390764040
if( skipNext ) pCur->skipNext = skipNext;
@@ -64478,11 +64611,11 @@
6447864611
temp = 0;
6447964612
src = data = pPage->aData;
6448064613
hdr = pPage->hdrOffset;
6448164614
cellOffset = pPage->cellOffset;
6448264615
nCell = pPage->nCell;
64483
- assert( nCell==get2byte(&data[hdr+3]) );
64616
+ assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
6448464617
iCellFirst = cellOffset + 2*nCell;
6448564618
usableSize = pPage->pBt->usableSize;
6448664619
6448764620
/* This block handles pages with two or fewer free blocks and nMaxFrag
6448864621
** or fewer fragmented bytes. In this case it is faster to move the
@@ -64489,15 +64622,11 @@
6448964622
** two (or one) blocks of cells using memmove() and add the required
6449064623
** offsets to each pointer in the cell-pointer array than it is to
6449164624
** reconstruct the entire page. */
6449264625
if( (int)data[hdr+7]<=nMaxFrag ){
6449364626
int iFree = get2byte(&data[hdr+1]);
64494
-
64495
- /* If the initial freeblock offset were out of bounds, that would
64496
- ** have been detected by btreeInitPage() when it was computing the
64497
- ** number of free bytes on the page. */
64498
- assert( iFree<=usableSize-4 );
64627
+ if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
6449964628
if( iFree ){
6450064629
int iFree2 = get2byte(&data[iFree]);
6450164630
if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
6450264631
if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
6450364632
u8 *pEnd = &data[cellOffset + nCell*2];
@@ -64512,11 +64641,14 @@
6451264641
if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
6451364642
sz2 = get2byte(&data[iFree2+2]);
6451464643
if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
6451564644
memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
6451664645
sz += sz2;
64646
+ }else if( iFree+sz>usableSize ){
64647
+ return SQLITE_CORRUPT_PAGE(pPage);
6451764648
}
64649
+
6451864650
cbrk = top+sz;
6451964651
assert( cbrk+(iFree-top) <= usableSize );
6452064652
memmove(&data[cbrk], &data[top], iFree-top);
6452164653
for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
6452264654
pc = get2byte(pAddr);
@@ -64563,10 +64695,11 @@
6456364695
memcpy(&data[cbrk], &src[pc], size);
6456464696
}
6456564697
data[hdr+7] = 0;
6456664698
6456764699
defragment_out:
64700
+ assert( pPage->nFree>=0 );
6456864701
if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
6456964702
return SQLITE_CORRUPT_PAGE(pPage);
6457064703
}
6457164704
assert( cbrk>=iCellFirst );
6457264705
put2byte(&data[hdr+5], cbrk);
@@ -64590,54 +64723,61 @@
6459064723
** Slots on the free list that are between 1 and 3 bytes larger than nByte
6459164724
** will be ignored if adding the extra space to the fragmentation count
6459264725
** causes the fragmentation count to exceed 60.
6459364726
*/
6459464727
static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
64595
- const int hdr = pPg->hdrOffset;
64596
- u8 * const aData = pPg->aData;
64597
- int iAddr = hdr + 1;
64598
- int pc = get2byte(&aData[iAddr]);
64599
- int x;
64600
- int usableSize = pPg->pBt->usableSize;
64601
- int size; /* Size of the free slot */
64728
+ const int hdr = pPg->hdrOffset; /* Offset to page header */
64729
+ u8 * const aData = pPg->aData; /* Page data */
64730
+ int iAddr = hdr + 1; /* Address of ptr to pc */
64731
+ int pc = get2byte(&aData[iAddr]); /* Address of a free slot */
64732
+ int x; /* Excess size of the slot */
64733
+ int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
64734
+ int size; /* Size of the free slot */
6460264735
6460364736
assert( pc>0 );
64604
- while( pc<=usableSize-4 ){
64737
+ while( pc<=maxPC ){
6460564738
/* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
6460664739
** freeblock form a big-endian integer which is the size of the freeblock
6460764740
** in bytes, including the 4-byte header. */
6460864741
size = get2byte(&aData[pc+2]);
6460964742
if( (x = size - nByte)>=0 ){
6461064743
testcase( x==4 );
6461164744
testcase( x==3 );
64612
- if( size+pc > usableSize ){
64613
- *pRc = SQLITE_CORRUPT_PAGE(pPg);
64614
- return 0;
64615
- }else if( x<4 ){
64745
+ if( x<4 ){
6461664746
/* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
6461764747
** number of bytes in fragments may not exceed 60. */
6461864748
if( aData[hdr+7]>57 ) return 0;
6461964749
6462064750
/* Remove the slot from the free-list. Update the number of
6462164751
** fragmented bytes within the page. */
6462264752
memcpy(&aData[iAddr], &aData[pc], 2);
6462364753
aData[hdr+7] += (u8)x;
64754
+ }else if( x+pc > maxPC ){
64755
+ /* This slot extends off the end of the usable part of the page */
64756
+ *pRc = SQLITE_CORRUPT_PAGE(pPg);
64757
+ return 0;
6462464758
}else{
6462564759
/* The slot remains on the free-list. Reduce its size to account
64626
- ** for the portion used by the new allocation. */
64760
+ ** for the portion used by the new allocation. */
6462764761
put2byte(&aData[pc+2], x);
6462864762
}
6462964763
return &aData[pc + x];
6463064764
}
6463164765
iAddr = pc;
6463264766
pc = get2byte(&aData[pc]);
64633
- if( pc<iAddr+size ) break;
64767
+ if( pc<=iAddr+size ){
64768
+ if( pc ){
64769
+ /* The next slot in the chain is not past the end of the current slot */
64770
+ *pRc = SQLITE_CORRUPT_PAGE(pPg);
64771
+ }
64772
+ return 0;
64773
+ }
6463464774
}
64635
- if( pc ){
64775
+ if( pc>maxPC+nByte-4 ){
64776
+ /* The free slot chain extends off the end of the page */
6463664777
*pRc = SQLITE_CORRUPT_PAGE(pPg);
6463764778
}
64638
-
6463964779
return 0;
6464064780
}
6464164781
6464264782
/*
6464364783
** Allocate nByte bytes of space from within the B-Tree page passed
@@ -64683,13 +64823,13 @@
6468364823
}else{
6468464824
return SQLITE_CORRUPT_PAGE(pPage);
6468564825
}
6468664826
}
6468764827
64688
- /* If there is enough space between gap and top for one more cell pointer
64689
- ** array entry offset, and if the freelist is not empty, then search the
64690
- ** freelist looking for a free slot big enough to satisfy the request.
64828
+ /* If there is enough space between gap and top for one more cell pointer,
64829
+ ** and if the freelist is not empty, then search the
64830
+ ** freelist looking for a slot big enough to satisfy the request.
6469164831
*/
6469264832
testcase( gap+2==top );
6469364833
testcase( gap+1==top );
6469464834
testcase( gap==top );
6469564835
if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
@@ -64707,19 +64847,20 @@
6470764847
** to see if defragmentation is necessary.
6470864848
*/
6470964849
testcase( gap+2+nByte==top );
6471064850
if( gap+2+nByte>top ){
6471164851
assert( pPage->nCell>0 || CORRUPT_DB );
64852
+ assert( pPage->nFree>=0 );
6471264853
rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
6471364854
if( rc ) return rc;
6471464855
top = get2byteNotZero(&data[hdr+5]);
6471564856
assert( gap+2+nByte<=top );
6471664857
}
6471764858
6471864859
6471964860
/* Allocate memory from the gap in between the cell pointer array
64720
- ** and the cell content area. The btreeInitPage() call has already
64861
+ ** and the cell content area. The btreeComputeFreeSpace() call has already
6472164862
** validated the freelist. Given that the freelist is valid, there
6472264863
** is no way that the allocation can extend off the end of the page.
6472364864
** The assert() below verifies the previous sentence.
6472464865
*/
6472564866
top -= nByte;
@@ -64734,11 +64875,11 @@
6473464875
** The first byte of the new free block is pPage->aData[iStart]
6473564876
** and the size of the block is iSize bytes.
6473664877
**
6473764878
** Adjacent freeblocks are coalesced.
6473864879
**
64739
-** Note that even though the freeblock list was checked by btreeInitPage(),
64880
+** Even though the freeblock list was checked by btreeComputeFreeSpace(),
6474064881
** that routine will not detect overlap between cells or freeblocks. Nor
6474164882
** does it detect cells or freeblocks that encrouch into the reserved bytes
6474264883
** at the end of the page. So do additional corruption checks inside this
6474364884
** routine and return SQLITE_CORRUPT if any problems are found.
6474464885
*/
@@ -64896,25 +65037,18 @@
6489665037
pPage->max1bytePayload = pBt->max1bytePayload;
6489765038
return SQLITE_OK;
6489865039
}
6489965040
6490065041
/*
64901
-** Initialize the auxiliary information for a disk block.
64902
-**
64903
-** Return SQLITE_OK on success. If we see that the page does
64904
-** not contain a well-formed database page, then return
64905
-** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
64906
-** guarantee that the page is well-formed. It only shows that
64907
-** we failed to detect any corruption.
65042
+** Compute the amount of freespace on the page. In other words, fill
65043
+** in the pPage->nFree field.
6490865044
*/
64909
-static int btreeInitPage(MemPage *pPage){
65045
+static int btreeComputeFreeSpace(MemPage *pPage){
6491065046
int pc; /* Address of a freeblock within pPage->aData[] */
6491165047
u8 hdr; /* Offset to beginning of page header */
6491265048
u8 *data; /* Equal to pPage->aData */
64913
- BtShared *pBt; /* The main btree structure */
6491465049
int usableSize; /* Amount of usable space on each page */
64915
- u16 cellOffset; /* Offset from start of page to first cell pointer */
6491665050
int nFree; /* Number of unused bytes on the page */
6491765051
int top; /* First byte of the cell content area */
6491865052
int iCellFirst; /* First allowable cell or freeblock offset */
6491965053
int iCellLast; /* Last possible cell or freeblock offset */
6492065054
@@ -64922,75 +65056,22 @@
6492265056
assert( pPage->pBt->db!=0 );
6492365057
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6492465058
assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
6492565059
assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
6492665060
assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
64927
- assert( pPage->isInit==0 );
65061
+ assert( pPage->isInit==1 );
65062
+ assert( pPage->nFree<0 );
6492865063
64929
- pBt = pPage->pBt;
65064
+ usableSize = pPage->pBt->usableSize;
6493065065
hdr = pPage->hdrOffset;
6493165066
data = pPage->aData;
64932
- /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
64933
- ** the b-tree page type. */
64934
- if( decodeFlags(pPage, data[hdr]) ){
64935
- return SQLITE_CORRUPT_PAGE(pPage);
64936
- }
64937
- assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
64938
- pPage->maskPage = (u16)(pBt->pageSize - 1);
64939
- pPage->nOverflow = 0;
64940
- usableSize = pBt->usableSize;
64941
- pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
64942
- pPage->aDataEnd = &data[usableSize];
64943
- pPage->aCellIdx = &data[cellOffset];
64944
- pPage->aDataOfst = &data[pPage->childPtrSize];
6494565067
/* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
6494665068
** the start of the cell content area. A zero value for this integer is
6494765069
** interpreted as 65536. */
6494865070
top = get2byteNotZero(&data[hdr+5]);
64949
- /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
64950
- ** number of cells on the page. */
64951
- pPage->nCell = get2byte(&data[hdr+3]);
64952
- if( pPage->nCell>MX_CELL(pBt) ){
64953
- /* To many cells for a single page. The page must be corrupt */
64954
- return SQLITE_CORRUPT_PAGE(pPage);
64955
- }
64956
- testcase( pPage->nCell==MX_CELL(pBt) );
64957
- /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
64958
- ** possible for a root page of a table that contains no rows) then the
64959
- ** offset to the cell content area will equal the page size minus the
64960
- ** bytes of reserved space. */
64961
- assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
64962
-
64963
- /* A malformed database page might cause us to read past the end
64964
- ** of page when parsing a cell.
64965
- **
64966
- ** The following block of code checks early to see if a cell extends
64967
- ** past the end of a page boundary and causes SQLITE_CORRUPT to be
64968
- ** returned if it does.
64969
- */
64970
- iCellFirst = cellOffset + 2*pPage->nCell;
65071
+ iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
6497165072
iCellLast = usableSize - 4;
64972
- if( pBt->db->flags & SQLITE_CellSizeCk ){
64973
- int i; /* Index into the cell pointer array */
64974
- int sz; /* Size of a cell */
64975
-
64976
- if( !pPage->leaf ) iCellLast--;
64977
- for(i=0; i<pPage->nCell; i++){
64978
- pc = get2byteAligned(&data[cellOffset+i*2]);
64979
- testcase( pc==iCellFirst );
64980
- testcase( pc==iCellLast );
64981
- if( pc<iCellFirst || pc>iCellLast ){
64982
- return SQLITE_CORRUPT_PAGE(pPage);
64983
- }
64984
- sz = pPage->xCellSize(pPage, &data[pc]);
64985
- testcase( pc+sz==usableSize );
64986
- if( pc+sz>usableSize ){
64987
- return SQLITE_CORRUPT_PAGE(pPage);
64988
- }
64989
- }
64990
- if( !pPage->leaf ) iCellLast++;
64991
- }
6499265073
6499365074
/* Compute the total free space on the page
6499465075
** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
6499565076
** start of the first freeblock on the page, or is zero if there are no
6499665077
** freeblocks. */
@@ -65034,11 +65115,104 @@
6503465115
*/
6503565116
if( nFree>usableSize ){
6503665117
return SQLITE_CORRUPT_PAGE(pPage);
6503765118
}
6503865119
pPage->nFree = (u16)(nFree - iCellFirst);
65120
+ return SQLITE_OK;
65121
+}
65122
+
65123
+/*
65124
+** Do additional sanity check after btreeInitPage() if
65125
+** PRAGMA cell_size_check=ON
65126
+*/
65127
+static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
65128
+ int iCellFirst; /* First allowable cell or freeblock offset */
65129
+ int iCellLast; /* Last possible cell or freeblock offset */
65130
+ int i; /* Index into the cell pointer array */
65131
+ int sz; /* Size of a cell */
65132
+ int pc; /* Address of a freeblock within pPage->aData[] */
65133
+ u8 *data; /* Equal to pPage->aData */
65134
+ int usableSize; /* Maximum usable space on the page */
65135
+ int cellOffset; /* Start of cell content area */
65136
+
65137
+ iCellFirst = pPage->cellOffset + 2*pPage->nCell;
65138
+ usableSize = pPage->pBt->usableSize;
65139
+ iCellLast = usableSize - 4;
65140
+ data = pPage->aData;
65141
+ cellOffset = pPage->cellOffset;
65142
+ if( !pPage->leaf ) iCellLast--;
65143
+ for(i=0; i<pPage->nCell; i++){
65144
+ pc = get2byteAligned(&data[cellOffset+i*2]);
65145
+ testcase( pc==iCellFirst );
65146
+ testcase( pc==iCellLast );
65147
+ if( pc<iCellFirst || pc>iCellLast ){
65148
+ return SQLITE_CORRUPT_PAGE(pPage);
65149
+ }
65150
+ sz = pPage->xCellSize(pPage, &data[pc]);
65151
+ testcase( pc+sz==usableSize );
65152
+ if( pc+sz>usableSize ){
65153
+ return SQLITE_CORRUPT_PAGE(pPage);
65154
+ }
65155
+ }
65156
+ return SQLITE_OK;
65157
+}
65158
+
65159
+/*
65160
+** Initialize the auxiliary information for a disk block.
65161
+**
65162
+** Return SQLITE_OK on success. If we see that the page does
65163
+** not contain a well-formed database page, then return
65164
+** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
65165
+** guarantee that the page is well-formed. It only shows that
65166
+** we failed to detect any corruption.
65167
+*/
65168
+static int btreeInitPage(MemPage *pPage){
65169
+ u8 *data; /* Equal to pPage->aData */
65170
+ BtShared *pBt; /* The main btree structure */
65171
+
65172
+ assert( pPage->pBt!=0 );
65173
+ assert( pPage->pBt->db!=0 );
65174
+ assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65175
+ assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
65176
+ assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
65177
+ assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
65178
+ assert( pPage->isInit==0 );
65179
+
65180
+ pBt = pPage->pBt;
65181
+ data = pPage->aData + pPage->hdrOffset;
65182
+ /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
65183
+ ** the b-tree page type. */
65184
+ if( decodeFlags(pPage, data[0]) ){
65185
+ return SQLITE_CORRUPT_PAGE(pPage);
65186
+ }
65187
+ assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
65188
+ pPage->maskPage = (u16)(pBt->pageSize - 1);
65189
+ pPage->nOverflow = 0;
65190
+ pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
65191
+ pPage->aCellIdx = data + pPage->childPtrSize + 8;
65192
+ pPage->aDataEnd = pPage->aData + pBt->usableSize;
65193
+ pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
65194
+ /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
65195
+ ** number of cells on the page. */
65196
+ pPage->nCell = get2byte(&data[3]);
65197
+ if( pPage->nCell>MX_CELL(pBt) ){
65198
+ /* To many cells for a single page. The page must be corrupt */
65199
+ return SQLITE_CORRUPT_PAGE(pPage);
65200
+ }
65201
+ testcase( pPage->nCell==MX_CELL(pBt) );
65202
+ /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
65203
+ ** possible for a root page of a table that contains no rows) then the
65204
+ ** offset to the cell content area will equal the page size minus the
65205
+ ** bytes of reserved space. */
65206
+ assert( pPage->nCell>0
65207
+ || get2byteNotZero(&data[5])==(int)pBt->usableSize
65208
+ || CORRUPT_DB );
65209
+ pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
6503965210
pPage->isInit = 1;
65211
+ if( pBt->db->flags & SQLITE_CellSizeCk ){
65212
+ return btreeCellSizeCheck(pPage);
65213
+ }
6504065214
return SQLITE_OK;
6504165215
}
6504265216
6504365217
/*
6504465218
** Set up a raw page so that it looks like a database page holding
@@ -65177,38 +65351,38 @@
6517765351
assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
6517865352
assert( pCur==0 || pCur->iPage>0 );
6517965353
6518065354
if( pgno>btreePagecount(pBt) ){
6518165355
rc = SQLITE_CORRUPT_BKPT;
65182
- goto getAndInitPage_error;
65356
+ goto getAndInitPage_error1;
6518365357
}
6518465358
rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
6518565359
if( rc ){
65186
- goto getAndInitPage_error;
65360
+ goto getAndInitPage_error1;
6518765361
}
6518865362
*ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
6518965363
if( (*ppPage)->isInit==0 ){
6519065364
btreePageFromDbPage(pDbPage, pgno, pBt);
6519165365
rc = btreeInitPage(*ppPage);
6519265366
if( rc!=SQLITE_OK ){
65193
- releasePage(*ppPage);
65194
- goto getAndInitPage_error;
65367
+ goto getAndInitPage_error2;
6519565368
}
6519665369
}
6519765370
assert( (*ppPage)->pgno==pgno );
6519865371
assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
6519965372
6520065373
/* If obtaining a child page for a cursor, we must verify that the page is
6520165374
** compatible with the root page. */
6520265375
if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
6520365376
rc = SQLITE_CORRUPT_PGNO(pgno);
65204
- releasePage(*ppPage);
65205
- goto getAndInitPage_error;
65377
+ goto getAndInitPage_error2;
6520665378
}
6520765379
return SQLITE_OK;
6520865380
65209
-getAndInitPage_error:
65381
+getAndInitPage_error2:
65382
+ releasePage(*ppPage);
65383
+getAndInitPage_error1:
6521065384
if( pCur ){
6521165385
pCur->iPage--;
6521265386
pCur->pPage = pCur->apPage[pCur->iPage];
6521365387
}
6521465388
testcase( pgno==0 );
@@ -68285,27 +68459,10 @@
6828568459
rc = SQLITE_OK;
6828668460
}
6828768461
return rc;
6828868462
}
6828968463
68290
-/*
68291
-** This function is a no-op if cursor pCur does not point to a valid row.
68292
-** Otherwise, if pCur is valid, configure it so that the next call to
68293
-** sqlite3BtreeNext() is a no-op.
68294
-*/
68295
-#ifndef SQLITE_OMIT_WINDOWFUNC
68296
-SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor *pCur){
68297
- /* We believe that the cursor must always be in the valid state when
68298
- ** this routine is called, but the proof is difficult, so we add an
68299
- ** ALWaYS() test just in case we are wrong. */
68300
- if( ALWAYS(pCur->eState==CURSOR_VALID) ){
68301
- pCur->eState = CURSOR_SKIPNEXT;
68302
- pCur->skipNext = 1;
68303
- }
68304
-}
68305
-#endif /* SQLITE_OMIT_WINDOWFUNC */
68306
-
6830768464
/* Move the cursor to the last entry in the table. Return SQLITE_OK
6830868465
** on success. Set *pRes to 0 if the cursor actually points to something
6830968466
** or set *pRes to 1 if the table is empty.
6831068467
*/
6831168468
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
@@ -68567,11 +68724,11 @@
6856768724
pCur->curFlags &= ~BTCF_ValidOvfl;
6856868725
if( rc ){
6856968726
sqlite3_free(pCellKey);
6857068727
goto moveto_finish;
6857168728
}
68572
- c = xRecordCompare(nCell, pCellKey, pIdxKey);
68729
+ c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
6857368730
sqlite3_free(pCellKey);
6857468731
}
6857568732
assert(
6857668733
(pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
6857768734
&& (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
@@ -69199,17 +69356,19 @@
6919969356
MemPage *pTrunk = 0; /* Free-list trunk page */
6920069357
Pgno iTrunk = 0; /* Page number of free-list trunk page */
6920169358
MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
6920269359
MemPage *pPage; /* Page being freed. May be NULL. */
6920369360
int rc; /* Return Code */
69204
- int nFree; /* Initial number of pages on free-list */
69361
+ u32 nFree; /* Initial number of pages on free-list */
6920569362
6920669363
assert( sqlite3_mutex_held(pBt->mutex) );
6920769364
assert( CORRUPT_DB || iPage>1 );
6920869365
assert( !pMemPage || pMemPage->pgno==iPage );
6920969366
69210
- if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
69367
+ if( iPage<2 || iPage>pBt->nPage ){
69368
+ return SQLITE_CORRUPT_BKPT;
69369
+ }
6921169370
if( pMemPage ){
6921269371
pPage = pMemPage;
6921369372
sqlite3PagerRef(pPage->pDbPage);
6921469373
}else{
6921569374
pPage = btreePageLookup(pBt, iPage);
@@ -69616,10 +69775,11 @@
6961669775
if( *pRC ) return;
6961769776
assert( idx>=0 && idx<pPage->nCell );
6961869777
assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
6961969778
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6962069779
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69780
+ assert( pPage->nFree>=0 );
6962169781
data = pPage->aData;
6962269782
ptr = &pPage->aCellIdx[2*idx];
6962369783
pc = get2byte(ptr);
6962469784
hdr = pPage->hdrOffset;
6962569785
testcase( pc==get2byte(&data[hdr+5]) );
@@ -69686,10 +69846,11 @@
6968669846
** malformed cell from a leaf page to an interior page, if the cell size
6968769847
** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
6968869848
** might be less than 8 (leaf-size + pointer) on the interior node. Hence
6968969849
** the term after the || in the following assert(). */
6969069850
assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
69851
+ assert( pPage->nFree>=0 );
6969169852
if( pPage->nOverflow || sz+2>pPage->nFree ){
6969269853
if( pTemp ){
6969369854
memcpy(pTemp, pCell, sz);
6969469855
pCell = pTemp;
6969569856
}
@@ -69743,11 +69904,11 @@
6974369904
memmove(pIns+2, pIns, 2*(pPage->nCell - i));
6974469905
put2byte(pIns, idx);
6974569906
pPage->nCell++;
6974669907
/* increment the cell count */
6974769908
if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
69748
- assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
69909
+ assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
6974969910
#ifndef SQLITE_OMIT_AUTOVACUUM
6975069911
if( pPage->pBt->autoVacuum ){
6975169912
/* The cell may contain a pointer to an overflow page. If so, write
6975269913
** the entry for the overflow page into the pointer map.
6975369914
*/
@@ -69830,12 +69991,17 @@
6983069991
**
6983169992
** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
6983269993
** are used and they point to the leaf pages only, and the ixNx value are:
6983369994
**
6983469995
** ixNx[0] = Number of cells in Child-1.
69835
-** ixNx[1] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
69836
-** ixNx[2] = Number of cells in Child-1 and Child-2 + both divider cells
69996
+** ixNx[1] = Number of cells in Child-1 and Child-2.
69997
+** ixNx[2] = Total number of cells.
69998
+**
69999
+** Sometimes when deleting, a child page can have zero cells. In those
70000
+** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
70001
+** entries, shift down. The end result is that each ixNx[] entry should
70002
+** be larger than the previous
6983770003
*/
6983870004
typedef struct CellArray CellArray;
6983970005
struct CellArray {
6984070006
int nCell; /* Number of cells in apCell[] */
6984170007
MemPage *pRef; /* Reference page */
@@ -70160,12 +70326,13 @@
7016070326
/* Add any overflow cells */
7016170327
for(i=0; i<pPg->nOverflow; i++){
7016270328
int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
7016370329
if( iCell>=0 && iCell<nNew ){
7016470330
pCellptr = &pPg->aCellIdx[iCell * 2];
70165
- assert( nCell>=iCell );
70166
- memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
70331
+ if( nCell>iCell ){
70332
+ memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
70333
+ }
7016770334
nCell++;
7016870335
if( pageInsertArray(
7016970336
pPg, pBegin, &pData, pCellptr,
7017070337
iCell+iNew, 1, pCArray
7017170338
) ) goto editpage_fail;
@@ -70237,12 +70404,14 @@
7023770404
Pgno pgnoNew; /* Page number of pNew */
7023870405
7023970406
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7024070407
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7024170408
assert( pPage->nOverflow==1 );
70242
-
70409
+
7024370410
if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
70411
+ assert( pPage->nFree>=0 );
70412
+ assert( pParent->nFree>=0 );
7024470413
7024570414
/* Allocate a new page. This page will become the right-sibling of
7024670415
** pPage. Make the parent page writable, so that the new divider cell
7024770416
** may be inserted. If both these operations are successful, proceed.
7024870417
*/
@@ -70408,10 +70577,11 @@
7040870577
** fairly obscure circumstances, even though it is a copy of initialized
7040970578
** page pFrom.
7041070579
*/
7041170580
pTo->isInit = 0;
7041270581
rc = btreeInitPage(pTo);
70582
+ if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
7041370583
if( rc!=SQLITE_OK ){
7041470584
*pRC = rc;
7041570585
return;
7041670586
}
7041770587
@@ -70516,10 +70686,11 @@
7051670686
assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
7051770687
7051870688
if( !aOvflSpace ){
7051970689
return SQLITE_NOMEM_BKPT;
7052070690
}
70691
+ assert( pParent->nFree>=0 );
7052170692
7052270693
/* Find the sibling pages to balance. Also locate the cells in pParent
7052370694
** that divide the siblings. An attempt is made to find NN siblings on
7052470695
** either side of pPage. More siblings are taken from one side, however,
7052570696
** if there are fewer than NN siblings on the other side. If pParent
@@ -70555,11 +70726,17 @@
7055570726
rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
7055670727
if( rc ){
7055770728
memset(apOld, 0, (i+1)*sizeof(MemPage*));
7055870729
goto balance_cleanup;
7055970730
}
70560
- nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
70731
+ if( apOld[i]->nFree<0 ){
70732
+ rc = btreeComputeFreeSpace(apOld[i]);
70733
+ if( rc ){
70734
+ memset(apOld, 0, (i)*sizeof(MemPage*));
70735
+ goto balance_cleanup;
70736
+ }
70737
+ }
7056170738
if( (i--)==0 ) break;
7056270739
7056370740
if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
7056470741
apDiv[i] = pParent->apOvfl[0];
7056570742
pgno = get4byte(apDiv[i]);
@@ -70599,10 +70776,11 @@
7059970776
}
7060070777
}
7060170778
7060270779
/* Make nMaxCells a multiple of 4 in order to preserve 8-byte
7060370780
** alignment */
70781
+ nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
7060470782
nMaxCells = (nMaxCells + 3)&~3;
7060570783
7060670784
/*
7060770785
** Allocate space for memory structures
7060870786
*/
@@ -70609,11 +70787,11 @@
7060970787
szScratch =
7061070788
nMaxCells*sizeof(u8*) /* b.apCell */
7061170789
+ nMaxCells*sizeof(u16) /* b.szCell */
7061270790
+ pBt->pageSize; /* aSpace1 */
7061370791
70614
- assert( szScratch<=6*(int)pBt->pageSize );
70792
+ assert( szScratch<=7*(int)pBt->pageSize );
7061570793
b.apCell = sqlite3StackAllocRaw(0, szScratch );
7061670794
if( b.apCell==0 ){
7061770795
rc = SQLITE_NOMEM_BKPT;
7061870796
goto balance_cleanup;
7061970797
}
@@ -70749,15 +70927,19 @@
7074970927
usableSpace = pBt->usableSize - 12 + leafCorrection;
7075070928
for(i=k=0; i<nOld; i++, k++){
7075170929
MemPage *p = apOld[i];
7075270930
b.apEnd[k] = p->aDataEnd;
7075370931
b.ixNx[k] = cntOld[i];
70932
+ if( k && b.ixNx[k]==b.ixNx[k-1] ){
70933
+ k--; /* Omit b.ixNx[] entry for child pages with no cells */
70934
+ }
7075470935
if( !leafData ){
7075570936
k++;
7075670937
b.apEnd[k] = pParent->aDataEnd;
7075770938
b.ixNx[k] = cntOld[i]+1;
7075870939
}
70940
+ assert( p->nFree>=0 );
7075970941
szNew[i] = usableSpace - p->nFree;
7076070942
for(j=0; j<p->nOverflow; j++){
7076170943
szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
7076270944
}
7076370945
cntNew[i] = cntOld[i];
@@ -70979,22 +71161,21 @@
7097971161
** populated, not here.
7098071162
*/
7098171163
if( ISAUTOVACUUM ){
7098271164
MemPage *pOld;
7098371165
MemPage *pNew = pOld = apNew[0];
70984
- u8 *aOld = pNew->aData;
7098571166
int cntOldNext = pNew->nCell + pNew->nOverflow;
70986
- int usableSize = pBt->usableSize;
7098771167
int iNew = 0;
7098871168
int iOld = 0;
7098971169
7099071170
for(i=0; i<b.nCell; i++){
7099171171
u8 *pCell = b.apCell[i];
70992
- if( i==cntOldNext ){
70993
- pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
71172
+ while( i==cntOldNext ){
71173
+ iOld++;
71174
+ assert( iOld<nNew || iOld<nOld );
71175
+ pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
7099471176
cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
70995
- aOld = pOld->aData;
7099671177
}
7099771178
if( i==cntNew[iNew] ){
7099871179
pNew = apNew[++iNew];
7099971180
if( !leafData ) continue;
7100071181
}
@@ -71005,11 +71186,11 @@
7100571186
** if sibling page iOld had the same page number as pNew, and if
7100671187
** pCell really was a part of sibling page iOld (not a divider or
7100771188
** overflow cell), we can skip updating the pointer map entries. */
7100871189
if( iOld>=nNew
7100971190
|| pNew->pgno!=aPgno[iOld]
71010
- || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
71191
+ || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
7101171192
){
7101271193
if( !leafCorrection ){
7101371194
ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
7101471195
}
7101571196
if( cachedCellSize(&b,i)>pNew->minLocal ){
@@ -71156,11 +71337,12 @@
7115671337
*/
7115771338
assert( nNew==1 || CORRUPT_DB );
7115871339
rc = defragmentPage(apNew[0], -1);
7115971340
testcase( rc!=SQLITE_OK );
7116071341
assert( apNew[0]->nFree ==
71161
- (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
71342
+ (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
71343
+ - apNew[0]->nCell*2)
7116271344
|| rc!=SQLITE_OK
7116371345
);
7116471346
copyNodeContent(apNew[0], pParent, &rc);
7116571347
freePage(apNew[0], &rc);
7116671348
}else if( ISAUTOVACUUM && !leafCorrection ){
@@ -71255,11 +71437,11 @@
7125571437
releasePage(pChild);
7125671438
return rc;
7125771439
}
7125871440
assert( sqlite3PagerIswriteable(pChild->pDbPage) );
7125971441
assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
71260
- assert( pChild->nCell==pRoot->nCell );
71442
+ assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
7126171443
7126271444
TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
7126371445
7126471446
/* Copy the overflow cells from pRoot to pChild */
7126571447
memcpy(pChild->aiOvfl, pRoot->aiOvfl,
@@ -71297,10 +71479,11 @@
7129771479
7129871480
do {
7129971481
int iPage = pCur->iPage;
7130071482
MemPage *pPage = pCur->pPage;
7130171483
71484
+ if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
7130271485
if( iPage==0 ){
7130371486
if( pPage->nOverflow ){
7130471487
/* The root page of the b-tree is overfull. In this case call the
7130571488
** balance_deeper() function to create a new child for the root-page
7130671489
** and copy the current contents of the root-page to it. The
@@ -71325,10 +71508,13 @@
7132571508
}else{
7132671509
MemPage * const pParent = pCur->apPage[iPage-1];
7132771510
int const iIdx = pCur->aiIdx[iPage-1];
7132871511
7132971512
rc = sqlite3PagerWrite(pParent->pDbPage);
71513
+ if( rc==SQLITE_OK && pParent->nFree<0 ){
71514
+ rc = btreeComputeFreeSpace(pParent);
71515
+ }
7133071516
if( rc==SQLITE_OK ){
7133171517
#ifndef SQLITE_OMIT_QUICKBALANCE
7133271518
if( pPage->intKeyLeaf
7133371519
&& pPage->nOverflow==1
7133471520
&& pPage->aiOvfl[0]==pPage->nCell
@@ -71671,10 +71857,14 @@
7167171857
assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
7167271858
7167371859
pPage = pCur->pPage;
7167471860
assert( pPage->intKey || pX->nKey>=0 );
7167571861
assert( pPage->leaf || !pPage->intKey );
71862
+ if( pPage->nFree<0 ){
71863
+ rc = btreeComputeFreeSpace(pPage);
71864
+ if( rc ) return rc;
71865
+ }
7167671866
7167771867
TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
7167871868
pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
7167971869
loc==0 ? "overwrite" : "new entry"));
7168071870
assert( pPage->isInit );
@@ -71813,18 +72003,22 @@
7181372003
assert( pBt->inTransaction==TRANS_WRITE );
7181472004
assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
7181572005
assert( pCur->curFlags & BTCF_WriteFlag );
7181672006
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
7181772007
assert( !hasReadConflicts(p, pCur->pgnoRoot) );
71818
- assert( pCur->ix<pCur->pPage->nCell );
71819
- assert( pCur->eState==CURSOR_VALID );
7182072008
assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
72009
+ if( pCur->eState==CURSOR_REQUIRESEEK ){
72010
+ rc = btreeRestoreCursorPosition(pCur);
72011
+ if( rc ) return rc;
72012
+ }
72013
+ assert( pCur->eState==CURSOR_VALID );
7182172014
7182272015
iCellDepth = pCur->iPage;
7182372016
iCellIdx = pCur->ix;
7182472017
pPage = pCur->pPage;
7182572018
pCell = findCell(pPage, iCellIdx);
72019
+ if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
7182672020
7182772021
/* If the bPreserve flag is set to true, then the cursor position must
7182872022
** be preserved following this delete operation. If the current delete
7182972023
** will cause a b-tree rebalance, then this is done by saving the cursor
7183072024
** key and leaving the cursor in CURSOR_REQUIRESEEK state before
@@ -71891,10 +72085,14 @@
7189172085
MemPage *pLeaf = pCur->pPage;
7189272086
int nCell;
7189372087
Pgno n;
7189472088
unsigned char *pTmp;
7189572089
72090
+ if( pLeaf->nFree<0 ){
72091
+ rc = btreeComputeFreeSpace(pLeaf);
72092
+ if( rc ) return rc;
72093
+ }
7189672094
if( iCellDepth<pCur->iPage-1 ){
7189772095
n = pCur->apPage[iCellDepth+1]->pgno;
7189872096
}else{
7189972097
n = pCur->pPage->pgno;
7190072098
}
@@ -72249,10 +72447,13 @@
7224972447
BtShared *pBt = p->pBt;
7225072448
7225172449
assert( sqlite3BtreeHoldsMutex(p) );
7225272450
assert( p->inTrans==TRANS_WRITE );
7225372451
assert( iTable>=2 );
72452
+ if( iTable>btreePagecount(pBt) ){
72453
+ return SQLITE_CORRUPT_BKPT;
72454
+ }
7225472455
7225572456
rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
7225672457
if( rc ) return rc;
7225772458
rc = sqlite3BtreeClearTable(p, iTable, 0);
7225872459
if( rc ){
@@ -72597,14 +72798,14 @@
7259772798
*/
7259872799
static void checkList(
7259972800
IntegrityCk *pCheck, /* Integrity checking context */
7260072801
int isFreeList, /* True for a freelist. False for overflow page list */
7260172802
int iPage, /* Page number for first page in the list */
72602
- int N /* Expected number of pages in the list */
72803
+ u32 N /* Expected number of pages in the list */
7260372804
){
7260472805
int i;
72605
- int expected = N;
72806
+ u32 expected = N;
7260672807
int nErrAtStart = pCheck->nErr;
7260772808
while( iPage!=0 && pCheck->mxErr ){
7260872809
DbPage *pOvflPage;
7260972810
unsigned char *pOvflData;
7261072811
if( checkRef(pCheck, iPage) ) break;
@@ -72782,10 +72983,15 @@
7278272983
assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
7278372984
checkAppendMsg(pCheck,
7278472985
"btreeInitPage() returns error code %d", rc);
7278572986
goto end_of_check;
7278672987
}
72988
+ if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
72989
+ assert( rc==SQLITE_CORRUPT );
72990
+ checkAppendMsg(pCheck, "free space corruption", rc);
72991
+ goto end_of_check;
72992
+ }
7278772993
data = pPage->aData;
7278872994
hdr = pPage->hdrOffset;
7278972995
7279072996
/* Set up for cell analysis */
7279172997
pCheck->zPfx = "On tree page %d cell %d: ";
@@ -72854,11 +73060,11 @@
7285473060
keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
7285573061
}
7285673062
7285773063
/* Check the content overflow list */
7285873064
if( info.nPayload>info.nLocal ){
72859
- int nPage; /* Number of pages on the overflow chain */
73065
+ u32 nPage; /* Number of pages on the overflow chain */
7286073066
Pgno pgnoOvfl; /* First page of the overflow chain */
7286173067
assert( pc + info.nSize - 4 <= usableSize );
7286273068
nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
7286373069
pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
7286473070
#ifndef SQLITE_OMIT_AUTOVACUUM
@@ -72914,23 +73120,23 @@
7291473120
** freeblocks on the page.
7291573121
*/
7291673122
i = get2byte(&data[hdr+1]);
7291773123
while( i>0 ){
7291873124
int size, j;
72919
- assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
73125
+ assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
7292073126
size = get2byte(&data[i+2]);
72921
- assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
73127
+ assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
7292273128
btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
7292373129
/* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
7292473130
** big-endian integer which is the offset in the b-tree page of the next
7292573131
** freeblock in the chain, or zero if the freeblock is the last on the
7292673132
** chain. */
7292773133
j = get2byte(&data[i]);
7292873134
/* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
7292973135
** increasing offset. */
72930
- assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
72931
- assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
73136
+ assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */
73137
+ assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
7293273138
i = j;
7293373139
}
7293473140
/* Analyze the min-heap looking for overlap between cells and/or
7293573141
** freeblocks, and counting the number of untracked bytes in nFrag.
7293673142
**
@@ -74270,11 +74476,11 @@
7427074476
assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
7427174477
((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
7427274478
((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
7427374479
7427474480
/* No other bits set */
74275
- assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
74481
+ assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
7427674482
|MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
7427774483
}else{
7427874484
/* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
7427974485
** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
7428074486
}
@@ -74391,12 +74597,11 @@
7439174597
return rc;
7439274598
#endif
7439374599
}
7439474600
7439574601
/*
74396
-** Make sure pMem->z points to a writable allocation of at least
74397
-** min(n,32) bytes.
74602
+** Make sure pMem->z points to a writable allocation of at least n bytes.
7439874603
**
7439974604
** If the bPreserve argument is true, then copy of the content of
7440074605
** pMem->z into the new allocation. pMem must be either a string or
7440174606
** blob if bPreserve is true. If bPreserve is false, any prior content
7440274607
** in pMem->z is discarded.
@@ -74411,11 +74616,10 @@
7441174616
assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
7441274617
testcase( bPreserve && pMem->z==0 );
7441374618
7441474619
assert( pMem->szMalloc==0
7441574620
|| pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
74416
- if( n<32 ) n = 32;
7441774621
if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
7441874622
pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
7441974623
bPreserve = 0;
7442074624
}else{
7442174625
if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
@@ -74513,17 +74717,19 @@
7451374717
*/
7451474718
#ifndef SQLITE_OMIT_INCRBLOB
7451574719
SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
7451674720
int nByte;
7451774721
assert( pMem->flags & MEM_Zero );
74518
- assert( pMem->flags&MEM_Blob );
74722
+ assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
74723
+ testcase( sqlite3_value_nochange(pMem) );
7451974724
assert( !sqlite3VdbeMemIsRowSet(pMem) );
7452074725
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
7452174726
7452274727
/* Set nByte to the number of bytes required to store the expanded blob. */
7452374728
nByte = pMem->n + pMem->u.nZero;
7452474729
if( nByte<=0 ){
74730
+ if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
7452574731
nByte = 1;
7452674732
}
7452774733
if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
7452874734
return SQLITE_NOMEM_BKPT;
7452974735
}
@@ -75260,11 +75466,10 @@
7526075466
flags = (enc==0?MEM_Blob:MEM_Str);
7526175467
if( nByte<0 ){
7526275468
assert( enc!=0 );
7526375469
if( enc==SQLITE_UTF8 ){
7526475470
nByte = 0x7fffffff & (int)strlen(z);
75265
- if( nByte>iLimit ) nByte = iLimit+1;
7526675471
}else{
7526775472
for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
7526875473
}
7526975474
flags |= MEM_Term;
7527075475
}
@@ -75272,33 +75477,34 @@
7527275477
/* The following block sets the new values of Mem.z and Mem.xDel. It
7527375478
** also sets a flag in local variable "flags" to indicate the memory
7527475479
** management (one of MEM_Dyn or MEM_Static).
7527575480
*/
7527675481
if( xDel==SQLITE_TRANSIENT ){
75277
- int nAlloc = nByte;
75482
+ u32 nAlloc = nByte;
7527875483
if( flags&MEM_Term ){
7527975484
nAlloc += (enc==SQLITE_UTF8?1:2);
7528075485
}
7528175486
if( nByte>iLimit ){
75282
- return SQLITE_TOOBIG;
75487
+ return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
7528375488
}
7528475489
testcase( nAlloc==0 );
7528575490
testcase( nAlloc==31 );
7528675491
testcase( nAlloc==32 );
75287
- if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
75492
+ if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
7528875493
return SQLITE_NOMEM_BKPT;
7528975494
}
7529075495
memcpy(pMem->z, z, nAlloc);
75291
- }else if( xDel==SQLITE_DYNAMIC ){
75292
- sqlite3VdbeMemRelease(pMem);
75293
- pMem->zMalloc = pMem->z = (char *)z;
75294
- pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
7529575496
}else{
7529675497
sqlite3VdbeMemRelease(pMem);
7529775498
pMem->z = (char *)z;
75298
- pMem->xDel = xDel;
75299
- flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
75499
+ if( xDel==SQLITE_DYNAMIC ){
75500
+ pMem->zMalloc = pMem->z;
75501
+ pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
75502
+ }else{
75503
+ pMem->xDel = xDel;
75504
+ flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
75505
+ }
7530075506
}
7530175507
7530275508
pMem->n = nByte;
7530375509
pMem->flags = flags;
7530475510
pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
@@ -76262,13 +76468,15 @@
7626276468
** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
7626376469
** by the minimum* amount required until the size reaches 512. Normal
7626476470
** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
7626576471
** size of the op array or add 1KB of space, whichever is smaller. */
7626676472
#ifdef SQLITE_TEST_REALLOC_STRESS
76267
- int nNew = (v->nOpAlloc>=512 ? v->nOpAlloc*2 : v->nOpAlloc+nOp);
76473
+ sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
76474
+ : (sqlite3_int64)v->nOpAlloc+nOp);
7626876475
#else
76269
- int nNew = (v->nOpAlloc ? v->nOpAlloc*2 : (int)(1024/sizeof(Op)));
76476
+ sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
76477
+ : (sqlite3_int64)(1024/sizeof(Op)));
7627076478
UNUSED_PARAMETER(nOp);
7627176479
#endif
7627276480
7627376481
/* Ensure that the size of a VDBE does not grow too large */
7627476482
if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
@@ -76744,10 +76952,11 @@
7674476952
7674576953
while( (pOp = opIterNext(&sIter))!=0 ){
7674676954
int opcode = pOp->opcode;
7674776955
if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
7674876956
|| opcode==OP_VDestroy
76957
+ || (opcode==OP_Function0 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL)
7674976958
|| ((opcode==OP_Halt || opcode==OP_HaltIfNull)
7675076959
&& ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
7675176960
){
7675276961
hasAbort = 1;
7675376962
break;
@@ -77051,11 +77260,11 @@
7705177260
int addrLoop, /* Address of loop counter */
7705277261
int addrVisit, /* Address of rows visited counter */
7705377262
LogEst nEst, /* Estimated number of output rows */
7705477263
const char *zName /* Name of table or index being scanned */
7705577264
){
77056
- int nByte = (p->nScan+1) * sizeof(ScanStatus);
77265
+ sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
7705777266
ScanStatus *aNew;
7705877267
aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
7705977268
if( aNew ){
7706077269
ScanStatus *pNew = &aNew[p->nScan++];
7706177270
pNew->addrExplain = addrExplain;
@@ -78172,13 +78381,13 @@
7817278381
/* An instance of this object describes bulk memory available for use
7817378382
** by subcomponents of a prepared statement. Space is allocated out
7817478383
** of a ReusableSpace object by the allocSpace() routine below.
7817578384
*/
7817678385
struct ReusableSpace {
78177
- u8 *pSpace; /* Available memory */
78178
- int nFree; /* Bytes of available memory */
78179
- int nNeeded; /* Total bytes that could not be allocated */
78386
+ u8 *pSpace; /* Available memory */
78387
+ sqlite3_int64 nFree; /* Bytes of available memory */
78388
+ sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
7818078389
};
7818178390
7818278391
/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
7818378392
** from the ReusableSpace object. Return a pointer to the allocated
7818478393
** memory on success. If insufficient memory is available in the
@@ -78194,11 +78403,11 @@
7819478403
** statement.
7819578404
*/
7819678405
static void *allocSpace(
7819778406
struct ReusableSpace *p, /* Bulk memory available for allocation */
7819878407
void *pBuf, /* Pointer to a prior allocation */
78199
- int nByte /* Bytes of memory needed */
78408
+ sqlite3_int64 nByte /* Bytes of memory needed */
7820078409
){
7820178410
assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
7820278411
if( pBuf==0 ){
7820378412
nByte = ROUND8(nByte);
7820478413
if( nByte <= p->nFree ){
@@ -81151,11 +81360,11 @@
8115181360
assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
8115281361
assert( db->init.busy==0 );
8115381362
assert( p->zSql!=0 );
8115481363
sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
8115581364
iElapse = (iNow - p->startTime)*1000000;
81156
-#ifndef SQLITE_OMIT_DEPRECATED
81365
+#ifndef SQLITE_OMIT_DEPRECATED
8115781366
if( db->xProfile ){
8115881367
db->xProfile(db->pProfileArg, p->zSql, iElapse);
8115981368
}
8116081369
#endif
8116181370
if( db->mTrace & SQLITE_TRACE_PROFILE ){
@@ -81358,10 +81567,15 @@
8135881567
8135981568
/* Return true if a parameter to xUpdate represents an unchanged column */
8136081569
SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
8136181570
return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
8136281571
}
81572
+
81573
+/* Return true if a parameter value originated from an sqlite3_bind() */
81574
+SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
81575
+ return (pVal->flags&MEM_FromBind)!=0;
81576
+}
8136381577
8136481578
/* Make a copy of an sqlite3_value object
8136581579
*/
8136681580
SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
8136781581
sqlite3_value *pNew;
@@ -82204,14 +82418,14 @@
8220482418
**
8220582419
** If the result is not a simple column reference (if it is an expression
8220682420
** or a constant) then useTypes 2, 3, and 4 return NULL.
8220782421
*/
8220882422
static const void *columnName(
82209
- sqlite3_stmt *pStmt,
82210
- int N,
82211
- const void *(*xFunc)(Mem*),
82212
- int useType
82423
+ sqlite3_stmt *pStmt, /* The statement */
82424
+ int N, /* Which column to get the name for */
82425
+ int useUtf16, /* True to return the name as UTF16 */
82426
+ int useType /* What type of name */
8221382427
){
8221482428
const void *ret;
8221582429
Vdbe *p;
8221682430
int n;
8221782431
sqlite3 *db;
@@ -82228,12 +82442,19 @@
8222882442
n = sqlite3_column_count(pStmt);
8222982443
if( N<n && N>=0 ){
8223082444
N += useType*n;
8223182445
sqlite3_mutex_enter(db->mutex);
8223282446
assert( db->mallocFailed==0 );
82233
- ret = xFunc(&p->aColName[N]);
82234
- /* A malloc may have failed inside of the xFunc() call. If this
82447
+#ifndef SQLITE_OMIT_UTF16
82448
+ if( useUtf16 ){
82449
+ ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
82450
+ }else
82451
+#endif
82452
+ {
82453
+ ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
82454
+ }
82455
+ /* A malloc may have failed inside of the _text() call. If this
8223582456
** is the case, clear the mallocFailed flag and return NULL.
8223682457
*/
8223782458
if( db->mallocFailed ){
8223882459
sqlite3OomClear(db);
8223982460
ret = 0;
@@ -82246,17 +82467,15 @@
8224682467
/*
8224782468
** Return the name of the Nth column of the result set returned by SQL
8224882469
** statement pStmt.
8224982470
*/
8225082471
SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
82251
- return columnName(
82252
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
82472
+ return columnName(pStmt, N, 0, COLNAME_NAME);
8225382473
}
8225482474
#ifndef SQLITE_OMIT_UTF16
8225582475
SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
82256
- return columnName(
82257
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
82476
+ return columnName(pStmt, N, 1, COLNAME_NAME);
8225882477
}
8225982478
#endif
8226082479
8226182480
/*
8226282481
** Constraint: If you have ENABLE_COLUMN_METADATA then you must
@@ -82271,17 +82490,15 @@
8227182490
/*
8227282491
** Return the column declaration type (if applicable) of the 'i'th column
8227382492
** of the result set of SQL statement pStmt.
8227482493
*/
8227582494
SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
82276
- return columnName(
82277
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
82495
+ return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
8227882496
}
8227982497
#ifndef SQLITE_OMIT_UTF16
8228082498
SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
82281
- return columnName(
82282
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
82499
+ return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
8228382500
}
8228482501
#endif /* SQLITE_OMIT_UTF16 */
8228582502
#endif /* SQLITE_OMIT_DECLTYPE */
8228682503
8228782504
#ifdef SQLITE_ENABLE_COLUMN_METADATA
@@ -82289,49 +82506,43 @@
8228982506
** Return the name of the database from which a result column derives.
8229082507
** NULL is returned if the result column is an expression or constant or
8229182508
** anything else which is not an unambiguous reference to a database column.
8229282509
*/
8229382510
SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
82294
- return columnName(
82295
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
82511
+ return columnName(pStmt, N, 0, COLNAME_DATABASE);
8229682512
}
8229782513
#ifndef SQLITE_OMIT_UTF16
8229882514
SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
82299
- return columnName(
82300
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
82515
+ return columnName(pStmt, N, 1, COLNAME_DATABASE);
8230182516
}
8230282517
#endif /* SQLITE_OMIT_UTF16 */
8230382518
8230482519
/*
8230582520
** Return the name of the table from which a result column derives.
8230682521
** NULL is returned if the result column is an expression or constant or
8230782522
** anything else which is not an unambiguous reference to a database column.
8230882523
*/
8230982524
SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
82310
- return columnName(
82311
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
82525
+ return columnName(pStmt, N, 0, COLNAME_TABLE);
8231282526
}
8231382527
#ifndef SQLITE_OMIT_UTF16
8231482528
SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
82315
- return columnName(
82316
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
82529
+ return columnName(pStmt, N, 1, COLNAME_TABLE);
8231782530
}
8231882531
#endif /* SQLITE_OMIT_UTF16 */
8231982532
8232082533
/*
8232182534
** Return the name of the table column from which a result column derives.
8232282535
** NULL is returned if the result column is an expression or constant or
8232382536
** anything else which is not an unambiguous reference to a database column.
8232482537
*/
8232582538
SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
82326
- return columnName(
82327
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
82539
+ return columnName(pStmt, N, 0, COLNAME_COLUMN);
8232882540
}
8232982541
#ifndef SQLITE_OMIT_UTF16
8233082542
SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
82331
- return columnName(
82332
- pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
82543
+ return columnName(pStmt, N, 1, COLNAME_COLUMN);
8233382544
}
8233482545
#endif /* SQLITE_OMIT_UTF16 */
8233582546
#endif /* SQLITE_ENABLE_COLUMN_METADATA */
8233682547
8233782548
@@ -82694,10 +82905,18 @@
8269482905
** database.
8269582906
*/
8269682907
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
8269782908
return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
8269882909
}
82910
+
82911
+/*
82912
+** Return 1 if the statement is an EXPLAIN and return 2 if the
82913
+** statement is an EXPLAIN QUERY PLAN
82914
+*/
82915
+SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
82916
+ return pStmt ? ((Vdbe*)pStmt)->explain : 0;
82917
+}
8269982918
8270082919
/*
8270182920
** Return true if the prepared statement is in need of being reset.
8270282921
*/
8270382922
SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
@@ -83384,16 +83603,24 @@
8338483603
/*
8338583604
** Invoke the VDBE coverage callback, if that callback is defined. This
8338683605
** feature is used for test suite validation only and does not appear an
8338783606
** production builds.
8338883607
**
83389
-** M is an integer between 2 and 4. 2 indicates a ordinary two-way
83390
-** branch (I=0 means fall through and I=1 means taken). 3 indicates
83391
-** a 3-way branch where the third way is when one of the operands is
83392
-** NULL. 4 indicates the OP_Jump instruction which has three destinations
83393
-** depending on whether the first operand is less than, equal to, or greater
83394
-** than the second.
83608
+** M is the type of branch. I is the direction taken for this instance of
83609
+** the branch.
83610
+**
83611
+** M: 2 - two-way branch (I=0: fall-thru 1: jump )
83612
+** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
83613
+** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
83614
+**
83615
+** In other words, if M is 2, then I is either 0 (for fall-through) or
83616
+** 1 (for when the branch is taken). If M is 3, the I is 0 for an
83617
+** ordinary fall-through, I is 1 if the branch was taken, and I is 2
83618
+** if the result of comparison is NULL. For M=3, I=2 the jump may or
83619
+** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
83620
+** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
83621
+** depending on if the operands are less than, equal, or greater than.
8339583622
**
8339683623
** iSrcLine is the source code line (from the __LINE__ macro) that
8339783624
** generated the VDBE instruction combined with flag bits. The source
8339883625
** code line number is in the lower 24 bits of iSrcLine and the upper
8339983626
** 8 bytes are flags. The lower three bits of the flags indicate
@@ -83400,13 +83627,13 @@
8340083627
** values for I that should never occur. For example, if the branch is
8340183628
** always taken, the flags should be 0x05 since the fall-through and
8340283629
** alternate branch are never taken. If a branch is never taken then
8340383630
** flags should be 0x06 since only the fall-through approach is allowed.
8340483631
**
83405
-** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
83632
+** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
8340683633
** interested in equal or not-equal. In other words, I==0 and I==2
83407
-** should be treated the same.
83634
+** should be treated as equivalent
8340883635
**
8340983636
** Since only a line number is retained, not the filename, this macro
8341083637
** only works for amalgamation builds. But that is ok, since these macros
8341183638
** should be no-ops except for special builds used to measure test coverage.
8341283639
*/
@@ -83426,10 +83653,22 @@
8342683653
** a branch really does go in one of those directions, assert right
8342783654
** away. */
8342883655
mNever = iSrcLine >> 24;
8342983656
assert( (I & mNever)==0 );
8343083657
if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
83658
+ /* Invoke the branch coverage callback with three arguments:
83659
+ ** iSrcLine - the line number of the VdbeCoverage() macro, with
83660
+ ** flags removed.
83661
+ ** I - Mask of bits 0x07 indicating which cases are are
83662
+ ** fulfilled by this instance of the jump. 0x01 means
83663
+ ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
83664
+ ** impossible cases (ex: if the comparison is never NULL)
83665
+ ** are filled in automatically so that the coverage
83666
+ ** measurement logic does not flag those impossible cases
83667
+ ** as missed coverage.
83668
+ ** M - Type of jump. Same as M argument above
83669
+ */
8343183670
I |= mNever;
8343283671
if( M==2 ) I |= 0x04;
8343383672
if( M==4 ){
8343483673
I |= 0x08;
8343583674
if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
@@ -83973,10 +84212,19 @@
8397384212
#endif
8397484213
/*** INSERT STACK UNION HERE ***/
8397584214
8397684215
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
8397784216
sqlite3VdbeEnter(p);
84217
+#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84218
+ if( db->xProgress ){
84219
+ u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
84220
+ assert( 0 < db->nProgressOps );
84221
+ nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
84222
+ }else{
84223
+ nProgressLimit = 0xffffffff;
84224
+ }
84225
+#endif
8397884226
if( p->rc==SQLITE_NOMEM ){
8397984227
/* This happens if a malloc() inside a call to sqlite3_column_text() or
8398084228
** sqlite3_column_text16() failed. */
8398184229
goto no_mem;
8398284230
}
@@ -83986,19 +84234,10 @@
8398684234
assert( p->explain==0 );
8398784235
p->pResultSet = 0;
8398884236
db->busyHandler.nBusy = 0;
8398984237
if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
8399084238
sqlite3VdbeIOTraceSql(p);
83991
-#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83992
- if( db->xProgress ){
83993
- u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
83994
- assert( 0 < db->nProgressOps );
83995
- nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
83996
- }else{
83997
- nProgressLimit = 0xffffffff;
83998
- }
83999
-#endif
8400084239
#ifdef SQLITE_DEBUG
8400184240
sqlite3BeginBenignMalloc();
8400284241
if( p->pc==0
8400384242
&& (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
8400484243
){
@@ -84170,14 +84409,15 @@
8417084409
** of VDBE ops have been executed (either since this invocation of
8417184410
** sqlite3VdbeExec() or since last time the progress callback was called).
8417284411
** If the progress callback returns non-zero, exit the virtual machine with
8417384412
** a return code SQLITE_ABORT.
8417484413
*/
84175
- if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
84414
+ while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
8417684415
assert( db->nProgressOps!=0 );
84177
- nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
84416
+ nProgressLimit += db->nProgressOps;
8417884417
if( db->xProgress(db->pProgressArg) ){
84418
+ nProgressLimit = 0xffffffff;
8417984419
rc = SQLITE_INTERRUPT;
8418084420
goto abort_due_to_error;
8418184421
}
8418284422
}
8418384423
#endif
@@ -84452,10 +84692,11 @@
8445284692
8445384693
#ifndef SQLITE_OMIT_UTF16
8445484694
if( encoding!=SQLITE_UTF8 ){
8445584695
rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
8445684696
assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
84697
+ if( rc ) goto too_big;
8445784698
if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
8445884699
assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
8445984700
assert( VdbeMemDynamic(pOut)==0 );
8446084701
pOut->szMalloc = 0;
8446184702
pOut->flags |= MEM_Static;
@@ -84464,11 +84705,10 @@
8446484705
}
8446584706
pOp->p4type = P4_DYNAMIC;
8446684707
pOp->p4.z = pOut->z;
8446784708
pOp->p1 = pOut->n;
8446884709
}
84469
- testcase( rc==SQLITE_TOOBIG );
8447084710
#endif
8447184711
if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
8447284712
goto too_big;
8447384713
}
8447484714
assert( rc==SQLITE_OK );
@@ -84586,11 +84826,14 @@
8458684826
pVar = &p->aVar[pOp->p1 - 1];
8458784827
if( sqlite3VdbeMemTooBig(pVar) ){
8458884828
goto too_big;
8458984829
}
8459084830
pOut = &aMem[pOp->p2];
84591
- sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
84831
+ if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
84832
+ memcpy(pOut, pVar, MEMCELLSIZE);
84833
+ pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
84834
+ pOut->flags |= MEM_Static|MEM_FromBind;
8459284835
UPDATE_MAX_BLOBSIZE(pOut);
8459384836
break;
8459484837
}
8459584838
8459684839
/* Opcode: Move P1 P2 P3 * *
@@ -84719,22 +84962,10 @@
8471984962
int i;
8472084963
assert( p->nResColumn==pOp->p2 );
8472184964
assert( pOp->p1>0 );
8472284965
assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
8472384966
84724
-#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84725
- /* Run the progress counter just before returning.
84726
- */
84727
- if( db->xProgress!=0
84728
- && nVmStep>=nProgressLimit
84729
- && db->xProgress(db->pProgressArg)!=0
84730
- ){
84731
- rc = SQLITE_INTERRUPT;
84732
- goto abort_due_to_error;
84733
- }
84734
-#endif
84735
-
8473684967
/* If this statement has violated immediate foreign key constraints, do
8473784968
** not return the number of rows modified. And do not RELEASE the statement
8473884969
** transaction. It needs to be rolled back. */
8473984970
if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
8474084971
assert( db->flags&SQLITE_CountRows );
@@ -85096,20 +85327,21 @@
8509685327
*/
8509785328
case OP_MustBeInt: { /* jump, in1 */
8509885329
pIn1 = &aMem[pOp->p1];
8509985330
if( (pIn1->flags & MEM_Int)==0 ){
8510085331
applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
85101
- VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
8510285332
if( (pIn1->flags & MEM_Int)==0 ){
85333
+ VdbeBranchTaken(1, 2);
8510385334
if( pOp->p2==0 ){
8510485335
rc = SQLITE_MISMATCH;
8510585336
goto abort_due_to_error;
8510685337
}else{
8510785338
goto jump_to_p2;
8510885339
}
8510985340
}
8511085341
}
85342
+ VdbeBranchTaken(0, 2);
8511185343
MemSetTypeFlag(pIn1, MEM_Int);
8511285344
break;
8511385345
}
8511485346
8511585347
#ifndef SQLITE_OMIT_FLOATING_POINT
@@ -85280,20 +85512,19 @@
8528085512
if( pOp->p5 & SQLITE_NULLEQ ){
8528185513
/* If SQLITE_NULLEQ is set (which will only happen if the operator is
8528285514
** OP_Eq or OP_Ne) then take the jump or not depending on whether
8528385515
** or not both operands are null.
8528485516
*/
85285
- assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
8528685517
assert( (flags1 & MEM_Cleared)==0 );
8528785518
assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
8528885519
testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
8528985520
if( (flags1&flags3&MEM_Null)!=0
8529085521
&& (flags3&MEM_Cleared)==0
8529185522
){
8529285523
res = 0; /* Operands are equal */
8529385524
}else{
85294
- res = 1; /* Operands are not equal */
85525
+ res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
8529585526
}
8529685527
}else{
8529785528
/* SQLITE_NULLEQ is clear and at least one operand is NULL,
8529885529
** then the result is always NULL.
8529985530
** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
@@ -85407,11 +85638,11 @@
8540785638
memAboutToChange(p, pOut);
8540885639
MemSetTypeFlag(pOut, MEM_Int);
8540985640
pOut->u.i = res2;
8541085641
REGISTER_TRACE(pOp->p2, pOut);
8541185642
}else{
85412
- VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
85643
+ VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
8541385644
if( res2 ){
8541485645
goto jump_to_p2;
8541585646
}
8541685647
}
8541785648
break;
@@ -85957,19 +86188,19 @@
8595786188
offset64 = aOffset[i];
8595886189
zHdr = zData + pC->iHdrOffset;
8595986190
zEndHdr = zData + aOffset[0];
8596086191
testcase( zHdr>=zEndHdr );
8596186192
do{
85962
- if( (t = zHdr[0])<0x80 ){
86193
+ if( (pC->aType[i] = t = zHdr[0])<0x80 ){
8596386194
zHdr++;
8596486195
offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
8596586196
}else{
8596686197
zHdr += sqlite3GetVarint32(zHdr, &t);
86198
+ pC->aType[i] = t;
8596786199
offset64 += sqlite3VdbeSerialTypeLen(t);
8596886200
}
85969
- pC->aType[i++] = t;
85970
- aOffset[i] = (u32)(offset64 & 0xffffffff);
86201
+ aOffset[++i] = (u32)(offset64 & 0xffffffff);
8597186202
}while( i<=p2 && zHdr<zEndHdr );
8597286203
8597386204
/* The record is corrupt if any of the following are true:
8597486205
** (1) the bytes of the header extend past the declared header size
8597586206
** (2) the entire header was used but not all data was used
@@ -86968,10 +87199,11 @@
8696887199
pCx->nullRow = 1;
8696987200
pCx->isEphemeral = 1;
8697087201
pCx->pKeyInfo = pOrig->pKeyInfo;
8697187202
pCx->isTable = pOrig->isTable;
8697287203
pCx->pgnoRoot = pOrig->pgnoRoot;
87204
+ pCx->isOrdered = pOrig->isOrdered;
8697387205
rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
8697487206
pCx->pKeyInfo, pCx->uc.pCursor);
8697587207
/* The sqlite3BtreeCursor() routine can only fail for the first cursor
8697687208
** opened for a database. Since there is already an open cursor when this
8697787209
** opcode is run, the sqlite3BtreeCursor() cannot fail */
@@ -88476,22 +88708,18 @@
8847688708
sqlite3_search_count--;
8847788709
#endif
8847888710
p->aCounter[SQLITE_STMTSTATUS_SORT]++;
8847988711
/* Fall through into OP_Rewind */
8848088712
}
88481
-/* Opcode: Rewind P1 P2 * * P5
88713
+/* Opcode: Rewind P1 P2 * * *
8848288714
**
8848388715
** The next use of the Rowid or Column or Next instruction for P1
8848488716
** will refer to the first entry in the database table or index.
8848588717
** If the table or index is empty, jump immediately to P2.
8848688718
** If the table or index is not empty, fall through to the following
8848788719
** instruction.
8848888720
**
88489
-** If P5 is non-zero and the table is not empty, then the "skip-next"
88490
-** flag is set on the cursor so that the next OP_Next instruction
88491
-** executed on it is a no-op.
88492
-**
8849388721
** This opcode leaves the cursor configured to move in forward order,
8849488722
** from the beginning toward the end. In other words, the cursor is
8849588723
** configured to use Next, not Prev.
8849688724
*/
8849788725
case OP_Rewind: { /* jump */
@@ -88498,10 +88726,11 @@
8849888726
VdbeCursor *pC;
8849988727
BtCursor *pCrsr;
8850088728
int res;
8850188729
8850288730
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88731
+ assert( pOp->p5==0 );
8850388732
pC = p->apCsr[pOp->p1];
8850488733
assert( pC!=0 );
8850588734
assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
8850688735
res = 1;
8850788736
#ifdef SQLITE_DEBUG
@@ -88512,13 +88741,10 @@
8851288741
}else{
8851388742
assert( pC->eCurType==CURTYPE_BTREE );
8851488743
pCrsr = pC->uc.pCursor;
8851588744
assert( pCrsr );
8851688745
rc = sqlite3BtreeFirst(pCrsr, &res);
88517
-#ifndef SQLITE_OMIT_WINDOWFUNC
88518
- if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
88519
-#endif
8852088746
pC->deferredMoveto = 0;
8852188747
pC->cacheStatus = CACHE_STALE;
8852288748
}
8852388749
if( rc ) goto abort_due_to_error;
8852488750
pC->nullRow = (u8)res;
@@ -89524,12 +89750,11 @@
8952489750
aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
8952589751
}
8952689752
}
8952789753
#endif
8952889754
pOp = &aOp[-1];
89529
-
89530
- break;
89755
+ goto check_for_interrupt;
8953189756
}
8953289757
8953389758
/* Opcode: Param P1 P2 * * *
8953489759
**
8953589760
** This opcode is only ever present in sub-programs called via the
@@ -89897,10 +90122,11 @@
8989790122
assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
8989890123
pMem = &aMem[pOp->p1];
8989990124
assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
8990090125
#ifndef SQLITE_OMIT_WINDOWFUNC
8990190126
if( pOp->p3 ){
90127
+ memAboutToChange(p, &aMem[pOp->p3]);
8990290128
rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
8990390129
pMem = &aMem[pOp->p3];
8990490130
}else
8990590131
#endif
8990690132
{
@@ -90934,11 +91160,20 @@
9093491160
9093591161
/* This is the only way out of this procedure. We have to
9093691162
** release the mutexes on btrees that were acquired at the
9093791163
** top. */
9093891164
vdbe_return:
90939
- testcase( nVmStep>0 );
91165
+#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
91166
+ while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
91167
+ nProgressLimit += db->nProgressOps;
91168
+ if( db->xProgress(db->pProgressArg) ){
91169
+ nProgressLimit = 0xffffffff;
91170
+ rc = SQLITE_INTERRUPT;
91171
+ goto abort_due_to_error;
91172
+ }
91173
+ }
91174
+#endif
9094091175
p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
9094191176
sqlite3VdbeLeave(p);
9094291177
assert( rc!=SQLITE_OK || nExtraDelete==0
9094391178
|| sqlite3_strlike("DELETE%",p->zSql,0)!=0
9094491179
);
@@ -92021,11 +92256,11 @@
9202192256
int nRem; /* Bytes remaining to copy */
9202292257
9202392258
/* Extend the p->aAlloc[] allocation if required. */
9202492259
if( p->nAlloc<nByte ){
9202592260
u8 *aNew;
92026
- int nNew = MAX(128, p->nAlloc*2);
92261
+ sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
9202792262
while( nByte>nNew ) nNew = nNew*2;
9202892263
aNew = sqlite3Realloc(p->aAlloc, nNew);
9202992264
if( !aNew ) return SQLITE_NOMEM_BKPT;
9203092265
p->nAlloc = nNew;
9203192266
p->aAlloc = aNew;
@@ -93312,19 +93547,23 @@
9331293547
if( pSorter->list.aMemory ){
9331393548
int nMin = pSorter->iMemory + nReq;
9331493549
9331593550
if( nMin>pSorter->nMemory ){
9331693551
u8 *aNew;
93317
- int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
93318
- int nNew = pSorter->nMemory * 2;
93552
+ sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
93553
+ int iListOff = -1;
93554
+ if( pSorter->list.pList ){
93555
+ iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
93556
+ }
9331993557
while( nNew < nMin ) nNew = nNew*2;
9332093558
if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
9332193559
if( nNew < nMin ) nNew = nMin;
93322
-
9332393560
aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
9332493561
if( !aNew ) return SQLITE_NOMEM_BKPT;
93325
- pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
93562
+ if( iListOff>=0 ){
93563
+ pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
93564
+ }
9332693565
pSorter->list.aMemory = aNew;
9332793566
pSorter->nMemory = nNew;
9332893567
}
9332993568
9333093569
pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
@@ -95306,10 +95545,14 @@
9530695545
assert( pExpr->x.pSelect==0 );
9530795546
pOrig = pEList->a[j].pExpr;
9530895547
if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
9530995548
sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
9531095549
return WRC_Abort;
95550
+ }
95551
+ if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
95552
+ sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
95553
+ return WRC_Abort;
9531195554
}
9531295555
if( sqlite3ExprVectorSize(pOrig)!=1 ){
9531395556
sqlite3ErrorMsg(pParse, "row value misused");
9531495557
return WRC_Abort;
9531595558
}
@@ -95597,10 +95840,11 @@
9559795840
int is_agg = 0; /* True if is an aggregate function */
9559895841
int nId; /* Number of characters in function name */
9559995842
const char *zId; /* The function name. */
9560095843
FuncDef *pDef; /* Information about the function */
9560195844
u8 enc = ENC(pParse->db); /* The database encoding */
95845
+ int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
9560295846
9560395847
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
9560495848
zId = pExpr->u.zToken;
9560595849
nId = sqlite3Strlen30(zId);
9560695850
pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
@@ -95718,12 +95962,15 @@
9571895962
sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
9571995963
nId, zId);
9572095964
pNC->nErr++;
9572195965
}
9572295966
if( is_agg ){
95967
+ /* Window functions may not be arguments of aggregate functions.
95968
+ ** Or arguments of other window functions. But aggregate functions
95969
+ ** may be arguments for window functions. */
9572395970
#ifndef SQLITE_OMIT_WINDOWFUNC
95724
- pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
95971
+ pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
9572595972
#else
9572695973
pNC->ncFlags &= ~NC_AllowAgg;
9572795974
#endif
9572895975
}
9572995976
}
@@ -95740,11 +95987,11 @@
9574095987
|| 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin)
9574195988
){
9574295989
pExpr->y.pWin->pNextWin = pSel->pWin;
9574395990
pSel->pWin = pExpr->y.pWin;
9574495991
}
95745
- pNC->ncFlags |= NC_AllowWin;
95992
+ pNC->ncFlags |= NC_HasWin;
9574695993
}else
9574795994
#endif /* SQLITE_OMIT_WINDOWFUNC */
9574895995
{
9574995996
NameContext *pNC2 = pNC;
9575095997
pExpr->op = TK_AGG_FUNCTION;
@@ -95758,12 +96005,12 @@
9575896005
assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
9575996006
testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
9576096007
pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
9576196008
9576296009
}
95763
- pNC->ncFlags |= NC_AllowAgg;
9576496010
}
96011
+ pNC->ncFlags |= savedAllowFlags;
9576596012
}
9576696013
/* FIX ME: Compute pExpr->affinity based on the expected return
9576796014
** type of the function
9576896015
*/
9576996016
return WRC_Prune;
@@ -96296,11 +96543,11 @@
9629696543
9629796544
/* Recursively resolve names in all subqueries
9629896545
*/
9629996546
for(i=0; i<p->pSrc->nSrc; i++){
9630096547
struct SrcList_item *pItem = &p->pSrc->a[i];
96301
- if( pItem->pSelect ){
96548
+ if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
9630296549
NameContext *pNC; /* Used to iterate name contexts */
9630396550
int nRef = 0; /* Refcount for pOuterNC and outer contexts */
9630496551
const char *zSavedContext = pParse->zAuthContext;
9630596552
9630696553
/* Count the total number of references to pOuterNC and all of its
@@ -96428,10 +96675,11 @@
9642896675
return WRC_Abort;
9642996676
}
9643096677
}
9643196678
}
9643296679
96680
+#ifndef SQLITE_OMIT_WINDOWFUNC
9643396681
if( IN_RENAME_OBJECT ){
9643496682
Window *pWin;
9643596683
for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
9643696684
if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
9643796685
|| sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
@@ -96438,10 +96686,11 @@
9643896686
){
9643996687
return WRC_Abort;
9644096688
}
9644196689
}
9644296690
}
96691
+#endif
9644396692
9644496693
/* If this is part of a compound SELECT, check that it has the right
9644596694
** number of expressions in the select list. */
9644696695
if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
9644796696
sqlite3SelectWrongNumTermsError(pParse, p->pNext);
@@ -96518,12 +96767,12 @@
9651896767
){
9651996768
u16 savedHasAgg;
9652096769
Walker w;
9652196770
9652296771
if( pExpr==0 ) return SQLITE_OK;
96523
- savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
96524
- pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
96772
+ savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
96773
+ pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
9652596774
w.pParse = pNC->pParse;
9652696775
w.xExprCallback = resolveExprStep;
9652796776
w.xSelectCallback = resolveSelectStep;
9652896777
w.xSelectCallback2 = 0;
9652996778
w.u.pNC = pNC;
@@ -96535,13 +96784,15 @@
9653596784
#endif
9653696785
sqlite3WalkExpr(&w, pExpr);
9653796786
#if SQLITE_MAX_EXPR_DEPTH>0
9653896787
w.pParse->nHeight -= pExpr->nHeight;
9653996788
#endif
96540
- if( pNC->ncFlags & NC_HasAgg ){
96541
- ExprSetProperty(pExpr, EP_Agg);
96542
- }
96789
+ assert( EP_Agg==NC_HasAgg );
96790
+ assert( EP_Win==NC_HasWin );
96791
+ testcase( pNC->ncFlags & NC_HasAgg );
96792
+ testcase( pNC->ncFlags & NC_HasWin );
96793
+ ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
9654396794
pNC->ncFlags |= savedHasAgg;
9654496795
return pNC->nErr>0 || w.pParse->nErr>0;
9654596796
}
9654696797
9654796798
/*
@@ -97493,11 +97744,11 @@
9749397744
p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
9749497745
}else{
9749597746
p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
9749697747
if( p ){
9749797748
memset(p, 0, sizeof(Expr));
97498
- p->op = op & TKFLG_MASK;
97749
+ p->op = op & 0xff;
9749997750
p->iAgg = -1;
9750097751
}
9750197752
sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
9750297753
}
9750397754
if( p ) {
@@ -97958,11 +98209,11 @@
9795898209
*/
9795998210
#ifndef SQLITE_OMIT_CTE
9796098211
static With *withDup(sqlite3 *db, With *p){
9796198212
With *pRet = 0;
9796298213
if( p ){
97963
- int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
98214
+ sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
9796498215
pRet = sqlite3DbMallocZero(db, nByte);
9796598216
if( pRet ){
9796698217
int i;
9796798218
pRet->nCte = p->nCte;
9796898219
for(i=0; i<p->nCte; i++){
@@ -98223,11 +98474,11 @@
9822398474
}
9822498475
pList->nExpr = 0;
9822598476
}else if( (pList->nExpr & (pList->nExpr-1))==0 ){
9822698477
ExprList *pNew;
9822798478
pNew = sqlite3DbRealloc(db, pList,
98228
- sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
98479
+ sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
9822998480
if( pNew==0 ){
9823098481
goto no_mem;
9823198482
}
9823298483
pList = pNew;
9823398484
}
@@ -101668,10 +101919,21 @@
101668101919
** be non-NULL, then the LEFT JOIN can be safely converted into an
101669101920
** ordinary join.
101670101921
*/
101671101922
SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
101672101923
Walker w;
101924
+ p = sqlite3ExprSkipCollate(p);
101925
+ while( p ){
101926
+ if( p->op==TK_NOTNULL ){
101927
+ p = p->pLeft;
101928
+ }else if( p->op==TK_AND ){
101929
+ if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
101930
+ p = p->pRight;
101931
+ }else{
101932
+ break;
101933
+ }
101934
+ }
101673101935
w.xExprCallback = impliesNotNullRow;
101674101936
w.xSelectCallback = 0;
101675101937
w.xSelectCallback2 = 0;
101676101938
w.eCode = 0;
101677101939
w.u.iCur = iTab;
@@ -102249,19 +102511,19 @@
102249102511
pVTab = 0;
102250102512
}
102251102513
}
102252102514
#endif
102253102515
102254
- /* Begin a transaction for database iDb.
102255
- ** Then modify the schema cookie (since the ALTER TABLE modifies the
102256
- ** schema). Open a statement transaction if the table is a virtual
102257
- ** table.
102258
- */
102516
+ /* Begin a transaction for database iDb. Then modify the schema cookie
102517
+ ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
102518
+ ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
102519
+ ** nested SQL may raise an exception. */
102259102520
v = sqlite3GetVdbe(pParse);
102260102521
if( v==0 ){
102261102522
goto exit_rename_table;
102262102523
}
102524
+ sqlite3MayAbort(pParse);
102263102525
102264102526
/* figure out how many UTF-8 characters are in zName */
102265102527
zTabName = pTab->zName;
102266102528
nTabName = sqlite3Utf8CharLen(zTabName, -1);
102267102529
@@ -102326,11 +102588,10 @@
102326102588
#ifndef SQLITE_OMIT_VIRTUALTABLE
102327102589
if( pVTab ){
102328102590
int i = ++pParse->nMem;
102329102591
sqlite3VdbeLoadString(v, i, zName);
102330102592
sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
102331
- sqlite3MayAbort(pParse);
102332102593
}
102333102594
#endif
102334102595
102335102596
renameReloadSchema(pParse, iDb);
102336102597
renameTestSchema(pParse, zDb, iDb==1);
@@ -102647,10 +102908,11 @@
102647102908
102648102909
/* Do the rename operation using a recursive UPDATE statement that
102649102910
** uses the sqlite_rename_column() SQL function to compute the new
102650102911
** CREATE statement text for the sqlite_master table.
102651102912
*/
102913
+ sqlite3MayAbort(pParse);
102652102914
zNew = sqlite3NameFromToken(db, pNew);
102653102915
if( !zNew ) goto exit_rename_column;
102654102916
assert( pNew->n>0 );
102655102917
bQuote = sqlite3Isquote(pNew->z[0]);
102656102918
sqlite3NestedParse(pParse,
@@ -105901,16 +106163,18 @@
105901106163
*/
105902106164
if( rc==SQLITE_OK ){
105903106165
sqlite3BtreeEnterAll(db);
105904106166
db->init.iDb = 0;
105905106167
db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
105906
- rc = sqlite3Init(db, &zErrDyn);
106168
+ if( !REOPEN_AS_MEMDB(db) ){
106169
+ rc = sqlite3Init(db, &zErrDyn);
106170
+ }
105907106171
sqlite3BtreeLeaveAll(db);
105908106172
assert( zErrDyn==0 || rc!=SQLITE_OK );
105909106173
}
105910106174
#ifdef SQLITE_USER_AUTHENTICATION
105911
- if( rc==SQLITE_OK ){
106175
+ if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
105912106176
u8 newAuth = 0;
105913106177
rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
105914106178
if( newAuth<db->auth.authLevel ){
105915106179
rc = SQLITE_AUTH_USER;
105916106180
}
@@ -106835,11 +107099,16 @@
106835107099
assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
106836107100
va_start(ap, zFormat);
106837107101
zSql = sqlite3VMPrintf(db, zFormat, ap);
106838107102
va_end(ap);
106839107103
if( zSql==0 ){
106840
- return; /* A malloc must have failed */
107104
+ /* This can result either from an OOM or because the formatted string
107105
+ ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
107106
+ ** an error */
107107
+ if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
107108
+ pParse->nErr++;
107109
+ return;
106841107110
}
106842107111
pParse->nested++;
106843107112
memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
106844107113
memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
106845107114
sqlite3RunParser(pParse, zSql, &zErrMsg);
@@ -107975,11 +108244,12 @@
107975108244
&& pCol
107976108245
&& sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
107977108246
&& sortOrder!=SQLITE_SO_DESC
107978108247
){
107979108248
if( IN_RENAME_OBJECT && pList ){
107980
- sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
108249
+ Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
108250
+ sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
107981108251
}
107982108252
pTab->iPKey = iCol;
107983108253
pTab->keyConf = (u8)onError;
107984108254
assert( autoInc==0 || autoInc==1 );
107985108255
pTab->tabFlags |= autoInc*TF_Autoincrement;
@@ -108396,10 +108666,11 @@
108396108666
if( db->mallocFailed || pParse->nErr ) return;
108397108667
pPk = sqlite3PrimaryKeyIndex(pTab);
108398108668
pTab->iPKey = -1;
108399108669
}else{
108400108670
pPk = sqlite3PrimaryKeyIndex(pTab);
108671
+ assert( pPk!=0 );
108401108672
108402108673
/*
108403108674
** Remove all redundant columns from the PRIMARY KEY. For example, change
108404108675
** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
108405108676
** code assumes the PRIMARY KEY contains no repeated columns.
@@ -108565,10 +108836,15 @@
108565108836
}
108566108837
p->tnum = db->init.newTnum;
108567108838
if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
108568108839
}
108569108840
108841
+ assert( (p->tabFlags & TF_HasPrimaryKey)==0
108842
+ || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
108843
+ assert( (p->tabFlags & TF_HasPrimaryKey)!=0
108844
+ || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
108845
+
108570108846
/* Special processing for WITHOUT ROWID Tables */
108571108847
if( tabOpts & TF_WithoutRowid ){
108572108848
if( (p->tabFlags & TF_Autoincrement) ){
108573108849
sqlite3ErrorMsg(pParse,
108574108850
"AUTOINCREMENT not allowed on WITHOUT ROWID tables");
@@ -109718,17 +109994,17 @@
109718109994
109719109995
assert( pTab!=0 );
109720109996
assert( pParse->nErr==0 );
109721109997
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
109722109998
&& db->init.busy==0
109999
+ && pTblName!=0
109723110000
#if SQLITE_USER_AUTHENTICATION
109724110001
&& sqlite3UserAuthTable(pTab->zName)==0
109725110002
#endif
109726110003
#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
109727110004
&& sqlite3StrICmp(&pTab->zName[7],"master")!=0
109728110005
#endif
109729
- && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
109730110006
){
109731110007
sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
109732110008
goto exit_create_index;
109733110009
}
109734110010
#ifndef SQLITE_OMIT_VIEW
@@ -109828,10 +110104,11 @@
109828110104
if( pList==0 ) goto exit_create_index;
109829110105
assert( pList->nExpr==1 );
109830110106
sqlite3ExprListSetSortOrder(pList, sortOrder);
109831110107
}else{
109832110108
sqlite3ExprListCheckLength(pParse, pList, "index");
110109
+ if( pParse->nErr ) goto exit_create_index;
109833110110
}
109834110111
109835110112
/* Figure out how many bytes of space are required to store explicitly
109836110113
** specified collation sequence names.
109837110114
*/
@@ -109846,10 +110123,11 @@
109846110123
/*
109847110124
** Allocate the index structure.
109848110125
*/
109849110126
nName = sqlite3Strlen30(zName);
109850110127
nExtraCol = pPk ? pPk->nKeyCol : 1;
110128
+ assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
109851110129
pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
109852110130
nName + nExtra + 1, &zExtra);
109853110131
if( db->mallocFailed ){
109854110132
goto exit_create_index;
109855110133
}
@@ -110329,23 +110607,22 @@
110329110607
int szEntry, /* Size of each object in the array */
110330110608
int *pnEntry, /* Number of objects currently in use */
110331110609
int *pIdx /* Write the index of a new slot here */
110332110610
){
110333110611
char *z;
110334
- int n = *pnEntry;
110612
+ sqlite3_int64 n = *pIdx = *pnEntry;
110335110613
if( (n & (n-1))==0 ){
110336
- int sz = (n==0) ? 1 : 2*n;
110614
+ sqlite3_int64 sz = (n==0) ? 1 : 2*n;
110337110615
void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
110338110616
if( pNew==0 ){
110339110617
*pIdx = -1;
110340110618
return pArray;
110341110619
}
110342110620
pArray = pNew;
110343110621
}
110344110622
z = (char*)pArray;
110345110623
memset(&z[n * szEntry], 0, szEntry);
110346
- *pIdx = n;
110347110624
++*pnEntry;
110348110625
return pArray;
110349110626
}
110350110627
110351110628
/*
@@ -110452,11 +110729,11 @@
110452110729
assert( iStart<=pSrc->nSrc );
110453110730
110454110731
/* Allocate additional space if needed */
110455110732
if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
110456110733
SrcList *pNew;
110457
- int nAlloc = pSrc->nSrc*2+nExtra;
110734
+ sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
110458110735
sqlite3 *db = pParse->db;
110459110736
110460110737
if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
110461110738
sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
110462110739
SQLITE_MAX_SRCLIST);
@@ -110959,11 +111236,12 @@
110959111236
char *zErr;
110960111237
int j;
110961111238
StrAccum errMsg;
110962111239
Table *pTab = pIdx->pTable;
110963111240
110964
- sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
111241
+ sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
111242
+ pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
110965111243
if( pIdx->aColExpr ){
110966111244
sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
110967111245
}else{
110968111246
for(j=0; j<pIdx->nKeyCol; j++){
110969111247
char *zCol;
@@ -111208,11 +111486,11 @@
111208111486
}
111209111487
}
111210111488
}
111211111489
111212111490
if( pWith ){
111213
- int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
111491
+ sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
111214111492
pNew = sqlite3DbRealloc(db, pWith, nByte);
111215111493
}else{
111216111494
pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
111217111495
}
111218111496
assert( (pNew!=0 && zName!=0) || db->mallocFailed );
@@ -114515,10 +114793,14 @@
114515114793
static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
114516114794
FuncDef *pDef;
114517114795
pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
114518114796
if( ALWAYS(pDef) ){
114519114797
pDef->funcFlags |= flagVal;
114798
+ }
114799
+ pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
114800
+ if( pDef ){
114801
+ pDef->funcFlags |= flagVal;
114520114802
}
114521114803
}
114522114804
114523114805
/*
114524114806
** Register the built-in LIKE and GLOB functions. The caseSensitive
@@ -117838,11 +118120,13 @@
117838118120
}
117839118121
}
117840118122
sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
117841118123
VdbeComment((v, "for %s", pIdx->zName));
117842118124
#ifdef SQLITE_ENABLE_NULL_TRIM
117843
- if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
118125
+ if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
118126
+ sqlite3SetMakeRecordP5(v, pIdx->pTable);
118127
+ }
117844118128
#endif
117845118129
117846118130
/* In an UPDATE operation, if this index is the PRIMARY KEY index
117847118131
** of a WITHOUT ROWID table and there has been no change the
117848118132
** primary key, then no collision is possible. The collision detection
@@ -118442,10 +118726,17 @@
118442118726
if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
118443118727
}
118444118728
if( pSrcIdx==0 ){
118445118729
return 0; /* pDestIdx has no corresponding index in pSrc */
118446118730
}
118731
+ if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
118732
+ && sqlite3FaultSim(411)==SQLITE_OK ){
118733
+ /* The sqlite3FaultSim() call allows this corruption test to be
118734
+ ** bypassed during testing, in order to exercise other corruption tests
118735
+ ** further downstream. */
118736
+ return 0; /* Corrupt schema - two indexes on the same btree */
118737
+ }
118447118738
}
118448118739
#ifndef SQLITE_OMIT_CHECK
118449118740
if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
118450118741
return 0; /* Tables have different CHECK constraints. Ticket #2252 */
118451118742
}
@@ -118519,11 +118810,11 @@
118519118810
addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
118520118811
VdbeCoverage(v);
118521118812
sqlite3RowidConstraint(pParse, onError, pDest);
118522118813
sqlite3VdbeJumpHere(v, addr2);
118523118814
autoIncStep(pParse, regAutoinc, regRowid);
118524
- }else if( pDest->pIndex==0 ){
118815
+ }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
118525118816
addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
118526118817
}else{
118527118818
addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118528118819
assert( (pDest->tabFlags & TF_Autoincrement)==0 );
118529118820
}
@@ -118582,11 +118873,11 @@
118582118873
if( i==pSrcIdx->nColumn ){
118583118874
idxInsFlags = OPFLAG_USESEEKRESULT;
118584118875
sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
118585118876
}
118586118877
}
118587
- if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
118878
+ if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
118588118879
idxInsFlags |= OPFLAG_NCHANGE;
118589118880
}
118590118881
sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
118591118882
sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
118592118883
sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
@@ -119094,10 +119385,13 @@
119094119385
void (*xValue)(sqlite3_context*),
119095119386
void (*xInv)(sqlite3_context*,int,sqlite3_value**),
119096119387
void(*xDestroy)(void*));
119097119388
/* Version 3.26.0 and later */
119098119389
const char *(*normalized_sql)(sqlite3_stmt*);
119390
+ /* Version 3.28.0 and later */
119391
+ int (*stmt_isexplain)(sqlite3_stmt*);
119392
+ int (*value_frombind)(sqlite3_value*);
119099119393
};
119100119394
119101119395
/*
119102119396
** This is the function signature used for all extension entry points. It
119103119397
** is also defined in the file "loadext.c".
@@ -119383,10 +119677,13 @@
119383119677
#define sqlite3_str_value sqlite3_api->str_value
119384119678
/* Version 3.25.0 and later */
119385119679
#define sqlite3_create_window_function sqlite3_api->create_window_function
119386119680
/* Version 3.26.0 and later */
119387119681
#define sqlite3_normalized_sql sqlite3_api->normalized_sql
119682
+/* Version 3.28.0 and later */
119683
+#define sqlite3_stmt_isexplain sqlite3_api->isexplain
119684
+#define sqlite3_value_frombind sqlite3_api->frombind
119388119685
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
119389119686
119390119687
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
119391119688
/* This case when the file really is being compiled as a loadable
119392119689
** extension */
@@ -119842,14 +120139,17 @@
119842120139
sqlite3_str_value,
119843120140
/* Version 3.25.0 and later */
119844120141
sqlite3_create_window_function,
119845120142
/* Version 3.26.0 and later */
119846120143
#ifdef SQLITE_ENABLE_NORMALIZE
119847
- sqlite3_normalized_sql
120144
+ sqlite3_normalized_sql,
119848120145
#else
119849
- 0
120146
+ 0,
119850120147
#endif
120148
+ /* Version 3.28.0 and later */
120149
+ sqlite3_stmt_isexplain,
120150
+ sqlite3_value_frombind
119851120151
};
119852120152
119853120153
/*
119854120154
** Attempt to load an SQLite extension library contained in the file
119855120155
** zFile. The entry point is zProc. zProc may be 0 in which case a
@@ -129625,11 +129925,11 @@
129625129925
** Update the accumulator memory cells for an aggregate based on
129626129926
** the current cursor position.
129627129927
**
129628129928
** If regAcc is non-zero and there are no min() or max() aggregates
129629129929
** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
129630
-** registers i register regAcc contains 0. The caller will take care
129930
+** registers if register regAcc contains 0. The caller will take care
129631129931
** of setting and clearing regAcc.
129632129932
*/
129633129933
static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
129634129934
Vdbe *v = pParse->pVdbe;
129635129935
int i;
@@ -133717,23 +134017,24 @@
133717134017
}
133718134018
133719134019
/*
133720134020
** This routine implements the OP_Vacuum opcode of the VDBE.
133721134021
*/
133722
-SQLITE_PRIVATE int sqlite3RunVacuum(
134022
+SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
133723134023
char **pzErrMsg, /* Write error message here */
133724134024
sqlite3 *db, /* Database connection */
133725134025
int iDb, /* Which attached DB to vacuum */
133726
- sqlite3_value *pOut /* Write results here, if not NULL */
134026
+ sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
133727134027
){
133728134028
int rc = SQLITE_OK; /* Return code from service routines */
133729134029
Btree *pMain; /* The database being vacuumed */
133730134030
Btree *pTemp; /* The temporary database we vacuum into */
133731134031
u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
133732134032
u64 saved_flags; /* Saved value of db->flags */
133733134033
int saved_nChange; /* Saved value of db->nChange */
133734134034
int saved_nTotalChange; /* Saved value of db->nTotalChange */
134035
+ u32 saved_openFlags; /* Saved value of db->openFlags */
133735134036
u8 saved_mTrace; /* Saved trace settings */
133736134037
Db *pDb = 0; /* Database to detach at end of vacuum */
133737134038
int isMemDb; /* True if vacuuming a :memory: database */
133738134039
int nRes; /* Bytes of reserved space at the end of each page */
133739134040
int nDb; /* Number of attached databases */
@@ -133740,22 +134041,25 @@
133740134041
const char *zDbMain; /* Schema name of database to vacuum */
133741134042
const char *zOut; /* Name of output file */
133742134043
133743134044
if( !db->autoCommit ){
133744134045
sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
133745
- return SQLITE_ERROR;
134046
+ return SQLITE_ERROR; /* IMP: R-12218-18073 */
133746134047
}
133747134048
if( db->nVdbeActive>1 ){
133748134049
sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
133749
- return SQLITE_ERROR;
134050
+ return SQLITE_ERROR; /* IMP: R-15610-35227 */
133750134051
}
134052
+ saved_openFlags = db->openFlags;
133751134053
if( pOut ){
133752134054
if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
133753134055
sqlite3SetString(pzErrMsg, db, "non-text filename");
133754134056
return SQLITE_ERROR;
133755134057
}
133756134058
zOut = (const char*)sqlite3_value_text(pOut);
134059
+ db->openFlags &= ~SQLITE_OPEN_READONLY;
134060
+ db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
133757134061
}else{
133758134062
zOut = "";
133759134063
}
133760134064
133761134065
/* Save the current value of the database flags so that it can be
@@ -133790,10 +134094,11 @@
133790134094
** time to parse and run the PRAGMA to turn journalling off than it does
133791134095
** to write the journal header file.
133792134096
*/
133793134097
nDb = db->nDb;
133794134098
rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
134099
+ db->openFlags = saved_openFlags;
133795134100
if( rc!=SQLITE_OK ) goto end_of_vacuum;
133796134101
assert( (db->nDb-1)==nDb );
133797134102
pDb = &db->aDb[nDb];
133798134103
assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
133799134104
pTemp = pDb->pBt;
@@ -133803,10 +134108,11 @@
133803134108
if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
133804134109
rc = SQLITE_ERROR;
133805134110
sqlite3SetString(pzErrMsg, db, "output file already exists");
133806134111
goto end_of_vacuum;
133807134112
}
134113
+ db->mDbFlags |= DBFLAG_VacuumInto;
133808134114
}
133809134115
nRes = sqlite3BtreeGetOptimalReserve(pMain);
133810134116
133811134117
/* A VACUUM cannot change the pagesize of an encrypted database. */
133812134118
#ifdef SQLITE_HAS_CODEC
@@ -134291,13 +134597,17 @@
134291134597
** Add a new module argument to pTable->azModuleArg[].
134292134598
** The string is not copied - the pointer is stored. The
134293134599
** string will be freed automatically when the table is
134294134600
** deleted.
134295134601
*/
134296
-static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
134297
- int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
134602
+static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
134603
+ sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
134298134604
char **azModuleArg;
134605
+ sqlite3 *db = pParse->db;
134606
+ if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
134607
+ sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
134608
+ }
134299134609
azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
134300134610
if( azModuleArg==0 ){
134301134611
sqlite3DbFree(db, zArg);
134302134612
}else{
134303134613
int i = pTable->nModuleArg++;
@@ -134328,13 +134638,13 @@
134328134638
assert( 0==pTable->pIndex );
134329134639
134330134640
db = pParse->db;
134331134641
134332134642
assert( pTable->nModuleArg==0 );
134333
- addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
134334
- addModuleArgument(db, pTable, 0);
134335
- addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
134643
+ addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
134644
+ addModuleArgument(pParse, pTable, 0);
134645
+ addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
134336134646
assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
134337134647
|| (pParse->sNameToken.z==pName1->z && pName2->z==0)
134338134648
);
134339134649
pParse->sNameToken.n = (int)(
134340134650
&pModuleName->z[pModuleName->n] - pParse->sNameToken.z
@@ -134363,11 +134673,11 @@
134363134673
static void addArgumentToVtab(Parse *pParse){
134364134674
if( pParse->sArg.z && pParse->pNewTable ){
134365134675
const char *z = (const char*)pParse->sArg.z;
134366134676
int n = pParse->sArg.n;
134367134677
sqlite3 *db = pParse->db;
134368
- addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
134678
+ addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
134369134679
}
134370134680
}
134371134681
134372134682
/*
134373134683
** The parser calls this routine after the CREATE VIRTUAL TABLE statement
@@ -134652,11 +134962,12 @@
134652134962
const int ARRAY_INCR = 5;
134653134963
134654134964
/* Grow the sqlite3.aVTrans array if required */
134655134965
if( (db->nVTrans%ARRAY_INCR)==0 ){
134656134966
VTable **aVTrans;
134657
- int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
134967
+ sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
134968
+ ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
134658134969
aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
134659134970
if( !aVTrans ){
134660134971
return SQLITE_NOMEM_BKPT;
134661134972
}
134662134973
memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
@@ -135148,13 +135459,13 @@
135148135459
pMod->pEpoTab = pTab;
135149135460
pTab->nTabRef = 1;
135150135461
pTab->pSchema = db->aDb[0].pSchema;
135151135462
assert( pTab->nModuleArg==0 );
135152135463
pTab->iPKey = -1;
135153
- addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
135154
- addModuleArgument(db, pTab, 0);
135155
- addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
135464
+ addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
135465
+ addModuleArgument(pParse, pTab, 0);
135466
+ addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
135156135467
rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
135157135468
if( rc ){
135158135469
sqlite3ErrorMsg(pParse, "%s", zErr);
135159135470
sqlite3DbFree(db, zErr);
135160135471
sqlite3VtabEponymousTableClear(db, pMod);
@@ -136987,10 +137298,38 @@
136987137298
sqlite3WalkExpr(&w, pWInfo->pWhere);
136988137299
sqlite3WalkExprList(&w, pWInfo->pOrderBy);
136989137300
sqlite3WalkExprList(&w, pWInfo->pResultSet);
136990137301
}
136991137302
}
137303
+
137304
+/*
137305
+** The pTruth expression is always true because it is the WHERE clause
137306
+** a partial index that is driving a query loop. Look through all of the
137307
+** WHERE clause terms on the query, and if any of those terms must be
137308
+** true because pTruth is true, then mark those WHERE clause terms as
137309
+** coded.
137310
+*/
137311
+static void whereApplyPartialIndexConstraints(
137312
+ Expr *pTruth,
137313
+ int iTabCur,
137314
+ WhereClause *pWC
137315
+){
137316
+ int i;
137317
+ WhereTerm *pTerm;
137318
+ while( pTruth->op==TK_AND ){
137319
+ whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
137320
+ pTruth = pTruth->pRight;
137321
+ }
137322
+ for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
137323
+ Expr *pExpr;
137324
+ if( pTerm->wtFlags & TERM_CODED ) continue;
137325
+ pExpr = pTerm->pExpr;
137326
+ if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
137327
+ pTerm->wtFlags |= TERM_CODED;
137328
+ }
137329
+ }
137330
+}
136992137331
136993137332
/*
136994137333
** Generate code for the start of the iLevel-th loop in the WHERE clause
136995137334
** implementation described by pWInfo.
136996137335
*/
@@ -137596,10 +137935,18 @@
137596137935
** https://sqlite.org/src/info/4e8e4857d32d401f
137597137936
*/
137598137937
if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
137599137938
whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
137600137939
}
137940
+
137941
+ /* If a partial index is driving the loop, try to eliminate WHERE clause
137942
+ ** terms from the query that must be true due to the WHERE clause of
137943
+ ** the partial index
137944
+ */
137945
+ if( pIdx->pPartIdxWhere ){
137946
+ whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
137947
+ }
137601137948
137602137949
/* Record the instruction used to terminate the loop. */
137603137950
if( pLoop->wsFlags & WHERE_ONEROW ){
137604137951
pLevel->op = OP_Noop;
137605137952
}else if( bRev ){
@@ -137760,11 +138107,16 @@
137760138107
testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
137761138108
pExpr = sqlite3ExprDup(db, pExpr, 0);
137762138109
pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
137763138110
}
137764138111
if( pAndExpr ){
137765
- pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
138112
+ /* The extra 0x10000 bit on the opcode is masked off and does not
138113
+ ** become part of the new Expr.op. However, it does make the
138114
+ ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
138115
+ ** prevents sqlite3PExpr() from implementing AND short-circuit
138116
+ ** optimization, which we do not want here. */
138117
+ pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
137766138118
}
137767138119
}
137768138120
137769138121
/* Run a separate WHERE clause for each term of the OR clause. After
137770138122
** eliminating duplicates from other WHERE clauses, the action for each
@@ -137990,12 +138342,13 @@
137990138342
continue;
137991138343
#else
137992138344
u32 x = pLevel->iLikeRepCntr;
137993138345
if( x>0 ){
137994138346
skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
138347
+ VdbeCoverageIf(v, (x&1)==1);
138348
+ VdbeCoverageIf(v, (x&1)==0);
137995138349
}
137996
- VdbeCoverage(v);
137997138350
#endif
137998138351
}
137999138352
#ifdef WHERETRACE_ENABLED /* 0xffff */
138000138353
if( sqlite3WhereTrace ){
138001138354
VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
@@ -139593,10 +139946,16 @@
139593139946
if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
139594139947
mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
139595139948
}else if( p->x.pList ){
139596139949
mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
139597139950
}
139951
+#ifndef SQLITE_OMIT_WINDOWFUNC
139952
+ if( p->op==TK_FUNCTION && p->y.pWin ){
139953
+ mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
139954
+ mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
139955
+ }
139956
+#endif
139598139957
return mask;
139599139958
}
139600139959
SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
139601139960
return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
139602139961
}
@@ -143009,15 +143368,15 @@
143009143368
WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
143010143369
WHERETRACE(0x40, (" VirtualOne: all usable\n"));
143011143370
rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
143012143371
143013143372
/* If the call to xBestIndex() with all terms enabled produced a plan
143014
- ** that does not require any source tables (IOW: a plan with mBest==0),
143015
- ** then there is no point in making any further calls to xBestIndex()
143016
- ** since they will all return the same result (if the xBestIndex()
143017
- ** implementation is sane). */
143018
- if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
143373
+ ** that does not require any source tables (IOW: a plan with mBest==0)
143374
+ ** and does not use an IN(...) operator, then there is no point in making
143375
+ ** any further calls to xBestIndex() since they will all return the same
143376
+ ** result (if the xBestIndex() implementation is sane). */
143377
+ if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
143019143378
int seenZero = 0; /* True if a plan with no prereqs seen */
143020143379
int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
143021143380
Bitmask mPrev = 0;
143022143381
Bitmask mBestNoIn = 0;
143023143382
@@ -145246,10 +145605,100 @@
145246145605
}
145247145606
sqlite3_result_int64(pCtx, p->nValue);
145248145607
}
145249145608
}
145250145609
145610
+/*
145611
+** Implementation of built-in window function nth_value(). This
145612
+** implementation is used in "slow mode" only - when the EXCLUDE clause
145613
+** is not set to the default value "NO OTHERS".
145614
+*/
145615
+struct NthValueCtx {
145616
+ i64 nStep;
145617
+ sqlite3_value *pValue;
145618
+};
145619
+static void nth_valueStepFunc(
145620
+ sqlite3_context *pCtx,
145621
+ int nArg,
145622
+ sqlite3_value **apArg
145623
+){
145624
+ struct NthValueCtx *p;
145625
+ p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145626
+ if( p ){
145627
+ i64 iVal;
145628
+ switch( sqlite3_value_numeric_type(apArg[1]) ){
145629
+ case SQLITE_INTEGER:
145630
+ iVal = sqlite3_value_int64(apArg[1]);
145631
+ break;
145632
+ case SQLITE_FLOAT: {
145633
+ double fVal = sqlite3_value_double(apArg[1]);
145634
+ if( ((i64)fVal)!=fVal ) goto error_out;
145635
+ iVal = (i64)fVal;
145636
+ break;
145637
+ }
145638
+ default:
145639
+ goto error_out;
145640
+ }
145641
+ if( iVal<=0 ) goto error_out;
145642
+
145643
+ p->nStep++;
145644
+ if( iVal==p->nStep ){
145645
+ p->pValue = sqlite3_value_dup(apArg[0]);
145646
+ if( !p->pValue ){
145647
+ sqlite3_result_error_nomem(pCtx);
145648
+ }
145649
+ }
145650
+ }
145651
+ UNUSED_PARAMETER(nArg);
145652
+ UNUSED_PARAMETER(apArg);
145653
+ return;
145654
+
145655
+ error_out:
145656
+ sqlite3_result_error(
145657
+ pCtx, "second argument to nth_value must be a positive integer", -1
145658
+ );
145659
+}
145660
+static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
145661
+ struct NthValueCtx *p;
145662
+ p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
145663
+ if( p && p->pValue ){
145664
+ sqlite3_result_value(pCtx, p->pValue);
145665
+ sqlite3_value_free(p->pValue);
145666
+ p->pValue = 0;
145667
+ }
145668
+}
145669
+#define nth_valueInvFunc noopStepFunc
145670
+#define nth_valueValueFunc noopValueFunc
145671
+
145672
+static void first_valueStepFunc(
145673
+ sqlite3_context *pCtx,
145674
+ int nArg,
145675
+ sqlite3_value **apArg
145676
+){
145677
+ struct NthValueCtx *p;
145678
+ p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145679
+ if( p && p->pValue==0 ){
145680
+ p->pValue = sqlite3_value_dup(apArg[0]);
145681
+ if( !p->pValue ){
145682
+ sqlite3_result_error_nomem(pCtx);
145683
+ }
145684
+ }
145685
+ UNUSED_PARAMETER(nArg);
145686
+ UNUSED_PARAMETER(apArg);
145687
+}
145688
+static void first_valueFinalizeFunc(sqlite3_context *pCtx){
145689
+ struct NthValueCtx *p;
145690
+ p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145691
+ if( p && p->pValue ){
145692
+ sqlite3_result_value(pCtx, p->pValue);
145693
+ sqlite3_value_free(p->pValue);
145694
+ p->pValue = 0;
145695
+ }
145696
+}
145697
+#define first_valueInvFunc noopStepFunc
145698
+#define first_valueValueFunc noopValueFunc
145699
+
145251145700
/*
145252145701
** Implementation of built-in window function rank(). Assumes that
145253145702
** the window frame has been set to:
145254145703
**
145255145704
** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
@@ -145281,75 +145730,90 @@
145281145730
145282145731
/*
145283145732
** Implementation of built-in window function percent_rank(). Assumes that
145284145733
** the window frame has been set to:
145285145734
**
145286
-** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145735
+** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145287145736
*/
145288145737
static void percent_rankStepFunc(
145289145738
sqlite3_context *pCtx,
145290145739
int nArg,
145291145740
sqlite3_value **apArg
145292145741
){
145293145742
struct CallCount *p;
145294
- UNUSED_PARAMETER(nArg); assert( nArg==1 );
145295
-
145743
+ UNUSED_PARAMETER(nArg); assert( nArg==0 );
145744
+ UNUSED_PARAMETER(apArg);
145745
+ p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145746
+ if( p ){
145747
+ p->nTotal++;
145748
+ }
145749
+}
145750
+static void percent_rankInvFunc(
145751
+ sqlite3_context *pCtx,
145752
+ int nArg,
145753
+ sqlite3_value **apArg
145754
+){
145755
+ struct CallCount *p;
145756
+ UNUSED_PARAMETER(nArg); assert( nArg==0 );
145757
+ UNUSED_PARAMETER(apArg);
145296145758
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145297
- if( p ){
145298
- if( p->nTotal==0 ){
145299
- p->nTotal = sqlite3_value_int64(apArg[0]);
145300
- }
145301
- p->nStep++;
145302
- if( p->nValue==0 ){
145303
- p->nValue = p->nStep;
145304
- }
145305
- }
145759
+ p->nStep++;
145306145760
}
145307145761
static void percent_rankValueFunc(sqlite3_context *pCtx){
145308145762
struct CallCount *p;
145309145763
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145310145764
if( p ){
145765
+ p->nValue = p->nStep;
145311145766
if( p->nTotal>1 ){
145312
- double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
145767
+ double r = (double)p->nValue / (double)(p->nTotal-1);
145313145768
sqlite3_result_double(pCtx, r);
145314145769
}else{
145315145770
sqlite3_result_double(pCtx, 0.0);
145316145771
}
145317
- p->nValue = 0;
145318145772
}
145319145773
}
145774
+#define percent_rankFinalizeFunc percent_rankValueFunc
145320145775
145321145776
/*
145322145777
** Implementation of built-in window function cume_dist(). Assumes that
145323145778
** the window frame has been set to:
145324145779
**
145325
-** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145780
+** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
145326145781
*/
145327145782
static void cume_distStepFunc(
145328145783
sqlite3_context *pCtx,
145329145784
int nArg,
145330145785
sqlite3_value **apArg
145331145786
){
145332145787
struct CallCount *p;
145333
- assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145334
-
145788
+ UNUSED_PARAMETER(nArg); assert( nArg==0 );
145789
+ UNUSED_PARAMETER(apArg);
145790
+ p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145791
+ if( p ){
145792
+ p->nTotal++;
145793
+ }
145794
+}
145795
+static void cume_distInvFunc(
145796
+ sqlite3_context *pCtx,
145797
+ int nArg,
145798
+ sqlite3_value **apArg
145799
+){
145800
+ struct CallCount *p;
145801
+ UNUSED_PARAMETER(nArg); assert( nArg==0 );
145802
+ UNUSED_PARAMETER(apArg);
145335145803
p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145336
- if( p ){
145337
- if( p->nTotal==0 ){
145338
- p->nTotal = sqlite3_value_int64(apArg[0]);
145339
- }
145340
- p->nStep++;
145341
- }
145804
+ p->nStep++;
145342145805
}
145343145806
static void cume_distValueFunc(sqlite3_context *pCtx){
145344145807
struct CallCount *p;
145345
- p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145346
- if( p && p->nTotal ){
145808
+ p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
145809
+ if( p ){
145347145810
double r = (double)(p->nStep) / (double)(p->nTotal);
145348145811
sqlite3_result_double(pCtx, r);
145349145812
}
145350145813
}
145814
+#define cume_distFinalizeFunc cume_distValueFunc
145351145815
145352145816
/*
145353145817
** Context object for ntile() window function.
145354145818
*/
145355145819
struct NtileCtx {
@@ -145360,44 +145824,54 @@
145360145824
145361145825
/*
145362145826
** Implementation of ntile(). This assumes that the window frame has
145363145827
** been coerced to:
145364145828
**
145365
-** ROWS UNBOUNDED PRECEDING AND CURRENT ROW
145829
+** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
145366145830
*/
145367145831
static void ntileStepFunc(
145368145832
sqlite3_context *pCtx,
145369145833
int nArg,
145370145834
sqlite3_value **apArg
145371145835
){
145372145836
struct NtileCtx *p;
145373
- assert( nArg==2 ); UNUSED_PARAMETER(nArg);
145837
+ assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145374145838
p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145375145839
if( p ){
145376145840
if( p->nTotal==0 ){
145377145841
p->nParam = sqlite3_value_int64(apArg[0]);
145378
- p->nTotal = sqlite3_value_int64(apArg[1]);
145379145842
if( p->nParam<=0 ){
145380145843
sqlite3_result_error(
145381145844
pCtx, "argument of ntile must be a positive integer", -1
145382145845
);
145383145846
}
145384145847
}
145385
- p->iRow++;
145848
+ p->nTotal++;
145386145849
}
145850
+}
145851
+static void ntileInvFunc(
145852
+ sqlite3_context *pCtx,
145853
+ int nArg,
145854
+ sqlite3_value **apArg
145855
+){
145856
+ struct NtileCtx *p;
145857
+ assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145858
+ UNUSED_PARAMETER(apArg);
145859
+ p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145860
+ p->iRow++;
145387145861
}
145388145862
static void ntileValueFunc(sqlite3_context *pCtx){
145389145863
struct NtileCtx *p;
145390145864
p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145391145865
if( p && p->nParam>0 ){
145392145866
int nSize = (p->nTotal / p->nParam);
145393145867
if( nSize==0 ){
145394
- sqlite3_result_int64(pCtx, p->iRow);
145868
+ sqlite3_result_int64(pCtx, p->iRow+1);
145395145869
}else{
145396145870
i64 nLarge = p->nTotal - p->nParam*nSize;
145397145871
i64 iSmall = nLarge*(nSize+1);
145398
- i64 iRow = p->iRow-1;
145872
+ i64 iRow = p->iRow;
145399145873
145400145874
assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
145401145875
145402145876
if( iRow<iSmall ){
145403145877
sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
@@ -145405,10 +145879,11 @@
145405145879
sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
145406145880
}
145407145881
}
145408145882
}
145409145883
}
145884
+#define ntileFinalizeFunc ntileValueFunc
145410145885
145411145886
/*
145412145887
** Context object for last_value() window function.
145413145888
*/
145414145889
struct LastValueCtx {
@@ -145454,11 +145929,11 @@
145454145929
}
145455145930
}
145456145931
}
145457145932
static void last_valueValueFunc(sqlite3_context *pCtx){
145458145933
struct LastValueCtx *p;
145459
- p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145934
+ p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
145460145935
if( p && p->pVal ){
145461145936
sqlite3_result_value(pCtx, p->pVal);
145462145937
}
145463145938
}
145464145939
static void last_valueFinalizeFunc(sqlite3_context *pCtx){
@@ -145544,25 +146019,36 @@
145544146019
SQLITE_PRIVATE void sqlite3WindowFunctions(void){
145545146020
static FuncDef aWindowFuncs[] = {
145546146021
WINDOWFUNCX(row_number, 0, 0),
145547146022
WINDOWFUNCX(dense_rank, 0, 0),
145548146023
WINDOWFUNCX(rank, 0, 0),
145549
- WINDOWFUNCX(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
145550
- WINDOWFUNCX(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
145551
- WINDOWFUNCX(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
146024
+ WINDOWFUNCALL(percent_rank, 0, 0),
146025
+ WINDOWFUNCALL(cume_dist, 0, 0),
146026
+ WINDOWFUNCALL(ntile, 1, 0),
145552146027
WINDOWFUNCALL(last_value, 1, 0),
145553
- WINDOWFUNCNOOP(nth_value, 2, 0),
145554
- WINDOWFUNCNOOP(first_value, 1, 0),
146028
+ WINDOWFUNCALL(nth_value, 2, 0),
146029
+ WINDOWFUNCALL(first_value, 1, 0),
145555146030
WINDOWFUNCNOOP(lead, 1, 0),
145556146031
WINDOWFUNCNOOP(lead, 2, 0),
145557146032
WINDOWFUNCNOOP(lead, 3, 0),
145558146033
WINDOWFUNCNOOP(lag, 1, 0),
145559146034
WINDOWFUNCNOOP(lag, 2, 0),
145560146035
WINDOWFUNCNOOP(lag, 3, 0),
145561146036
};
145562146037
sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
145563146038
}
146039
+
146040
+static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
146041
+ Window *p;
146042
+ for(p=pList; p; p=p->pNextWin){
146043
+ if( sqlite3StrICmp(p->zName, zName)==0 ) break;
146044
+ }
146045
+ if( p==0 ){
146046
+ sqlite3ErrorMsg(pParse, "no such window: %s", zName);
146047
+ }
146048
+ return p;
146049
+}
145564146050
145565146051
/*
145566146052
** This function is called immediately after resolving the function name
145567146053
** for a window function within a SELECT statement. Argument pList is a
145568146054
** linked list of WINDOW definitions for the current SELECT statement.
@@ -145583,52 +146069,70 @@
145583146069
Parse *pParse,
145584146070
Window *pList, /* List of named windows for this SELECT */
145585146071
Window *pWin, /* Window frame to update */
145586146072
FuncDef *pFunc /* Window function definition */
145587146073
){
145588
- if( pWin->zName && pWin->eType==0 ){
145589
- Window *p;
145590
- for(p=pList; p; p=p->pNextWin){
145591
- if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
145592
- }
145593
- if( p==0 ){
145594
- sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
145595
- return;
145596
- }
146074
+ if( pWin->zName && pWin->eFrmType==0 ){
146075
+ Window *p = windowFind(pParse, pList, pWin->zName);
146076
+ if( p==0 ) return;
145597146077
pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
145598146078
pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
145599146079
pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
145600146080
pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
145601146081
pWin->eStart = p->eStart;
145602146082
pWin->eEnd = p->eEnd;
145603
- pWin->eType = p->eType;
146083
+ pWin->eFrmType = p->eFrmType;
146084
+ pWin->eExclude = p->eExclude;
146085
+ }else{
146086
+ sqlite3WindowChain(pParse, pWin, pList);
145604146087
}
146088
+ if( (pWin->eFrmType==TK_RANGE)
146089
+ && (pWin->pStart || pWin->pEnd)
146090
+ && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
146091
+ ){
146092
+ sqlite3ErrorMsg(pParse,
146093
+ "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
146094
+ );
146095
+ }else
145605146096
if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
145606146097
sqlite3 *db = pParse->db;
145607146098
if( pWin->pFilter ){
145608146099
sqlite3ErrorMsg(pParse,
145609146100
"FILTER clause may only be used with aggregate window functions"
145610146101
);
145611
- }else
145612
- if( pFunc->zName==row_numberName || pFunc->zName==ntileName ){
145613
- sqlite3ExprDelete(db, pWin->pStart);
145614
- sqlite3ExprDelete(db, pWin->pEnd);
145615
- pWin->pStart = pWin->pEnd = 0;
145616
- pWin->eType = TK_ROWS;
145617
- pWin->eStart = TK_UNBOUNDED;
145618
- pWin->eEnd = TK_CURRENT;
145619
- }else
145620
-
145621
- if( pFunc->zName==dense_rankName || pFunc->zName==rankName
145622
- || pFunc->zName==percent_rankName || pFunc->zName==cume_distName
145623
- ){
145624
- sqlite3ExprDelete(db, pWin->pStart);
145625
- sqlite3ExprDelete(db, pWin->pEnd);
145626
- pWin->pStart = pWin->pEnd = 0;
145627
- pWin->eType = TK_RANGE;
145628
- pWin->eStart = TK_UNBOUNDED;
145629
- pWin->eEnd = TK_CURRENT;
146102
+ }else{
146103
+ struct WindowUpdate {
146104
+ const char *zFunc;
146105
+ int eFrmType;
146106
+ int eStart;
146107
+ int eEnd;
146108
+ } aUp[] = {
146109
+ { row_numberName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
146110
+ { dense_rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
146111
+ { rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
146112
+ { percent_rankName, TK_GROUPS, TK_CURRENT, TK_UNBOUNDED },
146113
+ { cume_distName, TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED },
146114
+ { ntileName, TK_ROWS, TK_CURRENT, TK_UNBOUNDED },
146115
+ { leadName, TK_ROWS, TK_UNBOUNDED, TK_UNBOUNDED },
146116
+ { lagName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
146117
+ };
146118
+ int i;
146119
+ for(i=0; i<ArraySize(aUp); i++){
146120
+ if( pFunc->zName==aUp[i].zFunc ){
146121
+ sqlite3ExprDelete(db, pWin->pStart);
146122
+ sqlite3ExprDelete(db, pWin->pEnd);
146123
+ pWin->pEnd = pWin->pStart = 0;
146124
+ pWin->eFrmType = aUp[i].eFrmType;
146125
+ pWin->eStart = aUp[i].eStart;
146126
+ pWin->eEnd = aUp[i].eEnd;
146127
+ pWin->eExclude = 0;
146128
+ if( pWin->eStart==TK_FOLLOWING ){
146129
+ pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
146130
+ }
146131
+ break;
146132
+ }
146133
+ }
145630146134
}
145631146135
}
145632146136
pWin->pFunc = pFunc;
145633146137
}
145634146138
@@ -145829,10 +146333,11 @@
145829146333
145830146334
/* Assign a cursor number for the ephemeral table used to buffer rows.
145831146335
** The OpenEphemeral instruction is coded later, after it is known how
145832146336
** many columns the table will have. */
145833146337
pMWin->iEphCsr = pParse->nTab++;
146338
+ pParse->nTab += 3;
145834146339
145835146340
selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
145836146341
selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
145837146342
pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
145838146343
@@ -145884,10 +146389,13 @@
145884146389
p->selFlags &= ~SF_Aggregate;
145885146390
sqlite3SelectPrep(pParse, pSub, 0);
145886146391
}
145887146392
145888146393
sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
146394
+ sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
146395
+ sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
146396
+ sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
145889146397
}else{
145890146398
sqlite3SelectDelete(db, pSub);
145891146399
}
145892146400
if( db->mallocFailed ) rc = SQLITE_NOMEM;
145893146401
}
@@ -145904,10 +146412,11 @@
145904146412
sqlite3ExprListDelete(db, p->pPartition);
145905146413
sqlite3ExprListDelete(db, p->pOrderBy);
145906146414
sqlite3ExprDelete(db, p->pEnd);
145907146415
sqlite3ExprDelete(db, p->pStart);
145908146416
sqlite3DbFree(db, p->zName);
146417
+ sqlite3DbFree(db, p->zBase);
145909146418
sqlite3DbFree(db, p);
145910146419
}
145911146420
}
145912146421
145913146422
/*
@@ -145940,34 +146449,32 @@
145940146449
/*
145941146450
** Allocate and return a new Window object describing a Window Definition.
145942146451
*/
145943146452
SQLITE_PRIVATE Window *sqlite3WindowAlloc(
145944146453
Parse *pParse, /* Parsing context */
145945
- int eType, /* Frame type. TK_RANGE or TK_ROWS */
146454
+ int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
145946146455
int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
145947146456
Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
145948146457
int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
145949
- Expr *pEnd /* End window size if TK_FOLLOWING or PRECEDING */
146458
+ Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
146459
+ u8 eExclude /* EXCLUDE clause */
145950146460
){
145951146461
Window *pWin = 0;
146462
+ int bImplicitFrame = 0;
145952146463
145953146464
/* Parser assures the following: */
145954
- assert( eType==TK_RANGE || eType==TK_ROWS );
146465
+ assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
145955146466
assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
145956146467
|| eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
145957146468
assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
145958146469
|| eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
145959146470
assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
145960146471
assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
145961146472
145962
-
145963
- /* If a frame is declared "RANGE" (not "ROWS"), then it may not use
145964
- ** either "<expr> PRECEDING" or "<expr> FOLLOWING".
145965
- */
145966
- if( eType==TK_RANGE && (pStart!=0 || pEnd!=0) ){
145967
- sqlite3ErrorMsg(pParse, "RANGE must use only UNBOUNDED or CURRENT ROW");
145968
- goto windowAllocErr;
146473
+ if( eType==0 ){
146474
+ bImplicitFrame = 1;
146475
+ eType = TK_RANGE;
145969146476
}
145970146477
145971146478
/* Additionally, the
145972146479
** starting boundary type may not occur earlier in the following list than
145973146480
** the ending boundary type:
@@ -145983,28 +146490,96 @@
145983146490
** frame boundary.
145984146491
*/
145985146492
if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
145986146493
|| (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
145987146494
){
145988
- sqlite3ErrorMsg(pParse, "unsupported frame delimiter for ROWS");
146495
+ sqlite3ErrorMsg(pParse, "unsupported frame specification");
145989146496
goto windowAllocErr;
145990146497
}
145991146498
145992146499
pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
145993146500
if( pWin==0 ) goto windowAllocErr;
145994
- pWin->eType = eType;
146501
+ pWin->eFrmType = eType;
145995146502
pWin->eStart = eStart;
145996146503
pWin->eEnd = eEnd;
146504
+ if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
146505
+ eExclude = TK_NO;
146506
+ }
146507
+ pWin->eExclude = eExclude;
146508
+ pWin->bImplicitFrame = bImplicitFrame;
145997146509
pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
145998146510
pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
145999146511
return pWin;
146000146512
146001146513
windowAllocErr:
146002146514
sqlite3ExprDelete(pParse->db, pEnd);
146003146515
sqlite3ExprDelete(pParse->db, pStart);
146004146516
return 0;
146005146517
}
146518
+
146519
+/*
146520
+** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
146521
+** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
146522
+** equivalent nul-terminated string.
146523
+*/
146524
+SQLITE_PRIVATE Window *sqlite3WindowAssemble(
146525
+ Parse *pParse,
146526
+ Window *pWin,
146527
+ ExprList *pPartition,
146528
+ ExprList *pOrderBy,
146529
+ Token *pBase
146530
+){
146531
+ if( pWin ){
146532
+ pWin->pPartition = pPartition;
146533
+ pWin->pOrderBy = pOrderBy;
146534
+ if( pBase ){
146535
+ pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
146536
+ }
146537
+ }else{
146538
+ sqlite3ExprListDelete(pParse->db, pPartition);
146539
+ sqlite3ExprListDelete(pParse->db, pOrderBy);
146540
+ }
146541
+ return pWin;
146542
+}
146543
+
146544
+/*
146545
+** Window *pWin has just been created from a WINDOW clause. Tokne pBase
146546
+** is the base window. Earlier windows from the same WINDOW clause are
146547
+** stored in the linked list starting at pWin->pNextWin. This function
146548
+** either updates *pWin according to the base specification, or else
146549
+** leaves an error in pParse.
146550
+*/
146551
+SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
146552
+ if( pWin->zBase ){
146553
+ sqlite3 *db = pParse->db;
146554
+ Window *pExist = windowFind(pParse, pList, pWin->zBase);
146555
+ if( pExist ){
146556
+ const char *zErr = 0;
146557
+ /* Check for errors */
146558
+ if( pWin->pPartition ){
146559
+ zErr = "PARTITION clause";
146560
+ }else if( pExist->pOrderBy && pWin->pOrderBy ){
146561
+ zErr = "ORDER BY clause";
146562
+ }else if( pExist->bImplicitFrame==0 ){
146563
+ zErr = "frame specification";
146564
+ }
146565
+ if( zErr ){
146566
+ sqlite3ErrorMsg(pParse,
146567
+ "cannot override %s of window: %s", zErr, pWin->zBase
146568
+ );
146569
+ }else{
146570
+ pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
146571
+ if( pExist->pOrderBy ){
146572
+ assert( pWin->pOrderBy==0 );
146573
+ pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
146574
+ }
146575
+ sqlite3DbFree(db, pWin->zBase);
146576
+ pWin->zBase = 0;
146577
+ }
146578
+ }
146579
+ }
146580
+}
146006146581
146007146582
/*
146008146583
** Attach window object pWin to expression p.
146009146584
*/
146010146585
SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
@@ -146030,13 +146605,14 @@
146030146605
/*
146031146606
** Return 0 if the two window objects are identical, or non-zero otherwise.
146032146607
** Identical window objects can be processed in a single scan.
146033146608
*/
146034146609
SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
146035
- if( p1->eType!=p2->eType ) return 1;
146610
+ if( p1->eFrmType!=p2->eFrmType ) return 1;
146036146611
if( p1->eStart!=p2->eStart ) return 1;
146037146612
if( p1->eEnd!=p2->eEnd ) return 1;
146613
+ if( p1->eExclude!=p2->eExclude ) return 1;
146038146614
if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
146039146615
if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
146040146616
if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
146041146617
if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
146042146618
return 0;
@@ -146049,16 +146625,31 @@
146049146625
** and initialize registers and cursors used by sqlite3WindowCodeStep().
146050146626
*/
146051146627
SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
146052146628
Window *pWin;
146053146629
Vdbe *v = sqlite3GetVdbe(pParse);
146054
- int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
146055
- nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
146056
- if( nPart ){
146630
+
146631
+ /* Allocate registers to use for PARTITION BY values, if any. Initialize
146632
+ ** said registers to NULL. */
146633
+ if( pMWin->pPartition ){
146634
+ int nExpr = pMWin->pPartition->nExpr;
146057146635
pMWin->regPart = pParse->nMem+1;
146058
- pParse->nMem += nPart;
146059
- sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
146636
+ pParse->nMem += nExpr;
146637
+ sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
146638
+ }
146639
+
146640
+ pMWin->regOne = ++pParse->nMem;
146641
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
146642
+
146643
+ if( pMWin->eExclude ){
146644
+ pMWin->regStartRowid = ++pParse->nMem;
146645
+ pMWin->regEndRowid = ++pParse->nMem;
146646
+ pMWin->csrApp = pParse->nTab++;
146647
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
146648
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
146649
+ sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
146650
+ return;
146060146651
}
146061146652
146062146653
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146063146654
FuncDef *p = pWin->pFunc;
146064146655
if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
@@ -146083,50 +146674,71 @@
146083146674
sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146084146675
}
146085146676
else if( p->zName==nth_valueName || p->zName==first_valueName ){
146086146677
/* Allocate two registers at pWin->regApp. These will be used to
146087146678
** store the start and end index of the current frame. */
146088
- assert( pMWin->iEphCsr );
146089146679
pWin->regApp = pParse->nMem+1;
146090146680
pWin->csrApp = pParse->nTab++;
146091146681
pParse->nMem += 2;
146092146682
sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146093146683
}
146094146684
else if( p->zName==leadName || p->zName==lagName ){
146095
- assert( pMWin->iEphCsr );
146096146685
pWin->csrApp = pParse->nTab++;
146097146686
sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146098146687
}
146099146688
}
146100146689
}
146690
+
146691
+#define WINDOW_STARTING_INT 0
146692
+#define WINDOW_ENDING_INT 1
146693
+#define WINDOW_NTH_VALUE_INT 2
146694
+#define WINDOW_STARTING_NUM 3
146695
+#define WINDOW_ENDING_NUM 4
146101146696
146102146697
/*
146103146698
** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
146104146699
** value of the second argument to nth_value() (eCond==2) has just been
146105146700
** evaluated and the result left in register reg. This function generates VM
146106146701
** code to check that the value is a non-negative integer and throws an
146107146702
** exception if it is not.
146108146703
*/
146109
-static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
146704
+static void windowCheckValue(Parse *pParse, int reg, int eCond){
146110146705
static const char *azErr[] = {
146111146706
"frame starting offset must be a non-negative integer",
146112146707
"frame ending offset must be a non-negative integer",
146113
- "second argument to nth_value must be a positive integer"
146708
+ "second argument to nth_value must be a positive integer",
146709
+ "frame starting offset must be a non-negative number",
146710
+ "frame ending offset must be a non-negative number",
146114146711
};
146115
- static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
146712
+ static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
146116146713
Vdbe *v = sqlite3GetVdbe(pParse);
146117146714
int regZero = sqlite3GetTempReg(pParse);
146118
- assert( eCond==0 || eCond==1 || eCond==2 );
146715
+ assert( eCond>=0 && eCond<ArraySize(azErr) );
146119146716
sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
146120
- sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
146121
- VdbeCoverageIf(v, eCond==0);
146122
- VdbeCoverageIf(v, eCond==1);
146123
- VdbeCoverageIf(v, eCond==2);
146717
+ if( eCond>=WINDOW_STARTING_NUM ){
146718
+ int regString = sqlite3GetTempReg(pParse);
146719
+ sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
146720
+ sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
146721
+ sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
146722
+ VdbeCoverage(v);
146723
+ assert( eCond==3 || eCond==4 );
146724
+ VdbeCoverageIf(v, eCond==3);
146725
+ VdbeCoverageIf(v, eCond==4);
146726
+ }else{
146727
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
146728
+ VdbeCoverage(v);
146729
+ assert( eCond==0 || eCond==1 || eCond==2 );
146730
+ VdbeCoverageIf(v, eCond==0);
146731
+ VdbeCoverageIf(v, eCond==1);
146732
+ VdbeCoverageIf(v, eCond==2);
146733
+ }
146124146734
sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
146125
- VdbeCoverageNeverNullIf(v, eCond==0);
146126
- VdbeCoverageNeverNullIf(v, eCond==1);
146735
+ VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
146736
+ VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
146127146737
VdbeCoverageNeverNullIf(v, eCond==2);
146738
+ VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
146739
+ VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
146128146740
sqlite3MayAbort(pParse);
146129146741
sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
146130146742
sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
146131146743
sqlite3ReleaseTempReg(pParse, regZero);
146132146744
}
@@ -146162,41 +146774,32 @@
146162146774
static void windowAggStep(
146163146775
Parse *pParse,
146164146776
Window *pMWin, /* Linked list of window functions */
146165146777
int csr, /* Read arguments from this cursor */
146166146778
int bInverse, /* True to invoke xInverse instead of xStep */
146167
- int reg, /* Array of registers */
146168
- int regPartSize /* Register containing size of partition */
146779
+ int reg /* Array of registers */
146169146780
){
146170146781
Vdbe *v = sqlite3GetVdbe(pParse);
146171146782
Window *pWin;
146172146783
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146173
- int flags = pWin->pFunc->funcFlags;
146784
+ FuncDef *pFunc = pWin->pFunc;
146174146785
int regArg;
146175146786
int nArg = windowArgCount(pWin);
146787
+ int i;
146176146788
146177
- if( csr>=0 ){
146178
- int i;
146179
- for(i=0; i<nArg; i++){
146789
+ for(i=0; i<nArg; i++){
146790
+ if( i!=1 || pFunc->zName!=nth_valueName ){
146180146791
sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
146181
- }
146182
- regArg = reg;
146183
- if( flags & SQLITE_FUNC_WINDOW_SIZE ){
146184
- if( nArg==0 ){
146185
- regArg = regPartSize;
146186
- }else{
146187
- sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
146188
- }
146189
- nArg++;
146190
- }
146191
- }else{
146192
- assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
146193
- regArg = reg + pWin->iArgCol;
146194
- }
146195
-
146196
- if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
146197
- && pWin->eStart!=TK_UNBOUNDED
146792
+ }else{
146793
+ sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
146794
+ }
146795
+ }
146796
+ regArg = reg;
146797
+
146798
+ if( pMWin->regStartRowid==0
146799
+ && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
146800
+ && (pWin->eStart!=TK_UNBOUNDED)
146198146801
){
146199146802
int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
146200146803
VdbeCoverage(v);
146201146804
if( bInverse==0 ){
146202146805
sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
@@ -146209,151 +146812,223 @@
146209146812
sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
146210146813
sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146211146814
}
146212146815
sqlite3VdbeJumpHere(v, addrIsNull);
146213146816
}else if( pWin->regApp ){
146214
- assert( pWin->pFunc->zName==nth_valueName
146215
- || pWin->pFunc->zName==first_valueName
146817
+ assert( pFunc->zName==nth_valueName
146818
+ || pFunc->zName==first_valueName
146216146819
);
146217146820
assert( bInverse==0 || bInverse==1 );
146218146821
sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
146219
- }else if( pWin->pFunc->zName==leadName
146220
- || pWin->pFunc->zName==lagName
146221
- ){
146222
- /* no-op */
146223
- }else{
146822
+ }else if( pFunc->xSFunc!=noopStepFunc ){
146224146823
int addrIf = 0;
146225146824
if( pWin->pFilter ){
146226146825
int regTmp;
146227146826
assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
146228146827
assert( nArg || pWin->pOwner->x.pList==0 );
146229
- if( csr>0 ){
146230
- regTmp = sqlite3GetTempReg(pParse);
146231
- sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146232
- }else{
146233
- regTmp = regArg + nArg;
146234
- }
146828
+ regTmp = sqlite3GetTempReg(pParse);
146829
+ sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146235146830
addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
146236146831
VdbeCoverage(v);
146237
- if( csr>0 ){
146238
- sqlite3ReleaseTempReg(pParse, regTmp);
146239
- }
146832
+ sqlite3ReleaseTempReg(pParse, regTmp);
146240146833
}
146241
- if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
146834
+ if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
146242146835
CollSeq *pColl;
146243146836
assert( nArg>0 );
146244146837
pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
146245146838
sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
146246146839
}
146247146840
sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
146248146841
bInverse, regArg, pWin->regAccum);
146249
- sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146842
+ sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
146250146843
sqlite3VdbeChangeP5(v, (u8)nArg);
146251146844
if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
146252146845
}
146253146846
}
146254146847
}
146255146848
146849
+typedef struct WindowCodeArg WindowCodeArg;
146850
+typedef struct WindowCsrAndReg WindowCsrAndReg;
146851
+struct WindowCsrAndReg {
146852
+ int csr;
146853
+ int reg;
146854
+};
146855
+
146856
+struct WindowCodeArg {
146857
+ Parse *pParse;
146858
+ Window *pMWin;
146859
+ Vdbe *pVdbe;
146860
+ int regGosub;
146861
+ int addrGosub;
146862
+ int regArg;
146863
+ int eDelete;
146864
+
146865
+ WindowCsrAndReg start;
146866
+ WindowCsrAndReg current;
146867
+ WindowCsrAndReg end;
146868
+};
146869
+
146256146870
/*
146257
-** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
146258
-** (bFinal==1) for each window function in the linked list starting at
146871
+** Values that may be passed as the second argument to windowCodeOp().
146872
+*/
146873
+#define WINDOW_RETURN_ROW 1
146874
+#define WINDOW_AGGINVERSE 2
146875
+#define WINDOW_AGGSTEP 3
146876
+
146877
+/*
146878
+** Generate VM code to read the window frames peer values from cursor csr into
146879
+** an array of registers starting at reg.
146880
+*/
146881
+static void windowReadPeerValues(
146882
+ WindowCodeArg *p,
146883
+ int csr,
146884
+ int reg
146885
+){
146886
+ Window *pMWin = p->pMWin;
146887
+ ExprList *pOrderBy = pMWin->pOrderBy;
146888
+ if( pOrderBy ){
146889
+ Vdbe *v = sqlite3GetVdbe(p->pParse);
146890
+ ExprList *pPart = pMWin->pPartition;
146891
+ int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
146892
+ int i;
146893
+ for(i=0; i<pOrderBy->nExpr; i++){
146894
+ sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
146895
+ }
146896
+ }
146897
+}
146898
+
146899
+/*
146900
+** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
146901
+** (bFin==1) for each window function in the linked list starting at
146259146902
** pMWin. Or, for built-in window-functions that do not use the standard
146260146903
** API, generate the equivalent VM code.
146261146904
*/
146262
-static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
146905
+static void windowAggFinal(WindowCodeArg *p, int bFin){
146906
+ Parse *pParse = p->pParse;
146907
+ Window *pMWin = p->pMWin;
146263146908
Vdbe *v = sqlite3GetVdbe(pParse);
146264146909
Window *pWin;
146265146910
146266146911
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146267
- if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
146268
- && pWin->eStart!=TK_UNBOUNDED
146912
+ if( pMWin->regStartRowid==0
146913
+ && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
146914
+ && (pWin->eStart!=TK_UNBOUNDED)
146269146915
){
146270146916
sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146271146917
sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
146272146918
VdbeCoverage(v);
146273146919
sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
146274146920
sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146275
- if( bFinal ){
146276
- sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146277
- }
146278146921
}else if( pWin->regApp ){
146922
+ assert( pMWin->regStartRowid==0 );
146279146923
}else{
146280
- if( bFinal ){
146281
- sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
146924
+ int nArg = windowArgCount(pWin);
146925
+ if( bFin ){
146926
+ sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
146282146927
sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146283146928
sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
146284146929
sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146285146930
}else{
146286
- sqlite3VdbeAddOp3(v, OP_AggValue, pWin->regAccum, windowArgCount(pWin),
146287
- pWin->regResult);
146931
+ sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
146288146932
sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146289146933
}
146290146934
}
146291146935
}
146292146936
}
146293146937
146294146938
/*
146295
-** This function generates VM code to invoke the sub-routine at address
146296
-** lblFlushPart once for each partition with the entire partition cached in
146297
-** the Window.iEphCsr temp table.
146939
+** Generate code to calculate the current values of all window functions in the
146940
+** p->pMWin list by doing a full scan of the current window frame. Store the
146941
+** results in the Window.regResult registers, ready to return the upper
146942
+** layer.
146298146943
*/
146299
-static void windowPartitionCache(
146300
- Parse *pParse,
146301
- Select *p, /* The rewritten SELECT statement */
146302
- WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */
146303
- int regFlushPart, /* Register to use with Gosub lblFlushPart */
146304
- int lblFlushPart, /* Subroutine to Gosub to */
146305
- int *pRegSize /* OUT: Register containing partition size */
146306
-){
146307
- Window *pMWin = p->pWin;
146308
- Vdbe *v = sqlite3GetVdbe(pParse);
146309
- int iSubCsr = p->pSrc->a[0].iCursor;
146310
- int nSub = p->pSrc->a[0].pTab->nCol;
146311
- int k;
146312
-
146313
- int reg = pParse->nMem+1;
146314
- int regRecord = reg+nSub;
146315
- int regRowid = regRecord+1;
146316
-
146317
- *pRegSize = regRowid;
146318
- pParse->nMem += nSub + 2;
146319
-
146320
- /* Load the column values for the row returned by the sub-select
146321
- ** into an array of registers starting at reg. */
146322
- for(k=0; k<nSub; k++){
146323
- sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
146324
- }
146325
- sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
146326
-
146327
- /* Check if this is the start of a new partition. If so, call the
146328
- ** flush_partition sub-routine. */
146329
- if( pMWin->pPartition ){
146944
+static void windowFullScan(WindowCodeArg *p){
146945
+ Window *pWin;
146946
+ Parse *pParse = p->pParse;
146947
+ Window *pMWin = p->pMWin;
146948
+ Vdbe *v = p->pVdbe;
146949
+
146950
+ int regCRowid = 0; /* Current rowid value */
146951
+ int regCPeer = 0; /* Current peer values */
146952
+ int regRowid = 0; /* AggStep rowid value */
146953
+ int regPeer = 0; /* AggStep peer values */
146954
+
146955
+ int nPeer;
146956
+ int lblNext;
146957
+ int lblBrk;
146958
+ int addrNext;
146959
+ int csr = pMWin->csrApp;
146960
+
146961
+ nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
146962
+
146963
+ lblNext = sqlite3VdbeMakeLabel(pParse);
146964
+ lblBrk = sqlite3VdbeMakeLabel(pParse);
146965
+
146966
+ regCRowid = sqlite3GetTempReg(pParse);
146967
+ regRowid = sqlite3GetTempReg(pParse);
146968
+ if( nPeer ){
146969
+ regCPeer = sqlite3GetTempRange(pParse, nPeer);
146970
+ regPeer = sqlite3GetTempRange(pParse, nPeer);
146971
+ }
146972
+
146973
+ sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
146974
+ windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
146975
+
146976
+ for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146977
+ sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146978
+ }
146979
+
146980
+ sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
146981
+ VdbeCoverage(v);
146982
+ addrNext = sqlite3VdbeCurrentAddr(v);
146983
+ sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
146984
+ sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
146985
+ VdbeCoverageNeverNull(v);
146986
+
146987
+ if( pMWin->eExclude==TK_CURRENT ){
146988
+ sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
146989
+ VdbeCoverageNeverNull(v);
146990
+ }else if( pMWin->eExclude!=TK_NO ){
146330146991
int addr;
146331
- ExprList *pPart = pMWin->pPartition;
146332
- int nPart = pPart->nExpr;
146333
- int regNewPart = reg + pMWin->nBufferCol;
146334
- KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
146335
-
146336
- addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
146337
- sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146338
- sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
146339
- VdbeCoverageEqNe(v);
146340
- sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
146341
- sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146342
- VdbeComment((v, "call flush_partition"));
146343
- }
146344
-
146345
- /* Buffer the current row in the ephemeral table. */
146346
- sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
146347
- sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
146348
-
146349
- /* End of the input loop */
146350
- sqlite3WhereEnd(pWInfo);
146351
-
146352
- /* Invoke "flush_partition" to deal with the final (or only) partition */
146353
- sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146354
- VdbeComment((v, "call flush_partition"));
146992
+ int addrEq = 0;
146993
+ KeyInfo *pKeyInfo = 0;
146994
+
146995
+ if( pMWin->pOrderBy ){
146996
+ pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
146997
+ }
146998
+ if( pMWin->eExclude==TK_TIES ){
146999
+ addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
147000
+ VdbeCoverageNeverNull(v);
147001
+ }
147002
+ if( pKeyInfo ){
147003
+ windowReadPeerValues(p, csr, regPeer);
147004
+ sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
147005
+ sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147006
+ addr = sqlite3VdbeCurrentAddr(v)+1;
147007
+ sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
147008
+ VdbeCoverageEqNe(v);
147009
+ }else{
147010
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
147011
+ }
147012
+ if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
147013
+ }
147014
+
147015
+ windowAggStep(pParse, pMWin, csr, 0, p->regArg);
147016
+
147017
+ sqlite3VdbeResolveLabel(v, lblNext);
147018
+ sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
147019
+ VdbeCoverage(v);
147020
+ sqlite3VdbeJumpHere(v, addrNext-1);
147021
+ sqlite3VdbeJumpHere(v, addrNext+1);
147022
+ sqlite3ReleaseTempReg(pParse, regRowid);
147023
+ sqlite3ReleaseTempReg(pParse, regCRowid);
147024
+ if( nPeer ){
147025
+ sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
147026
+ sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
147027
+ }
147028
+
147029
+ windowAggFinal(p, 1);
146355147030
}
146356147031
146357147032
/*
146358147033
** Invoke the sub-routine at regGosub (generated by code in select.c) to
146359147034
** return the current row of Window.iEphCsr. If all window functions are
@@ -146365,114 +147040,78 @@
146365147040
** nth_value()
146366147041
** first_value()
146367147042
** lag()
146368147043
** lead()
146369147044
*/
146370
-static void windowReturnOneRow(
146371
- Parse *pParse,
146372
- Window *pMWin,
146373
- int regGosub,
146374
- int addrGosub
146375
-){
146376
- Vdbe *v = sqlite3GetVdbe(pParse);
146377
- Window *pWin;
146378
- for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146379
- FuncDef *pFunc = pWin->pFunc;
146380
- if( pFunc->zName==nth_valueName
146381
- || pFunc->zName==first_valueName
146382
- ){
146383
- int csr = pWin->csrApp;
146384
- int lbl = sqlite3VdbeMakeLabel(pParse);
146385
- int tmpReg = sqlite3GetTempReg(pParse);
146386
- sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146387
-
146388
- if( pFunc->zName==nth_valueName ){
146389
- sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
146390
- windowCheckIntValue(pParse, tmpReg, 2);
146391
- }else{
146392
- sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
146393
- }
146394
- sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
146395
- sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
146396
- VdbeCoverageNeverNull(v);
146397
- sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
146398
- VdbeCoverageNeverTaken(v);
146399
- sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146400
- sqlite3VdbeResolveLabel(v, lbl);
146401
- sqlite3ReleaseTempReg(pParse, tmpReg);
146402
- }
146403
- else if( pFunc->zName==leadName || pFunc->zName==lagName ){
146404
- int nArg = pWin->pOwner->x.pList->nExpr;
146405
- int iEph = pMWin->iEphCsr;
146406
- int csr = pWin->csrApp;
146407
- int lbl = sqlite3VdbeMakeLabel(pParse);
146408
- int tmpReg = sqlite3GetTempReg(pParse);
146409
-
146410
- if( nArg<3 ){
146411
- sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146412
- }else{
146413
- sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
146414
- }
146415
- sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
146416
- if( nArg<2 ){
146417
- int val = (pFunc->zName==leadName ? 1 : -1);
146418
- sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
146419
- }else{
146420
- int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
146421
- int tmpReg2 = sqlite3GetTempReg(pParse);
146422
- sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
146423
- sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
146424
- sqlite3ReleaseTempReg(pParse, tmpReg2);
146425
- }
146426
-
146427
- sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
146428
- VdbeCoverage(v);
146429
- sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146430
- sqlite3VdbeResolveLabel(v, lbl);
146431
- sqlite3ReleaseTempReg(pParse, tmpReg);
146432
- }
146433
- }
146434
- sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
146435
-}
146436
-
146437
-/*
146438
-** Invoke the code generated by windowReturnOneRow() and, optionally, the
146439
-** xInverse() function for each window function, for one or more rows
146440
-** from the Window.iEphCsr temp table. This routine generates VM code
146441
-** similar to:
146442
-**
146443
-** while( regCtr>0 ){
146444
-** regCtr--;
146445
-** windowReturnOneRow()
146446
-** if( bInverse ){
146447
-** AggInverse
146448
-** }
146449
-** Next (Window.iEphCsr)
146450
-** }
146451
-*/
146452
-static void windowReturnRows(
146453
- Parse *pParse,
146454
- Window *pMWin, /* List of window functions */
146455
- int regCtr, /* Register containing number of rows */
146456
- int regGosub, /* Register for Gosub addrGosub */
146457
- int addrGosub, /* Address of sub-routine for ReturnOneRow */
146458
- int regInvArg, /* Array of registers for xInverse args */
146459
- int regInvSize /* Register containing size of partition */
146460
-){
146461
- int addr;
146462
- Vdbe *v = sqlite3GetVdbe(pParse);
146463
- windowAggFinal(pParse, pMWin, 0);
146464
- addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
146465
- VdbeCoverage(v);
146466
- sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
146467
- windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146468
- if( regInvArg ){
146469
- windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
146470
- }
146471
- sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
146472
- VdbeCoverage(v);
146473
- sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
147045
+static void windowReturnOneRow(WindowCodeArg *p){
147046
+ Window *pMWin = p->pMWin;
147047
+ Vdbe *v = p->pVdbe;
147048
+
147049
+ if( pMWin->regStartRowid ){
147050
+ windowFullScan(p);
147051
+ }else{
147052
+ Parse *pParse = p->pParse;
147053
+ Window *pWin;
147054
+
147055
+ for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147056
+ FuncDef *pFunc = pWin->pFunc;
147057
+ if( pFunc->zName==nth_valueName
147058
+ || pFunc->zName==first_valueName
147059
+ ){
147060
+ int csr = pWin->csrApp;
147061
+ int lbl = sqlite3VdbeMakeLabel(pParse);
147062
+ int tmpReg = sqlite3GetTempReg(pParse);
147063
+ sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
147064
+
147065
+ if( pFunc->zName==nth_valueName ){
147066
+ sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
147067
+ windowCheckValue(pParse, tmpReg, 2);
147068
+ }else{
147069
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
147070
+ }
147071
+ sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
147072
+ sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
147073
+ VdbeCoverageNeverNull(v);
147074
+ sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
147075
+ VdbeCoverageNeverTaken(v);
147076
+ sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
147077
+ sqlite3VdbeResolveLabel(v, lbl);
147078
+ sqlite3ReleaseTempReg(pParse, tmpReg);
147079
+ }
147080
+ else if( pFunc->zName==leadName || pFunc->zName==lagName ){
147081
+ int nArg = pWin->pOwner->x.pList->nExpr;
147082
+ int csr = pWin->csrApp;
147083
+ int lbl = sqlite3VdbeMakeLabel(pParse);
147084
+ int tmpReg = sqlite3GetTempReg(pParse);
147085
+ int iEph = pMWin->iEphCsr;
147086
+
147087
+ if( nArg<3 ){
147088
+ sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
147089
+ }else{
147090
+ sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
147091
+ }
147092
+ sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
147093
+ if( nArg<2 ){
147094
+ int val = (pFunc->zName==leadName ? 1 : -1);
147095
+ sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
147096
+ }else{
147097
+ int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
147098
+ int tmpReg2 = sqlite3GetTempReg(pParse);
147099
+ sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
147100
+ sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
147101
+ sqlite3ReleaseTempReg(pParse, tmpReg2);
147102
+ }
147103
+
147104
+ sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
147105
+ VdbeCoverage(v);
147106
+ sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
147107
+ sqlite3VdbeResolveLabel(v, lbl);
147108
+ sqlite3ReleaseTempReg(pParse, tmpReg);
147109
+ }
147110
+ }
147111
+ }
147112
+ sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
146474147113
}
146475147114
146476147115
/*
146477147116
** Generate code to set the accumulator register for each window function
146478147117
** in the linked list passed as the second argument to NULL. And perform
@@ -146486,693 +147125,269 @@
146486147125
Window *pWin;
146487147126
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146488147127
FuncDef *pFunc = pWin->pFunc;
146489147128
sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146490147129
nArg = MAX(nArg, windowArgCount(pWin));
146491
- if( pFunc->zName==nth_valueName
146492
- || pFunc->zName==first_valueName
146493
- ){
146494
- sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
146495
- sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146496
- }
146497
-
146498
- if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
146499
- assert( pWin->eStart!=TK_UNBOUNDED );
146500
- sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146501
- sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
147130
+ if( pMWin->regStartRowid==0 ){
147131
+ if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
147132
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
147133
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
147134
+ }
147135
+
147136
+ if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
147137
+ assert( pWin->eStart!=TK_UNBOUNDED );
147138
+ sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
147139
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
147140
+ }
146502147141
}
146503147142
}
146504147143
regArg = pParse->nMem+1;
146505147144
pParse->nMem += nArg;
146506147145
return regArg;
146507147146
}
146508147147
146509
-
146510
-/*
146511
-** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
146512
-** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
146513
-** ROW". Pseudo-code for each follows.
146514
-**
146515
-** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
146516
-**
146517
-** ...
146518
-** if( new partition ){
146519
-** Gosub flush_partition
146520
-** }
146521
-** Insert (record in eph-table)
146522
-** sqlite3WhereEnd()
146523
-** Gosub flush_partition
146524
-**
146525
-** flush_partition:
146526
-** Once {
146527
-** OpenDup (iEphCsr -> csrStart)
146528
-** OpenDup (iEphCsr -> csrEnd)
146529
-** }
146530
-** regStart = <expr1> // PRECEDING expression
146531
-** regEnd = <expr2> // FOLLOWING expression
146532
-** if( regStart<0 || regEnd<0 ){ error! }
146533
-** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
146534
-** Next(csrEnd) // if EOF skip Aggstep
146535
-** Aggstep (csrEnd)
146536
-** if( (regEnd--)<=0 ){
146537
-** AggFinal (xValue)
146538
-** Gosub addrGosub
146539
-** Next(csr) // if EOF goto flush_partition_done
146540
-** if( (regStart--)<=0 ){
146541
-** AggInverse (csrStart)
146542
-** Next(csrStart)
146543
-** }
146544
-** }
146545
-** flush_partition_done:
146546
-** ResetSorter (csr)
146547
-** Return
146548
-**
146549
-** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
146550
-** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
146551
-** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
146552
-**
146553
-** These are similar to the above. For "CURRENT ROW", intialize the
146554
-** register to 0. For "UNBOUNDED PRECEDING" to infinity.
146555
-**
146556
-** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
146557
-** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146558
-**
146559
-** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
146560
-** while( 1 ){
146561
-** Next(csrEnd) // Exit while(1) at EOF
146562
-** Aggstep (csrEnd)
146563
-** }
146564
-** while( 1 ){
146565
-** AggFinal (xValue)
146566
-** Gosub addrGosub
146567
-** Next(csr) // if EOF goto flush_partition_done
146568
-** if( (regStart--)<=0 ){
146569
-** AggInverse (csrStart)
146570
-** Next(csrStart)
146571
-** }
146572
-** }
146573
-**
146574
-** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
146575
-** condition is always true (as if regStart were initialized to 0).
146576
-**
146577
-** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146578
-**
146579
-** This is the only RANGE case handled by this routine. It modifies the
146580
-** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
146581
-** be:
146582
-**
146583
-** while( 1 ){
146584
-** AggFinal (xValue)
146585
-** while( 1 ){
146586
-** regPeer++
146587
-** Gosub addrGosub
146588
-** Next(csr) // if EOF goto flush_partition_done
146589
-** if( new peer ) break;
146590
-** }
146591
-** while( (regPeer--)>0 ){
146592
-** AggInverse (csrStart)
146593
-** Next(csrStart)
146594
-** }
146595
-** }
146596
-**
146597
-** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
146598
-**
146599
-** regEnd = regEnd - regStart
146600
-** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
146601
-** Aggstep (csrEnd)
146602
-** Next(csrEnd) // if EOF fall-through
146603
-** if( (regEnd--)<=0 ){
146604
-** if( (regStart--)<=0 ){
146605
-** AggFinal (xValue)
146606
-** Gosub addrGosub
146607
-** Next(csr) // if EOF goto flush_partition_done
146608
-** }
146609
-** AggInverse (csrStart)
146610
-** Next (csrStart)
146611
-** }
146612
-**
146613
-** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
146614
-**
146615
-** Replace the bit after "Rewind" in the above with:
146616
-**
146617
-** if( (regEnd--)<=0 ){
146618
-** AggStep (csrEnd)
146619
-** Next (csrEnd)
146620
-** }
146621
-** AggFinal (xValue)
146622
-** Gosub addrGosub
146623
-** Next(csr) // if EOF goto flush_partition_done
146624
-** if( (regStart--)<=0 ){
146625
-** AggInverse (csr2)
146626
-** Next (csr2)
146627
-** }
146628
-**
146629
-*/
146630
-static void windowCodeRowExprStep(
146631
- Parse *pParse,
146632
- Select *p,
146633
- WhereInfo *pWInfo,
146634
- int regGosub,
146635
- int addrGosub
146636
-){
146637
- Window *pMWin = p->pWin;
146638
- Vdbe *v = sqlite3GetVdbe(pParse);
146639
- int regFlushPart; /* Register for "Gosub flush_partition" */
146640
- int lblFlushPart; /* Label for "Gosub flush_partition" */
146641
- int lblFlushDone; /* Label for "Gosub flush_partition_done" */
146642
-
146643
- int regArg;
146644
- int addr;
146645
- int csrStart = pParse->nTab++;
146646
- int csrEnd = pParse->nTab++;
146647
- int regStart; /* Value of <expr> PRECEDING */
146648
- int regEnd; /* Value of <expr> FOLLOWING */
146649
- int addrGoto;
146650
- int addrTop;
146651
- int addrIfPos1 = 0;
146652
- int addrIfPos2 = 0;
146653
- int regSize = 0;
146654
-
146655
- assert( pMWin->eStart==TK_PRECEDING
146656
- || pMWin->eStart==TK_CURRENT
146657
- || pMWin->eStart==TK_FOLLOWING
146658
- || pMWin->eStart==TK_UNBOUNDED
146659
- );
146660
- assert( pMWin->eEnd==TK_FOLLOWING
146661
- || pMWin->eEnd==TK_CURRENT
146662
- || pMWin->eEnd==TK_UNBOUNDED
146663
- || pMWin->eEnd==TK_PRECEDING
146664
- );
146665
-
146666
- /* Allocate register and label for the "flush_partition" sub-routine. */
146667
- regFlushPart = ++pParse->nMem;
146668
- lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146669
- lblFlushDone = sqlite3VdbeMakeLabel(pParse);
146670
-
146671
- regStart = ++pParse->nMem;
146672
- regEnd = ++pParse->nMem;
146673
-
146674
- windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146675
-
146676
- addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146677
-
146678
- /* Start of "flush_partition" */
146679
- sqlite3VdbeResolveLabel(v, lblFlushPart);
146680
- sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
146681
- VdbeCoverage(v);
146682
- VdbeComment((v, "Flush_partition subroutine"));
146683
- sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
146684
- sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
146685
-
146686
- /* If either regStart or regEnd are not non-negative integers, throw
146687
- ** an exception. */
146688
- if( pMWin->pStart ){
146689
- sqlite3ExprCode(pParse, pMWin->pStart, regStart);
146690
- windowCheckIntValue(pParse, regStart, 0);
146691
- }
146692
- if( pMWin->pEnd ){
146693
- sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
146694
- windowCheckIntValue(pParse, regEnd, 1);
146695
- }
146696
-
146697
- /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
146698
- **
146699
- ** if( regEnd<regStart ){
146700
- ** // The frame always consists of 0 rows
146701
- ** regStart = regSize;
146702
- ** }
146703
- ** regEnd = regEnd - regStart;
146704
- */
146705
- if( pMWin->pEnd && pMWin->eStart==TK_FOLLOWING ){
146706
- assert( pMWin->pStart!=0 );
146707
- assert( pMWin->eEnd==TK_FOLLOWING );
146708
- sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
146709
- VdbeCoverageNeverNull(v);
146710
- sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146711
- sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
146712
- }
146713
-
146714
- if( pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
146715
- assert( pMWin->pEnd!=0 );
146716
- assert( pMWin->eStart==TK_PRECEDING );
146717
- sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
146718
- VdbeCoverageNeverNull(v);
146719
- sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146720
- sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
146721
- }
146722
-
146723
- /* Initialize the accumulator register for each window function to NULL */
146724
- regArg = windowInitAccum(pParse, pMWin);
146725
-
146726
- sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
146727
- VdbeCoverage(v);
146728
- sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
146729
- VdbeCoverageNeverTaken(v);
146730
- sqlite3VdbeChangeP5(v, 1);
146731
- sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
146732
- VdbeCoverageNeverTaken(v);
146733
- sqlite3VdbeChangeP5(v, 1);
146734
-
146735
- /* Invoke AggStep function for each window function using the row that
146736
- ** csrEnd currently points to. Or, if csrEnd is already at EOF,
146737
- ** do nothing. */
146738
- addrTop = sqlite3VdbeCurrentAddr(v);
146739
- if( pMWin->eEnd==TK_PRECEDING ){
146740
- addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146741
- VdbeCoverage(v);
146742
- }
146743
- sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
146744
- VdbeCoverage(v);
146745
- addr = sqlite3VdbeAddOp0(v, OP_Goto);
146746
- windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
146747
- if( pMWin->eEnd==TK_UNBOUNDED ){
146748
- sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146749
- sqlite3VdbeJumpHere(v, addr);
146750
- addrTop = sqlite3VdbeCurrentAddr(v);
146751
- }else{
146752
- sqlite3VdbeJumpHere(v, addr);
146753
- if( pMWin->eEnd==TK_PRECEDING ){
146754
- sqlite3VdbeJumpHere(v, addrIfPos1);
146755
- }
146756
- }
146757
-
146758
- if( pMWin->eEnd==TK_FOLLOWING ){
146759
- addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146760
- VdbeCoverage(v);
146761
- }
146762
- if( pMWin->eStart==TK_FOLLOWING ){
146763
- addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
146764
- VdbeCoverage(v);
146765
- }
146766
- windowAggFinal(pParse, pMWin, 0);
146767
- windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146768
- sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
146769
- VdbeCoverage(v);
146770
- sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
146771
- if( pMWin->eStart==TK_FOLLOWING ){
146772
- sqlite3VdbeJumpHere(v, addrIfPos2);
146773
- }
146774
-
146775
- if( pMWin->eStart==TK_CURRENT
146776
- || pMWin->eStart==TK_PRECEDING
146777
- || pMWin->eStart==TK_FOLLOWING
146778
- ){
146779
- int lblSkipInverse = sqlite3VdbeMakeLabel(pParse);;
146780
- if( pMWin->eStart==TK_PRECEDING ){
146781
- sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1);
146782
- VdbeCoverage(v);
146783
- }
146784
- if( pMWin->eStart==TK_FOLLOWING ){
146785
- sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
146786
- VdbeCoverage(v);
146787
- sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse);
146788
- }else{
146789
- sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
146790
- VdbeCoverageAlwaysTaken(v);
146791
- }
146792
- windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
146793
- sqlite3VdbeResolveLabel(v, lblSkipInverse);
146794
- }
146795
- if( pMWin->eEnd==TK_FOLLOWING ){
146796
- sqlite3VdbeJumpHere(v, addrIfPos1);
146797
- }
146798
- sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146799
-
146800
- /* flush_partition_done: */
146801
- sqlite3VdbeResolveLabel(v, lblFlushDone);
146802
- sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146803
- sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
146804
- VdbeComment((v, "end flush_partition subroutine"));
146805
-
146806
- /* Jump to here to skip over flush_partition */
146807
- sqlite3VdbeJumpHere(v, addrGoto);
146808
-}
146809
-
146810
-/*
146811
-** This function does the work of sqlite3WindowCodeStep() for cases that
146812
-** would normally be handled by windowCodeDefaultStep() when there are
146813
-** one or more built-in window-functions that require the entire partition
146814
-** to be cached in a temp table before any rows can be returned. Additionally.
146815
-** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
146816
-** this function.
146817
-**
146818
-** Pseudo-code corresponding to the VM code generated by this function
146819
-** for each type of window follows.
146820
-**
146821
-** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146822
-**
146823
-** flush_partition:
146824
-** Once {
146825
-** OpenDup (iEphCsr -> csrLead)
146826
-** }
146827
-** Integer ctr 0
146828
-** foreach row (csrLead){
146829
-** if( new peer ){
146830
-** AggFinal (xValue)
146831
-** for(i=0; i<ctr; i++){
146832
-** Gosub addrGosub
146833
-** Next iEphCsr
146834
-** }
146835
-** Integer ctr 0
146836
-** }
146837
-** AggStep (csrLead)
146838
-** Incr ctr
146839
-** }
146840
-**
146841
-** AggFinal (xFinalize)
146842
-** for(i=0; i<ctr; i++){
146843
-** Gosub addrGosub
146844
-** Next iEphCsr
146845
-** }
146846
-**
146847
-** ResetSorter (csr)
146848
-** Return
146849
-**
146850
-** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146851
-**
146852
-** As above, except that the "if( new peer )" branch is always taken.
146853
-**
146854
-** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
146855
-**
146856
-** As above, except that each of the for() loops becomes:
146857
-**
146858
-** for(i=0; i<ctr; i++){
146859
-** Gosub addrGosub
146860
-** AggInverse (iEphCsr)
146861
-** Next iEphCsr
146862
-** }
146863
-**
146864
-** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146865
-**
146866
-** flush_partition:
146867
-** Once {
146868
-** OpenDup (iEphCsr -> csrLead)
146869
-** }
146870
-** foreach row (csrLead) {
146871
-** AggStep (csrLead)
146872
-** }
146873
-** foreach row (iEphCsr) {
146874
-** Gosub addrGosub
146875
-** }
146876
-**
146877
-** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146878
-**
146879
-** flush_partition:
146880
-** Once {
146881
-** OpenDup (iEphCsr -> csrLead)
146882
-** }
146883
-** foreach row (csrLead){
146884
-** AggStep (csrLead)
146885
-** }
146886
-** Rewind (csrLead)
146887
-** Integer ctr 0
146888
-** foreach row (csrLead){
146889
-** if( new peer ){
146890
-** AggFinal (xValue)
146891
-** for(i=0; i<ctr; i++){
146892
-** Gosub addrGosub
146893
-** AggInverse (iEphCsr)
146894
-** Next iEphCsr
146895
-** }
146896
-** Integer ctr 0
146897
-** }
146898
-** Incr ctr
146899
-** }
146900
-**
146901
-** AggFinal (xFinalize)
146902
-** for(i=0; i<ctr; i++){
146903
-** Gosub addrGosub
146904
-** Next iEphCsr
146905
-** }
146906
-**
146907
-** ResetSorter (csr)
146908
-** Return
146909
-*/
146910
-static void windowCodeCacheStep(
146911
- Parse *pParse,
146912
- Select *p,
146913
- WhereInfo *pWInfo,
146914
- int regGosub,
146915
- int addrGosub
146916
-){
146917
- Window *pMWin = p->pWin;
146918
- Vdbe *v = sqlite3GetVdbe(pParse);
146919
- int k;
146920
- int addr;
146921
- ExprList *pPart = pMWin->pPartition;
146922
- ExprList *pOrderBy = pMWin->pOrderBy;
146923
- int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
146924
- int regNewPeer;
146925
-
146926
- int addrGoto; /* Address of Goto used to jump flush_par.. */
146927
- int addrNext; /* Jump here for next iteration of loop */
146928
- int regFlushPart;
146929
- int lblFlushPart;
146930
- int csrLead;
146931
- int regCtr;
146932
- int regArg; /* Register array to martial function args */
146933
- int regSize;
146934
- int lblEmpty;
146935
- int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT
146936
- && pMWin->eEnd==TK_UNBOUNDED;
146937
-
146938
- assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
146939
- || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
146940
- || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
146941
- || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED)
146942
- );
146943
-
146944
- lblEmpty = sqlite3VdbeMakeLabel(pParse);
146945
- regNewPeer = pParse->nMem+1;
146946
- pParse->nMem += nPeer;
146947
-
146948
- /* Allocate register and label for the "flush_partition" sub-routine. */
146949
- regFlushPart = ++pParse->nMem;
146950
- lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146951
-
146952
- csrLead = pParse->nTab++;
146953
- regCtr = ++pParse->nMem;
146954
-
146955
- windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146956
- addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146957
-
146958
- /* Start of "flush_partition" */
146959
- sqlite3VdbeResolveLabel(v, lblFlushPart);
146960
- sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
146961
- VdbeCoverage(v);
146962
- sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
146963
-
146964
- /* Initialize the accumulator register for each window function to NULL */
146965
- regArg = windowInitAccum(pParse, pMWin);
146966
-
146967
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
146968
- sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146969
- VdbeCoverage(v);
146970
- sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
146971
- VdbeCoverageNeverTaken(v);
146972
-
146973
- if( bReverse ){
146974
- int addr2 = sqlite3VdbeCurrentAddr(v);
146975
- windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
146976
- sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr2);
146977
- VdbeCoverage(v);
146978
- sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146979
- VdbeCoverageNeverTaken(v);
146980
- }
146981
- addrNext = sqlite3VdbeCurrentAddr(v);
146982
-
146983
- if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
146984
- int bCurrent = (pMWin->eStart==TK_CURRENT);
146985
- int addrJump = 0; /* Address of OP_Jump below */
146986
- if( pMWin->eType==TK_RANGE ){
146987
- int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
146988
- int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
146989
- KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
146990
- for(k=0; k<nPeer; k++){
146991
- sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
146992
- }
146993
- addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
146994
- sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146995
- addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
146996
- VdbeCoverage(v);
146997
- sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
146998
- }
146999
-
147000
- windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub,
147001
- (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
147002
- );
147003
- if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147004
- }
147005
-
147006
- if( bReverse==0 ){
147007
- windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
147008
- }
147009
- sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
147010
- sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
147011
- VdbeCoverage(v);
147012
-
147013
- windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
147014
-
147015
- sqlite3VdbeResolveLabel(v, lblEmpty);
147016
- sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147017
- sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147018
-
147019
- /* Jump to here to skip over flush_partition */
147020
- sqlite3VdbeJumpHere(v, addrGoto);
147021
-}
147022
-
147023
-
147024
-/*
147025
-** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147026
-**
147027
-** ...
147028
-** if( new partition ){
147029
-** AggFinal (xFinalize)
147030
-** Gosub addrGosub
147031
-** ResetSorter eph-table
147032
-** }
147033
-** else if( new peer ){
147034
-** AggFinal (xValue)
147035
-** Gosub addrGosub
147036
-** ResetSorter eph-table
147037
-** }
147038
-** AggStep
147039
-** Insert (record into eph-table)
147040
-** sqlite3WhereEnd()
147041
-** AggFinal (xFinalize)
147042
-** Gosub addrGosub
147043
-**
147044
-** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147045
-**
147046
-** As above, except take no action for a "new peer". Invoke
147047
-** the sub-routine once only for each partition.
147048
-**
147049
-** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
147050
-**
147051
-** As above, except that the "new peer" condition is handled in the
147052
-** same way as "new partition" (so there is no "else if" block).
147053
-**
147054
-** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147055
-**
147056
-** As above, except assume every row is a "new peer".
147057
-*/
147058
-static void windowCodeDefaultStep(
147059
- Parse *pParse,
147060
- Select *p,
147061
- WhereInfo *pWInfo,
147062
- int regGosub,
147063
- int addrGosub
147064
-){
147065
- Window *pMWin = p->pWin;
147066
- Vdbe *v = sqlite3GetVdbe(pParse);
147067
- int k;
147068
- int iSubCsr = p->pSrc->a[0].iCursor;
147069
- int nSub = p->pSrc->a[0].pTab->nCol;
147070
- int reg = pParse->nMem+1;
147071
- int regRecord = reg+nSub;
147072
- int regRowid = regRecord+1;
147073
- int addr;
147074
- ExprList *pPart = pMWin->pPartition;
147075
- ExprList *pOrderBy = pMWin->pOrderBy;
147076
-
147077
- assert( pMWin->eType==TK_RANGE
147078
- || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147079
- );
147080
-
147081
- assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147082
- || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
147083
- || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
147084
- || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
147085
- );
147086
-
147087
- if( pMWin->eEnd==TK_UNBOUNDED ){
147088
- pOrderBy = 0;
147089
- }
147090
-
147091
- pParse->nMem += nSub + 2;
147092
-
147093
- /* Load the individual column values of the row returned by
147094
- ** the sub-select into an array of registers. */
147095
- for(k=0; k<nSub; k++){
147096
- sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
147097
- }
147098
-
147099
- /* Check if this is the start of a new partition or peer group. */
147100
- if( pPart || pOrderBy ){
147101
- int nPart = (pPart ? pPart->nExpr : 0);
147102
- int addrGoto = 0;
147103
- int addrJump = 0;
147104
- int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
147105
-
147106
- if( pPart ){
147107
- int regNewPart = reg + pMWin->nBufferCol;
147108
- KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
147109
- addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
147110
- sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147111
- addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147112
- VdbeCoverageEqNe(v);
147113
- windowAggFinal(pParse, pMWin, 1);
147114
- if( pOrderBy ){
147115
- addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
147116
- }
147117
- }
147118
-
147119
- if( pOrderBy ){
147120
- int regNewPeer = reg + pMWin->nBufferCol + nPart;
147121
- int regPeer = pMWin->regPart + nPart;
147122
-
147123
- if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147124
- if( pMWin->eType==TK_RANGE ){
147125
- KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
147126
- addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
147127
- sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147128
- addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147129
- VdbeCoverage(v);
147130
- }else{
147131
- addrJump = 0;
147132
- }
147133
- windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
147134
- if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
147135
- }
147136
-
147137
- sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147138
- VdbeCoverage(v);
147139
- sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147140
- sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147141
- VdbeCoverage(v);
147142
-
147143
- sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147144
- sqlite3VdbeAddOp3(
147145
- v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
147146
- );
147147
-
147148
- if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147149
- }
147150
-
147151
- /* Invoke step function for window functions */
147152
- windowAggStep(pParse, pMWin, -1, 0, reg, 0);
147153
-
147154
- /* Buffer the current row in the ephemeral table. */
147155
- if( pMWin->nBufferCol>0 ){
147156
- sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
147157
- }else{
147158
- sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
147159
- sqlite3VdbeAppendP4(v, (void*)"", 0);
147160
- }
147161
- sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
147162
- sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
147163
-
147164
- /* End the database scan loop. */
147165
- sqlite3WhereEnd(pWInfo);
147166
-
147167
- windowAggFinal(pParse, pMWin, 1);
147168
- sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147169
- VdbeCoverage(v);
147170
- sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147171
- sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147172
- VdbeCoverage(v);
147173
-}
147148
+/*
147149
+** Return true if the current frame should be cached in the ephemeral table,
147150
+** even if there are no xInverse() calls required.
147151
+*/
147152
+static int windowCacheFrame(Window *pMWin){
147153
+ Window *pWin;
147154
+ if( pMWin->regStartRowid ) return 1;
147155