Fossil SCM

Update the built-in SQLite to the latest 3.41.0 alpha with the query planner tuning enhancements, as a beta-test of SQLite looking for performance regressions.

drh 2022-12-05 03:39 trunk
Commit 4ddd8847097118d11a2eb29c76191b528faa0533aea0ca7d5a6db2f62f804962
3 files changed +346 -272 +1075 -588 +31 -13
+346 -272
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -2056,11 +2056,11 @@
20562056
}
20572057
20582058
/* Compute a string using sqlite3_vsnprintf() with a maximum length
20592059
** of 50 bytes and add it to the hash.
20602060
*/
2061
-static void hash_step_vformat(
2061
+static void sha3_step_vformat(
20622062
SHA3Context *p, /* Add content to this context */
20632063
const char *zFormat,
20642064
...
20652065
){
20662066
va_list ap;
@@ -2152,11 +2152,11 @@
21522152
}
21532153
nCol = sqlite3_column_count(pStmt);
21542154
z = sqlite3_sql(pStmt);
21552155
if( z ){
21562156
n = (int)strlen(z);
2157
- hash_step_vformat(&cx,"S%d:",n);
2157
+ sha3_step_vformat(&cx,"S%d:",n);
21582158
SHA3Update(&cx,(unsigned char*)z,n);
21592159
}
21602160
21612161
/* Compute a hash over the result of the query */
21622162
while( SQLITE_ROW==sqlite3_step(pStmt) ){
@@ -2196,18 +2196,18 @@
21962196
break;
21972197
}
21982198
case SQLITE_TEXT: {
21992199
int n2 = sqlite3_column_bytes(pStmt, i);
22002200
const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2201
- hash_step_vformat(&cx,"T%d:",n2);
2201
+ sha3_step_vformat(&cx,"T%d:",n2);
22022202
SHA3Update(&cx, z2, n2);
22032203
break;
22042204
}
22052205
case SQLITE_BLOB: {
22062206
int n2 = sqlite3_column_bytes(pStmt, i);
22072207
const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2208
- hash_step_vformat(&cx,"B%d:",n2);
2208
+ sha3_step_vformat(&cx,"B%d:",n2);
22092209
SHA3Update(&cx, z2, n2);
22102210
break;
22112211
}
22122212
}
22132213
}
@@ -3928,11 +3928,11 @@
39283928
}else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
39293929
&& (p->z[p->i+1]&0xc0)==0x80 ){
39303930
c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
39313931
p->i += 2;
39323932
if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
3933
- }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
3933
+ }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80
39343934
&& (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
39353935
c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
39363936
| (p->z[p->i+2]&0x3f);
39373937
p->i += 3;
39383938
if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
@@ -4455,19 +4455,19 @@
44554455
/* The following is a performance optimization. If the regex begins with
44564456
** ".*" (if the input regex lacks an initial "^") and afterwards there are
44574457
** one or more matching characters, enter those matching characters into
44584458
** zInit[]. The re_match() routine can then search ahead in the input
44594459
** string looking for the initial match without having to run the whole
4460
- ** regex engine over the string. Do not worry able trying to match
4460
+ ** regex engine over the string. Do not worry about trying to match
44614461
** unicode characters beyond plane 0 - those are very rare and this is
44624462
** just an optimization. */
44634463
if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
44644464
for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
44654465
unsigned x = pRe->aArg[i];
4466
- if( x<=127 ){
4466
+ if( x<=0x7f ){
44674467
pRe->zInit[j++] = (unsigned char)x;
4468
- }else if( x<=0xfff ){
4468
+ }else if( x<=0x7ff ){
44694469
pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
44704470
pRe->zInit[j++] = 0x80 | (x&0x3f);
44714471
}else if( x<=0xffff ){
44724472
pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12));
44734473
pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
@@ -11410,10 +11410,263 @@
1141011410
#define SQLITE_SHELL_HAVE_RECOVER 1
1141111411
#else
1141211412
#define SQLITE_SHELL_HAVE_RECOVER 0
1141311413
#endif
1141411414
#if SQLITE_SHELL_HAVE_RECOVER
11415
+/************************* Begin ../ext/recover/sqlite3recover.h ******************/
11416
+/*
11417
+** 2022-08-27
11418
+**
11419
+** The author disclaims copyright to this source code. In place of
11420
+** a legal notice, here is a blessing:
11421
+**
11422
+** May you do good and not evil.
11423
+** May you find forgiveness for yourself and forgive others.
11424
+** May you share freely, never taking more than you give.
11425
+**
11426
+*************************************************************************
11427
+**
11428
+** This file contains the public interface to the "recover" extension -
11429
+** an SQLite extension designed to recover data from corrupted database
11430
+** files.
11431
+*/
11432
+
11433
+/*
11434
+** OVERVIEW:
11435
+**
11436
+** To use the API to recover data from a corrupted database, an
11437
+** application:
11438
+**
11439
+** 1) Creates an sqlite3_recover handle by calling either
11440
+** sqlite3_recover_init() or sqlite3_recover_init_sql().
11441
+**
11442
+** 2) Configures the new handle using one or more calls to
11443
+** sqlite3_recover_config().
11444
+**
11445
+** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
11446
+** the handle until it returns something other than SQLITE_OK. If it
11447
+** returns SQLITE_DONE, then the recovery operation completed without
11448
+** error. If it returns some other non-SQLITE_OK value, then an error
11449
+** has occurred.
11450
+**
11451
+** 4) Retrieves any error code and English language error message using the
11452
+** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
11453
+** respectively.
11454
+**
11455
+** 5) Destroys the sqlite3_recover handle and frees all resources
11456
+** using sqlite3_recover_finish().
11457
+**
11458
+** The application may abandon the recovery operation at any point
11459
+** before it is finished by passing the sqlite3_recover handle to
11460
+** sqlite3_recover_finish(). This is not an error, but the final state
11461
+** of the output database, or the results of running the partial script
11462
+** delivered to the SQL callback, are undefined.
11463
+*/
11464
+
11465
+#ifndef _SQLITE_RECOVER_H
11466
+#define _SQLITE_RECOVER_H
11467
+
11468
+/* #include "sqlite3.h" */
11469
+
11470
+#ifdef __cplusplus
11471
+extern "C" {
11472
+#endif
11473
+
11474
+/*
11475
+** An instance of the sqlite3_recover object represents a recovery
11476
+** operation in progress.
11477
+**
11478
+** Constructors:
11479
+**
11480
+** sqlite3_recover_init()
11481
+** sqlite3_recover_init_sql()
11482
+**
11483
+** Destructor:
11484
+**
11485
+** sqlite3_recover_finish()
11486
+**
11487
+** Methods:
11488
+**
11489
+** sqlite3_recover_config()
11490
+** sqlite3_recover_errcode()
11491
+** sqlite3_recover_errmsg()
11492
+** sqlite3_recover_run()
11493
+** sqlite3_recover_step()
11494
+*/
11495
+typedef struct sqlite3_recover sqlite3_recover;
11496
+
11497
+/*
11498
+** These two APIs attempt to create and return a new sqlite3_recover object.
11499
+** In both cases the first two arguments identify the (possibly
11500
+** corrupt) database to recover data from. The first argument is an open
11501
+** database handle and the second the name of a database attached to that
11502
+** handle (i.e. "main", "temp" or the name of an attached database).
11503
+**
11504
+** If sqlite3_recover_init() is used to create the new sqlite3_recover
11505
+** handle, then data is recovered into a new database, identified by
11506
+** string parameter zUri. zUri may be an absolute or relative file path,
11507
+** or may be an SQLite URI. If the identified database file already exists,
11508
+** it is overwritten.
11509
+**
11510
+** If sqlite3_recover_init_sql() is invoked, then any recovered data will
11511
+** be returned to the user as a series of SQL statements. Executing these
11512
+** SQL statements results in the same database as would have been created
11513
+** had sqlite3_recover_init() been used. For each SQL statement in the
11514
+** output, the callback function passed as the third argument (xSql) is
11515
+** invoked once. The first parameter is a passed a copy of the fourth argument
11516
+** to this function (pCtx) as its first parameter, and a pointer to a
11517
+** nul-terminated buffer containing the SQL statement formated as UTF-8 as
11518
+** the second. If the xSql callback returns any value other than SQLITE_OK,
11519
+** then processing is immediately abandoned and the value returned used as
11520
+** the recover handle error code (see below).
11521
+**
11522
+** If an out-of-memory error occurs, NULL may be returned instead of
11523
+** a valid handle. In all other cases, it is the responsibility of the
11524
+** application to avoid resource leaks by ensuring that
11525
+** sqlite3_recover_finish() is called on all allocated handles.
11526
+*/
11527
+sqlite3_recover *sqlite3_recover_init(
11528
+ sqlite3* db,
11529
+ const char *zDb,
11530
+ const char *zUri
11531
+);
11532
+sqlite3_recover *sqlite3_recover_init_sql(
11533
+ sqlite3* db,
11534
+ const char *zDb,
11535
+ int (*xSql)(void*, const char*),
11536
+ void *pCtx
11537
+);
11538
+
11539
+/*
11540
+** Configure an sqlite3_recover object that has just been created using
11541
+** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
11542
+** may only be called before the first call to sqlite3_recover_step()
11543
+** or sqlite3_recover_run() on the object.
11544
+**
11545
+** The second argument passed to this function must be one of the
11546
+** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
11547
+** depend on the specific SQLITE_RECOVER_* symbol in use.
11548
+**
11549
+** SQLITE_OK is returned if the configuration operation was successful,
11550
+** or an SQLite error code otherwise.
11551
+*/
11552
+int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
11553
+
11554
+/*
11555
+** SQLITE_RECOVER_LOST_AND_FOUND:
11556
+** The pArg argument points to a string buffer containing the name
11557
+** of a "lost-and-found" table in the output database, or NULL. If
11558
+** the argument is non-NULL and the database contains seemingly
11559
+** valid pages that cannot be associated with any table in the
11560
+** recovered part of the schema, data is extracted from these
11561
+** pages to add to the lost-and-found table.
11562
+**
11563
+** SQLITE_RECOVER_FREELIST_CORRUPT:
11564
+** The pArg value must actually be a pointer to a value of type
11565
+** int containing value 0 or 1 cast as a (void*). If this option is set
11566
+** (argument is 1) and a lost-and-found table has been configured using
11567
+** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
11568
+** corrupt and an attempt is made to recover records from pages that
11569
+** appear to be linked into the freelist. Otherwise, pages on the freelist
11570
+** are ignored. Setting this option can recover more data from the
11571
+** database, but often ends up "recovering" deleted records. The default
11572
+** value is 0 (clear).
11573
+**
11574
+** SQLITE_RECOVER_ROWIDS:
11575
+** The pArg value must actually be a pointer to a value of type
11576
+** int containing value 0 or 1 cast as a (void*). If this option is set
11577
+** (argument is 1), then an attempt is made to recover rowid values
11578
+** that are not also INTEGER PRIMARY KEY values. If this option is
11579
+** clear, then new rowids are assigned to all recovered rows. The
11580
+** default value is 1 (set).
11581
+**
11582
+** SQLITE_RECOVER_SLOWINDEXES:
11583
+** The pArg value must actually be a pointer to a value of type
11584
+** int containing value 0 or 1 cast as a (void*). If this option is clear
11585
+** (argument is 0), then when creating an output database, the recover
11586
+** module creates and populates non-UNIQUE indexes right at the end of the
11587
+** recovery operation - after all recoverable data has been inserted
11588
+** into the new database. This is faster overall, but means that the
11589
+** final call to sqlite3_recover_step() for a recovery operation may
11590
+** be need to create a large number of indexes, which may be very slow.
11591
+**
11592
+** Or, if this option is set (argument is 1), then non-UNIQUE indexes
11593
+** are created in the output database before it is populated with
11594
+** recovered data. This is slower overall, but avoids the slow call
11595
+** to sqlite3_recover_step() at the end of the recovery operation.
11596
+**
11597
+** The default option value is 0.
11598
+*/
11599
+#define SQLITE_RECOVER_LOST_AND_FOUND 1
11600
+#define SQLITE_RECOVER_FREELIST_CORRUPT 2
11601
+#define SQLITE_RECOVER_ROWIDS 3
11602
+#define SQLITE_RECOVER_SLOWINDEXES 4
11603
+
11604
+/*
11605
+** Perform a unit of work towards the recovery operation. This function
11606
+** must normally be called multiple times to complete database recovery.
11607
+**
11608
+** If no error occurs but the recovery operation is not completed, this
11609
+** function returns SQLITE_OK. If recovery has been completed successfully
11610
+** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
11611
+** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
11612
+** considered an error if some or all of the data cannot be recovered
11613
+** due to database corruption.
11614
+**
11615
+** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
11616
+** all further such calls on the same recover handle are no-ops that return
11617
+** the same non-SQLITE_OK value.
11618
+*/
11619
+int sqlite3_recover_step(sqlite3_recover*);
11620
+
11621
+/*
11622
+** Run the recovery operation to completion. Return SQLITE_OK if successful,
11623
+** or an SQLite error code otherwise. Calling this function is the same
11624
+** as executing:
11625
+**
11626
+** while( SQLITE_OK==sqlite3_recover_step(p) );
11627
+** return sqlite3_recover_errcode(p);
11628
+*/
11629
+int sqlite3_recover_run(sqlite3_recover*);
11630
+
11631
+/*
11632
+** If an error has been encountered during a prior call to
11633
+** sqlite3_recover_step(), then this function attempts to return a
11634
+** pointer to a buffer containing an English language explanation of
11635
+** the error. If no error message is available, or if an out-of memory
11636
+** error occurs while attempting to allocate a buffer in which to format
11637
+** the error message, NULL is returned.
11638
+**
11639
+** The returned buffer remains valid until the sqlite3_recover handle is
11640
+** destroyed using sqlite3_recover_finish().
11641
+*/
11642
+const char *sqlite3_recover_errmsg(sqlite3_recover*);
11643
+
11644
+/*
11645
+** If this function is called on an sqlite3_recover handle after
11646
+** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
11647
+*/
11648
+int sqlite3_recover_errcode(sqlite3_recover*);
11649
+
11650
+/*
11651
+** Clean up a recovery object created by a call to sqlite3_recover_init().
11652
+** The results of using a recovery object with any API after it has been
11653
+** passed to this function are undefined.
11654
+**
11655
+** This function returns the same value as sqlite3_recover_errcode().
11656
+*/
11657
+int sqlite3_recover_finish(sqlite3_recover*);
11658
+
11659
+
11660
+#ifdef __cplusplus
11661
+} /* end of the 'extern "C"' block */
11662
+#endif
11663
+
11664
+#endif /* ifndef _SQLITE_RECOVER_H */
11665
+
11666
+/************************* End ../ext/recover/sqlite3recover.h ********************/
11667
+# ifndef SQLITE_HAVE_SQLITE3R
1141511668
/************************* Begin ../ext/recover/dbdata.c ******************/
1141611669
/*
1141711670
** 2019-04-17
1141811671
**
1141911672
** The author disclaims copyright to this source code. In place of
@@ -12355,262 +12608,10 @@
1235512608
}
1235612609
1235712610
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1235812611
1235912612
/************************* End ../ext/recover/dbdata.c ********************/
12360
-/************************* Begin ../ext/recover/sqlite3recover.h ******************/
12361
-/*
12362
-** 2022-08-27
12363
-**
12364
-** The author disclaims copyright to this source code. In place of
12365
-** a legal notice, here is a blessing:
12366
-**
12367
-** May you do good and not evil.
12368
-** May you find forgiveness for yourself and forgive others.
12369
-** May you share freely, never taking more than you give.
12370
-**
12371
-*************************************************************************
12372
-**
12373
-** This file contains the public interface to the "recover" extension -
12374
-** an SQLite extension designed to recover data from corrupted database
12375
-** files.
12376
-*/
12377
-
12378
-/*
12379
-** OVERVIEW:
12380
-**
12381
-** To use the API to recover data from a corrupted database, an
12382
-** application:
12383
-**
12384
-** 1) Creates an sqlite3_recover handle by calling either
12385
-** sqlite3_recover_init() or sqlite3_recover_init_sql().
12386
-**
12387
-** 2) Configures the new handle using one or more calls to
12388
-** sqlite3_recover_config().
12389
-**
12390
-** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
12391
-** the handle until it returns something other than SQLITE_OK. If it
12392
-** returns SQLITE_DONE, then the recovery operation completed without
12393
-** error. If it returns some other non-SQLITE_OK value, then an error
12394
-** has occurred.
12395
-**
12396
-** 4) Retrieves any error code and English language error message using the
12397
-** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
12398
-** respectively.
12399
-**
12400
-** 5) Destroys the sqlite3_recover handle and frees all resources
12401
-** using sqlite3_recover_finish().
12402
-**
12403
-** The application may abandon the recovery operation at any point
12404
-** before it is finished by passing the sqlite3_recover handle to
12405
-** sqlite3_recover_finish(). This is not an error, but the final state
12406
-** of the output database, or the results of running the partial script
12407
-** delivered to the SQL callback, are undefined.
12408
-*/
12409
-
12410
-#ifndef _SQLITE_RECOVER_H
12411
-#define _SQLITE_RECOVER_H
12412
-
12413
-/* #include "sqlite3.h" */
12414
-
12415
-#ifdef __cplusplus
12416
-extern "C" {
12417
-#endif
12418
-
12419
-/*
12420
-** An instance of the sqlite3_recover object represents a recovery
12421
-** operation in progress.
12422
-**
12423
-** Constructors:
12424
-**
12425
-** sqlite3_recover_init()
12426
-** sqlite3_recover_init_sql()
12427
-**
12428
-** Destructor:
12429
-**
12430
-** sqlite3_recover_finish()
12431
-**
12432
-** Methods:
12433
-**
12434
-** sqlite3_recover_config()
12435
-** sqlite3_recover_errcode()
12436
-** sqlite3_recover_errmsg()
12437
-** sqlite3_recover_run()
12438
-** sqlite3_recover_step()
12439
-*/
12440
-typedef struct sqlite3_recover sqlite3_recover;
12441
-
12442
-/*
12443
-** These two APIs attempt to create and return a new sqlite3_recover object.
12444
-** In both cases the first two arguments identify the (possibly
12445
-** corrupt) database to recover data from. The first argument is an open
12446
-** database handle and the second the name of a database attached to that
12447
-** handle (i.e. "main", "temp" or the name of an attached database).
12448
-**
12449
-** If sqlite3_recover_init() is used to create the new sqlite3_recover
12450
-** handle, then data is recovered into a new database, identified by
12451
-** string parameter zUri. zUri may be an absolute or relative file path,
12452
-** or may be an SQLite URI. If the identified database file already exists,
12453
-** it is overwritten.
12454
-**
12455
-** If sqlite3_recover_init_sql() is invoked, then any recovered data will
12456
-** be returned to the user as a series of SQL statements. Executing these
12457
-** SQL statements results in the same database as would have been created
12458
-** had sqlite3_recover_init() been used. For each SQL statement in the
12459
-** output, the callback function passed as the third argument (xSql) is
12460
-** invoked once. The first parameter is a passed a copy of the fourth argument
12461
-** to this function (pCtx) as its first parameter, and a pointer to a
12462
-** nul-terminated buffer containing the SQL statement formated as UTF-8 as
12463
-** the second. If the xSql callback returns any value other than SQLITE_OK,
12464
-** then processing is immediately abandoned and the value returned used as
12465
-** the recover handle error code (see below).
12466
-**
12467
-** If an out-of-memory error occurs, NULL may be returned instead of
12468
-** a valid handle. In all other cases, it is the responsibility of the
12469
-** application to avoid resource leaks by ensuring that
12470
-** sqlite3_recover_finish() is called on all allocated handles.
12471
-*/
12472
-sqlite3_recover *sqlite3_recover_init(
12473
- sqlite3* db,
12474
- const char *zDb,
12475
- const char *zUri
12476
-);
12477
-sqlite3_recover *sqlite3_recover_init_sql(
12478
- sqlite3* db,
12479
- const char *zDb,
12480
- int (*xSql)(void*, const char*),
12481
- void *pCtx
12482
-);
12483
-
12484
-/*
12485
-** Configure an sqlite3_recover object that has just been created using
12486
-** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
12487
-** may only be called before the first call to sqlite3_recover_step()
12488
-** or sqlite3_recover_run() on the object.
12489
-**
12490
-** The second argument passed to this function must be one of the
12491
-** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
12492
-** depend on the specific SQLITE_RECOVER_* symbol in use.
12493
-**
12494
-** SQLITE_OK is returned if the configuration operation was successful,
12495
-** or an SQLite error code otherwise.
12496
-*/
12497
-int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
12498
-
12499
-/*
12500
-** SQLITE_RECOVER_LOST_AND_FOUND:
12501
-** The pArg argument points to a string buffer containing the name
12502
-** of a "lost-and-found" table in the output database, or NULL. If
12503
-** the argument is non-NULL and the database contains seemingly
12504
-** valid pages that cannot be associated with any table in the
12505
-** recovered part of the schema, data is extracted from these
12506
-** pages to add to the lost-and-found table.
12507
-**
12508
-** SQLITE_RECOVER_FREELIST_CORRUPT:
12509
-** The pArg value must actually be a pointer to a value of type
12510
-** int containing value 0 or 1 cast as a (void*). If this option is set
12511
-** (argument is 1) and a lost-and-found table has been configured using
12512
-** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
12513
-** corrupt and an attempt is made to recover records from pages that
12514
-** appear to be linked into the freelist. Otherwise, pages on the freelist
12515
-** are ignored. Setting this option can recover more data from the
12516
-** database, but often ends up "recovering" deleted records. The default
12517
-** value is 0 (clear).
12518
-**
12519
-** SQLITE_RECOVER_ROWIDS:
12520
-** The pArg value must actually be a pointer to a value of type
12521
-** int containing value 0 or 1 cast as a (void*). If this option is set
12522
-** (argument is 1), then an attempt is made to recover rowid values
12523
-** that are not also INTEGER PRIMARY KEY values. If this option is
12524
-** clear, then new rowids are assigned to all recovered rows. The
12525
-** default value is 1 (set).
12526
-**
12527
-** SQLITE_RECOVER_SLOWINDEXES:
12528
-** The pArg value must actually be a pointer to a value of type
12529
-** int containing value 0 or 1 cast as a (void*). If this option is clear
12530
-** (argument is 0), then when creating an output database, the recover
12531
-** module creates and populates non-UNIQUE indexes right at the end of the
12532
-** recovery operation - after all recoverable data has been inserted
12533
-** into the new database. This is faster overall, but means that the
12534
-** final call to sqlite3_recover_step() for a recovery operation may
12535
-** be need to create a large number of indexes, which may be very slow.
12536
-**
12537
-** Or, if this option is set (argument is 1), then non-UNIQUE indexes
12538
-** are created in the output database before it is populated with
12539
-** recovered data. This is slower overall, but avoids the slow call
12540
-** to sqlite3_recover_step() at the end of the recovery operation.
12541
-**
12542
-** The default option value is 0.
12543
-*/
12544
-#define SQLITE_RECOVER_LOST_AND_FOUND 1
12545
-#define SQLITE_RECOVER_FREELIST_CORRUPT 2
12546
-#define SQLITE_RECOVER_ROWIDS 3
12547
-#define SQLITE_RECOVER_SLOWINDEXES 4
12548
-
12549
-/*
12550
-** Perform a unit of work towards the recovery operation. This function
12551
-** must normally be called multiple times to complete database recovery.
12552
-**
12553
-** If no error occurs but the recovery operation is not completed, this
12554
-** function returns SQLITE_OK. If recovery has been completed successfully
12555
-** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
12556
-** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
12557
-** considered an error if some or all of the data cannot be recovered
12558
-** due to database corruption.
12559
-**
12560
-** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
12561
-** all further such calls on the same recover handle are no-ops that return
12562
-** the same non-SQLITE_OK value.
12563
-*/
12564
-int sqlite3_recover_step(sqlite3_recover*);
12565
-
12566
-/*
12567
-** Run the recovery operation to completion. Return SQLITE_OK if successful,
12568
-** or an SQLite error code otherwise. Calling this function is the same
12569
-** as executing:
12570
-**
12571
-** while( SQLITE_OK==sqlite3_recover_step(p) );
12572
-** return sqlite3_recover_errcode(p);
12573
-*/
12574
-int sqlite3_recover_run(sqlite3_recover*);
12575
-
12576
-/*
12577
-** If an error has been encountered during a prior call to
12578
-** sqlite3_recover_step(), then this function attempts to return a
12579
-** pointer to a buffer containing an English language explanation of
12580
-** the error. If no error message is available, or if an out-of memory
12581
-** error occurs while attempting to allocate a buffer in which to format
12582
-** the error message, NULL is returned.
12583
-**
12584
-** The returned buffer remains valid until the sqlite3_recover handle is
12585
-** destroyed using sqlite3_recover_finish().
12586
-*/
12587
-const char *sqlite3_recover_errmsg(sqlite3_recover*);
12588
-
12589
-/*
12590
-** If this function is called on an sqlite3_recover handle after
12591
-** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
12592
-*/
12593
-int sqlite3_recover_errcode(sqlite3_recover*);
12594
-
12595
-/*
12596
-** Clean up a recovery object created by a call to sqlite3_recover_init().
12597
-** The results of using a recovery object with any API after it has been
12598
-** passed to this function are undefined.
12599
-**
12600
-** This function returns the same value as sqlite3_recover_errcode().
12601
-*/
12602
-int sqlite3_recover_finish(sqlite3_recover*);
12603
-
12604
-
12605
-#ifdef __cplusplus
12606
-} /* end of the 'extern "C"' block */
12607
-#endif
12608
-
12609
-#endif /* ifndef _SQLITE_RECOVER_H */
12610
-
12611
-/************************* End ../ext/recover/sqlite3recover.h ********************/
1261212613
/************************* Begin ../ext/recover/sqlite3recover.c ******************/
1261312614
/*
1261412615
** 2022-08-27
1261512616
**
1261612617
** The author disclaims copyright to this source code. In place of
@@ -14627,10 +14628,11 @@
1462714628
sqlite3_free(pTab);
1462814629
}
1462914630
p->pTblList = 0;
1463014631
sqlite3_finalize(p->pGetPage);
1463114632
p->pGetPage = 0;
14633
+ sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
1463214634
1463314635
{
1463414636
#ifndef NDEBUG
1463514637
int res =
1463614638
#endif
@@ -14925,10 +14927,11 @@
1492514927
**
1492614928
** Also preserved are:
1492714929
**
1492814930
** + first freelist page (32-bits at offset 32)
1492914931
** + size of freelist (32-bits at offset 36)
14932
+ ** + the wal-mode flags (16-bits at offset 18)
1493014933
**
1493114934
** We also try to preserve the auto-vacuum, incr-value, user-version
1493214935
** and application-id fields - all 32 bit quantities at offsets
1493314936
** 52, 60, 64 and 68. All other fields are set to known good values.
1493414937
**
@@ -14988,11 +14991,12 @@
1498814991
p->pgsz = nByte;
1498914992
p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
1499014993
if( p->pPage1Cache ){
1499114994
p->pPage1Disk = &p->pPage1Cache[nByte];
1499214995
memcpy(p->pPage1Disk, aBuf, nByte);
14993
-
14996
+ aHdr[18] = a[18];
14997
+ aHdr[19] = a[19];
1499414998
recoverPutU32(&aHdr[28], dbsz);
1499514999
recoverPutU32(&aHdr[56], enc);
1499615000
recoverPutU16(&aHdr[105], pgsz-nReserve);
1499715001
if( pgsz==65536 ) pgsz = 1;
1499815002
recoverPutU16(&aHdr[16], pgsz);
@@ -15184,10 +15188,11 @@
1518415188
/* Open the output database. And register required virtual tables and
1518515189
** user functions with the new handle. */
1518615190
recoverOpenOutput(p);
1518715191
1518815192
/* Open transactions on both the input and output databases. */
15193
+ sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
1518915194
recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
1519015195
recoverExec(p, p->dbIn, "BEGIN");
1519115196
if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
1519215197
recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
1519315198
recoverTransferSettings(p);
@@ -15467,10 +15472,14 @@
1546715472
}
1546815473
1546915474
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1547015475
1547115476
/************************* End ../ext/recover/sqlite3recover.c ********************/
15477
+# endif
15478
+#endif
15479
+#ifdef SQLITE_SHELL_EXTSRC
15480
+# include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
1547215481
#endif
1547315482
1547415483
#if defined(SQLITE_ENABLE_SESSION)
1547515484
/*
1547615485
** State information for a single open session
@@ -16273,11 +16282,11 @@
1627316282
"readfile",
1627416283
"writefile",
1627516284
"zipfile",
1627616285
"zipfile_cds",
1627716286
};
16278
- UNUSED_PARAMETER(zA2);
16287
+ UNUSED_PARAMETER(zA1);
1627916288
UNUSED_PARAMETER(zA3);
1628016289
UNUSED_PARAMETER(zA4);
1628116290
switch( op ){
1628216291
case SQLITE_ATTACH: {
1628316292
#ifndef SQLITE_SHELL_FIDDLE
@@ -16288,11 +16297,11 @@
1628816297
break;
1628916298
}
1629016299
case SQLITE_FUNCTION: {
1629116300
int i;
1629216301
for(i=0; i<ArraySize(azProhibitedFunctions); i++){
16293
- if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
16302
+ if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
1629416303
failIfSafeMode(p, "cannot use the %s() function in safe mode",
1629516304
azProhibitedFunctions[i]);
1629616305
}
1629716306
}
1629816307
break;
@@ -19513,10 +19522,11 @@
1951319522
sqlite3_open(":memory:", &p->db);
1951419523
return;
1951519524
}
1951619525
exit(1);
1951719526
}
19527
+
1951819528
#ifndef SQLITE_OMIT_LOAD_EXTENSION
1951919529
sqlite3_enable_load_extension(p->db, 1);
1952019530
#endif
1952119531
sqlite3_shathree_init(p->db, 0, 0);
1952219532
sqlite3_uint_init(p->db, 0, 0);
@@ -19535,10 +19545,38 @@
1953519545
if( !p->bSafeModePersist ){
1953619546
sqlite3_zipfile_init(p->db, 0, 0);
1953719547
sqlite3_sqlar_init(p->db, 0, 0);
1953819548
}
1953919549
#endif
19550
+#ifdef SQLITE_SHELL_EXTFUNCS
19551
+ /* Create a preprocessing mechanism for extensions to make
19552
+ * their own provisions for being built into the shell.
19553
+ * This is a short-span macro. See further below for usage.
19554
+ */
19555
+#define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
19556
+#define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
19557
+ /* Let custom-included extensions get their ..._init() called.
19558
+ * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
19559
+ * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
19560
+ * inititialization routine to be called.
19561
+ */
19562
+ {
19563
+ int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
19564
+ /* Let custom-included extensions expose their functionality.
19565
+ * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
19566
+ * the SQL functions, virtual tables, collating sequences or
19567
+ * VFS's implemented by the extension to be registered.
19568
+ */
19569
+ if( irc==SQLITE_OK
19570
+ || irc==SQLITE_OK_LOAD_PERMANENTLY ){
19571
+ SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
19572
+ }
19573
+#undef SHELL_SUB_MACRO
19574
+#undef SHELL_SUBMACRO
19575
+ }
19576
+#endif
19577
+
1954019578
sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
1954119579
shellAddSchemaName, 0, 0);
1954219580
sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
1954319581
shellModuleSchema, 0, 0);
1954419582
sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
@@ -19555,10 +19593,11 @@
1955519593
sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
1955619594
editFunc, 0, 0);
1955719595
sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
1955819596
editFunc, 0, 0);
1955919597
#endif
19598
+
1956019599
if( p->openMode==SHELL_OPEN_ZIPFILE ){
1956119600
char *zSql = sqlite3_mprintf(
1956219601
"CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
1956319602
shell_check_oom(zSql);
1956419603
sqlite3_exec(p->db, zSql, 0, 0, 0);
@@ -25054,11 +25093,11 @@
2505425093
rc = 1;
2505525094
goto meta_command_exit;
2505625095
}
2505725096
}else{
2505825097
output_file_close(p->traceOut);
25059
- p->traceOut = output_file_open(azArg[1], 0);
25098
+ p->traceOut = output_file_open(z, 0);
2506025099
}
2506125100
}
2506225101
if( p->traceOut==0 ){
2506325102
sqlite3_trace_v2(p->db, 0, 0, 0);
2506425103
}else{
@@ -25357,17 +25396,15 @@
2535725396
return 0;
2535825397
return quickscan(zLine, QSS_Start)==QSS_Start;
2535925398
}
2536025399
2536125400
/*
25362
-** We need a default sqlite3_complete() implementation to use in case
25363
-** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
25364
-** any arbitrary text is a complete SQL statement. This is not very
25365
-** user-friendly, but it does seem to work.
25401
+** The CLI needs a working sqlite3_complete() to work properly. So error
25402
+** out of the build if compiling with SQLITE_OMIT_COMPLETE.
2536625403
*/
2536725404
#ifdef SQLITE_OMIT_COMPLETE
25368
-#define sqlite3_complete(x) 1
25405
+# error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
2536925406
#endif
2537025407
2537125408
/*
2537225409
** Return true if zSql is a complete SQL statement. Return false if it
2537325410
** ends in the middle of a string literal or C-style comment.
@@ -25650,14 +25687,48 @@
2565025687
home_dir = z;
2565125688
}
2565225689
2565325690
return home_dir;
2565425691
}
25692
+
25693
+/*
25694
+** On non-Windows platforms, look for $XDG_CONFIG_HOME.
25695
+** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
25696
+** the path to it, else return 0. The result is cached for
25697
+** subsequent calls.
25698
+*/
25699
+static const char *find_xdg_config(void){
25700
+#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
25701
+ || defined(__RTP__) || defined(_WRS_KERNEL)
25702
+ return 0;
25703
+#else
25704
+ static int alreadyTried = 0;
25705
+ static char *zConfig = 0;
25706
+ const char *zXdgHome;
25707
+
25708
+ if( alreadyTried!=0 ){
25709
+ return zConfig;
25710
+ }
25711
+ alreadyTried = 1;
25712
+ zXdgHome = getenv("XDG_CONFIG_HOME");
25713
+ if( zXdgHome==0 ){
25714
+ return 0;
25715
+ }
25716
+ zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
25717
+ shell_check_oom(zConfig);
25718
+ if( access(zConfig,0)!=0 ){
25719
+ sqlite3_free(zConfig);
25720
+ zConfig = 0;
25721
+ }
25722
+ return zConfig;
25723
+#endif
25724
+}
2565525725
2565625726
/*
2565725727
** Read input from the file given by sqliterc_override. Or if that
25658
-** parameter is NULL, take input from ~/.sqliterc
25728
+** parameter is NULL, take input from the first of find_xdg_config()
25729
+** or ~/.sqliterc which is found.
2565925730
**
2566025731
** Returns the number of errors.
2566125732
*/
2566225733
static void process_sqliterc(
2566325734
ShellState *p, /* Configuration data */
@@ -25667,11 +25738,14 @@
2566725738
const char *sqliterc = sqliterc_override;
2566825739
char *zBuf = 0;
2566925740
FILE *inSaved = p->in;
2567025741
int savedLineno = p->lineno;
2567125742
25672
- if (sqliterc == NULL) {
25743
+ if( sqliterc == NULL ){
25744
+ sqliterc = find_xdg_config();
25745
+ }
25746
+ if( sqliterc == NULL ){
2567325747
home_dir = find_home_dir(0);
2567425748
if( home_dir==0 ){
2567525749
raw_printf(stderr, "-- warning: cannot find home directory;"
2567625750
" cannot read ~/.sqliterc\n");
2567725751
return;
@@ -26130,11 +26204,11 @@
2613026204
if( zVfs ){
2613126205
sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
2613226206
if( pVfs ){
2613326207
sqlite3_vfs_register(pVfs, 1);
2613426208
}else{
26135
- utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
26209
+ utf8_printf(stderr, "no such VFS: \"%s\"\n", zVfs);
2613626210
exit(1);
2613726211
}
2613826212
}
2613926213
2614026214
if( data.pAuxDb->zDbFilename==0 ){
2614126215
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -2056,11 +2056,11 @@
2056 }
2057
2058 /* Compute a string using sqlite3_vsnprintf() with a maximum length
2059 ** of 50 bytes and add it to the hash.
2060 */
2061 static void hash_step_vformat(
2062 SHA3Context *p, /* Add content to this context */
2063 const char *zFormat,
2064 ...
2065 ){
2066 va_list ap;
@@ -2152,11 +2152,11 @@
2152 }
2153 nCol = sqlite3_column_count(pStmt);
2154 z = sqlite3_sql(pStmt);
2155 if( z ){
2156 n = (int)strlen(z);
2157 hash_step_vformat(&cx,"S%d:",n);
2158 SHA3Update(&cx,(unsigned char*)z,n);
2159 }
2160
2161 /* Compute a hash over the result of the query */
2162 while( SQLITE_ROW==sqlite3_step(pStmt) ){
@@ -2196,18 +2196,18 @@
2196 break;
2197 }
2198 case SQLITE_TEXT: {
2199 int n2 = sqlite3_column_bytes(pStmt, i);
2200 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2201 hash_step_vformat(&cx,"T%d:",n2);
2202 SHA3Update(&cx, z2, n2);
2203 break;
2204 }
2205 case SQLITE_BLOB: {
2206 int n2 = sqlite3_column_bytes(pStmt, i);
2207 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2208 hash_step_vformat(&cx,"B%d:",n2);
2209 SHA3Update(&cx, z2, n2);
2210 break;
2211 }
2212 }
2213 }
@@ -3928,11 +3928,11 @@
3928 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
3929 && (p->z[p->i+1]&0xc0)==0x80 ){
3930 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
3931 p->i += 2;
3932 if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
3933 }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
3934 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
3935 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
3936 | (p->z[p->i+2]&0x3f);
3937 p->i += 3;
3938 if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
@@ -4455,19 +4455,19 @@
4455 /* The following is a performance optimization. If the regex begins with
4456 ** ".*" (if the input regex lacks an initial "^") and afterwards there are
4457 ** one or more matching characters, enter those matching characters into
4458 ** zInit[]. The re_match() routine can then search ahead in the input
4459 ** string looking for the initial match without having to run the whole
4460 ** regex engine over the string. Do not worry able trying to match
4461 ** unicode characters beyond plane 0 - those are very rare and this is
4462 ** just an optimization. */
4463 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
4464 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
4465 unsigned x = pRe->aArg[i];
4466 if( x<=127 ){
4467 pRe->zInit[j++] = (unsigned char)x;
4468 }else if( x<=0xfff ){
4469 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
4470 pRe->zInit[j++] = 0x80 | (x&0x3f);
4471 }else if( x<=0xffff ){
4472 pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12));
4473 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
@@ -11410,10 +11410,263 @@
11410 #define SQLITE_SHELL_HAVE_RECOVER 1
11411 #else
11412 #define SQLITE_SHELL_HAVE_RECOVER 0
11413 #endif
11414 #if SQLITE_SHELL_HAVE_RECOVER
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11415 /************************* Begin ../ext/recover/dbdata.c ******************/
11416 /*
11417 ** 2019-04-17
11418 **
11419 ** The author disclaims copyright to this source code. In place of
@@ -12355,262 +12608,10 @@
12355 }
12356
12357 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
12358
12359 /************************* End ../ext/recover/dbdata.c ********************/
12360 /************************* Begin ../ext/recover/sqlite3recover.h ******************/
12361 /*
12362 ** 2022-08-27
12363 **
12364 ** The author disclaims copyright to this source code. In place of
12365 ** a legal notice, here is a blessing:
12366 **
12367 ** May you do good and not evil.
12368 ** May you find forgiveness for yourself and forgive others.
12369 ** May you share freely, never taking more than you give.
12370 **
12371 *************************************************************************
12372 **
12373 ** This file contains the public interface to the "recover" extension -
12374 ** an SQLite extension designed to recover data from corrupted database
12375 ** files.
12376 */
12377
12378 /*
12379 ** OVERVIEW:
12380 **
12381 ** To use the API to recover data from a corrupted database, an
12382 ** application:
12383 **
12384 ** 1) Creates an sqlite3_recover handle by calling either
12385 ** sqlite3_recover_init() or sqlite3_recover_init_sql().
12386 **
12387 ** 2) Configures the new handle using one or more calls to
12388 ** sqlite3_recover_config().
12389 **
12390 ** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
12391 ** the handle until it returns something other than SQLITE_OK. If it
12392 ** returns SQLITE_DONE, then the recovery operation completed without
12393 ** error. If it returns some other non-SQLITE_OK value, then an error
12394 ** has occurred.
12395 **
12396 ** 4) Retrieves any error code and English language error message using the
12397 ** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
12398 ** respectively.
12399 **
12400 ** 5) Destroys the sqlite3_recover handle and frees all resources
12401 ** using sqlite3_recover_finish().
12402 **
12403 ** The application may abandon the recovery operation at any point
12404 ** before it is finished by passing the sqlite3_recover handle to
12405 ** sqlite3_recover_finish(). This is not an error, but the final state
12406 ** of the output database, or the results of running the partial script
12407 ** delivered to the SQL callback, are undefined.
12408 */
12409
12410 #ifndef _SQLITE_RECOVER_H
12411 #define _SQLITE_RECOVER_H
12412
12413 /* #include "sqlite3.h" */
12414
12415 #ifdef __cplusplus
12416 extern "C" {
12417 #endif
12418
12419 /*
12420 ** An instance of the sqlite3_recover object represents a recovery
12421 ** operation in progress.
12422 **
12423 ** Constructors:
12424 **
12425 ** sqlite3_recover_init()
12426 ** sqlite3_recover_init_sql()
12427 **
12428 ** Destructor:
12429 **
12430 ** sqlite3_recover_finish()
12431 **
12432 ** Methods:
12433 **
12434 ** sqlite3_recover_config()
12435 ** sqlite3_recover_errcode()
12436 ** sqlite3_recover_errmsg()
12437 ** sqlite3_recover_run()
12438 ** sqlite3_recover_step()
12439 */
12440 typedef struct sqlite3_recover sqlite3_recover;
12441
12442 /*
12443 ** These two APIs attempt to create and return a new sqlite3_recover object.
12444 ** In both cases the first two arguments identify the (possibly
12445 ** corrupt) database to recover data from. The first argument is an open
12446 ** database handle and the second the name of a database attached to that
12447 ** handle (i.e. "main", "temp" or the name of an attached database).
12448 **
12449 ** If sqlite3_recover_init() is used to create the new sqlite3_recover
12450 ** handle, then data is recovered into a new database, identified by
12451 ** string parameter zUri. zUri may be an absolute or relative file path,
12452 ** or may be an SQLite URI. If the identified database file already exists,
12453 ** it is overwritten.
12454 **
12455 ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
12456 ** be returned to the user as a series of SQL statements. Executing these
12457 ** SQL statements results in the same database as would have been created
12458 ** had sqlite3_recover_init() been used. For each SQL statement in the
12459 ** output, the callback function passed as the third argument (xSql) is
12460 ** invoked once. The first parameter is a passed a copy of the fourth argument
12461 ** to this function (pCtx) as its first parameter, and a pointer to a
12462 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
12463 ** the second. If the xSql callback returns any value other than SQLITE_OK,
12464 ** then processing is immediately abandoned and the value returned used as
12465 ** the recover handle error code (see below).
12466 **
12467 ** If an out-of-memory error occurs, NULL may be returned instead of
12468 ** a valid handle. In all other cases, it is the responsibility of the
12469 ** application to avoid resource leaks by ensuring that
12470 ** sqlite3_recover_finish() is called on all allocated handles.
12471 */
12472 sqlite3_recover *sqlite3_recover_init(
12473 sqlite3* db,
12474 const char *zDb,
12475 const char *zUri
12476 );
12477 sqlite3_recover *sqlite3_recover_init_sql(
12478 sqlite3* db,
12479 const char *zDb,
12480 int (*xSql)(void*, const char*),
12481 void *pCtx
12482 );
12483
12484 /*
12485 ** Configure an sqlite3_recover object that has just been created using
12486 ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
12487 ** may only be called before the first call to sqlite3_recover_step()
12488 ** or sqlite3_recover_run() on the object.
12489 **
12490 ** The second argument passed to this function must be one of the
12491 ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
12492 ** depend on the specific SQLITE_RECOVER_* symbol in use.
12493 **
12494 ** SQLITE_OK is returned if the configuration operation was successful,
12495 ** or an SQLite error code otherwise.
12496 */
12497 int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
12498
12499 /*
12500 ** SQLITE_RECOVER_LOST_AND_FOUND:
12501 ** The pArg argument points to a string buffer containing the name
12502 ** of a "lost-and-found" table in the output database, or NULL. If
12503 ** the argument is non-NULL and the database contains seemingly
12504 ** valid pages that cannot be associated with any table in the
12505 ** recovered part of the schema, data is extracted from these
12506 ** pages to add to the lost-and-found table.
12507 **
12508 ** SQLITE_RECOVER_FREELIST_CORRUPT:
12509 ** The pArg value must actually be a pointer to a value of type
12510 ** int containing value 0 or 1 cast as a (void*). If this option is set
12511 ** (argument is 1) and a lost-and-found table has been configured using
12512 ** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
12513 ** corrupt and an attempt is made to recover records from pages that
12514 ** appear to be linked into the freelist. Otherwise, pages on the freelist
12515 ** are ignored. Setting this option can recover more data from the
12516 ** database, but often ends up "recovering" deleted records. The default
12517 ** value is 0 (clear).
12518 **
12519 ** SQLITE_RECOVER_ROWIDS:
12520 ** The pArg value must actually be a pointer to a value of type
12521 ** int containing value 0 or 1 cast as a (void*). If this option is set
12522 ** (argument is 1), then an attempt is made to recover rowid values
12523 ** that are not also INTEGER PRIMARY KEY values. If this option is
12524 ** clear, then new rowids are assigned to all recovered rows. The
12525 ** default value is 1 (set).
12526 **
12527 ** SQLITE_RECOVER_SLOWINDEXES:
12528 ** The pArg value must actually be a pointer to a value of type
12529 ** int containing value 0 or 1 cast as a (void*). If this option is clear
12530 ** (argument is 0), then when creating an output database, the recover
12531 ** module creates and populates non-UNIQUE indexes right at the end of the
12532 ** recovery operation - after all recoverable data has been inserted
12533 ** into the new database. This is faster overall, but means that the
12534 ** final call to sqlite3_recover_step() for a recovery operation may
12535 ** be need to create a large number of indexes, which may be very slow.
12536 **
12537 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
12538 ** are created in the output database before it is populated with
12539 ** recovered data. This is slower overall, but avoids the slow call
12540 ** to sqlite3_recover_step() at the end of the recovery operation.
12541 **
12542 ** The default option value is 0.
12543 */
12544 #define SQLITE_RECOVER_LOST_AND_FOUND 1
12545 #define SQLITE_RECOVER_FREELIST_CORRUPT 2
12546 #define SQLITE_RECOVER_ROWIDS 3
12547 #define SQLITE_RECOVER_SLOWINDEXES 4
12548
12549 /*
12550 ** Perform a unit of work towards the recovery operation. This function
12551 ** must normally be called multiple times to complete database recovery.
12552 **
12553 ** If no error occurs but the recovery operation is not completed, this
12554 ** function returns SQLITE_OK. If recovery has been completed successfully
12555 ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
12556 ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
12557 ** considered an error if some or all of the data cannot be recovered
12558 ** due to database corruption.
12559 **
12560 ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
12561 ** all further such calls on the same recover handle are no-ops that return
12562 ** the same non-SQLITE_OK value.
12563 */
12564 int sqlite3_recover_step(sqlite3_recover*);
12565
12566 /*
12567 ** Run the recovery operation to completion. Return SQLITE_OK if successful,
12568 ** or an SQLite error code otherwise. Calling this function is the same
12569 ** as executing:
12570 **
12571 ** while( SQLITE_OK==sqlite3_recover_step(p) );
12572 ** return sqlite3_recover_errcode(p);
12573 */
12574 int sqlite3_recover_run(sqlite3_recover*);
12575
12576 /*
12577 ** If an error has been encountered during a prior call to
12578 ** sqlite3_recover_step(), then this function attempts to return a
12579 ** pointer to a buffer containing an English language explanation of
12580 ** the error. If no error message is available, or if an out-of memory
12581 ** error occurs while attempting to allocate a buffer in which to format
12582 ** the error message, NULL is returned.
12583 **
12584 ** The returned buffer remains valid until the sqlite3_recover handle is
12585 ** destroyed using sqlite3_recover_finish().
12586 */
12587 const char *sqlite3_recover_errmsg(sqlite3_recover*);
12588
12589 /*
12590 ** If this function is called on an sqlite3_recover handle after
12591 ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
12592 */
12593 int sqlite3_recover_errcode(sqlite3_recover*);
12594
12595 /*
12596 ** Clean up a recovery object created by a call to sqlite3_recover_init().
12597 ** The results of using a recovery object with any API after it has been
12598 ** passed to this function are undefined.
12599 **
12600 ** This function returns the same value as sqlite3_recover_errcode().
12601 */
12602 int sqlite3_recover_finish(sqlite3_recover*);
12603
12604
12605 #ifdef __cplusplus
12606 } /* end of the 'extern "C"' block */
12607 #endif
12608
12609 #endif /* ifndef _SQLITE_RECOVER_H */
12610
12611 /************************* End ../ext/recover/sqlite3recover.h ********************/
12612 /************************* Begin ../ext/recover/sqlite3recover.c ******************/
12613 /*
12614 ** 2022-08-27
12615 **
12616 ** The author disclaims copyright to this source code. In place of
@@ -14627,10 +14628,11 @@
14627 sqlite3_free(pTab);
14628 }
14629 p->pTblList = 0;
14630 sqlite3_finalize(p->pGetPage);
14631 p->pGetPage = 0;
 
14632
14633 {
14634 #ifndef NDEBUG
14635 int res =
14636 #endif
@@ -14925,10 +14927,11 @@
14925 **
14926 ** Also preserved are:
14927 **
14928 ** + first freelist page (32-bits at offset 32)
14929 ** + size of freelist (32-bits at offset 36)
 
14930 **
14931 ** We also try to preserve the auto-vacuum, incr-value, user-version
14932 ** and application-id fields - all 32 bit quantities at offsets
14933 ** 52, 60, 64 and 68. All other fields are set to known good values.
14934 **
@@ -14988,11 +14991,12 @@
14988 p->pgsz = nByte;
14989 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
14990 if( p->pPage1Cache ){
14991 p->pPage1Disk = &p->pPage1Cache[nByte];
14992 memcpy(p->pPage1Disk, aBuf, nByte);
14993
 
14994 recoverPutU32(&aHdr[28], dbsz);
14995 recoverPutU32(&aHdr[56], enc);
14996 recoverPutU16(&aHdr[105], pgsz-nReserve);
14997 if( pgsz==65536 ) pgsz = 1;
14998 recoverPutU16(&aHdr[16], pgsz);
@@ -15184,10 +15188,11 @@
15184 /* Open the output database. And register required virtual tables and
15185 ** user functions with the new handle. */
15186 recoverOpenOutput(p);
15187
15188 /* Open transactions on both the input and output databases. */
 
15189 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
15190 recoverExec(p, p->dbIn, "BEGIN");
15191 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
15192 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
15193 recoverTransferSettings(p);
@@ -15467,10 +15472,14 @@
15467 }
15468
15469 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
15470
15471 /************************* End ../ext/recover/sqlite3recover.c ********************/
 
 
 
 
15472 #endif
15473
15474 #if defined(SQLITE_ENABLE_SESSION)
15475 /*
15476 ** State information for a single open session
@@ -16273,11 +16282,11 @@
16273 "readfile",
16274 "writefile",
16275 "zipfile",
16276 "zipfile_cds",
16277 };
16278 UNUSED_PARAMETER(zA2);
16279 UNUSED_PARAMETER(zA3);
16280 UNUSED_PARAMETER(zA4);
16281 switch( op ){
16282 case SQLITE_ATTACH: {
16283 #ifndef SQLITE_SHELL_FIDDLE
@@ -16288,11 +16297,11 @@
16288 break;
16289 }
16290 case SQLITE_FUNCTION: {
16291 int i;
16292 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
16293 if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
16294 failIfSafeMode(p, "cannot use the %s() function in safe mode",
16295 azProhibitedFunctions[i]);
16296 }
16297 }
16298 break;
@@ -19513,10 +19522,11 @@
19513 sqlite3_open(":memory:", &p->db);
19514 return;
19515 }
19516 exit(1);
19517 }
 
19518 #ifndef SQLITE_OMIT_LOAD_EXTENSION
19519 sqlite3_enable_load_extension(p->db, 1);
19520 #endif
19521 sqlite3_shathree_init(p->db, 0, 0);
19522 sqlite3_uint_init(p->db, 0, 0);
@@ -19535,10 +19545,38 @@
19535 if( !p->bSafeModePersist ){
19536 sqlite3_zipfile_init(p->db, 0, 0);
19537 sqlite3_sqlar_init(p->db, 0, 0);
19538 }
19539 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19540 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
19541 shellAddSchemaName, 0, 0);
19542 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
19543 shellModuleSchema, 0, 0);
19544 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
@@ -19555,10 +19593,11 @@
19555 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
19556 editFunc, 0, 0);
19557 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
19558 editFunc, 0, 0);
19559 #endif
 
19560 if( p->openMode==SHELL_OPEN_ZIPFILE ){
19561 char *zSql = sqlite3_mprintf(
19562 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
19563 shell_check_oom(zSql);
19564 sqlite3_exec(p->db, zSql, 0, 0, 0);
@@ -25054,11 +25093,11 @@
25054 rc = 1;
25055 goto meta_command_exit;
25056 }
25057 }else{
25058 output_file_close(p->traceOut);
25059 p->traceOut = output_file_open(azArg[1], 0);
25060 }
25061 }
25062 if( p->traceOut==0 ){
25063 sqlite3_trace_v2(p->db, 0, 0, 0);
25064 }else{
@@ -25357,17 +25396,15 @@
25357 return 0;
25358 return quickscan(zLine, QSS_Start)==QSS_Start;
25359 }
25360
25361 /*
25362 ** We need a default sqlite3_complete() implementation to use in case
25363 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
25364 ** any arbitrary text is a complete SQL statement. This is not very
25365 ** user-friendly, but it does seem to work.
25366 */
25367 #ifdef SQLITE_OMIT_COMPLETE
25368 #define sqlite3_complete(x) 1
25369 #endif
25370
25371 /*
25372 ** Return true if zSql is a complete SQL statement. Return false if it
25373 ** ends in the middle of a string literal or C-style comment.
@@ -25650,14 +25687,48 @@
25650 home_dir = z;
25651 }
25652
25653 return home_dir;
25654 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25655
25656 /*
25657 ** Read input from the file given by sqliterc_override. Or if that
25658 ** parameter is NULL, take input from ~/.sqliterc
 
25659 **
25660 ** Returns the number of errors.
25661 */
25662 static void process_sqliterc(
25663 ShellState *p, /* Configuration data */
@@ -25667,11 +25738,14 @@
25667 const char *sqliterc = sqliterc_override;
25668 char *zBuf = 0;
25669 FILE *inSaved = p->in;
25670 int savedLineno = p->lineno;
25671
25672 if (sqliterc == NULL) {
 
 
 
25673 home_dir = find_home_dir(0);
25674 if( home_dir==0 ){
25675 raw_printf(stderr, "-- warning: cannot find home directory;"
25676 " cannot read ~/.sqliterc\n");
25677 return;
@@ -26130,11 +26204,11 @@
26130 if( zVfs ){
26131 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
26132 if( pVfs ){
26133 sqlite3_vfs_register(pVfs, 1);
26134 }else{
26135 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
26136 exit(1);
26137 }
26138 }
26139
26140 if( data.pAuxDb->zDbFilename==0 ){
26141
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -2056,11 +2056,11 @@
2056 }
2057
2058 /* Compute a string using sqlite3_vsnprintf() with a maximum length
2059 ** of 50 bytes and add it to the hash.
2060 */
2061 static void sha3_step_vformat(
2062 SHA3Context *p, /* Add content to this context */
2063 const char *zFormat,
2064 ...
2065 ){
2066 va_list ap;
@@ -2152,11 +2152,11 @@
2152 }
2153 nCol = sqlite3_column_count(pStmt);
2154 z = sqlite3_sql(pStmt);
2155 if( z ){
2156 n = (int)strlen(z);
2157 sha3_step_vformat(&cx,"S%d:",n);
2158 SHA3Update(&cx,(unsigned char*)z,n);
2159 }
2160
2161 /* Compute a hash over the result of the query */
2162 while( SQLITE_ROW==sqlite3_step(pStmt) ){
@@ -2196,18 +2196,18 @@
2196 break;
2197 }
2198 case SQLITE_TEXT: {
2199 int n2 = sqlite3_column_bytes(pStmt, i);
2200 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2201 sha3_step_vformat(&cx,"T%d:",n2);
2202 SHA3Update(&cx, z2, n2);
2203 break;
2204 }
2205 case SQLITE_BLOB: {
2206 int n2 = sqlite3_column_bytes(pStmt, i);
2207 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2208 sha3_step_vformat(&cx,"B%d:",n2);
2209 SHA3Update(&cx, z2, n2);
2210 break;
2211 }
2212 }
2213 }
@@ -3928,11 +3928,11 @@
3928 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
3929 && (p->z[p->i+1]&0xc0)==0x80 ){
3930 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
3931 p->i += 2;
3932 if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
3933 }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80
3934 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
3935 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
3936 | (p->z[p->i+2]&0x3f);
3937 p->i += 3;
3938 if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
@@ -4455,19 +4455,19 @@
4455 /* The following is a performance optimization. If the regex begins with
4456 ** ".*" (if the input regex lacks an initial "^") and afterwards there are
4457 ** one or more matching characters, enter those matching characters into
4458 ** zInit[]. The re_match() routine can then search ahead in the input
4459 ** string looking for the initial match without having to run the whole
4460 ** regex engine over the string. Do not worry about trying to match
4461 ** unicode characters beyond plane 0 - those are very rare and this is
4462 ** just an optimization. */
4463 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
4464 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
4465 unsigned x = pRe->aArg[i];
4466 if( x<=0x7f ){
4467 pRe->zInit[j++] = (unsigned char)x;
4468 }else if( x<=0x7ff ){
4469 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
4470 pRe->zInit[j++] = 0x80 | (x&0x3f);
4471 }else if( x<=0xffff ){
4472 pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12));
4473 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
@@ -11410,10 +11410,263 @@
11410 #define SQLITE_SHELL_HAVE_RECOVER 1
11411 #else
11412 #define SQLITE_SHELL_HAVE_RECOVER 0
11413 #endif
11414 #if SQLITE_SHELL_HAVE_RECOVER
11415 /************************* Begin ../ext/recover/sqlite3recover.h ******************/
11416 /*
11417 ** 2022-08-27
11418 **
11419 ** The author disclaims copyright to this source code. In place of
11420 ** a legal notice, here is a blessing:
11421 **
11422 ** May you do good and not evil.
11423 ** May you find forgiveness for yourself and forgive others.
11424 ** May you share freely, never taking more than you give.
11425 **
11426 *************************************************************************
11427 **
11428 ** This file contains the public interface to the "recover" extension -
11429 ** an SQLite extension designed to recover data from corrupted database
11430 ** files.
11431 */
11432
11433 /*
11434 ** OVERVIEW:
11435 **
11436 ** To use the API to recover data from a corrupted database, an
11437 ** application:
11438 **
11439 ** 1) Creates an sqlite3_recover handle by calling either
11440 ** sqlite3_recover_init() or sqlite3_recover_init_sql().
11441 **
11442 ** 2) Configures the new handle using one or more calls to
11443 ** sqlite3_recover_config().
11444 **
11445 ** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
11446 ** the handle until it returns something other than SQLITE_OK. If it
11447 ** returns SQLITE_DONE, then the recovery operation completed without
11448 ** error. If it returns some other non-SQLITE_OK value, then an error
11449 ** has occurred.
11450 **
11451 ** 4) Retrieves any error code and English language error message using the
11452 ** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
11453 ** respectively.
11454 **
11455 ** 5) Destroys the sqlite3_recover handle and frees all resources
11456 ** using sqlite3_recover_finish().
11457 **
11458 ** The application may abandon the recovery operation at any point
11459 ** before it is finished by passing the sqlite3_recover handle to
11460 ** sqlite3_recover_finish(). This is not an error, but the final state
11461 ** of the output database, or the results of running the partial script
11462 ** delivered to the SQL callback, are undefined.
11463 */
11464
11465 #ifndef _SQLITE_RECOVER_H
11466 #define _SQLITE_RECOVER_H
11467
11468 /* #include "sqlite3.h" */
11469
11470 #ifdef __cplusplus
11471 extern "C" {
11472 #endif
11473
11474 /*
11475 ** An instance of the sqlite3_recover object represents a recovery
11476 ** operation in progress.
11477 **
11478 ** Constructors:
11479 **
11480 ** sqlite3_recover_init()
11481 ** sqlite3_recover_init_sql()
11482 **
11483 ** Destructor:
11484 **
11485 ** sqlite3_recover_finish()
11486 **
11487 ** Methods:
11488 **
11489 ** sqlite3_recover_config()
11490 ** sqlite3_recover_errcode()
11491 ** sqlite3_recover_errmsg()
11492 ** sqlite3_recover_run()
11493 ** sqlite3_recover_step()
11494 */
11495 typedef struct sqlite3_recover sqlite3_recover;
11496
11497 /*
11498 ** These two APIs attempt to create and return a new sqlite3_recover object.
11499 ** In both cases the first two arguments identify the (possibly
11500 ** corrupt) database to recover data from. The first argument is an open
11501 ** database handle and the second the name of a database attached to that
11502 ** handle (i.e. "main", "temp" or the name of an attached database).
11503 **
11504 ** If sqlite3_recover_init() is used to create the new sqlite3_recover
11505 ** handle, then data is recovered into a new database, identified by
11506 ** string parameter zUri. zUri may be an absolute or relative file path,
11507 ** or may be an SQLite URI. If the identified database file already exists,
11508 ** it is overwritten.
11509 **
11510 ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
11511 ** be returned to the user as a series of SQL statements. Executing these
11512 ** SQL statements results in the same database as would have been created
11513 ** had sqlite3_recover_init() been used. For each SQL statement in the
11514 ** output, the callback function passed as the third argument (xSql) is
11515 ** invoked once. The first parameter is a passed a copy of the fourth argument
11516 ** to this function (pCtx) as its first parameter, and a pointer to a
11517 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
11518 ** the second. If the xSql callback returns any value other than SQLITE_OK,
11519 ** then processing is immediately abandoned and the value returned used as
11520 ** the recover handle error code (see below).
11521 **
11522 ** If an out-of-memory error occurs, NULL may be returned instead of
11523 ** a valid handle. In all other cases, it is the responsibility of the
11524 ** application to avoid resource leaks by ensuring that
11525 ** sqlite3_recover_finish() is called on all allocated handles.
11526 */
11527 sqlite3_recover *sqlite3_recover_init(
11528 sqlite3* db,
11529 const char *zDb,
11530 const char *zUri
11531 );
11532 sqlite3_recover *sqlite3_recover_init_sql(
11533 sqlite3* db,
11534 const char *zDb,
11535 int (*xSql)(void*, const char*),
11536 void *pCtx
11537 );
11538
11539 /*
11540 ** Configure an sqlite3_recover object that has just been created using
11541 ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
11542 ** may only be called before the first call to sqlite3_recover_step()
11543 ** or sqlite3_recover_run() on the object.
11544 **
11545 ** The second argument passed to this function must be one of the
11546 ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
11547 ** depend on the specific SQLITE_RECOVER_* symbol in use.
11548 **
11549 ** SQLITE_OK is returned if the configuration operation was successful,
11550 ** or an SQLite error code otherwise.
11551 */
11552 int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
11553
11554 /*
11555 ** SQLITE_RECOVER_LOST_AND_FOUND:
11556 ** The pArg argument points to a string buffer containing the name
11557 ** of a "lost-and-found" table in the output database, or NULL. If
11558 ** the argument is non-NULL and the database contains seemingly
11559 ** valid pages that cannot be associated with any table in the
11560 ** recovered part of the schema, data is extracted from these
11561 ** pages to add to the lost-and-found table.
11562 **
11563 ** SQLITE_RECOVER_FREELIST_CORRUPT:
11564 ** The pArg value must actually be a pointer to a value of type
11565 ** int containing value 0 or 1 cast as a (void*). If this option is set
11566 ** (argument is 1) and a lost-and-found table has been configured using
11567 ** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
11568 ** corrupt and an attempt is made to recover records from pages that
11569 ** appear to be linked into the freelist. Otherwise, pages on the freelist
11570 ** are ignored. Setting this option can recover more data from the
11571 ** database, but often ends up "recovering" deleted records. The default
11572 ** value is 0 (clear).
11573 **
11574 ** SQLITE_RECOVER_ROWIDS:
11575 ** The pArg value must actually be a pointer to a value of type
11576 ** int containing value 0 or 1 cast as a (void*). If this option is set
11577 ** (argument is 1), then an attempt is made to recover rowid values
11578 ** that are not also INTEGER PRIMARY KEY values. If this option is
11579 ** clear, then new rowids are assigned to all recovered rows. The
11580 ** default value is 1 (set).
11581 **
11582 ** SQLITE_RECOVER_SLOWINDEXES:
11583 ** The pArg value must actually be a pointer to a value of type
11584 ** int containing value 0 or 1 cast as a (void*). If this option is clear
11585 ** (argument is 0), then when creating an output database, the recover
11586 ** module creates and populates non-UNIQUE indexes right at the end of the
11587 ** recovery operation - after all recoverable data has been inserted
11588 ** into the new database. This is faster overall, but means that the
11589 ** final call to sqlite3_recover_step() for a recovery operation may
11590 ** be need to create a large number of indexes, which may be very slow.
11591 **
11592 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
11593 ** are created in the output database before it is populated with
11594 ** recovered data. This is slower overall, but avoids the slow call
11595 ** to sqlite3_recover_step() at the end of the recovery operation.
11596 **
11597 ** The default option value is 0.
11598 */
11599 #define SQLITE_RECOVER_LOST_AND_FOUND 1
11600 #define SQLITE_RECOVER_FREELIST_CORRUPT 2
11601 #define SQLITE_RECOVER_ROWIDS 3
11602 #define SQLITE_RECOVER_SLOWINDEXES 4
11603
11604 /*
11605 ** Perform a unit of work towards the recovery operation. This function
11606 ** must normally be called multiple times to complete database recovery.
11607 **
11608 ** If no error occurs but the recovery operation is not completed, this
11609 ** function returns SQLITE_OK. If recovery has been completed successfully
11610 ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
11611 ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
11612 ** considered an error if some or all of the data cannot be recovered
11613 ** due to database corruption.
11614 **
11615 ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
11616 ** all further such calls on the same recover handle are no-ops that return
11617 ** the same non-SQLITE_OK value.
11618 */
11619 int sqlite3_recover_step(sqlite3_recover*);
11620
11621 /*
11622 ** Run the recovery operation to completion. Return SQLITE_OK if successful,
11623 ** or an SQLite error code otherwise. Calling this function is the same
11624 ** as executing:
11625 **
11626 ** while( SQLITE_OK==sqlite3_recover_step(p) );
11627 ** return sqlite3_recover_errcode(p);
11628 */
11629 int sqlite3_recover_run(sqlite3_recover*);
11630
11631 /*
11632 ** If an error has been encountered during a prior call to
11633 ** sqlite3_recover_step(), then this function attempts to return a
11634 ** pointer to a buffer containing an English language explanation of
11635 ** the error. If no error message is available, or if an out-of memory
11636 ** error occurs while attempting to allocate a buffer in which to format
11637 ** the error message, NULL is returned.
11638 **
11639 ** The returned buffer remains valid until the sqlite3_recover handle is
11640 ** destroyed using sqlite3_recover_finish().
11641 */
11642 const char *sqlite3_recover_errmsg(sqlite3_recover*);
11643
11644 /*
11645 ** If this function is called on an sqlite3_recover handle after
11646 ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
11647 */
11648 int sqlite3_recover_errcode(sqlite3_recover*);
11649
11650 /*
11651 ** Clean up a recovery object created by a call to sqlite3_recover_init().
11652 ** The results of using a recovery object with any API after it has been
11653 ** passed to this function are undefined.
11654 **
11655 ** This function returns the same value as sqlite3_recover_errcode().
11656 */
11657 int sqlite3_recover_finish(sqlite3_recover*);
11658
11659
11660 #ifdef __cplusplus
11661 } /* end of the 'extern "C"' block */
11662 #endif
11663
11664 #endif /* ifndef _SQLITE_RECOVER_H */
11665
11666 /************************* End ../ext/recover/sqlite3recover.h ********************/
11667 # ifndef SQLITE_HAVE_SQLITE3R
11668 /************************* Begin ../ext/recover/dbdata.c ******************/
11669 /*
11670 ** 2019-04-17
11671 **
11672 ** The author disclaims copyright to this source code. In place of
@@ -12355,262 +12608,10 @@
12608 }
12609
12610 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
12611
12612 /************************* End ../ext/recover/dbdata.c ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12613 /************************* Begin ../ext/recover/sqlite3recover.c ******************/
12614 /*
12615 ** 2022-08-27
12616 **
12617 ** The author disclaims copyright to this source code. In place of
@@ -14627,10 +14628,11 @@
14628 sqlite3_free(pTab);
14629 }
14630 p->pTblList = 0;
14631 sqlite3_finalize(p->pGetPage);
14632 p->pGetPage = 0;
14633 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
14634
14635 {
14636 #ifndef NDEBUG
14637 int res =
14638 #endif
@@ -14925,10 +14927,11 @@
14927 **
14928 ** Also preserved are:
14929 **
14930 ** + first freelist page (32-bits at offset 32)
14931 ** + size of freelist (32-bits at offset 36)
14932 ** + the wal-mode flags (16-bits at offset 18)
14933 **
14934 ** We also try to preserve the auto-vacuum, incr-value, user-version
14935 ** and application-id fields - all 32 bit quantities at offsets
14936 ** 52, 60, 64 and 68. All other fields are set to known good values.
14937 **
@@ -14988,11 +14991,12 @@
14991 p->pgsz = nByte;
14992 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
14993 if( p->pPage1Cache ){
14994 p->pPage1Disk = &p->pPage1Cache[nByte];
14995 memcpy(p->pPage1Disk, aBuf, nByte);
14996 aHdr[18] = a[18];
14997 aHdr[19] = a[19];
14998 recoverPutU32(&aHdr[28], dbsz);
14999 recoverPutU32(&aHdr[56], enc);
15000 recoverPutU16(&aHdr[105], pgsz-nReserve);
15001 if( pgsz==65536 ) pgsz = 1;
15002 recoverPutU16(&aHdr[16], pgsz);
@@ -15184,10 +15188,11 @@
15188 /* Open the output database. And register required virtual tables and
15189 ** user functions with the new handle. */
15190 recoverOpenOutput(p);
15191
15192 /* Open transactions on both the input and output databases. */
15193 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
15194 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
15195 recoverExec(p, p->dbIn, "BEGIN");
15196 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
15197 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
15198 recoverTransferSettings(p);
@@ -15467,10 +15472,14 @@
15472 }
15473
15474 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
15475
15476 /************************* End ../ext/recover/sqlite3recover.c ********************/
15477 # endif
15478 #endif
15479 #ifdef SQLITE_SHELL_EXTSRC
15480 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
15481 #endif
15482
15483 #if defined(SQLITE_ENABLE_SESSION)
15484 /*
15485 ** State information for a single open session
@@ -16273,11 +16282,11 @@
16282 "readfile",
16283 "writefile",
16284 "zipfile",
16285 "zipfile_cds",
16286 };
16287 UNUSED_PARAMETER(zA1);
16288 UNUSED_PARAMETER(zA3);
16289 UNUSED_PARAMETER(zA4);
16290 switch( op ){
16291 case SQLITE_ATTACH: {
16292 #ifndef SQLITE_SHELL_FIDDLE
@@ -16288,11 +16297,11 @@
16297 break;
16298 }
16299 case SQLITE_FUNCTION: {
16300 int i;
16301 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
16302 if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
16303 failIfSafeMode(p, "cannot use the %s() function in safe mode",
16304 azProhibitedFunctions[i]);
16305 }
16306 }
16307 break;
@@ -19513,10 +19522,11 @@
19522 sqlite3_open(":memory:", &p->db);
19523 return;
19524 }
19525 exit(1);
19526 }
19527
19528 #ifndef SQLITE_OMIT_LOAD_EXTENSION
19529 sqlite3_enable_load_extension(p->db, 1);
19530 #endif
19531 sqlite3_shathree_init(p->db, 0, 0);
19532 sqlite3_uint_init(p->db, 0, 0);
@@ -19535,10 +19545,38 @@
19545 if( !p->bSafeModePersist ){
19546 sqlite3_zipfile_init(p->db, 0, 0);
19547 sqlite3_sqlar_init(p->db, 0, 0);
19548 }
19549 #endif
19550 #ifdef SQLITE_SHELL_EXTFUNCS
19551 /* Create a preprocessing mechanism for extensions to make
19552 * their own provisions for being built into the shell.
19553 * This is a short-span macro. See further below for usage.
19554 */
19555 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
19556 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
19557 /* Let custom-included extensions get their ..._init() called.
19558 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
19559 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
19560 * inititialization routine to be called.
19561 */
19562 {
19563 int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
19564 /* Let custom-included extensions expose their functionality.
19565 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
19566 * the SQL functions, virtual tables, collating sequences or
19567 * VFS's implemented by the extension to be registered.
19568 */
19569 if( irc==SQLITE_OK
19570 || irc==SQLITE_OK_LOAD_PERMANENTLY ){
19571 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
19572 }
19573 #undef SHELL_SUB_MACRO
19574 #undef SHELL_SUBMACRO
19575 }
19576 #endif
19577
19578 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
19579 shellAddSchemaName, 0, 0);
19580 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
19581 shellModuleSchema, 0, 0);
19582 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
@@ -19555,10 +19593,11 @@
19593 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
19594 editFunc, 0, 0);
19595 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
19596 editFunc, 0, 0);
19597 #endif
19598
19599 if( p->openMode==SHELL_OPEN_ZIPFILE ){
19600 char *zSql = sqlite3_mprintf(
19601 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
19602 shell_check_oom(zSql);
19603 sqlite3_exec(p->db, zSql, 0, 0, 0);
@@ -25054,11 +25093,11 @@
25093 rc = 1;
25094 goto meta_command_exit;
25095 }
25096 }else{
25097 output_file_close(p->traceOut);
25098 p->traceOut = output_file_open(z, 0);
25099 }
25100 }
25101 if( p->traceOut==0 ){
25102 sqlite3_trace_v2(p->db, 0, 0, 0);
25103 }else{
@@ -25357,17 +25396,15 @@
25396 return 0;
25397 return quickscan(zLine, QSS_Start)==QSS_Start;
25398 }
25399
25400 /*
25401 ** The CLI needs a working sqlite3_complete() to work properly. So error
25402 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
 
 
25403 */
25404 #ifdef SQLITE_OMIT_COMPLETE
25405 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
25406 #endif
25407
25408 /*
25409 ** Return true if zSql is a complete SQL statement. Return false if it
25410 ** ends in the middle of a string literal or C-style comment.
@@ -25650,14 +25687,48 @@
25687 home_dir = z;
25688 }
25689
25690 return home_dir;
25691 }
25692
25693 /*
25694 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
25695 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
25696 ** the path to it, else return 0. The result is cached for
25697 ** subsequent calls.
25698 */
25699 static const char *find_xdg_config(void){
25700 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
25701 || defined(__RTP__) || defined(_WRS_KERNEL)
25702 return 0;
25703 #else
25704 static int alreadyTried = 0;
25705 static char *zConfig = 0;
25706 const char *zXdgHome;
25707
25708 if( alreadyTried!=0 ){
25709 return zConfig;
25710 }
25711 alreadyTried = 1;
25712 zXdgHome = getenv("XDG_CONFIG_HOME");
25713 if( zXdgHome==0 ){
25714 return 0;
25715 }
25716 zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
25717 shell_check_oom(zConfig);
25718 if( access(zConfig,0)!=0 ){
25719 sqlite3_free(zConfig);
25720 zConfig = 0;
25721 }
25722 return zConfig;
25723 #endif
25724 }
25725
25726 /*
25727 ** Read input from the file given by sqliterc_override. Or if that
25728 ** parameter is NULL, take input from the first of find_xdg_config()
25729 ** or ~/.sqliterc which is found.
25730 **
25731 ** Returns the number of errors.
25732 */
25733 static void process_sqliterc(
25734 ShellState *p, /* Configuration data */
@@ -25667,11 +25738,14 @@
25738 const char *sqliterc = sqliterc_override;
25739 char *zBuf = 0;
25740 FILE *inSaved = p->in;
25741 int savedLineno = p->lineno;
25742
25743 if( sqliterc == NULL ){
25744 sqliterc = find_xdg_config();
25745 }
25746 if( sqliterc == NULL ){
25747 home_dir = find_home_dir(0);
25748 if( home_dir==0 ){
25749 raw_printf(stderr, "-- warning: cannot find home directory;"
25750 " cannot read ~/.sqliterc\n");
25751 return;
@@ -26130,11 +26204,11 @@
26204 if( zVfs ){
26205 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
26206 if( pVfs ){
26207 sqlite3_vfs_register(pVfs, 1);
26208 }else{
26209 utf8_printf(stderr, "no such VFS: \"%s\"\n", zVfs);
26210 exit(1);
26211 }
26212 }
26213
26214 if( data.pAuxDb->zDbFilename==0 ){
26215
+1075 -588
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.40.0. By combining all the individual C code files into this
3
+** version 3.41.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.
@@ -450,13 +450,13 @@
450450
**
451451
** See also: [sqlite3_libversion()],
452452
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453453
** [sqlite_version()] and [sqlite_source_id()].
454454
*/
455
-#define SQLITE_VERSION "3.40.0"
456
-#define SQLITE_VERSION_NUMBER 3040000
457
-#define SQLITE_SOURCE_ID "2022-11-16 19:57:21 5689f0d9ad1be532b274508938b25ff0d63027b8cc31f796dfaa2cca71d53642"
455
+#define SQLITE_VERSION "3.41.0"
456
+#define SQLITE_VERSION_NUMBER 3041000
457
+#define SQLITE_SOURCE_ID "2022-12-05 02:52:37 1b779afa3ed2f35a110e460fc6ed13cba744db85b9924149ab028b100d1e1e12"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -1496,10 +1496,16 @@
14961496
** by clients within the current process, only within other processes.
14971497
** </ul>
14981498
**
14991499
** <li>[[SQLITE_FCNTL_CKSM_FILE]]
15001500
** Used by the cksmvfs VFS module only.
1501
+**
1502
+** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1503
+** If there is currently no transaction open on the database, and the
1504
+** database is not a temp db, then this file-control purges the contents
1505
+** of the in-memory page cache. If there is an open transaction, or if
1506
+** the db is a temp-db, it is a no-op, not an error.
15011507
** </ul>
15021508
*/
15031509
#define SQLITE_FCNTL_LOCKSTATE 1
15041510
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
15051511
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1538,10 +1544,11 @@
15381544
#define SQLITE_FCNTL_CKPT_DONE 37
15391545
#define SQLITE_FCNTL_RESERVE_BYTES 38
15401546
#define SQLITE_FCNTL_CKPT_START 39
15411547
#define SQLITE_FCNTL_EXTERNAL_READER 40
15421548
#define SQLITE_FCNTL_CKSM_FILE 41
1549
+#define SQLITE_FCNTL_RESET_CACHE 42
15431550
15441551
/* deprecated names */
15451552
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
15461553
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
15471554
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -5846,20 +5853,10 @@
58465853
** such a conversion is possible without loss of information (in other
58475854
** words, if the value is a string that looks like a number)
58485855
** then the conversion is performed. Otherwise no conversion occurs.
58495856
** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
58505857
**
5851
-** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5852
-** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5853
-** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5854
-** returns something other than SQLITE_TEXT, then the return value from
5855
-** sqlite3_value_encoding(X) is meaningless. ^Calls to
5856
-** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5857
-** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5858
-** sqlite3_value_bytes16(X) might change the encoding of the value X and
5859
-** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5860
-**
58615858
** ^Within the [xUpdate] method of a [virtual table], the
58625859
** sqlite3_value_nochange(X) interface returns true if and only if
58635860
** the column corresponding to X is unchanged by the UPDATE operation
58645861
** that the xUpdate method call was invoked to implement and if
58655862
** and the prior [xColumn] method call that was invoked to extracted
@@ -5920,10 +5917,31 @@
59205917
SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
59215918
SQLITE_API int sqlite3_value_type(sqlite3_value*);
59225919
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
59235920
SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
59245921
SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5922
+
5923
+/*
5924
+** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
5925
+** METHOD: sqlite3_value
5926
+**
5927
+** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5928
+** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
5929
+** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5930
+** returns something other than SQLITE_TEXT, then the return value from
5931
+** sqlite3_value_encoding(X) is meaningless. ^Calls to
5932
+** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
5933
+** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
5934
+** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
5935
+** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5936
+**
5937
+** This routine is intended for used by applications that test and validate
5938
+** the SQLite implementation. This routine is inquiring about the opaque
5939
+** internal state of an [sqlite3_value] object. Ordinary applications should
5940
+** not need to know what the internal state of an sqlite3_value object is and
5941
+** hence should not need to use this interface.
5942
+*/
59255943
SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
59265944
59275945
/*
59285946
** CAPI3REF: Finding The Subtype Of SQL Values
59295947
** METHOD: sqlite3_value
@@ -14526,18 +14544,41 @@
1452614544
#endif
1452714545
#if defined(SQLITE_DEBUG) \
1452814546
&& (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
1452914547
|| defined(SQLITE_ENABLE_TREETRACE))
1453014548
# define TREETRACE_ENABLED 1
14531
-# define SELECTTRACE(K,P,S,X) \
14549
+# define TREETRACE(K,P,S,X) \
1453214550
if(sqlite3TreeTrace&(K)) \
1453314551
sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
1453414552
sqlite3DebugPrintf X
1453514553
#else
14536
-# define SELECTTRACE(K,P,S,X)
14554
+# define TREETRACE(K,P,S,X)
1453714555
# define TREETRACE_ENABLED 0
1453814556
#endif
14557
+
14558
+/* TREETRACE flag meanings:
14559
+**
14560
+** 0x00000001 Beginning and end of SELECT processing
14561
+** 0x00000002 WHERE clause processing
14562
+** 0x00000004 Query flattener
14563
+** 0x00000008 Result-set wildcard expansion
14564
+** 0x00000010 Query name resolution
14565
+** 0x00000020 Aggregate analysis
14566
+** 0x00000040 Window functions
14567
+** 0x00000080 Generated column names
14568
+** 0x00000100 Move HAVING terms into WHERE
14569
+** 0x00000200 Count-of-view optimization
14570
+** 0x00000400 Compound SELECT processing
14571
+** 0x00000800 Drop superfluous ORDER BY
14572
+** 0x00001000 LEFT JOIN simplifies to JOIN
14573
+** 0x00002000 Constant propagation
14574
+** 0x00004000 Push-down optimization
14575
+** 0x00008000 After all FROM-clause analysis
14576
+** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
14577
+** 0x00020000 Transform DISTINCT into GROUP BY
14578
+** 0x00040000 SELECT tree dump after all code has been generated
14579
+*/
1453914580
1454014581
/*
1454114582
** Macros for "wheretrace"
1454214583
*/
1454314584
SQLITE_PRIVATE u32 sqlite3WhereTrace;
@@ -14546,10 +14587,40 @@
1454614587
# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
1454714588
# define WHERETRACE_ENABLED 1
1454814589
#else
1454914590
# define WHERETRACE(K,X)
1455014591
#endif
14592
+
14593
+/*
14594
+** Bits for the sqlite3WhereTrace mask:
14595
+**
14596
+** (---any--) Top-level block structure
14597
+** 0x-------F High-level debug messages
14598
+** 0x----FFF- More detail
14599
+** 0xFFFF---- Low-level debug messages
14600
+**
14601
+** 0x00000001 Code generation
14602
+** 0x00000002 Solver
14603
+** 0x00000004 Solver costs
14604
+** 0x00000008 WhereLoop inserts
14605
+**
14606
+** 0x00000010 Display sqlite3_index_info xBestIndex calls
14607
+** 0x00000020 Range an equality scan metrics
14608
+** 0x00000040 IN operator decisions
14609
+** 0x00000080 WhereLoop cost adjustements
14610
+** 0x00000100
14611
+** 0x00000200 Covering index decisions
14612
+** 0x00000400 OR optimization
14613
+** 0x00000800 Index scanner
14614
+** 0x00001000 More details associated with code generation
14615
+** 0x00002000
14616
+** 0x00004000 Show all WHERE terms at key points
14617
+** 0x00008000 Show the full SELECT statement at key places
14618
+**
14619
+** 0x00010000 Show more detail when printing WHERE terms
14620
+** 0x00020000 Show WHERE terms returned from whereScanNext()
14621
+*/
1455114622
1455214623
1455314624
/*
1455414625
** An instance of the following structure is used to store the busy-handler
1455514626
** callback for a given sqlite handle.
@@ -15711,10 +15782,12 @@
1571115782
#ifndef SQLITE_OMIT_WAL
1571215783
SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
1571315784
#endif
1571415785
1571515786
SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
15787
+
15788
+SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree*);
1571615789
1571715790
/*
1571815791
** If we are not using shared cache, then there is no need to
1571915792
** use mutexes to access the BtShared structures. So make the
1572015793
** Enter and Leave procedures no-ops.
@@ -18106,20 +18179,19 @@
1810618179
struct AggInfo {
1810718180
u8 directMode; /* Direct rendering mode means take data directly
1810818181
** from source tables rather than from accumulators */
1810918182
u8 useSortingIdx; /* In direct mode, reference the sorting index rather
1811018183
** than the source table */
18184
+ u16 nSortingColumn; /* Number of columns in the sorting index */
1811118185
int sortingIdx; /* Cursor number of the sorting index */
1811218186
int sortingIdxPTab; /* Cursor number of pseudo-table */
18113
- int nSortingColumn; /* Number of columns in the sorting index */
18114
- int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
18187
+ int iFirstReg; /* First register in range for aCol[] and aFunc[] */
1811518188
ExprList *pGroupBy; /* The group by clause */
1811618189
struct AggInfo_col { /* For each column used in source tables */
1811718190
Table *pTab; /* Source table */
1811818191
Expr *pCExpr; /* The original expression */
1811918192
int iTable; /* Cursor number of the source table */
18120
- int iMem; /* Memory location that acts as accumulator */
1812118193
i16 iColumn; /* Column number within the source table */
1812218194
i16 iSorterColumn; /* Column number in the sorting index */
1812318195
} *aCol;
1812418196
int nColumn; /* Number of used entries in aCol[] */
1812518197
int nAccumulator; /* Number of columns that show through to the output.
@@ -18126,18 +18198,28 @@
1812618198
** Additional columns are used only as parameters to
1812718199
** aggregate functions */
1812818200
struct AggInfo_func { /* For each aggregate function */
1812918201
Expr *pFExpr; /* Expression encoding the function */
1813018202
FuncDef *pFunc; /* The aggregate function implementation */
18131
- int iMem; /* Memory location that acts as accumulator */
1813218203
int iDistinct; /* Ephemeral table used to enforce DISTINCT */
1813318204
int iDistAddr; /* Address of OP_OpenEphemeral */
1813418205
} *aFunc;
1813518206
int nFunc; /* Number of entries in aFunc[] */
1813618207
u32 selId; /* Select to which this AggInfo belongs */
1813718208
};
1813818209
18210
+/*
18211
+** Macros to compute aCol[] and aFunc[] register numbers.
18212
+**
18213
+** These macros should not be used prior to the call to
18214
+** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18215
+** The assert()s that are part of this macro verify that constraint.
18216
+*/
18217
+#define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18218
+#define AggInfoFuncReg(A,I) \
18219
+ (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18220
+
1813918221
/*
1814018222
** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
1814118223
** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
1814218224
** than 32767 we have to make it 32-bit. 16-bit is preferred because
1814318225
** it uses less memory in the Expr object, which is a big memory user
@@ -19046,11 +19128,11 @@
1904619128
** of the base register during check-constraint eval */
1904719129
int nLabel; /* The *negative* of the number of labels used */
1904819130
int nLabelAlloc; /* Number of slots in aLabel */
1904919131
int *aLabel; /* Space to hold the labels */
1905019132
ExprList *pConstExpr;/* Constant expressions */
19051
- IndexedExpr *pIdxExpr;/* List of expressions used by active indexes */
19133
+ IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
1905219134
Token constraintName;/* Name of the constraint currently being parsed */
1905319135
yDbMask writeMask; /* Start a write transaction on these databases */
1905419136
yDbMask cookieMask; /* Bitmask of schema verified databases */
1905519137
int regRowid; /* Register holding rowid of CREATE TABLE entry */
1905619138
int regRoot; /* Register holding root page number for new objects */
@@ -20393,10 +20475,13 @@
2039320475
SQLITE_PRIVATE const char *sqlite3ErrName(int);
2039420476
#endif
2039520477
2039620478
#ifndef SQLITE_OMIT_DESERIALIZE
2039720479
SQLITE_PRIVATE int sqlite3MemdbInit(void);
20480
+SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs*);
20481
+#else
20482
+# define sqlite3IsMemdb(X) 0
2039820483
#endif
2039920484
2040020485
SQLITE_PRIVATE const char *sqlite3ErrStr(int);
2040120486
SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
2040220487
SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
@@ -20889,10 +20974,14 @@
2088920974
#endif
2089020975
2089120976
#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
2089220977
SQLITE_PRIVATE int sqlite3KvvfsInit(void);
2089320978
#endif
20979
+
20980
+#if defined(VDBE_PROFILE) || defined(SQLITE_PERFORMANCE_TRACE)
20981
+SQLITE_PRIVATE sqlite3_uint64 sqlite3Hwtime(void);
20982
+#endif
2089420983
2089520984
#endif /* SQLITEINT_H */
2089620985
2089720986
/************** End of sqliteInt.h *******************************************/
2089820987
/************** Begin file os_common.h ***************************************/
@@ -20931,105 +21020,10 @@
2093121020
** Macros for performance tracing. Normally turned off. Only works
2093221021
** on i486 hardware.
2093321022
*/
2093421023
#ifdef SQLITE_PERFORMANCE_TRACE
2093521024
20936
-/*
20937
-** hwtime.h contains inline assembler code for implementing
20938
-** high-performance timing routines.
20939
-*/
20940
-/************** Include hwtime.h in the middle of os_common.h ****************/
20941
-/************** Begin file hwtime.h ******************************************/
20942
-/*
20943
-** 2008 May 27
20944
-**
20945
-** The author disclaims copyright to this source code. In place of
20946
-** a legal notice, here is a blessing:
20947
-**
20948
-** May you do good and not evil.
20949
-** May you find forgiveness for yourself and forgive others.
20950
-** May you share freely, never taking more than you give.
20951
-**
20952
-******************************************************************************
20953
-**
20954
-** This file contains inline asm code for retrieving "high-performance"
20955
-** counters for x86 and x86_64 class CPUs.
20956
-*/
20957
-#ifndef SQLITE_HWTIME_H
20958
-#define SQLITE_HWTIME_H
20959
-
20960
-/*
20961
-** The following routine only works on pentium-class (or newer) processors.
20962
-** It uses the RDTSC opcode to read the cycle count value out of the
20963
-** processor and returns that value. This can be used for high-res
20964
-** profiling.
20965
-*/
20966
-#if !defined(__STRICT_ANSI__) && \
20967
- (defined(__GNUC__) || defined(_MSC_VER)) && \
20968
- (defined(i386) || defined(__i386__) || defined(_M_IX86))
20969
-
20970
- #if defined(__GNUC__)
20971
-
20972
- __inline__ sqlite_uint64 sqlite3Hwtime(void){
20973
- unsigned int lo, hi;
20974
- __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
20975
- return (sqlite_uint64)hi << 32 | lo;
20976
- }
20977
-
20978
- #elif defined(_MSC_VER)
20979
-
20980
- __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
20981
- __asm {
20982
- rdtsc
20983
- ret ; return value at EDX:EAX
20984
- }
20985
- }
20986
-
20987
- #endif
20988
-
20989
-#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
20990
-
20991
- __inline__ sqlite_uint64 sqlite3Hwtime(void){
20992
- unsigned long val;
20993
- __asm__ __volatile__ ("rdtsc" : "=A" (val));
20994
- return val;
20995
- }
20996
-
20997
-#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
20998
-
20999
- __inline__ sqlite_uint64 sqlite3Hwtime(void){
21000
- unsigned long long retval;
21001
- unsigned long junk;
21002
- __asm__ __volatile__ ("\n\
21003
- 1: mftbu %1\n\
21004
- mftb %L0\n\
21005
- mftbu %0\n\
21006
- cmpw %0,%1\n\
21007
- bne 1b"
21008
- : "=r" (retval), "=r" (junk));
21009
- return retval;
21010
- }
21011
-
21012
-#else
21013
-
21014
- /*
21015
- ** asm() is needed for hardware timing support. Without asm(),
21016
- ** disable the sqlite3Hwtime() routine.
21017
- **
21018
- ** sqlite3Hwtime() is only used for some obscure debugging
21019
- ** and analysis configurations, not in any deliverable, so this
21020
- ** should not be a great loss.
21021
- */
21022
-SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
21023
-
21024
-#endif
21025
-
21026
-#endif /* !defined(SQLITE_HWTIME_H) */
21027
-
21028
-/************** End of hwtime.h **********************************************/
21029
-/************** Continuing where we left off in os_common.h ******************/
21030
-
2103121025
static sqlite_uint64 g_start;
2103221026
static sqlite_uint64 g_elapsed;
2103321027
#define TIMER_START g_start=sqlite3Hwtime()
2103421028
#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
2103521029
#define TIMER_ELAPSED g_elapsed
@@ -29200,11 +29194,11 @@
2920029194
** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
2920129195
**
2920229196
** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
2920329197
** This provides a 256-byte safety margin for defense against 32-bit
2920429198
** signed integer overflow bugs when computing memory allocation sizes.
29205
-** Parnoid applications might want to reduce the maximum allocation size
29199
+** Paranoid applications might want to reduce the maximum allocation size
2920629200
** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
2920729201
** or even smaller would be reasonable upper bounds on the size of a memory
2920829202
** allocations for most applications.
2920929203
*/
2921029204
#ifndef SQLITE_MAX_ALLOCATION_SIZE
@@ -29714,13 +29708,18 @@
2971429708
** SQL statement. Make a copy of this phrase in space obtained form
2971529709
** sqlite3DbMalloc(). Omit leading and trailing whitespace.
2971629710
*/
2971729711
SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
2971829712
int n;
29713
+#ifdef SQLITE_DEBUG
29714
+ /* Because of the way the parser works, the span is guaranteed to contain
29715
+ ** at least one non-space character */
29716
+ for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]<zEnd ); }
29717
+#endif
2971929718
while( sqlite3Isspace(zStart[0]) ) zStart++;
2972029719
n = (int)(zEnd - zStart);
29721
- while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
29720
+ while( sqlite3Isspace(zStart[n-1]) ) n--;
2972229721
return sqlite3DbStrNDup(db, zStart, n);
2972329722
}
2972429723
2972529724
/*
2972629725
** Free any prior content in *pz and replace it with a copy of zNew.
@@ -31698,11 +31697,11 @@
3169831697
if( pExpr==0 ){
3169931698
sqlite3TreeViewLine(pView, "nil");
3170031699
sqlite3TreeViewPop(&pView);
3170131700
return;
3170231701
}
31703
- if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
31702
+ if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){
3170431703
StrAccum x;
3170531704
sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
3170631705
sqlite3_str_appendf(&x, " fg.af=%x.%c",
3170731706
pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
3170831707
if( ExprHasProperty(pExpr, EP_OuterON) ){
@@ -31715,10 +31714,13 @@
3171531714
sqlite3_str_appendf(&x, " DDL");
3171631715
}
3171731716
if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
3171831717
sqlite3_str_appendf(&x, " IMMUTABLE");
3171931718
}
31719
+ if( pExpr->pAggInfo!=0 ){
31720
+ sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg);
31721
+ }
3172031722
sqlite3StrAccumFinish(&x);
3172131723
}else{
3172231724
zFlgs[0] = 0;
3172331725
}
3172431726
switch( pExpr->op ){
@@ -35193,10 +35195,106 @@
3519335195
i += pIn[i+1];
3519435196
}while( i<mx );
3519535197
return 0;
3519635198
}
3519735199
35200
+/*
35201
+** High-resolution hardware timer used for debugging and testing only.
35202
+*/
35203
+#if defined(VDBE_PROFILE) || defined(SQLITE_PERFORMANCE_TRACE)
35204
+/************** Include hwtime.h in the middle of util.c *********************/
35205
+/************** Begin file hwtime.h ******************************************/
35206
+/*
35207
+** 2008 May 27
35208
+**
35209
+** The author disclaims copyright to this source code. In place of
35210
+** a legal notice, here is a blessing:
35211
+**
35212
+** May you do good and not evil.
35213
+** May you find forgiveness for yourself and forgive others.
35214
+** May you share freely, never taking more than you give.
35215
+**
35216
+******************************************************************************
35217
+**
35218
+** This file contains inline asm code for retrieving "high-performance"
35219
+** counters for x86 and x86_64 class CPUs.
35220
+*/
35221
+#ifndef SQLITE_HWTIME_H
35222
+#define SQLITE_HWTIME_H
35223
+
35224
+/*
35225
+** The following routine only works on pentium-class (or newer) processors.
35226
+** It uses the RDTSC opcode to read the cycle count value out of the
35227
+** processor and returns that value. This can be used for high-res
35228
+** profiling.
35229
+*/
35230
+#if !defined(__STRICT_ANSI__) && \
35231
+ (defined(__GNUC__) || defined(_MSC_VER)) && \
35232
+ (defined(i386) || defined(__i386__) || defined(_M_IX86))
35233
+
35234
+ #if defined(__GNUC__)
35235
+
35236
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){
35237
+ unsigned int lo, hi;
35238
+ __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
35239
+ return (sqlite_uint64)hi << 32 | lo;
35240
+ }
35241
+
35242
+ #elif defined(_MSC_VER)
35243
+
35244
+ __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
35245
+ __asm {
35246
+ rdtsc
35247
+ ret ; return value at EDX:EAX
35248
+ }
35249
+ }
35250
+
35251
+ #endif
35252
+
35253
+#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
35254
+
35255
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){
35256
+ unsigned long val;
35257
+ __asm__ __volatile__ ("rdtsc" : "=A" (val));
35258
+ return val;
35259
+ }
35260
+
35261
+#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
35262
+
35263
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){
35264
+ unsigned long long retval;
35265
+ unsigned long junk;
35266
+ __asm__ __volatile__ ("\n\
35267
+ 1: mftbu %1\n\
35268
+ mftb %L0\n\
35269
+ mftbu %0\n\
35270
+ cmpw %0,%1\n\
35271
+ bne 1b"
35272
+ : "=r" (retval), "=r" (junk));
35273
+ return retval;
35274
+ }
35275
+
35276
+#else
35277
+
35278
+ /*
35279
+ ** asm() is needed for hardware timing support. Without asm(),
35280
+ ** disable the sqlite3Hwtime() routine.
35281
+ **
35282
+ ** sqlite3Hwtime() is only used for some obscure debugging
35283
+ ** and analysis configurations, not in any deliverable, so this
35284
+ ** should not be a great loss.
35285
+ */
35286
+SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
35287
+
35288
+#endif
35289
+
35290
+#endif /* !defined(SQLITE_HWTIME_H) */
35291
+
35292
+/************** End of hwtime.h **********************************************/
35293
+/************** Continuing where we left off in util.c ***********************/
35294
+#endif
35295
+
3519835296
/************** End of util.c ************************************************/
3519935297
/************** Begin file hash.c ********************************************/
3520035298
/*
3520135299
** 2001 September 22
3520235300
**
@@ -35727,11 +35825,13 @@
3572735825
int isJournal; /* True if this is a journal file */
3572835826
unsigned int nJrnl; /* Space allocated for aJrnl[] */
3572935827
char *aJrnl; /* Journal content */
3573035828
int szPage; /* Last known page size */
3573135829
sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35830
+ char *aData; /* Buffer to hold page data */
3573235831
};
35832
+#define SQLITE_KVOS_SZ 133073
3573335833
3573435834
/*
3573535835
** Methods for KVVfsFile
3573635836
*/
3573735837
static int kvvfsClose(sqlite3_file*);
@@ -36168,10 +36268,11 @@
3616836268
KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
3616936269
3617036270
SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
3617136271
pFile->isJournal ? "journal" : "db"));
3617236272
sqlite3_free(pFile->aJrnl);
36273
+ sqlite3_free(pFile->aData);
3617336274
return SQLITE_OK;
3617436275
}
3617536276
3617636277
/*
3617736278
** Read from the -journal file.
@@ -36216,11 +36317,11 @@
3621636317
){
3621736318
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
3621836319
unsigned int pgno;
3621936320
int got, n;
3622036321
char zKey[30];
36221
- char aData[133073];
36322
+ char *aData = pFile->aData;
3622236323
assert( iOfst>=0 );
3622336324
assert( iAmt>=0 );
3622436325
SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
3622536326
if( iOfst+iAmt>=512 ){
3622636327
if( (iOfst % iAmt)!=0 ){
@@ -36233,19 +36334,20 @@
3623336334
pgno = 1 + iOfst/iAmt;
3623436335
}else{
3623536336
pgno = 1;
3623636337
}
3623736338
sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36238
- got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36339
+ got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
36340
+ aData, SQLITE_KVOS_SZ-1);
3623936341
if( got<0 ){
3624036342
n = 0;
3624136343
}else{
3624236344
aData[got] = 0;
3624336345
if( iOfst+iAmt<512 ){
3624436346
int k = iOfst+iAmt;
3624536347
aData[k*2] = 0;
36246
- n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36348
+ n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
3624736349
if( n>=iOfst+iAmt ){
3624836350
memcpy(zBuf, &aData[2000+iOfst], iAmt);
3624936351
n = iAmt;
3625036352
}else{
3625136353
n = 0;
@@ -36300,11 +36402,11 @@
3630036402
sqlite_int64 iOfst
3630136403
){
3630236404
KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
3630336405
unsigned int pgno;
3630436406
char zKey[30];
36305
- char aData[131073];
36407
+ char *aData = pFile->aData;
3630636408
SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
3630736409
assert( iAmt>=512 && iAmt<=65536 );
3630836410
assert( (iAmt & (iAmt-1))==0 );
3630936411
assert( pFile->szPage<0 || pFile->szPage==iAmt );
3631036412
pFile->szPage = iAmt;
@@ -36508,10 +36610,14 @@
3650836610
}
3650936611
if( zName[0]=='s' ){
3651036612
pFile->zClass = "session";
3651136613
}else{
3651236614
pFile->zClass = "local";
36615
+ }
36616
+ pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
36617
+ if( pFile->aData==0 ){
36618
+ return SQLITE_NOMEM;
3651336619
}
3651436620
pFile->aJrnl = 0;
3651536621
pFile->nJrnl = 0;
3651636622
pFile->szPage = -1;
3651736623
pFile->szDb = -1;
@@ -43326,11 +43432,11 @@
4332643432
** requested from the underlying operating system, a number which
4332743433
** might be greater than or equal to the argument, but not less
4332843434
** than the argument.
4332943435
*/
4333043436
static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
43331
-#if OS_VXWORKS
43437
+#if OS_VXWORKS || _POSIX_C_SOURCE >= 199309L
4333243438
struct timespec sp;
4333343439
4333443440
sp.tv_sec = microseconds / 1000000;
4333543441
sp.tv_nsec = (microseconds % 1000000) * 1000;
4333643442
nanosleep(&sp, NULL);
@@ -51816,10 +51922,17 @@
5181651922
sqlite3_free(pData);
5181751923
}
5181851924
sqlite3_mutex_leave(db->mutex);
5181951925
return rc;
5182051926
}
51927
+
51928
+/*
51929
+** Return true if the VFS is the memvfs.
51930
+*/
51931
+SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
51932
+ return pVfs==&memdb_vfs;
51933
+}
5182151934
5182251935
/*
5182351936
** This routine is called when the extension is loaded.
5182451937
** Register the new VFS.
5182551938
*/
@@ -62136,11 +62249,15 @@
6213662249
** The return value to this routine is always safe to use with
6213762250
** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
6213862251
*/
6213962252
SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
6214062253
static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
62141
- return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
62254
+ if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
62255
+ return &zFake[4];
62256
+ }else{
62257
+ return pPager->zFilename;
62258
+ }
6214262259
}
6214362260
6214462261
/*
6214562262
** Return the VFS structure for the pager.
6214662263
*/
@@ -67719,13 +67836,13 @@
6771967836
** within an expression that is an argument to another macro
6772067837
** (sqliteMallocRaw), it is not possible to use conditional compilation.
6772167838
** So, this macro is defined instead.
6772267839
*/
6772367840
#ifndef SQLITE_OMIT_AUTOVACUUM
67724
-#define ISAUTOVACUUM (pBt->autoVacuum)
67841
+#define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
6772567842
#else
67726
-#define ISAUTOVACUUM 0
67843
+#define ISAUTOVACUUM(pBt) 0
6772767844
#endif
6772867845
6772967846
6773067847
/*
6773167848
** This structure is passed around through all the sanity checking routines
@@ -69973,66 +70090,71 @@
6997370090
** and initialize fields of the MemPage structure accordingly.
6997470091
**
6997570092
** Only the following combinations are supported. Anything different
6997670093
** indicates a corrupt database files:
6997770094
**
69978
-** PTF_ZERODATA
69979
-** PTF_ZERODATA | PTF_LEAF
69980
-** PTF_LEAFDATA | PTF_INTKEY
69981
-** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
70095
+** PTF_ZERODATA (0x02, 2)
70096
+** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
70097
+** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
70098
+** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
6998270099
*/
6998370100
static int decodeFlags(MemPage *pPage, int flagByte){
6998470101
BtShared *pBt; /* A copy of pPage->pBt */
6998570102
6998670103
assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
6998770104
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69988
- pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
69989
- flagByte &= ~PTF_LEAF;
69990
- pPage->childPtrSize = 4-4*pPage->leaf;
6999170105
pBt = pPage->pBt;
69992
- if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
69993
- /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
69994
- ** interior table b-tree page. */
69995
- assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
69996
- /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
69997
- ** leaf table b-tree page. */
69998
- assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
69999
- pPage->intKey = 1;
70000
- if( pPage->leaf ){
70106
+ pPage->max1bytePayload = pBt->max1bytePayload;
70107
+ if( flagByte>=(PTF_ZERODATA | PTF_LEAF) ){
70108
+ pPage->childPtrSize = 0;
70109
+ pPage->leaf = 1;
70110
+ if( flagByte==(PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF) ){
7000170111
pPage->intKeyLeaf = 1;
7000270112
pPage->xCellSize = cellSizePtrTableLeaf;
7000370113
pPage->xParseCell = btreeParseCellPtr;
70114
+ pPage->intKey = 1;
70115
+ pPage->maxLocal = pBt->maxLeaf;
70116
+ pPage->minLocal = pBt->minLeaf;
70117
+ }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){
70118
+ pPage->intKey = 0;
70119
+ pPage->intKeyLeaf = 0;
70120
+ pPage->xCellSize = cellSizePtr;
70121
+ pPage->xParseCell = btreeParseCellPtrIndex;
70122
+ pPage->maxLocal = pBt->maxLocal;
70123
+ pPage->minLocal = pBt->minLocal;
7000470124
}else{
70125
+ pPage->intKey = 0;
70126
+ pPage->intKeyLeaf = 0;
70127
+ pPage->xCellSize = cellSizePtr;
70128
+ pPage->xParseCell = btreeParseCellPtrIndex;
70129
+ return SQLITE_CORRUPT_PAGE(pPage);
70130
+ }
70131
+ }else{
70132
+ pPage->childPtrSize = 4;
70133
+ pPage->leaf = 0;
70134
+ if( flagByte==(PTF_ZERODATA) ){
70135
+ pPage->intKey = 0;
70136
+ pPage->intKeyLeaf = 0;
70137
+ pPage->xCellSize = cellSizePtr;
70138
+ pPage->xParseCell = btreeParseCellPtrIndex;
70139
+ pPage->maxLocal = pBt->maxLocal;
70140
+ pPage->minLocal = pBt->minLocal;
70141
+ }else if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
7000570142
pPage->intKeyLeaf = 0;
7000670143
pPage->xCellSize = cellSizePtrNoPayload;
7000770144
pPage->xParseCell = btreeParseCellPtrNoPayload;
70008
- }
70009
- pPage->maxLocal = pBt->maxLeaf;
70010
- pPage->minLocal = pBt->minLeaf;
70011
- }else if( flagByte==PTF_ZERODATA ){
70012
- /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
70013
- ** interior index b-tree page. */
70014
- assert( (PTF_ZERODATA)==2 );
70015
- /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
70016
- ** leaf index b-tree page. */
70017
- assert( (PTF_ZERODATA|PTF_LEAF)==10 );
70018
- pPage->intKey = 0;
70019
- pPage->intKeyLeaf = 0;
70020
- pPage->xCellSize = cellSizePtr;
70021
- pPage->xParseCell = btreeParseCellPtrIndex;
70022
- pPage->maxLocal = pBt->maxLocal;
70023
- pPage->minLocal = pBt->minLocal;
70024
- }else{
70025
- /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
70026
- ** an error. */
70027
- pPage->intKey = 0;
70028
- pPage->intKeyLeaf = 0;
70029
- pPage->xCellSize = cellSizePtr;
70030
- pPage->xParseCell = btreeParseCellPtrIndex;
70031
- return SQLITE_CORRUPT_PAGE(pPage);
70032
- }
70033
- pPage->max1bytePayload = pBt->max1bytePayload;
70145
+ pPage->intKey = 1;
70146
+ pPage->maxLocal = pBt->maxLeaf;
70147
+ pPage->minLocal = pBt->minLeaf;
70148
+ }else{
70149
+ pPage->intKey = 0;
70150
+ pPage->intKeyLeaf = 0;
70151
+ pPage->xCellSize = cellSizePtr;
70152
+ pPage->xParseCell = btreeParseCellPtrIndex;
70153
+ return SQLITE_CORRUPT_PAGE(pPage);
70154
+ }
70155
+ }
7003470156
return SQLITE_OK;
7003570157
}
7003670158
7003770159
/*
7003870160
** Compute the amount of freespace on the page. In other words, fill
@@ -73568,13 +73690,29 @@
7356873690
7356973691
/* Move the cursor to the last entry in the table. Return SQLITE_OK
7357073692
** on success. Set *pRes to 0 if the cursor actually points to something
7357173693
** or set *pRes to 1 if the table is empty.
7357273694
*/
73695
+static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){
73696
+ int rc = moveToRoot(pCur);
73697
+ if( rc==SQLITE_OK ){
73698
+ assert( pCur->eState==CURSOR_VALID );
73699
+ *pRes = 0;
73700
+ rc = moveToRightmost(pCur);
73701
+ if( rc==SQLITE_OK ){
73702
+ pCur->curFlags |= BTCF_AtLast;
73703
+ }else{
73704
+ pCur->curFlags &= ~BTCF_AtLast;
73705
+ }
73706
+ }else if( rc==SQLITE_EMPTY ){
73707
+ assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
73708
+ *pRes = 1;
73709
+ rc = SQLITE_OK;
73710
+ }
73711
+ return rc;
73712
+}
7357373713
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
73574
- int rc;
73575
-
7357673714
assert( cursorOwnsBtShared(pCur) );
7357773715
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
7357873716
7357973717
/* If the cursor already points to the last entry, this is a no-op. */
7358073718
if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
@@ -73591,27 +73729,11 @@
7359173729
assert( pCur->pPage->leaf );
7359273730
#endif
7359373731
*pRes = 0;
7359473732
return SQLITE_OK;
7359573733
}
73596
-
73597
- rc = moveToRoot(pCur);
73598
- if( rc==SQLITE_OK ){
73599
- assert( pCur->eState==CURSOR_VALID );
73600
- *pRes = 0;
73601
- rc = moveToRightmost(pCur);
73602
- if( rc==SQLITE_OK ){
73603
- pCur->curFlags |= BTCF_AtLast;
73604
- }else{
73605
- pCur->curFlags &= ~BTCF_AtLast;
73606
- }
73607
- }else if( rc==SQLITE_EMPTY ){
73608
- assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
73609
- *pRes = 1;
73610
- rc = SQLITE_OK;
73611
- }
73612
- return rc;
73734
+ return btreeLast(pCur, pRes);
7361373735
}
7361473736
7361573737
/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
7361673738
** table near the key intKey. Return a success code.
7361773739
**
@@ -74674,11 +74796,11 @@
7467474796
}
7467574797
7467674798
/* If the database supports auto-vacuum, write an entry in the pointer-map
7467774799
** to indicate that the page is free.
7467874800
*/
74679
- if( ISAUTOVACUUM ){
74801
+ if( ISAUTOVACUUM(pBt) ){
7468074802
ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
7468174803
if( rc ) goto freepage_out;
7468274804
}
7468374805
7468474806
/* Now manipulate the actual database free-list structure. There are two
@@ -75114,28 +75236,24 @@
7511475236
** pTemp is not null. Regardless of pTemp, allocate a new entry
7511575237
** in pPage->apOvfl[] and make it point to the cell content (either
7511675238
** in pTemp or the original pCell) and also record its index.
7511775239
** Allocating a new entry in pPage->aCell[] implies that
7511875240
** pPage->nOverflow is incremented.
75119
-**
75120
-** *pRC must be SQLITE_OK when this routine is called.
7512175241
*/
75122
-static void insertCell(
75242
+static int insertCell(
7512375243
MemPage *pPage, /* Page into which we are copying */
7512475244
int i, /* New cell becomes the i-th cell of the page */
7512575245
u8 *pCell, /* Content of the new cell */
7512675246
int sz, /* Bytes of content in pCell */
7512775247
u8 *pTemp, /* Temp storage space for pCell, if needed */
75128
- Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
75129
- int *pRC /* Read and write return code from here */
75248
+ Pgno iChild /* If non-zero, replace first 4 bytes with this value */
7513075249
){
7513175250
int idx = 0; /* Where to write new cell content in data[] */
7513275251
int j; /* Loop counter */
7513375252
u8 *data; /* The content of the whole page */
7513475253
u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
7513575254
75136
- assert( *pRC==SQLITE_OK );
7513775255
assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
7513875256
assert( MX_CELL(pPage->pBt)<=10921 );
7513975257
assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
7514075258
assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
7514175259
assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
@@ -75166,18 +75284,17 @@
7516675284
assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
7516775285
assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
7516875286
}else{
7516975287
int rc = sqlite3PagerWrite(pPage->pDbPage);
7517075288
if( rc!=SQLITE_OK ){
75171
- *pRC = rc;
75172
- return;
75289
+ return rc;
7517375290
}
7517475291
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
7517575292
data = pPage->aData;
7517675293
assert( &data[pPage->cellOffset]==pPage->aCellIdx );
7517775294
rc = allocateSpace(pPage, sz, &idx);
75178
- if( rc ){ *pRC = rc; return; }
75295
+ if( rc ){ return rc; }
7517975296
/* The allocateSpace() routine guarantees the following properties
7518075297
** if it returns successfully */
7518175298
assert( idx >= 0 );
7518275299
assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
7518375300
assert( idx+sz <= (int)pPage->pBt->usableSize );
@@ -75200,17 +75317,20 @@
7520075317
/* increment the cell count */
7520175318
if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
7520275319
assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
7520375320
#ifndef SQLITE_OMIT_AUTOVACUUM
7520475321
if( pPage->pBt->autoVacuum ){
75322
+ int rc2 = SQLITE_OK;
7520575323
/* The cell may contain a pointer to an overflow page. If so, write
7520675324
** the entry for the overflow page into the pointer map.
7520775325
*/
75208
- ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
75326
+ ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
75327
+ if( rc2 ) return rc2;
7520975328
}
7521075329
#endif
7521175330
}
75331
+ return SQLITE_OK;
7521275332
}
7521375333
7521475334
/*
7521575335
** The following parameters determine how many adjacent pages get involved
7521675336
** in a balancing operation. NN is the number of neighbors on either side
@@ -75307,18 +75427,20 @@
7530775427
/*
7530875428
** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
7530975429
** computed.
7531075430
*/
7531175431
static void populateCellCache(CellArray *p, int idx, int N){
75432
+ MemPage *pRef = p->pRef;
75433
+ u16 *szCell = p->szCell;
7531275434
assert( idx>=0 && idx+N<=p->nCell );
7531375435
while( N>0 ){
7531475436
assert( p->apCell[idx]!=0 );
75315
- if( p->szCell[idx]==0 ){
75316
- p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
75437
+ if( szCell[idx]==0 ){
75438
+ szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
7531775439
}else{
7531875440
assert( CORRUPT_DB ||
75319
- p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
75441
+ szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
7532075442
}
7532175443
idx++;
7532275444
N--;
7532375445
}
7532475446
}
@@ -75516,12 +75638,12 @@
7551675638
u8 * const pEnd = &aData[pPg->pBt->usableSize];
7551775639
u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
7551875640
int nRet = 0;
7551975641
int i;
7552075642
int iEnd = iFirst + nCell;
75521
- u8 *pFree = 0;
75522
- int szFree = 0;
75643
+ u8 *pFree = 0; /* \__ Parameters for pending call to */
75644
+ int szFree = 0; /* / freeSpace() */
7552375645
7552475646
for(i=iFirst; i<iEnd; i++){
7552575647
u8 *pCell = pCArray->apCell[i];
7552675648
if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
7552775649
int sz;
@@ -75538,10 +75660,13 @@
7553875660
szFree = sz;
7553975661
if( pFree+sz>pEnd ){
7554075662
return 0;
7554175663
}
7554275664
}else{
75665
+ /* The current cell is adjacent to and before the pFree cell.
75666
+ ** Combine the two regions into one to reduce the number of calls
75667
+ ** to freeSpace(). */
7554375668
pFree = pCell;
7554475669
szFree += sz;
7554575670
}
7554675671
nRet++;
7554775672
}
@@ -75745,11 +75870,11 @@
7574575870
** of the parent page are still manipulated by thh code below.
7574675871
** That is Ok, at this point the parent page is guaranteed to
7574775872
** be marked as dirty. Returning an error code will cause a
7574875873
** rollback, undoing any changes made to the parent page.
7574975874
*/
75750
- if( ISAUTOVACUUM ){
75875
+ if( ISAUTOVACUUM(pBt) ){
7575175876
ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
7575275877
if( szCell>pNew->minLocal ){
7575375878
ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
7575475879
}
7575575880
}
@@ -75773,12 +75898,12 @@
7577375898
pStop = &pCell[9];
7577475899
while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
7577575900
7577675901
/* Insert the new divider cell into pParent. */
7577775902
if( rc==SQLITE_OK ){
75778
- insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
75779
- 0, pPage->pgno, &rc);
75903
+ rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
75904
+ 0, pPage->pgno);
7578075905
}
7578175906
7578275907
/* Set the right-child pointer of pParent to point to the new page. */
7578375908
put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
7578475909
@@ -75883,11 +76008,11 @@
7588376008
}
7588476009
7588576010
/* If this is an auto-vacuum database, update the pointer-map entries
7588676011
** for any b-tree or overflow pages that pTo now contains the pointers to.
7588776012
*/
75888
- if( ISAUTOVACUUM ){
76013
+ if( ISAUTOVACUUM(pBt) ){
7588976014
*pRC = setChildPtrmaps(pTo);
7589076015
}
7589176016
}
7589276017
}
7589376018
@@ -76307,19 +76432,21 @@
7630776432
7630876433
r = cntNew[i-1] - 1;
7630976434
d = r + 1 - leafData;
7631076435
(void)cachedCellSize(&b, d);
7631176436
do{
76437
+ int szR, szD;
7631276438
assert( d<nMaxCells );
7631376439
assert( r<nMaxCells );
76314
- (void)cachedCellSize(&b, r);
76440
+ szR = cachedCellSize(&b, r);
76441
+ szD = b.szCell[d];
7631576442
if( szRight!=0
76316
- && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
76443
+ && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
7631776444
break;
7631876445
}
76319
- szRight += b.szCell[d] + 2;
76320
- szLeft -= b.szCell[r] + 2;
76446
+ szRight += szD + 2;
76447
+ szLeft -= szR + 2;
7632176448
cntNew[i-1] = r;
7632276449
r--;
7632376450
d--;
7632476451
}while( r>=0 );
7632576452
szNew[i] = szRight;
@@ -76369,11 +76496,11 @@
7636976496
apNew[i] = pNew;
7637076497
nNew++;
7637176498
cntOld[i] = b.nCell;
7637276499
7637376500
/* Set the pointer-map entry for the new sibling page. */
76374
- if( ISAUTOVACUUM ){
76501
+ if( ISAUTOVACUUM(pBt) ){
7637576502
ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
7637676503
if( rc!=SQLITE_OK ){
7637776504
goto balance_cleanup;
7637876505
}
7637976506
}
@@ -76462,11 +76589,11 @@
7646276589
** If the sibling pages are not leaves, then the pointer map entry
7646376590
** associated with the right-child of each sibling may also need to be
7646476591
** updated. This happens below, after the sibling pages have been
7646576592
** populated, not here.
7646676593
*/
76467
- if( ISAUTOVACUUM ){
76594
+ if( ISAUTOVACUUM(pBt) ){
7646876595
MemPage *pOld;
7646976596
MemPage *pNew = pOld = apNew[0];
7647076597
int cntOldNext = pNew->nCell + pNew->nOverflow;
7647176598
int iNew = 0;
7647276599
int iOld = 0;
@@ -76559,11 +76686,11 @@
7655976686
pSrcEnd = b.apEnd[k];
7656076687
if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){
7656176688
rc = SQLITE_CORRUPT_BKPT;
7656276689
goto balance_cleanup;
7656376690
}
76564
- insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
76691
+ rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
7656576692
if( rc!=SQLITE_OK ) goto balance_cleanup;
7656676693
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7656776694
}
7656876695
7656976696
/* Now update the actual sibling pages. The order in which they are updated
@@ -76655,11 +76782,11 @@
7665576782
- apNew[0]->nCell*2)
7665676783
|| rc!=SQLITE_OK
7665776784
);
7665876785
copyNodeContent(apNew[0], pParent, &rc);
7665976786
freePage(apNew[0], &rc);
76660
- }else if( ISAUTOVACUUM && !leafCorrection ){
76787
+ }else if( ISAUTOVACUUM(pBt) && !leafCorrection ){
7666176788
/* Fix the pointer map entries associated with the right-child of each
7666276789
** sibling page. All other pointer map entries have already been taken
7666376790
** care of. */
7666476791
for(i=0; i<nNew; i++){
7666576792
u32 key = get4byte(&apNew[i]->aData[8]);
@@ -76676,11 +76803,11 @@
7667676803
for(i=nNew; i<nOld; i++){
7667776804
freePage(apOld[i], &rc);
7667876805
}
7667976806
7668076807
#if 0
76681
- if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
76808
+ if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
7668276809
/* The ptrmapCheckPages() contains assert() statements that verify that
7668376810
** all pointer map pages are set correctly. This is helpful while
7668476811
** debugging. This is usually disabled because a corrupt database may
7668576812
** cause an assert() statement to fail. */
7668676813
ptrmapCheckPages(apNew, nNew);
@@ -76738,11 +76865,11 @@
7673876865
*/
7673976866
rc = sqlite3PagerWrite(pRoot->pDbPage);
7674076867
if( rc==SQLITE_OK ){
7674176868
rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
7674276869
copyNodeContent(pRoot, pChild, &rc);
76743
- if( ISAUTOVACUUM ){
76870
+ if( ISAUTOVACUUM(pBt) ){
7674476871
ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
7674576872
}
7674676873
}
7674776874
if( rc ){
7674876875
*ppChild = 0;
@@ -77070,11 +77197,10 @@
7707077197
int loc = seekResult; /* -1: before desired location +1: after */
7707177198
int szNew = 0;
7707277199
int idx;
7707377200
MemPage *pPage;
7707477201
Btree *p = pCur->pBtree;
77075
- BtShared *pBt = p->pBt;
7707677202
unsigned char *oldCell;
7707777203
unsigned char *newCell = 0;
7707877204
7707977205
assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
7708077206
assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
@@ -77089,11 +77215,11 @@
7708977215
** that the cursor is already where it needs to be and returns without
7709077216
** doing any work. To avoid thwarting these optimizations, it is important
7709177217
** not to clear the cursor here.
7709277218
*/
7709377219
if( pCur->curFlags & BTCF_Multiple ){
77094
- rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
77220
+ rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
7709577221
if( rc ) return rc;
7709677222
if( loc && pCur->iPage<0 ){
7709777223
/* This can only happen if the schema is corrupt such that there is more
7709877224
** than one table or index with the same root page as used by the cursor.
7709977225
** Which can only happen if the SQLITE_NoSchemaError flag was set when
@@ -77113,12 +77239,12 @@
7711377239
if( rc && rc!=SQLITE_EMPTY ) return rc;
7711477240
}
7711577241
7711677242
assert( cursorOwnsBtShared(pCur) );
7711777243
assert( (pCur->curFlags & BTCF_WriteFlag)!=0
77118
- && pBt->inTransaction==TRANS_WRITE
77119
- && (pBt->btsFlags & BTS_READ_ONLY)==0 );
77244
+ && p->pBt->inTransaction==TRANS_WRITE
77245
+ && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
7712077246
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
7712177247
7712277248
/* Assert that the caller has been consistent. If this cursor was opened
7712377249
** expecting an index b-tree, then the caller should be inserting blob
7712477250
** keys with no associated data. If the cursor was opened expecting an
@@ -77231,30 +77357,32 @@
7723177357
7723277358
TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
7723377359
pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
7723477360
loc==0 ? "overwrite" : "new entry"));
7723577361
assert( pPage->isInit || CORRUPT_DB );
77236
- newCell = pBt->pTmpSpace;
77362
+ newCell = p->pBt->pTmpSpace;
7723777363
assert( newCell!=0 );
77364
+ assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
7723877365
if( flags & BTREE_PREFORMAT ){
7723977366
rc = SQLITE_OK;
77240
- szNew = pBt->nPreformatSize;
77367
+ szNew = p->pBt->nPreformatSize;
7724177368
if( szNew<4 ) szNew = 4;
77242
- if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
77369
+ if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
7724377370
CellInfo info;
7724477371
pPage->xParseCell(pPage, newCell, &info);
7724577372
if( info.nPayload!=info.nLocal ){
7724677373
Pgno ovfl = get4byte(&newCell[szNew-4]);
77247
- ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
77374
+ ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
77375
+ if( NEVER(rc) ) goto end_insert;
7724877376
}
7724977377
}
7725077378
}else{
7725177379
rc = fillInCell(pPage, newCell, pX, &szNew);
77380
+ if( rc ) goto end_insert;
7725277381
}
77253
- if( rc ) goto end_insert;
7725477382
assert( szNew==pPage->xCellSize(pPage, newCell) );
77255
- assert( szNew <= MX_CELL_SIZE(pBt) );
77383
+ assert( szNew <= MX_CELL_SIZE(p->pBt) );
7725677384
idx = pCur->ix;
7725777385
if( loc==0 ){
7725877386
CellInfo info;
7725977387
assert( idx>=0 );
7726077388
if( idx>=pPage->nCell ){
@@ -77270,11 +77398,11 @@
7727077398
}
7727177399
BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
7727277400
testcase( pCur->curFlags & BTCF_ValidOvfl );
7727377401
invalidateOverflowCache(pCur);
7727477402
if( info.nSize==szNew && info.nLocal==info.nPayload
77275
- && (!ISAUTOVACUUM || szNew<pPage->minLocal)
77403
+ && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
7727677404
){
7727777405
/* Overwrite the old cell with the new if they are the same size.
7727877406
** We could also try to do this if the old cell is smaller, then add
7727977407
** the leftover space to the free list. But experiments show that
7728077408
** doing that is no faster then skipping this optimization and just
@@ -77300,11 +77428,11 @@
7730077428
idx = ++pCur->ix;
7730177429
pCur->curFlags &= ~BTCF_ValidNKey;
7730277430
}else{
7730377431
assert( pPage->leaf );
7730477432
}
77305
- insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
77433
+ rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
7730677434
assert( pPage->nOverflow==0 || rc==SQLITE_OK );
7730777435
assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
7730877436
7730977437
/* If no error has occurred and pPage has an overflow cell, call balance()
7731077438
** to redistribute the cells within the tree. Since balance() may move
@@ -77373,11 +77501,10 @@
7737377501
** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
7737477502
**
7737577503
** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
7737677504
*/
7737777505
SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
77378
- int rc = SQLITE_OK;
7737977506
BtShared *pBt = pDest->pBt;
7738077507
u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
7738177508
const u8 *aIn; /* Pointer to next input buffer */
7738277509
u32 nIn; /* Size of input buffer aIn[] */
7738377510
u32 nRem; /* Bytes of data still to copy */
@@ -77396,11 +77523,13 @@
7739677523
}
7739777524
nRem = pSrc->info.nPayload;
7739877525
if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
7739977526
memcpy(aOut, aIn, nIn);
7740077527
pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
77528
+ return SQLITE_OK;
7740177529
}else{
77530
+ int rc = SQLITE_OK;
7740277531
Pager *pSrcPager = pSrc->pBt->pPager;
7740377532
u8 *pPgnoOut = 0;
7740477533
Pgno ovflIn = 0;
7740577534
DbPage *pPageIn = 0;
7740677535
MemPage *pPageOut = 0;
@@ -77448,11 +77577,11 @@
7744877577
if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
7744977578
Pgno pgnoNew;
7745077579
MemPage *pNew = 0;
7745177580
rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
7745277581
put4byte(pPgnoOut, pgnoNew);
77453
- if( ISAUTOVACUUM && pPageOut ){
77582
+ if( ISAUTOVACUUM(pBt) && pPageOut ){
7745477583
ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
7745577584
}
7745677585
releasePage(pPageOut);
7745777586
pPageOut = pNew;
7745877587
if( pPageOut ){
@@ -77464,13 +77593,12 @@
7746477593
}
7746577594
}while( nRem>0 && rc==SQLITE_OK );
7746677595
7746777596
releasePage(pPageOut);
7746877597
sqlite3PagerUnref(pPageIn);
77598
+ return rc;
7746977599
}
77470
-
77471
- return rc;
7747277600
}
7747377601
7747477602
/*
7747577603
** Delete the entry that the cursor is pointing to.
7747677604
**
@@ -77621,11 +77749,11 @@
7762177749
assert( MX_CELL_SIZE(pBt) >= nCell );
7762277750
pTmp = pBt->pTmpSpace;
7762377751
assert( pTmp!=0 );
7762477752
rc = sqlite3PagerWrite(pLeaf->pDbPage);
7762577753
if( rc==SQLITE_OK ){
77626
- insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
77754
+ rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
7762777755
}
7762877756
dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
7762977757
if( rc ) return rc;
7763077758
}
7763177759
@@ -79144,10 +79272,21 @@
7914479272
7914579273
/*
7914679274
** Return the size of the header added to each page by this module.
7914779275
*/
7914879276
SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
79277
+
79278
+/*
79279
+** If no transaction is active and the database is not a temp-db, clear
79280
+** the in-memory pager cache.
79281
+*/
79282
+SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree *p){
79283
+ BtShared *pBt = p->pBt;
79284
+ if( pBt->inTransaction==TRANS_NONE ){
79285
+ sqlite3PagerClearCache(pBt->pPager);
79286
+ }
79287
+}
7914979288
7915079289
#if !defined(SQLITE_OMIT_SHARED_CACHE)
7915179290
/*
7915279291
** Return true if the Btree passed as the only argument is sharable.
7915379292
*/
@@ -83384,11 +83523,11 @@
8338483523
assert( n!=P4_INT32 && n!=P4_VTAB );
8338583524
assert( n<=0 );
8338683525
if( p->db->mallocFailed ){
8338783526
freeP4(p->db, n, pP4);
8338883527
}else{
83389
- assert( pP4!=0 );
83528
+ assert( pP4!=0 || n==P4_DYNAMIC );
8339083529
assert( p->nOp>0 );
8339183530
pOp = &p->aOp[p->nOp-1];
8339283531
assert( pOp->p4type==P4_NOTUSED );
8339383532
pOp->p4type = n;
8339483533
pOp->p4.p = pP4;
@@ -87760,11 +87899,14 @@
8776087899
void (*xDel)(void *),
8776187900
unsigned char enc
8776287901
){
8776387902
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
8776487903
assert( xDel!=SQLITE_DYNAMIC );
87765
- if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
87904
+ if( enc!=SQLITE_UTF8 ){
87905
+ if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
87906
+ n &= ~(u64)1;
87907
+ }
8776687908
if( n>0x7fffffff ){
8776787909
(void)invokeValueDestructor(z, xDel, pCtx);
8776887910
}else{
8776987911
setResultStrOrError(pCtx, z, (int)n, enc, xDel);
8777087912
}
@@ -87775,29 +87917,29 @@
8777587917
const void *z,
8777687918
int n,
8777787919
void (*xDel)(void *)
8777887920
){
8777987921
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87780
- setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
87922
+ setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel);
8778187923
}
8778287924
SQLITE_API void sqlite3_result_text16be(
8778387925
sqlite3_context *pCtx,
8778487926
const void *z,
8778587927
int n,
8778687928
void (*xDel)(void *)
8778787929
){
8778887930
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87789
- setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
87931
+ setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel);
8779087932
}
8779187933
SQLITE_API void sqlite3_result_text16le(
8779287934
sqlite3_context *pCtx,
8779387935
const void *z,
8779487936
int n,
8779587937
void (*xDel)(void *)
8779687938
){
8779787939
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87798
- setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
87940
+ setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel);
8779987941
}
8780087942
#endif /* SQLITE_OMIT_UTF16 */
8780187943
SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
8780287944
Mem *pOut = pCtx->pOut;
8780387945
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
@@ -88858,22 +89000,25 @@
8885889000
sqlite3_uint64 nData,
8885989001
void (*xDel)(void*),
8886089002
unsigned char enc
8886189003
){
8886289004
assert( xDel!=SQLITE_DYNAMIC );
88863
- if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
89005
+ if( enc!=SQLITE_UTF8 ){
89006
+ if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
89007
+ nData &= ~(u16)1;
89008
+ }
8886489009
return bindText(pStmt, i, zData, nData, xDel, enc);
8886589010
}
8886689011
#ifndef SQLITE_OMIT_UTF16
8886789012
SQLITE_API int sqlite3_bind_text16(
8886889013
sqlite3_stmt *pStmt,
8886989014
int i,
8887089015
const void *zData,
88871
- int nData,
89016
+ int n,
8887289017
void (*xDel)(void*)
8887389018
){
88874
- return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
89019
+ return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE);
8887589020
}
8887689021
#endif /* SQLITE_OMIT_UTF16 */
8887789022
SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
8887889023
int rc;
8887989024
switch( sqlite3_value_type((sqlite3_value*)pValue) ){
@@ -90242,20 +90387,10 @@
9024290387
#else
9024390388
# define REGISTER_TRACE(R,M)
9024490389
#endif
9024590390
9024690391
90247
-#ifdef VDBE_PROFILE
90248
-
90249
-/*
90250
-** hwtime.h contains inline assembler code for implementing
90251
-** high-performance timing routines.
90252
-*/
90253
-/* #include "hwtime.h" */
90254
-
90255
-#endif
90256
-
9025790392
#ifndef NDEBUG
9025890393
/*
9025990394
** This function is only called from within an assert() expression. It
9026090395
** checks that the sqlite3.nTransaction variable is correctly set to
9026190396
** the number of non-transaction savepoints currently in the
@@ -95203,10 +95338,11 @@
9520395338
x.nZero = pData->u.nZero;
9520495339
}else{
9520595340
x.nZero = 0;
9520695341
}
9520795342
x.pKey = 0;
95343
+ assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
9520895344
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
9520995345
(pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
9521095346
seekResult
9521195347
);
9521295348
pC->deferredMoveto = 0;
@@ -105097,11 +105233,11 @@
105097105233
pExpr = pExpr->pLeft;
105098105234
assert( pExpr!=0 );
105099105235
}
105100105236
op = pExpr->op;
105101105237
if( op==TK_REGISTER ) op = pExpr->op2;
105102
- if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
105238
+ if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
105103105239
assert( ExprUseYTab(pExpr) );
105104105240
assert( pExpr->y.pTab!=0 );
105105105241
return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
105106105242
}
105107105243
if( op==TK_SELECT ){
@@ -105217,11 +105353,13 @@
105217105353
CollSeq *pColl = 0;
105218105354
const Expr *p = pExpr;
105219105355
while( p ){
105220105356
int op = p->op;
105221105357
if( op==TK_REGISTER ) op = p->op2;
105222
- if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
105358
+ if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
105359
+ || op==TK_COLUMN || op==TK_TRIGGER
105360
+ ){
105223105361
int j;
105224105362
assert( ExprUseYTab(p) );
105225105363
assert( p->y.pTab!=0 );
105226105364
if( (j = p->iColumn)>=0 ){
105227105365
const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
@@ -109081,11 +109219,11 @@
109081109219
}
109082109220
return target;
109083109221
}
109084109222
109085109223
/*
109086
-** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109224
+** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
109087109225
** If it is, then resolve the expression by reading from the index and
109088109226
** return the register into which the value has been read. If pExpr is
109089109227
** not an indexed expression, then return negative.
109090109228
*/
109091109229
static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
@@ -109093,11 +109231,11 @@
109093109231
Expr *pExpr, /* The expression to potentially bypass */
109094109232
int target /* Where to store the result of the expression */
109095109233
){
109096109234
IndexedExpr *p;
109097109235
Vdbe *v;
109098
- for(p=pParse->pIdxExpr; p; p=p->pIENext){
109236
+ for(p=pParse->pIdxEpr; p; p=p->pIENext){
109099109237
int iDataCur = p->iDataCur;
109100109238
if( iDataCur<0 ) continue;
109101109239
if( pParse->iSelfTab ){
109102109240
if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109103109241
iDataCur = -1;
@@ -109113,14 +109251,14 @@
109113109251
sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109114109252
VdbeCoverage(v);
109115109253
sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109116109254
VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109117109255
sqlite3VdbeGoto(v, 0);
109118
- p = pParse->pIdxExpr;
109119
- pParse->pIdxExpr = 0;
109256
+ p = pParse->pIdxEpr;
109257
+ pParse->pIdxEpr = 0;
109120109258
sqlite3ExprCode(pParse, pExpr, target);
109121
- pParse->pIdxExpr = p;
109259
+ pParse->pIdxEpr = p;
109122109260
sqlite3VdbeJumpHere(v, addr+2);
109123109261
}else{
109124109262
sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109125109263
VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109126109264
}
@@ -109155,11 +109293,11 @@
109155109293
assert( v!=0 );
109156109294
109157109295
expr_code_doover:
109158109296
if( pExpr==0 ){
109159109297
op = TK_NULL;
109160
- }else if( pParse->pIdxExpr!=0
109298
+ }else if( pParse->pIdxEpr!=0
109161109299
&& !ExprHasProperty(pExpr, EP_Leaf)
109162109300
&& (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109163109301
){
109164109302
return r1;
109165109303
}else{
@@ -109172,26 +109310,32 @@
109172109310
struct AggInfo_col *pCol;
109173109311
assert( pAggInfo!=0 );
109174109312
assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109175109313
pCol = &pAggInfo->aCol[pExpr->iAgg];
109176109314
if( !pAggInfo->directMode ){
109177
- assert( pCol->iMem>0 );
109178
- return pCol->iMem;
109315
+ return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
109179109316
}else if( pAggInfo->useSortingIdx ){
109180109317
Table *pTab = pCol->pTab;
109181109318
sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109182109319
pCol->iSorterColumn, target);
109183
- if( pCol->iColumn<0 ){
109320
+ if( pTab==0 ){
109321
+ /* No comment added */
109322
+ }else if( pCol->iColumn<0 ){
109184109323
VdbeComment((v,"%s.rowid",pTab->zName));
109185
- }else if( ALWAYS(pTab!=0) ){
109324
+ }else{
109186109325
VdbeComment((v,"%s.%s",
109187109326
pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
109188109327
if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
109189109328
sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
109190109329
}
109191109330
}
109192109331
return target;
109332
+ }else if( pExpr->y.pTab==0 ){
109333
+ /* This case happens when the argument to an aggregate function
109334
+ ** is rewritten by aggregateConvertIndexedExprRefToColumn() */
109335
+ sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
109336
+ return target;
109193109337
}
109194109338
/* Otherwise, fall thru into the TK_COLUMN case */
109195109339
/* no break */ deliberate_fall_through
109196109340
}
109197109341
case TK_COLUMN: {
@@ -109485,11 +109629,11 @@
109485109629
|| NEVER(pExpr->iAgg>=pInfo->nFunc)
109486109630
){
109487109631
assert( !ExprHasProperty(pExpr, EP_IntValue) );
109488109632
sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr);
109489109633
}else{
109490
- return pInfo->aFunc[pExpr->iAgg].iMem;
109634
+ return AggInfoFuncReg(pInfo, pExpr->iAgg);
109491109635
}
109492109636
break;
109493109637
}
109494109638
case TK_FUNCTION: {
109495109639
ExprList *pFarg; /* List of function arguments */
@@ -109774,11 +109918,11 @@
109774109918
u8 okConstFactor = pParse->okConstFactor;
109775109919
AggInfo *pAggInfo = pExpr->pAggInfo;
109776109920
if( pAggInfo ){
109777109921
assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109778109922
if( !pAggInfo->directMode ){
109779
- inReg = pAggInfo->aCol[pExpr->iAgg].iMem;
109923
+ inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
109780109924
break;
109781109925
}
109782109926
if( pExpr->pAggInfo->useSortingIdx ){
109783109927
sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109784109928
pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
@@ -111285,10 +111429,76 @@
111285111429
&pInfo->nFunc,
111286111430
&i
111287111431
);
111288111432
return i;
111289111433
}
111434
+
111435
+/*
111436
+** Search the AggInfo object for an aCol[] entry that has iTable and iColumn.
111437
+** Return the index in aCol[] of the entry that describes that column.
111438
+**
111439
+** If no prior entry is found, create a new one and return -1. The
111440
+** new column will have an idex of pAggInfo->nColumn-1.
111441
+*/
111442
+static void findOrCreateAggInfoColumn(
111443
+ Parse *pParse, /* Parsing context */
111444
+ AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */
111445
+ Expr *pExpr /* Expr describing the column to find or insert */
111446
+){
111447
+ struct AggInfo_col *pCol;
111448
+ int k;
111449
+
111450
+ assert( pAggInfo->iFirstReg==0 );
111451
+ pCol = pAggInfo->aCol;
111452
+ for(k=0; k<pAggInfo->nColumn; k++, pCol++){
111453
+ if( pCol->iTable==pExpr->iTable
111454
+ && pCol->iColumn==pExpr->iColumn
111455
+ && pExpr->op!=TK_IF_NULL_ROW
111456
+ ){
111457
+ goto fix_up_expr;
111458
+ }
111459
+ }
111460
+ k = addAggInfoColumn(pParse->db, pAggInfo);
111461
+ if( k<0 ){
111462
+ /* OOM on resize */
111463
+ assert( pParse->db->mallocFailed );
111464
+ return;
111465
+ }
111466
+ pCol = &pAggInfo->aCol[k];
111467
+ assert( ExprUseYTab(pExpr) );
111468
+ pCol->pTab = pExpr->y.pTab;
111469
+ pCol->iTable = pExpr->iTable;
111470
+ pCol->iColumn = pExpr->iColumn;
111471
+ pCol->iSorterColumn = -1;
111472
+ pCol->pCExpr = pExpr;
111473
+ if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
111474
+ int j, n;
111475
+ ExprList *pGB = pAggInfo->pGroupBy;
111476
+ struct ExprList_item *pTerm = pGB->a;
111477
+ n = pGB->nExpr;
111478
+ for(j=0; j<n; j++, pTerm++){
111479
+ Expr *pE = pTerm->pExpr;
111480
+ if( pE->op==TK_COLUMN
111481
+ && pE->iTable==pExpr->iTable
111482
+ && pE->iColumn==pExpr->iColumn
111483
+ ){
111484
+ pCol->iSorterColumn = j;
111485
+ break;
111486
+ }
111487
+ }
111488
+ }
111489
+ if( pCol->iSorterColumn<0 ){
111490
+ pCol->iSorterColumn = pAggInfo->nSortingColumn++;
111491
+ }
111492
+fix_up_expr:
111493
+ ExprSetVVAProperty(pExpr, EP_NoReduce);
111494
+ pExpr->pAggInfo = pAggInfo;
111495
+ if( pExpr->op==TK_COLUMN ){
111496
+ pExpr->op = TK_AGG_COLUMN;
111497
+ }
111498
+ pExpr->iAgg = (i16)k;
111499
+}
111290111500
111291111501
/*
111292111502
** This is the xExprCallback for a tree walker. It is used to
111293111503
** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
111294111504
** for additional information.
@@ -111299,11 +111509,40 @@
111299111509
Parse *pParse = pNC->pParse;
111300111510
SrcList *pSrcList = pNC->pSrcList;
111301111511
AggInfo *pAggInfo = pNC->uNC.pAggInfo;
111302111512
111303111513
assert( pNC->ncFlags & NC_UAggInfo );
111514
+ assert( pAggInfo->iFirstReg==0 );
111304111515
switch( pExpr->op ){
111516
+ default: {
111517
+ IndexedExpr *pIEpr;
111518
+ Expr tmp;
111519
+ assert( pParse->iSelfTab==0 );
111520
+ if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
111521
+ if( pParse->pIdxEpr==0 ) break;
111522
+ for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
111523
+ int iDataCur = pIEpr->iDataCur;
111524
+ if( iDataCur<0 ) continue;
111525
+ if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
111526
+ }
111527
+ if( pIEpr==0 ) break;
111528
+ if( NEVER(!ExprUseYTab(pExpr)) ) break;
111529
+
111530
+ /* If we reach this point, it means that expression pExpr can be
111531
+ ** translated into a reference to an index column as described by
111532
+ ** pIEpr.
111533
+ */
111534
+ memset(&tmp, 0, sizeof(tmp));
111535
+ tmp.op = TK_AGG_COLUMN;
111536
+ tmp.iTable = pIEpr->iIdxCur;
111537
+ tmp.iColumn = pIEpr->iIdxCol;
111538
+ findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp);
111539
+ pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
111540
+ pExpr->pAggInfo = pAggInfo;
111541
+ pExpr->iAgg = tmp.iAgg;
111542
+ return WRC_Prune;
111543
+ }
111305111544
case TK_IF_NULL_ROW:
111306111545
case TK_AGG_COLUMN:
111307111546
case TK_COLUMN: {
111308111547
testcase( pExpr->op==TK_AGG_COLUMN );
111309111548
testcase( pExpr->op==TK_COLUMN );
@@ -111311,71 +111550,13 @@
111311111550
/* Check to see if the column is in one of the tables in the FROM
111312111551
** clause of the aggregate query */
111313111552
if( ALWAYS(pSrcList!=0) ){
111314111553
SrcItem *pItem = pSrcList->a;
111315111554
for(i=0; i<pSrcList->nSrc; i++, pItem++){
111316
- struct AggInfo_col *pCol;
111317111555
assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
111318111556
if( pExpr->iTable==pItem->iCursor ){
111319
- /* If we reach this point, it means that pExpr refers to a table
111320
- ** that is in the FROM clause of the aggregate query.
111321
- **
111322
- ** Make an entry for the column in pAggInfo->aCol[] if there
111323
- ** is not an entry there already.
111324
- */
111325
- int k;
111326
- pCol = pAggInfo->aCol;
111327
- for(k=0; k<pAggInfo->nColumn; k++, pCol++){
111328
- if( pCol->iTable==pExpr->iTable
111329
- && pCol->iColumn==pExpr->iColumn
111330
- && pExpr->op!=TK_IF_NULL_ROW
111331
- ){
111332
- break;
111333
- }
111334
- }
111335
- if( (k>=pAggInfo->nColumn)
111336
- && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
111337
- ){
111338
- pCol = &pAggInfo->aCol[k];
111339
- assert( ExprUseYTab(pExpr) );
111340
- pCol->pTab = pExpr->y.pTab;
111341
- pCol->iTable = pExpr->iTable;
111342
- pCol->iColumn = pExpr->iColumn;
111343
- pCol->iMem = ++pParse->nMem;
111344
- pCol->iSorterColumn = -1;
111345
- pCol->pCExpr = pExpr;
111346
- if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
111347
- int j, n;
111348
- ExprList *pGB = pAggInfo->pGroupBy;
111349
- struct ExprList_item *pTerm = pGB->a;
111350
- n = pGB->nExpr;
111351
- for(j=0; j<n; j++, pTerm++){
111352
- Expr *pE = pTerm->pExpr;
111353
- if( pE->op==TK_COLUMN
111354
- && pE->iTable==pExpr->iTable
111355
- && pE->iColumn==pExpr->iColumn
111356
- ){
111357
- pCol->iSorterColumn = j;
111358
- break;
111359
- }
111360
- }
111361
- }
111362
- if( pCol->iSorterColumn<0 ){
111363
- pCol->iSorterColumn = pAggInfo->nSortingColumn++;
111364
- }
111365
- }
111366
- /* There is now an entry for pExpr in pAggInfo->aCol[] (either
111367
- ** because it was there before or because we just created it).
111368
- ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
111369
- ** pAggInfo->aCol[] entry.
111370
- */
111371
- ExprSetVVAProperty(pExpr, EP_NoReduce);
111372
- pExpr->pAggInfo = pAggInfo;
111373
- if( pExpr->op==TK_COLUMN ){
111374
- pExpr->op = TK_AGG_COLUMN;
111375
- }
111376
- pExpr->iAgg = (i16)k;
111557
+ findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr);
111377111558
break;
111378111559
} /* endif pExpr->iTable==pItem->iCursor */
111379111560
} /* end loop over pSrcList */
111380111561
}
111381111562
return WRC_Prune;
@@ -111401,11 +111582,10 @@
111401111582
i = addAggInfoFunc(pParse->db, pAggInfo);
111402111583
if( i>=0 ){
111403111584
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
111404111585
pItem = &pAggInfo->aFunc[i];
111405111586
pItem->pFExpr = pExpr;
111406
- pItem->iMem = ++pParse->nMem;
111407111587
assert( ExprUseUToken(pExpr) );
111408111588
pItem->pFunc = sqlite3FindFunction(pParse->db,
111409111589
pExpr->u.zToken,
111410111590
pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
111411111591
if( pExpr->flags & EP_Distinct ){
@@ -126002,21 +126182,19 @@
126002126182
default:
126003126183
return;
126004126184
}
126005126185
ans = log(x)/b;
126006126186
}else{
126007
- ans = log(x);
126008126187
switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
126009126188
case 1:
126010
- /* Convert from natural logarithm to log base 10 */
126011
- ans /= M_LN10;
126189
+ ans = log10(x);
126012126190
break;
126013126191
case 2:
126014
- /* Convert from natural logarithm to log base 2 */
126015
- ans /= M_LN2;
126192
+ ans = log2(x);
126016126193
break;
126017126194
default:
126195
+ ans = log(x);
126018126196
break;
126019126197
}
126020126198
}
126021126199
sqlite3_result_double(context, ans);
126022126200
}
@@ -129565,10 +129743,11 @@
129565129743
/* no break */ deliberate_fall_through
129566129744
case OE_Rollback:
129567129745
case OE_Fail: {
129568129746
char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
129569129747
pCol->zCnName);
129748
+ testcase( zMsg==0 && db->mallocFailed==0 );
129570129749
sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
129571129750
onError, iReg);
129572129751
sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
129573129752
sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
129574129753
VdbeCoverage(v);
@@ -132296,11 +132475,11 @@
132296132475
0,
132297132476
0,
132298132477
#endif
132299132478
sqlite3_db_name,
132300132479
/* Version 3.40.0 and later */
132301
- sqlite3_value_type
132480
+ sqlite3_value_encoding
132302132481
};
132303132482
132304132483
/* True if x is the directory separator character
132305132484
*/
132306132485
#if SQLITE_OS_WIN
@@ -139355,11 +139534,11 @@
139355139534
#endif
139356139535
139357139536
if( pParse->colNamesSet ) return;
139358139537
/* Column names are determined by the left-most term of a compound select */
139359139538
while( pSelect->pPrior ) pSelect = pSelect->pPrior;
139360
- SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
139539
+ TREETRACE(0x80,pParse,pSelect,("generating column names\n"));
139361139540
pTabList = pSelect->pSrc;
139362139541
pEList = pSelect->pEList;
139363139542
assert( v!=0 );
139364139543
assert( pTabList!=0 );
139365139544
pParse->colNamesSet = 1;
@@ -140141,11 +140320,11 @@
140141140320
int nLimit = 0; /* Initialize to suppress harmless compiler warning */
140142140321
assert( !pPrior->pLimit );
140143140322
pPrior->iLimit = p->iLimit;
140144140323
pPrior->iOffset = p->iOffset;
140145140324
pPrior->pLimit = p->pLimit;
140146
- SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL left...\n"));
140325
+ TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
140147140326
rc = sqlite3Select(pParse, pPrior, &dest);
140148140327
pPrior->pLimit = 0;
140149140328
if( rc ){
140150140329
goto multi_select_end;
140151140330
}
@@ -140159,11 +140338,11 @@
140159140338
sqlite3VdbeAddOp3(v, OP_OffsetLimit,
140160140339
p->iLimit, p->iOffset+1, p->iOffset);
140161140340
}
140162140341
}
140163140342
ExplainQueryPlan((pParse, 1, "UNION ALL"));
140164
- SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL right...\n"));
140343
+ TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
140165140344
rc = sqlite3Select(pParse, p, &dest);
140166140345
testcase( rc!=SQLITE_OK );
140167140346
pDelete = p->pPrior;
140168140347
p->pPrior = pPrior;
140169140348
p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
@@ -140212,11 +140391,11 @@
140212140391
140213140392
/* Code the SELECT statements to our left
140214140393
*/
140215140394
assert( !pPrior->pOrderBy );
140216140395
sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
140217
- SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
140396
+ TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
140218140397
rc = sqlite3Select(pParse, pPrior, &uniondest);
140219140398
if( rc ){
140220140399
goto multi_select_end;
140221140400
}
140222140401
@@ -140232,11 +140411,11 @@
140232140411
pLimit = p->pLimit;
140233140412
p->pLimit = 0;
140234140413
uniondest.eDest = op;
140235140414
ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
140236140415
sqlite3SelectOpName(p->op)));
140237
- SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
140416
+ TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
140238140417
rc = sqlite3Select(pParse, p, &uniondest);
140239140418
testcase( rc!=SQLITE_OK );
140240140419
assert( p->pOrderBy==0 );
140241140420
pDelete = p->pPrior;
140242140421
p->pPrior = pPrior;
@@ -140293,11 +140472,11 @@
140293140472
assert( p->pEList );
140294140473
140295140474
/* Code the SELECTs to our left into temporary table "tab1".
140296140475
*/
140297140476
sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
140298
- SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT left...\n"));
140477
+ TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
140299140478
rc = sqlite3Select(pParse, pPrior, &intersectdest);
140300140479
if( rc ){
140301140480
goto multi_select_end;
140302140481
}
140303140482
@@ -140310,11 +140489,11 @@
140310140489
pLimit = p->pLimit;
140311140490
p->pLimit = 0;
140312140491
intersectdest.iSDParm = tab2;
140313140492
ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
140314140493
sqlite3SelectOpName(p->op)));
140315
- SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT right...\n"));
140494
+ TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
140316140495
rc = sqlite3Select(pParse, p, &intersectdest);
140317140496
testcase( rc!=SQLITE_OK );
140318140497
pDelete = p->pPrior;
140319140498
p->pPrior = pPrior;
140320140499
if( p->nSelectRow>pPrior->nSelectRow ){
@@ -141313,10 +141492,38 @@
141313141492
while( pSel->pPrior ){
141314141493
pSel = pSel->pPrior;
141315141494
}
141316141495
return pSel->pEList;
141317141496
}
141497
+
141498
+/*
141499
+** Return true if any of the result-set columns in the compound query
141500
+** have incompatible affinities on one or more arms of the compound.
141501
+*/
141502
+static int compoundHasDifferentAffinities(Select *p){
141503
+ int ii;
141504
+ ExprList *pList;
141505
+ assert( p!=0 );
141506
+ assert( p->pEList!=0 );
141507
+ assert( p->pPrior!=0 );
141508
+ pList = p->pEList;
141509
+ for(ii=0; ii<pList->nExpr; ii++){
141510
+ char aff;
141511
+ Select *pSub1;
141512
+ assert( pList->a[ii].pExpr!=0 );
141513
+ aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
141514
+ for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
141515
+ assert( pSub1->pEList!=0 );
141516
+ assert( pSub1->pEList->nExpr>ii );
141517
+ assert( pSub1->pEList->a[ii].pExpr!=0 );
141518
+ if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
141519
+ return 1;
141520
+ }
141521
+ }
141522
+ }
141523
+ return 0;
141524
+}
141318141525
141319141526
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
141320141527
/*
141321141528
** This routine attempts to flatten subqueries as a performance optimization.
141322141529
** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -141417,11 +141624,12 @@
141417141624
** (17f) the subquery must not be the RHS of a LEFT JOIN.
141418141625
** (17g) either the subquery is the first element of the outer
141419141626
** query or there are no RIGHT or FULL JOINs in any arm
141420141627
** of the subquery. (This is a duplicate of condition (27b).)
141421141628
** (17h) The corresponding result set expressions in all arms of the
141422
-** compound must have the same affinity.
141629
+** compound must have the same affinity. (See restriction (9)
141630
+** on the push-down optimization.)
141423141631
**
141424141632
** The parent and sub-query may contain WHERE clauses. Subject to
141425141633
** rules (11), (13) and (14), they may also contain ORDER BY,
141426141634
** LIMIT and OFFSET clauses. The subquery cannot use any compound
141427141635
** operator other than UNION ALL because all the other compound
@@ -141636,23 +141844,11 @@
141636141844
141637141845
/* Restriction (23) */
141638141846
if( (p->selFlags & SF_Recursive) ) return 0;
141639141847
141640141848
/* Restriction (17h) */
141641
- for(ii=0; ii<pSub->pEList->nExpr; ii++){
141642
- char aff;
141643
- assert( pSub->pEList->a[ii].pExpr!=0 );
141644
- aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
141645
- for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
141646
- assert( pSub1->pEList!=0 );
141647
- assert( pSub1->pEList->nExpr>ii );
141648
- assert( pSub1->pEList->a[ii].pExpr!=0 );
141649
- if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
141650
- return 0;
141651
- }
141652
- }
141653
- }
141849
+ if( compoundHasDifferentAffinities(pSub) ) return 0;
141654141850
141655141851
if( pSrc->nSrc>1 ){
141656141852
if( pParse->nSelect>500 ) return 0;
141657141853
if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
141658141854
aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
@@ -141659,11 +141855,11 @@
141659141855
if( aCsrMap ) aCsrMap[0] = pParse->nTab;
141660141856
}
141661141857
}
141662141858
141663141859
/***** If we reach this point, flattening is permitted. *****/
141664
- SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
141860
+ TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n",
141665141861
pSub->selId, pSub, iFrom));
141666141862
141667141863
/* Authorize the subquery */
141668141864
pParse->zAuthContext = pSubitem->zName;
141669141865
TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
@@ -141738,11 +141934,11 @@
141738141934
}
141739141935
pNew->pPrior = pPrior;
141740141936
if( pPrior ) pPrior->pNext = pNew;
141741141937
pNew->pNext = p;
141742141938
p->pPrior = pNew;
141743
- SELECTTRACE(2,pParse,p,("compound-subquery flattener"
141939
+ TREETRACE(0x4,pParse,p,("compound-subquery flattener"
141744141940
" creates %u as peer\n",pNew->selId));
141745141941
}
141746141942
assert( pSubitem->pSelect==0 );
141747141943
}
141748141944
sqlite3DbFree(db, aCsrMap);
@@ -141918,12 +142114,12 @@
141918142114
sqlite3AggInfoPersistWalkerInit(&w, pParse);
141919142115
sqlite3WalkSelect(&w,pSub1);
141920142116
sqlite3SelectDelete(db, pSub1);
141921142117
141922142118
#if TREETRACE_ENABLED
141923
- if( sqlite3TreeTrace & 0x100 ){
141924
- SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
142119
+ if( sqlite3TreeTrace & 0x4 ){
142120
+ TREETRACE(0x4,pParse,p,("After flattening:\n"));
141925142121
sqlite3TreeViewSelect(0, p, 0);
141926142122
}
141927142123
#endif
141928142124
141929142125
return 1;
@@ -142293,16 +142489,18 @@
142293142489
**
142294142490
** (7) The inner query is a Common Table Expression (CTE) that should
142295142491
** be materialized. (This restriction is implemented in the calling
142296142492
** routine.)
142297142493
**
142298
-** (8) The subquery may not be a compound that uses UNION, INTERSECT,
142299
-** or EXCEPT. (We could, perhaps, relax this restriction to allow
142300
-** this case if none of the comparisons operators between left and
142301
-** right arms of the compound use a collation other than BINARY.
142302
-** But it is a lot of work to check that case for an obscure and
142303
-** minor optimization, so we omit it for now.)
142494
+** (8) If the subquery is a compound that uses UNION, INTERSECT,
142495
+** or EXCEPT, then all of the result set columns for all arms of
142496
+** the compound must use the BINARY collating sequence.
142497
+**
142498
+** (9) If the subquery is a compound, then all arms of the compound must
142499
+** have the same affinity. (This is the same as restriction (17h)
142500
+** for query flattening.)
142501
+**
142304142502
**
142305142503
** Return 0 if no changes are made and non-zero if one or more WHERE clause
142306142504
** terms are duplicated into the subquery.
142307142505
*/
142308142506
static int pushDownWhereTerms(
@@ -142315,24 +142513,48 @@
142315142513
int nChng = 0;
142316142514
if( pWhere==0 ) return 0;
142317142515
if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
142318142516
if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
142319142517
142320
-#ifndef SQLITE_OMIT_WINDOWFUNC
142321142518
if( pSubq->pPrior ){
142322142519
Select *pSel;
142520
+ int notUnionAll = 0;
142323142521
for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142324142522
u8 op = pSel->op;
142325142523
assert( op==TK_ALL || op==TK_SELECT
142326142524
|| op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
142327
- if( op!=TK_ALL && op!=TK_SELECT ) return 0; /* restriction (8) */
142525
+ if( op!=TK_ALL && op!=TK_SELECT ){
142526
+ notUnionAll = 1;
142527
+ }
142528
+#ifndef SQLITE_OMIT_WINDOWFUNC
142328142529
if( pSel->pWin ) return 0; /* restriction (6b) */
142530
+#endif
142531
+ }
142532
+ if( compoundHasDifferentAffinities(pSubq) ){
142533
+ return 0; /* restriction (9) */
142534
+ }
142535
+ if( notUnionAll ){
142536
+ /* If any of the compound arms are connected using UNION, INTERSECT,
142537
+ ** or EXCEPT, then we must ensure that none of the columns use a
142538
+ ** non-BINARY collating sequence. */
142539
+ for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142540
+ int ii;
142541
+ const ExprList *pList = pSel->pEList;
142542
+ assert( pList!=0 );
142543
+ for(ii=0; ii<pList->nExpr; ii++){
142544
+ CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
142545
+ if( !sqlite3IsBinary(pColl) ){
142546
+ return 0; /* Restriction (8) */
142547
+ }
142548
+ }
142549
+ }
142329142550
}
142330142551
}else{
142552
+#ifndef SQLITE_OMIT_WINDOWFUNC
142331142553
if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
142554
+#endif
142332142555
}
142333
-#endif
142334142556
142335142557
#ifdef SQLITE_DEBUG
142336142558
/* Only the first term of a compound can have a WITH clause. But make
142337142559
** sure no other terms are marked SF_Recursive in case something changes
142338142560
** in the future.
@@ -143332,12 +143554,12 @@
143332143554
if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
143333143555
p->selFlags |= SF_ComplexResult;
143334143556
}
143335143557
}
143336143558
#if TREETRACE_ENABLED
143337
- if( sqlite3TreeTrace & 0x100 ){
143338
- SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
143559
+ if( sqlite3TreeTrace & 0x8 ){
143560
+ TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
143339143561
sqlite3TreeViewSelect(0, p, 0);
143340143562
}
143341143563
#endif
143342143564
return WRC_Continue;
143343143565
}
@@ -143467,10 +143689,177 @@
143467143689
if( pParse->nErr ) return;
143468143690
sqlite3ResolveSelectNames(pParse, p, pOuterNC);
143469143691
if( pParse->nErr ) return;
143470143692
sqlite3SelectAddTypeInfo(pParse, p);
143471143693
}
143694
+
143695
+#if TREETRACE_ENABLED
143696
+/*
143697
+** Display all information about an AggInfo object
143698
+*/
143699
+static void printAggInfo(AggInfo *pAggInfo){
143700
+ int ii;
143701
+ for(ii=0; ii<pAggInfo->nColumn; ii++){
143702
+ struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
143703
+ sqlite3DebugPrintf(
143704
+ "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
143705
+ " iSorterColumn=%d %s\n",
143706
+ ii, pCol->pTab ? pCol->pTab->zName : "NULL",
143707
+ pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
143708
+ pCol->iSorterColumn,
143709
+ ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
143710
+ sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
143711
+ }
143712
+ for(ii=0; ii<pAggInfo->nFunc; ii++){
143713
+ sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
143714
+ ii, AggInfoFuncReg(pAggInfo,ii));
143715
+ sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
143716
+ }
143717
+}
143718
+#endif /* TREETRACE_ENABLED */
143719
+
143720
+/*
143721
+** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
143722
+** entries for columns that are arguments to aggregate functions but which
143723
+** are not otherwise used.
143724
+**
143725
+** The aCol[] entries in AggInfo prior to nAccumulator are columns that
143726
+** are referenced outside of aggregate functions. These might be columns
143727
+** that are part of the GROUP by clause, for example. Other database engines
143728
+** would throw an error if there is a column reference that is not in the
143729
+** GROUP BY clause and that is not part of an aggregate function argument.
143730
+** But SQLite allows this.
143731
+**
143732
+** The aCol[] entries beginning with the aCol[nAccumulator] and following
143733
+** are column references that are used exclusively as arguments to
143734
+** aggregate functions. This routine is responsible for computing
143735
+** (or recomputing) those aCol[] entries.
143736
+*/
143737
+static void analyzeAggFuncArgs(
143738
+ AggInfo *pAggInfo,
143739
+ NameContext *pNC
143740
+){
143741
+ int i;
143742
+ assert( pAggInfo!=0 );
143743
+ assert( pAggInfo->iFirstReg==0 );
143744
+ pNC->ncFlags |= NC_InAggFunc;
143745
+ for(i=0; i<pAggInfo->nFunc; i++){
143746
+ Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
143747
+ assert( ExprUseXList(pExpr) );
143748
+ sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
143749
+#ifndef SQLITE_OMIT_WINDOWFUNC
143750
+ assert( !IsWindowFunc(pExpr) );
143751
+ if( ExprHasProperty(pExpr, EP_WinFunc) ){
143752
+ sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
143753
+ }
143754
+#endif
143755
+ }
143756
+ pNC->ncFlags &= ~NC_InAggFunc;
143757
+}
143758
+
143759
+/*
143760
+** An index on expressions is being used in the inner loop of an
143761
+** aggregate query with a GROUP BY clause. This routine attempts
143762
+** to adjust the AggInfo object to take advantage of index and to
143763
+** perhaps use the index as a covering index.
143764
+**
143765
+*/
143766
+static void optimizeAggregateUseOfIndexedExpr(
143767
+ Parse *pParse, /* Parsing context */
143768
+ Select *pSelect, /* The SELECT statement being processed */
143769
+ AggInfo *pAggInfo, /* The aggregate info */
143770
+ NameContext *pNC /* Name context used to resolve agg-func args */
143771
+){
143772
+ assert( pAggInfo->iFirstReg==0 );
143773
+ pAggInfo->nColumn = pAggInfo->nAccumulator;
143774
+ if( ALWAYS(pAggInfo->nSortingColumn>0) ){
143775
+ if( pAggInfo->nColumn==0 ){
143776
+ pAggInfo->nSortingColumn = 0;
143777
+ }else{
143778
+ pAggInfo->nSortingColumn =
143779
+ pAggInfo->aCol[pAggInfo->nColumn-1].iSorterColumn+1;
143780
+ }
143781
+ }
143782
+ analyzeAggFuncArgs(pAggInfo, pNC);
143783
+#if TREETRACE_ENABLED
143784
+ if( sqlite3TreeTrace & 0x20 ){
143785
+ IndexedExpr *pIEpr;
143786
+ TREETRACE(0x20, pParse, pSelect,
143787
+ ("AggInfo (possibly) adjusted for Indexed Exprs\n"));
143788
+ sqlite3TreeViewSelect(0, pSelect, 0);
143789
+ for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
143790
+ printf("data-cursor=%d index={%d,%d}\n",
143791
+ pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
143792
+ sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
143793
+ }
143794
+ printAggInfo(pAggInfo);
143795
+ }
143796
+#else
143797
+ UNUSED_PARAMETER(pSelect);
143798
+ UNUSED_PARAMETER(pParse);
143799
+#endif
143800
+}
143801
+
143802
+/*
143803
+** Walker callback for aggregateConvertIndexedExprRefToColumn().
143804
+*/
143805
+static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){
143806
+ AggInfo *pAggInfo;
143807
+ struct AggInfo_col *pCol;
143808
+ UNUSED_PARAMETER(pWalker);
143809
+ if( pExpr->pAggInfo==0 ) return WRC_Continue;
143810
+ if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
143811
+ if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
143812
+ if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
143813
+ pAggInfo = pExpr->pAggInfo;
143814
+ assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
143815
+ pCol = &pAggInfo->aCol[pExpr->iAgg];
143816
+ pExpr->op = TK_AGG_COLUMN;
143817
+ pExpr->iTable = pCol->iTable;
143818
+ pExpr->iColumn = pCol->iColumn;
143819
+ return WRC_Prune;
143820
+}
143821
+
143822
+/*
143823
+** Convert every pAggInfo->aFunc[].pExpr such that any node within
143824
+** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
143825
+** opcode.
143826
+*/
143827
+static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){
143828
+ int i;
143829
+ Walker w;
143830
+ memset(&w, 0, sizeof(w));
143831
+ w.xExprCallback = aggregateIdxEprRefToColCallback;
143832
+ for(i=0; i<pAggInfo->nFunc; i++){
143833
+ sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
143834
+ }
143835
+}
143836
+
143837
+
143838
+/*
143839
+** Allocate a block of registers so that there is one register for each
143840
+** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
143841
+** register in this block is stored in pAggInfo->iFirstReg.
143842
+**
143843
+** This routine may only be called once for each AggInfo object. Prior
143844
+** to calling this routine:
143845
+**
143846
+** * The aCol[] and aFunc[] arrays may be modified
143847
+** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used
143848
+**
143849
+** After clling this routine:
143850
+**
143851
+** * The aCol[] and aFunc[] arrays are fixed
143852
+** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used
143853
+**
143854
+*/
143855
+static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){
143856
+ assert( pAggInfo!=0 );
143857
+ assert( pAggInfo->iFirstReg==0 );
143858
+ pAggInfo->iFirstReg = pParse->nMem + 1;
143859
+ pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
143860
+}
143472143861
143473143862
/*
143474143863
** Reset the aggregate accumulator.
143475143864
**
143476143865
** The aggregate accumulator is a set of memory cells that hold
@@ -143481,28 +143870,17 @@
143481143870
static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
143482143871
Vdbe *v = pParse->pVdbe;
143483143872
int i;
143484143873
struct AggInfo_func *pFunc;
143485143874
int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
143875
+ assert( pAggInfo->iFirstReg>0 );
143486143876
assert( pParse->db->pParse==pParse );
143487143877
assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
143488143878
if( nReg==0 ) return;
143489143879
if( pParse->nErr ) return;
143490
-#ifdef SQLITE_DEBUG
143491
- /* Verify that all AggInfo registers are within the range specified by
143492
- ** AggInfo.mnReg..AggInfo.mxReg */
143493
- assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
143494
- for(i=0; i<pAggInfo->nColumn; i++){
143495
- assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
143496
- && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
143497
- }
143498
- for(i=0; i<pAggInfo->nFunc; i++){
143499
- assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
143500
- && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
143501
- }
143502
-#endif
143503
- sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
143880
+ sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg,
143881
+ pAggInfo->iFirstReg+nReg-1);
143504143882
for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
143505143883
if( pFunc->iDistinct>=0 ){
143506143884
Expr *pE = pFunc->pFExpr;
143507143885
assert( ExprUseXList(pE) );
143508143886
if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
@@ -143530,19 +143908,20 @@
143530143908
struct AggInfo_func *pF;
143531143909
for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
143532143910
ExprList *pList;
143533143911
assert( ExprUseXList(pF->pFExpr) );
143534143912
pList = pF->pFExpr->x.pList;
143535
- sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
143913
+ sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i),
143914
+ pList ? pList->nExpr : 0);
143536143915
sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
143537143916
}
143538143917
}
143539143918
143540143919
143541143920
/*
143542
-** Update the accumulator memory cells for an aggregate based on
143543
-** the current cursor position.
143921
+** Generate code that will update the accumulator memory cells for an
143922
+** aggregate based on the current cursor position.
143544143923
**
143545143924
** If regAcc is non-zero and there are no min() or max() aggregates
143546143925
** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
143547143926
** registers if register regAcc contains 0. The caller will take care
143548143927
** of setting and clearing regAcc.
@@ -143558,10 +143937,12 @@
143558143937
int regHit = 0;
143559143938
int addrHitTest = 0;
143560143939
struct AggInfo_func *pF;
143561143940
struct AggInfo_col *pC;
143562143941
143942
+ assert( pAggInfo->iFirstReg>0 );
143943
+ if( pParse->nErr ) return;
143563143944
pAggInfo->directMode = 1;
143564143945
for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
143565143946
int nArg;
143566143947
int addrNext = 0;
143567143948
int regAgg;
@@ -143618,11 +143999,11 @@
143618143999
pColl = pParse->db->pDfltColl;
143619144000
}
143620144001
if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
143621144002
sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
143622144003
}
143623
- sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
144004
+ sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i));
143624144005
sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
143625144006
sqlite3VdbeChangeP5(v, (u8)nArg);
143626144007
sqlite3ReleaseTempRange(pParse, regAgg, nArg);
143627144008
if( addrNext ){
143628144009
sqlite3VdbeResolveLabel(v, addrNext);
@@ -143633,11 +144014,11 @@
143633144014
}
143634144015
if( regHit ){
143635144016
addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
143636144017
}
143637144018
for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
143638
- sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
144019
+ sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
143639144020
}
143640144021
143641144022
pAggInfo->directMode = 0;
143642144023
if( addrHitTest ){
143643144024
sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
@@ -143729,11 +144110,11 @@
143729144110
sWalker.xExprCallback = havingToWhereExprCb;
143730144111
sWalker.u.pSelect = p;
143731144112
sqlite3WalkExpr(&sWalker, p->pHaving);
143732144113
#if TREETRACE_ENABLED
143733144114
if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
143734
- SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
144115
+ TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
143735144116
sqlite3TreeViewSelect(0, p, 0);
143736144117
}
143737144118
#endif
143738144119
}
143739144120
@@ -143861,12 +144242,12 @@
143861144242
}
143862144243
p->pEList->a[0].pExpr = pExpr;
143863144244
p->selFlags &= ~SF_Aggregate;
143864144245
143865144246
#if TREETRACE_ENABLED
143866
- if( sqlite3TreeTrace & 0x400 ){
143867
- SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
144247
+ if( sqlite3TreeTrace & 0x200 ){
144248
+ TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
143868144249
sqlite3TreeViewSelect(0, p, 0);
143869144250
}
143870144251
#endif
143871144252
return 1;
143872144253
}
@@ -143938,12 +144319,12 @@
143938144319
return 1;
143939144320
}
143940144321
assert( db->mallocFailed==0 );
143941144322
if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
143942144323
#if TREETRACE_ENABLED
143943
- SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
143944
- if( sqlite3TreeTrace & 0x10100 ){
144324
+ TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
144325
+ if( sqlite3TreeTrace & 0x10000 ){
143945144326
if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
143946144327
sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
143947144328
__FILE__, __LINE__);
143948144329
}
143949144330
sqlite3ShowSelect(p);
@@ -143959,12 +144340,12 @@
143959144340
pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
143960144341
pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
143961144342
/* All of these destinations are also able to ignore the ORDER BY clause */
143962144343
if( p->pOrderBy ){
143963144344
#if TREETRACE_ENABLED
143964
- SELECTTRACE(1,pParse,p, ("dropping superfluous ORDER BY:\n"));
143965
- if( sqlite3TreeTrace & 0x100 ){
144345
+ TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
144346
+ if( sqlite3TreeTrace & 0x800 ){
143966144347
sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
143967144348
}
143968144349
#endif
143969144350
sqlite3ParserAddCleanup(pParse,
143970144351
(void(*)(sqlite3*,void*))sqlite3ExprListDelete,
@@ -143980,12 +144361,12 @@
143980144361
goto select_end;
143981144362
}
143982144363
assert( db->mallocFailed==0 );
143983144364
assert( p->pEList!=0 );
143984144365
#if TREETRACE_ENABLED
143985
- if( sqlite3TreeTrace & 0x104 ){
143986
- SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
144366
+ if( sqlite3TreeTrace & 0x10 ){
144367
+ TREETRACE(0x10,pParse,p, ("after name resolution:\n"));
143987144368
sqlite3TreeViewSelect(0, p, 0);
143988144369
}
143989144370
#endif
143990144371
143991144372
/* If the SF_UFSrcCheck flag is set, then this function is being called
@@ -144022,12 +144403,12 @@
144022144403
if( sqlite3WindowRewrite(pParse, p) ){
144023144404
assert( pParse->nErr );
144024144405
goto select_end;
144025144406
}
144026144407
#if TREETRACE_ENABLED
144027
- if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
144028
- SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
144408
+ if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
144409
+ TREETRACE(0x40,pParse,p, ("after window rewrite:\n"));
144029144410
sqlite3TreeViewSelect(0, p, 0);
144030144411
}
144031144412
#endif
144032144413
#endif /* SQLITE_OMIT_WINDOWFUNC */
144033144414
pTabList = p->pSrc;
@@ -144054,11 +144435,11 @@
144054144435
*/
144055144436
if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
144056144437
&& sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
144057144438
&& OptimizationEnabled(db, SQLITE_SimplifyJoin)
144058144439
){
144059
- SELECTTRACE(0x100,pParse,p,
144440
+ TREETRACE(0x1000,pParse,p,
144060144441
("LEFT-JOIN simplifies to JOIN on term %d\n",i));
144061144442
pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
144062144443
assert( pItem->iCursor>=0 );
144063144444
unsetJoinExpr(p->pWhere, pItem->iCursor,
144064144445
pTabList->a[0].fg.jointype & JT_LTORJ);
@@ -144110,11 +144491,11 @@
144110144491
&& pSub->pLimit==0 /* Condition (1) */
144111144492
&& (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
144112144493
&& (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
144113144494
&& OptimizationEnabled(db, SQLITE_OmitOrderBy)
144114144495
){
144115
- SELECTTRACE(0x100,pParse,p,
144496
+ TREETRACE(0x800,pParse,p,
144116144497
("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
144117144498
sqlite3ParserAddCleanup(pParse,
144118144499
(void(*)(sqlite3*,void*))sqlite3ExprListDelete,
144119144500
pSub->pOrderBy);
144120144501
pSub->pOrderBy = 0;
@@ -144165,12 +144546,12 @@
144165144546
** procedure.
144166144547
*/
144167144548
if( p->pPrior ){
144168144549
rc = multiSelect(pParse, p, pDest);
144169144550
#if TREETRACE_ENABLED
144170
- SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
144171
- if( (sqlite3TreeTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
144551
+ TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
144552
+ if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)==0 ){
144172144553
sqlite3TreeViewSelect(0, p, 0);
144173144554
}
144174144555
#endif
144175144556
if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
144176144557
return rc;
@@ -144186,17 +144567,17 @@
144186144567
&& p->pWhere->op==TK_AND
144187144568
&& OptimizationEnabled(db, SQLITE_PropagateConst)
144188144569
&& propagateConstants(pParse, p)
144189144570
){
144190144571
#if TREETRACE_ENABLED
144191
- if( sqlite3TreeTrace & 0x100 ){
144192
- SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
144572
+ if( sqlite3TreeTrace & 0x2000 ){
144573
+ TREETRACE(0x2000,pParse,p,("After constant propagation:\n"));
144193144574
sqlite3TreeViewSelect(0, p, 0);
144194144575
}
144195144576
#endif
144196144577
}else{
144197
- SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
144578
+ TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n"));
144198144579
}
144199144580
144200144581
#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
144201144582
if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
144202144583
&& countOfViewOptimization(pParse, p)
@@ -144265,19 +144646,19 @@
144265144646
&& (pItem->fg.isCte==0
144266144647
|| (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
144267144648
&& pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
144268144649
){
144269144650
#if TREETRACE_ENABLED
144270
- if( sqlite3TreeTrace & 0x100 ){
144271
- SELECTTRACE(0x100,pParse,p,
144651
+ if( sqlite3TreeTrace & 0x4000 ){
144652
+ TREETRACE(0x4000,pParse,p,
144272144653
("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
144273144654
sqlite3TreeViewSelect(0, p, 0);
144274144655
}
144275144656
#endif
144276144657
assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
144277144658
}else{
144278
- SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
144659
+ TREETRACE(0x4000,pParse,p,("Push-down not possible\n"));
144279144660
}
144280144661
144281144662
zSavedAuthContext = pParse->zAuthContext;
144282144663
pParse->zAuthContext = pItem->zName;
144283144664
@@ -144388,12 +144769,12 @@
144388144769
pGroupBy = p->pGroupBy;
144389144770
pHaving = p->pHaving;
144390144771
sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
144391144772
144392144773
#if TREETRACE_ENABLED
144393
- if( sqlite3TreeTrace & 0x400 ){
144394
- SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
144774
+ if( sqlite3TreeTrace & 0x8000 ){
144775
+ TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
144395144776
sqlite3TreeViewSelect(0, p, 0);
144396144777
}
144397144778
#endif
144398144779
144399144780
/* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
@@ -144425,12 +144806,12 @@
144425144806
** original setting of the SF_Distinct flag, not the current setting */
144426144807
assert( sDistinct.isTnct );
144427144808
sDistinct.isTnct = 2;
144428144809
144429144810
#if TREETRACE_ENABLED
144430
- if( sqlite3TreeTrace & 0x400 ){
144431
- SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
144811
+ if( sqlite3TreeTrace & 0x20000 ){
144812
+ TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
144432144813
sqlite3TreeViewSelect(0, p, 0);
144433144814
}
144434144815
#endif
144435144816
}
144436144817
@@ -144512,11 +144893,11 @@
144512144893
#endif
144513144894
assert( WHERE_USE_LIMIT==SF_FixedLimit );
144514144895
144515144896
144516144897
/* Begin the database scan. */
144517
- SELECTTRACE(1,pParse,p,("WhereBegin\n"));
144898
+ TREETRACE(0x2,pParse,p,("WhereBegin\n"));
144518144899
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
144519144900
p->pEList, p, wctrlFlags, p->nSelectRow);
144520144901
if( pWInfo==0 ) goto select_end;
144521144902
if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
144522144903
p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
@@ -144529,11 +144910,11 @@
144529144910
sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
144530144911
if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
144531144912
sSort.pOrderBy = 0;
144532144913
}
144533144914
}
144534
- SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
144915
+ TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
144535144916
144536144917
/* If sorting index that was created by a prior OP_OpenEphemeral
144537144918
** instruction ended up not being needed, then change the OP_OpenEphemeral
144538144919
** into an OP_Noop.
144539144920
*/
@@ -144568,11 +144949,11 @@
144568144949
sqlite3WhereContinueLabel(pWInfo),
144569144950
sqlite3WhereBreakLabel(pWInfo));
144570144951
144571144952
/* End the database scan loop.
144572144953
*/
144573
- SELECTTRACE(1,pParse,p,("WhereEnd\n"));
144954
+ TREETRACE(0x2,pParse,p,("WhereEnd\n"));
144574144955
sqlite3WhereEnd(pWInfo);
144575144956
}
144576144957
}else{
144577144958
/* This case when there exist aggregate functions or a GROUP BY clause
144578144959
** or both */
@@ -144654,11 +145035,10 @@
144654145035
memset(&sNC, 0, sizeof(sNC));
144655145036
sNC.pParse = pParse;
144656145037
sNC.pSrcList = pTabList;
144657145038
sNC.uNC.pAggInfo = pAggInfo;
144658145039
VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
144659
- pAggInfo->mnReg = pParse->nMem+1;
144660145040
pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
144661145041
pAggInfo->pGroupBy = pGroupBy;
144662145042
sqlite3ExprAnalyzeAggList(&sNC, pEList);
144663145043
sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
144664145044
if( pHaving ){
@@ -144675,49 +145055,21 @@
144675145055
if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
144676145056
minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
144677145057
}else{
144678145058
minMaxFlag = WHERE_ORDERBY_NORMAL;
144679145059
}
144680
- for(i=0; i<pAggInfo->nFunc; i++){
144681
- Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
144682
- assert( ExprUseXList(pExpr) );
144683
- sNC.ncFlags |= NC_InAggFunc;
144684
- sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
144685
-#ifndef SQLITE_OMIT_WINDOWFUNC
144686
- assert( !IsWindowFunc(pExpr) );
144687
- if( ExprHasProperty(pExpr, EP_WinFunc) ){
144688
- sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
144689
- }
144690
-#endif
144691
- sNC.ncFlags &= ~NC_InAggFunc;
144692
- }
144693
- pAggInfo->mxReg = pParse->nMem;
145060
+ analyzeAggFuncArgs(pAggInfo, &sNC);
144694145061
if( db->mallocFailed ) goto select_end;
144695145062
#if TREETRACE_ENABLED
144696
- if( sqlite3TreeTrace & 0x400 ){
144697
- int ii;
144698
- SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
145063
+ if( sqlite3TreeTrace & 0x20 ){
145064
+ TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
144699145065
sqlite3TreeViewSelect(0, p, 0);
144700145066
if( minMaxFlag ){
144701145067
sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
144702145068
sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
144703145069
}
144704
- for(ii=0; ii<pAggInfo->nColumn; ii++){
144705
- struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
144706
- sqlite3DebugPrintf(
144707
- "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
144708
- " iSorterColumn=%d\n",
144709
- ii, pCol->pTab ? pCol->pTab->zName : "NULL",
144710
- pCol->iTable, pCol->iColumn, pCol->iMem,
144711
- pCol->iSorterColumn);
144712
- sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
144713
- }
144714
- for(ii=0; ii<pAggInfo->nFunc; ii++){
144715
- sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
144716
- ii, pAggInfo->aFunc[ii].iMem);
144717
- sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
144718
- }
145070
+ printAggInfo(pAggInfo);
144719145071
}
144720145072
#endif
144721145073
144722145074
144723145075
/* Processing for aggregates with GROUP BY is very different and
@@ -144782,21 +145134,25 @@
144782145134
** This might involve two separate loops with an OP_Sort in between, or
144783145135
** it might be a single loop that uses an index to extract information
144784145136
** in the right order to begin with.
144785145137
*/
144786145138
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
144787
- SELECTTRACE(1,pParse,p,("WhereBegin\n"));
145139
+ TREETRACE(0x2,pParse,p,("WhereBegin\n"));
144788145140
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
144789145141
p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
144790145142
| (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
144791145143
);
144792145144
if( pWInfo==0 ){
144793145145
sqlite3ExprListDelete(db, pDistinct);
144794145146
goto select_end;
144795145147
}
145148
+ if( pParse->pIdxEpr ){
145149
+ optimizeAggregateUseOfIndexedExpr(pParse, p, pAggInfo, &sNC);
145150
+ }
145151
+ assignAggregateRegisters(pParse, pAggInfo);
144796145152
eDist = sqlite3WhereIsDistinct(pWInfo);
144797
- SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
145153
+ TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
144798145154
if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
144799145155
/* The optimizer is able to deliver rows in group by order so
144800145156
** we do not have to sort. The OP_OpenEphemeral table will be
144801145157
** cancelled later because we still need to use the pKeyInfo
144802145158
*/
@@ -144841,19 +145197,36 @@
144841145197
regRecord = sqlite3GetTempReg(pParse);
144842145198
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
144843145199
sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
144844145200
sqlite3ReleaseTempReg(pParse, regRecord);
144845145201
sqlite3ReleaseTempRange(pParse, regBase, nCol);
144846
- SELECTTRACE(1,pParse,p,("WhereEnd\n"));
145202
+ TREETRACE(0x2,pParse,p,("WhereEnd\n"));
144847145203
sqlite3WhereEnd(pWInfo);
144848145204
pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
144849145205
sortOut = sqlite3GetTempReg(pParse);
144850145206
sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
144851145207
sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
144852145208
VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
144853145209
pAggInfo->useSortingIdx = 1;
144854145210
}
145211
+
145212
+ /* If there entries in pAgggInfo->aFunc[] that contain subexpressions
145213
+ ** that are indexed (and that were previously identified and tagged
145214
+ ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
145215
+ ** must now be converted into a TK_AGG_COLUMN node so that the value
145216
+ ** is correctly pulled from the index rather than being recomputed. */
145217
+ if( pParse->pIdxEpr ){
145218
+ aggregateConvertIndexedExprRefToColumn(pAggInfo);
145219
+#if TREETRACE_ENABLED
145220
+ if( sqlite3TreeTrace & 0x20 ){
145221
+ TREETRACE(0x20, pParse, p,
145222
+ ("AggInfo function expressions converted to reference index\n"));
145223
+ sqlite3TreeViewSelect(0, p, 0);
145224
+ printAggInfo(pAggInfo);
145225
+ }
145226
+#endif
145227
+ }
144855145228
144856145229
/* If the index or temporary table used by the GROUP BY sort
144857145230
** will naturally deliver rows in the order required by the ORDER BY
144858145231
** clause, cancel the ephemeral table open coded earlier.
144859145232
**
@@ -144919,11 +145292,11 @@
144919145292
*/
144920145293
if( groupBySort ){
144921145294
sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
144922145295
VdbeCoverage(v);
144923145296
}else{
144924
- SELECTTRACE(1,pParse,p,("WhereEnd\n"));
145297
+ TREETRACE(0x2,pParse,p,("WhereEnd\n"));
144925145298
sqlite3WhereEnd(pWInfo);
144926145299
sqlite3VdbeChangeToNoop(v, addrSortingIdx);
144927145300
}
144928145301
sqlite3ExprListDelete(db, pDistinct);
144929145302
@@ -145029,11 +145402,12 @@
145029145402
/* Open a read-only cursor, execute the OP_Count, close the cursor. */
145030145403
sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, (int)iRoot, iDb, 1);
145031145404
if( pKeyInfo ){
145032145405
sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
145033145406
}
145034
- sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
145407
+ assignAggregateRegisters(pParse, pAggInfo);
145408
+ sqlite3VdbeAddOp2(v, OP_Count, iCsr, AggInfoFuncReg(pAggInfo,0));
145035145409
sqlite3VdbeAddOp1(v, OP_Close, iCsr);
145036145410
explainSimpleCount(pParse, pTab, pBest);
145037145411
}else{
145038145412
int regAcc = 0; /* "populate accumulators" flag */
145039145413
ExprList *pDistinct = 0;
@@ -145065,10 +145439,11 @@
145065145439
}else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
145066145440
assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
145067145441
pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
145068145442
distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
145069145443
}
145444
+ assignAggregateRegisters(pParse, pAggInfo);
145070145445
145071145446
/* This case runs if the aggregate has no GROUP BY clause. The
145072145447
** processing is much simpler since there is only a single row
145073145448
** of output.
145074145449
*/
@@ -145081,17 +145456,17 @@
145081145456
** be an appropriate ORDER BY expression for the optimization.
145082145457
*/
145083145458
assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
145084145459
assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
145085145460
145086
- SELECTTRACE(1,pParse,p,("WhereBegin\n"));
145461
+ TREETRACE(0x2,pParse,p,("WhereBegin\n"));
145087145462
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
145088145463
pDistinct, p, minMaxFlag|distFlag, 0);
145089145464
if( pWInfo==0 ){
145090145465
goto select_end;
145091145466
}
145092
- SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
145467
+ TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
145093145468
eDist = sqlite3WhereIsDistinct(pWInfo);
145094145469
updateAccumulator(pParse, regAcc, pAggInfo, eDist);
145095145470
if( eDist!=WHERE_DISTINCT_NOOP ){
145096145471
struct AggInfo_func *pF = pAggInfo->aFunc;
145097145472
if( pF ){
@@ -145101,11 +145476,11 @@
145101145476
145102145477
if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
145103145478
if( minMaxFlag ){
145104145479
sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
145105145480
}
145106
- SELECTTRACE(1,pParse,p,("WhereEnd\n"));
145481
+ TREETRACE(0x2,pParse,p,("WhereEnd\n"));
145107145482
sqlite3WhereEnd(pWInfo);
145108145483
finalizeAggFunctions(pParse, pAggInfo);
145109145484
}
145110145485
145111145486
sSort.pOrderBy = 0;
@@ -145148,11 +145523,11 @@
145148145523
sqlite3ExprListDelete(db, pMinMaxOrderBy);
145149145524
#ifdef SQLITE_DEBUG
145150145525
if( pAggInfo && !db->mallocFailed ){
145151145526
for(i=0; i<pAggInfo->nColumn; i++){
145152145527
Expr *pExpr = pAggInfo->aCol[i].pCExpr;
145153
- assert( pExpr!=0 );
145528
+ if( pExpr==0 ) continue;
145154145529
assert( pExpr->pAggInfo==pAggInfo );
145155145530
assert( pExpr->iAgg==i );
145156145531
}
145157145532
for(i=0; i<pAggInfo->nFunc; i++){
145158145533
Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
@@ -145162,12 +145537,12 @@
145162145537
}
145163145538
}
145164145539
#endif
145165145540
145166145541
#if TREETRACE_ENABLED
145167
- SELECTTRACE(0x1,pParse,p,("end processing\n"));
145168
- if( (sqlite3TreeTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
145542
+ TREETRACE(0x1,pParse,p,("end processing\n"));
145543
+ if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
145169145544
sqlite3TreeViewSelect(0, p, 0);
145170145545
}
145171145546
#endif
145172145547
ExplainQueryPlanPop(pParse);
145173145548
return rc;
@@ -145437,11 +145812,11 @@
145437145812
while( p ){
145438145813
Trigger *pTrig = (Trigger *)sqliteHashData(p);
145439145814
if( pTrig->pTabSchema==pTab->pSchema
145440145815
&& pTrig->table
145441145816
&& 0==sqlite3StrICmp(pTrig->table, pTab->zName)
145442
- && pTrig->pTabSchema!=pTmpSchema
145817
+ && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
145443145818
){
145444145819
pTrig->pNext = pList;
145445145820
pList = pTrig;
145446145821
}else if( pTrig->op==TK_RETURNING ){
145447145822
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -150944,10 +151319,11 @@
150944151319
#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
150945151320
#define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
150946151321
#define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
150947151322
#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
150948151323
#define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
151324
+#define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
150949151325
150950151326
#endif /* !defined(SQLITE_WHEREINT_H) */
150951151327
150952151328
/************** End of whereInt.h ********************************************/
150953151329
/************** Continuing where we left off in wherecode.c ******************/
@@ -151289,11 +151665,11 @@
151289151665
pTerm->wtFlags |= TERM_LIKECOND;
151290151666
}else{
151291151667
pTerm->wtFlags |= TERM_CODED;
151292151668
}
151293151669
#ifdef WHERETRACE_ENABLED
151294
- if( sqlite3WhereTrace & 0x20000 ){
151670
+ if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
151295151671
sqlite3DebugPrintf("DISABLE-");
151296151672
sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
151297151673
}
151298151674
#endif
151299151675
if( pTerm->iParent<0 ) break;
@@ -152276,17 +152652,19 @@
152276152652
pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
152277152653
iCur = pTabItem->iCursor;
152278152654
pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
152279152655
bRev = (pWInfo->revMask>>iLevel)&1;
152280152656
VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
152281
-#if WHERETRACE_ENABLED /* 0x20800 */
152282
- if( sqlite3WhereTrace & 0x800 ){
152657
+#if WHERETRACE_ENABLED /* 0x4001 */
152658
+ if( sqlite3WhereTrace & 0x1 ){
152283152659
sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
152284152660
iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
152285
- sqlite3WhereLoopPrint(pLoop, pWC);
152661
+ if( sqlite3WhereTrace & 0x1000 ){
152662
+ sqlite3WhereLoopPrint(pLoop, pWC);
152663
+ }
152286152664
}
152287
- if( sqlite3WhereTrace & 0x20000 ){
152665
+ if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
152288152666
if( iLevel==0 ){
152289152667
sqlite3DebugPrintf("WHERE clause being coded:\n");
152290152668
sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
152291152669
}
152292152670
sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
@@ -153206,11 +153584,11 @@
153206153584
pAndExpr->pLeft = pOrExpr;
153207153585
pOrExpr = pAndExpr;
153208153586
}
153209153587
/* Loop through table entries that match term pOrTerm. */
153210153588
ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
153211
- WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
153589
+ WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
153212153590
pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
153213153591
WHERE_OR_SUBCLAUSE, iCovCur);
153214153592
assert( pSubWInfo || pParse->nErr );
153215153593
if( pSubWInfo ){
153216153594
WhereLoop *pSubLoop;
@@ -153443,16 +153821,16 @@
153443153821
VdbeCoverageIf(v, (x&1)==1);
153444153822
VdbeCoverageIf(v, (x&1)==0);
153445153823
}
153446153824
#endif
153447153825
}
153448
-#ifdef WHERETRACE_ENABLED /* 0xffff */
153826
+#ifdef WHERETRACE_ENABLED /* 0xffffffff */
153449153827
if( sqlite3WhereTrace ){
153450153828
VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
153451153829
pWC->nTerm-j, pTerm, iLoop));
153452153830
}
153453
- if( sqlite3WhereTrace & 0x800 ){
153831
+ if( sqlite3WhereTrace & 0x4000 ){
153454153832
sqlite3DebugPrintf("Coding auxiliary constraint:\n");
153455153833
sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
153456153834
}
153457153835
#endif
153458153836
sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
@@ -153477,12 +153855,12 @@
153477153855
if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
153478153856
if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
153479153857
if( pTerm->leftCursor!=iCur ) continue;
153480153858
if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
153481153859
pE = pTerm->pExpr;
153482
-#ifdef WHERETRACE_ENABLED /* 0x800 */
153483
- if( sqlite3WhereTrace & 0x800 ){
153860
+#ifdef WHERETRACE_ENABLED /* 0x4001 */
153861
+ if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
153484153862
sqlite3DebugPrintf("Coding transitive constraint:\n");
153485153863
sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
153486153864
}
153487153865
#endif
153488153866
assert( !ExprHasProperty(pE, EP_OuterON) );
@@ -153593,17 +153971,17 @@
153593153971
sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
153594153972
pTerm->wtFlags |= TERM_CODED;
153595153973
}
153596153974
}
153597153975
153598
-#if WHERETRACE_ENABLED /* 0x20800 */
153599
- if( sqlite3WhereTrace & 0x20000 ){
153976
+#if WHERETRACE_ENABLED /* 0x4001 */
153977
+ if( sqlite3WhereTrace & 0x4000 ){
153600153978
sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
153601153979
iLevel);
153602153980
sqlite3WhereClausePrint(pWC);
153603153981
}
153604
- if( sqlite3WhereTrace & 0x800 ){
153982
+ if( sqlite3WhereTrace & 0x1 ){
153605153983
sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
153606153984
iLevel, (u64)pLevel->notReady);
153607153985
}
153608153986
#endif
153609153987
return pLevel->notReady;
@@ -156259,11 +156637,11 @@
156259156637
** are no-ops.
156260156638
*/
156261156639
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
156262156640
static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
156263156641
int i;
156264
- if( !sqlite3WhereTrace ) return;
156642
+ if( (sqlite3WhereTrace & 0x10)==0 ) return;
156265156643
for(i=0; i<p->nConstraint; i++){
156266156644
sqlite3DebugPrintf(
156267156645
" constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
156268156646
i,
156269156647
p->aConstraint[i].iColumn,
@@ -156279,11 +156657,11 @@
156279156657
p->aOrderBy[i].desc);
156280156658
}
156281156659
}
156282156660
static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
156283156661
int i;
156284
- if( !sqlite3WhereTrace ) return;
156662
+ if( (sqlite3WhereTrace & 0x10)==0 ) return;
156285156663
for(i=0; i<p->nConstraint; i++){
156286156664
sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
156287156665
i,
156288156666
p->aConstraintUsage[i].argvIndex,
156289156667
p->aConstraintUsage[i].omit);
@@ -157296,11 +157674,11 @@
157296157674
** using the method described in the header comment for this function. */
157297157675
if( nDiff!=1 || pUpper==0 || pLower==0 ){
157298157676
int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
157299157677
pLoop->nOut -= nAdjust;
157300157678
*pbDone = 1;
157301
- WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
157679
+ WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
157302157680
nLower, nUpper, nAdjust*-1, pLoop->nOut));
157303157681
}
157304157682
157305157683
}else{
157306157684
assert( *pbDone==0 );
@@ -157474,11 +157852,11 @@
157474157852
nNew = 10; assert( 10==sqlite3LogEst(2) );
157475157853
}
157476157854
if( nNew<nOut ){
157477157855
nOut = nNew;
157478157856
}
157479
- WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
157857
+ WHERETRACE(0x20, ("STAT4 range scan: %u..%u est=%d\n",
157480157858
(u32)iLower, (u32)iUpper, nOut));
157481157859
}
157482157860
}else{
157483157861
int bDone = 0;
157484157862
rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
@@ -157507,11 +157885,11 @@
157507157885
nOut -= (pLower!=0) + (pUpper!=0);
157508157886
if( nNew<10 ) nNew = 10;
157509157887
if( nNew<nOut ) nOut = nNew;
157510157888
#if defined(WHERETRACE_ENABLED)
157511157889
if( pLoop->nOut>nOut ){
157512
- WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
157890
+ WHERETRACE(0x20,("Range scan lowers nOut from %d to %d\n",
157513157891
pLoop->nOut, nOut));
157514157892
}
157515157893
#endif
157516157894
pLoop->nOut = (LogEst)nOut;
157517157895
return rc;
@@ -157572,11 +157950,11 @@
157572157950
if( rc!=SQLITE_OK ) return rc;
157573157951
if( bOk==0 ) return SQLITE_NOTFOUND;
157574157952
pBuilder->nRecValid = nEq;
157575157953
157576157954
whereKeyStats(pParse, p, pRec, 0, a);
157577
- WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
157955
+ WHERETRACE(0x20,("equality scan regions %s(%d): %d\n",
157578157956
p->zName, nEq-1, (int)a[1]));
157579157957
*pnRow = a[1];
157580157958
157581157959
return rc;
157582157960
}
@@ -157622,11 +158000,11 @@
157622158000
}
157623158001
157624158002
if( rc==SQLITE_OK ){
157625158003
if( nRowEst > nRow0 ) nRowEst = nRow0;
157626158004
*pnRow = nRowEst;
157627
- WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
158005
+ WHERETRACE(0x20,("IN row estimate: est=%d\n", nRowEst));
157628158006
}
157629158007
assert( pBuilder->nRecValid==nRecValid );
157630158008
return rc;
157631158009
}
157632158010
#endif /* SQLITE_ENABLE_STAT4 */
@@ -157731,11 +158109,11 @@
157731158109
sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
157732158110
}else{
157733158111
sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
157734158112
}
157735158113
sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
157736
- if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
158114
+ if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
157737158115
int i;
157738158116
for(i=0; i<p->nLTerm; i++){
157739158117
sqlite3WhereTermPrint(p->aLTerm[i], i);
157740158118
}
157741158119
}
@@ -158609,11 +158987,11 @@
158609158987
** to be true for half or more of the rows in the table.
158610158988
** See tag-202002240-1 */
158611158989
&& pNew->nOut+10 > pProbe->aiRowLogEst[0]
158612158990
){
158613158991
#if WHERETRACE_ENABLED /* 0x01 */
158614
- if( sqlite3WhereTrace & 0x01 ){
158992
+ if( sqlite3WhereTrace & 0x20 ){
158615158993
sqlite3DebugPrintf(
158616158994
"STAT4 determines term has low selectivity:\n");
158617158995
sqlite3WhereTermPrint(pTerm, 999);
158618158996
}
158619158997
#endif
@@ -158646,13 +159024,21 @@
158646159024
/* Set rCostIdx to the cost of visiting selected rows in index. Add
158647159025
** it to pNew->rRun, which is currently set to the cost of the index
158648159026
** seek only. Then, if this is a non-covering index, add the cost of
158649159027
** visiting the rows in the main table. */
158650159028
assert( pSrc->pTab->szTabRow>0 );
158651
- rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
159029
+ if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
159030
+ /* The pProbe->szIdxRow is low for an IPK table since the interior
159031
+ ** pages are small. Thuse szIdxRow gives a good estimate of seek cost.
159032
+ ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
159033
+ ** under-estimate the scanning cost. */
159034
+ rCostIdx = pNew->nOut + 16;
159035
+ }else{
159036
+ rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
159037
+ }
158652159038
pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
158653
- if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
159039
+ if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
158654159040
pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
158655159041
}
158656159042
ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
158657159043
158658159044
nOutUnadjusted = pNew->nOut;
@@ -158800,21 +159186,44 @@
158800159186
return 1;
158801159187
}
158802159188
}
158803159189
return 0;
158804159190
}
159191
+
159192
+/*
159193
+** pIdx is an index containing expressions. Check it see if any of the
159194
+** expressions in the index match the pExpr expression.
159195
+*/
159196
+static int exprIsCoveredByIndex(
159197
+ const Expr *pExpr,
159198
+ const Index *pIdx,
159199
+ int iTabCur
159200
+){
159201
+ int i;
159202
+ for(i=0; i<pIdx->nColumn; i++){
159203
+ if( pIdx->aiColumn[i]==XN_EXPR
159204
+ && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
159205
+ ){
159206
+ return 1;
159207
+ }
159208
+ }
159209
+ return 0;
159210
+}
158805159211
158806159212
/*
158807159213
** Structure passed to the whereIsCoveringIndex Walker callback.
158808159214
*/
159215
+typedef struct CoveringIndexCheck CoveringIndexCheck;
158809159216
struct CoveringIndexCheck {
158810159217
Index *pIdx; /* The index */
158811159218
int iTabCur; /* Cursor number for the corresponding table */
159219
+ u8 bExpr; /* Uses an indexed expression */
159220
+ u8 bUnidx; /* Uses an unindexed column not within an indexed expr */
158812159221
};
158813159222
158814159223
/*
158815
-** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
159224
+** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
158816159225
**
158817159226
** If the Expr node references the table with cursor pCk->iTabCur, then
158818159227
** make sure that column is covered by the index pCk->pIdx. We know that
158819159228
** all columns less than 63 (really BMS-1) are covered, so we don't need
158820159229
** to check them. But we do need to check any column at 63 or greater.
@@ -158822,75 +159231,107 @@
158822159231
** If the index does not cover the column, then set pWalk->eCode to
158823159232
** non-zero and return WRC_Abort to stop the search.
158824159233
**
158825159234
** If this node does not disprove that the index can be a covering index,
158826159235
** then just return WRC_Continue, to continue the search.
159236
+**
159237
+** If pCk->pIdx contains indexed expressions and one of those expressions
159238
+** matches pExpr, then prune the search.
158827159239
*/
158828159240
static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
158829
- int i; /* Loop counter */
158830
- const Index *pIdx; /* The index of interest */
158831
- const i16 *aiColumn; /* Columns contained in the index */
158832
- u16 nColumn; /* Number of columns in the index */
158833
- if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
158834
- if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
158835
- if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
158836
- pIdx = pWalk->u.pCovIdxCk->pIdx;
158837
- aiColumn = pIdx->aiColumn;
158838
- nColumn = pIdx->nColumn;
158839
- for(i=0; i<nColumn; i++){
158840
- if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
158841
- }
158842
- pWalk->eCode = 1;
158843
- return WRC_Abort;
159241
+ int i; /* Loop counter */
159242
+ const Index *pIdx; /* The index of interest */
159243
+ const i16 *aiColumn; /* Columns contained in the index */
159244
+ u16 nColumn; /* Number of columns in the index */
159245
+ CoveringIndexCheck *pCk; /* Info about this search */
159246
+
159247
+ pCk = pWalk->u.pCovIdxCk;
159248
+ pIdx = pCk->pIdx;
159249
+ if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
159250
+ /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
159251
+ if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
159252
+ pIdx = pWalk->u.pCovIdxCk->pIdx;
159253
+ aiColumn = pIdx->aiColumn;
159254
+ nColumn = pIdx->nColumn;
159255
+ for(i=0; i<nColumn; i++){
159256
+ if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
159257
+ }
159258
+ pCk->bUnidx = 1;
159259
+ return WRC_Abort;
159260
+ }else if( pIdx->bHasExpr
159261
+ && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
159262
+ pCk->bExpr = 1;
159263
+ return WRC_Prune;
159264
+ }
159265
+ return WRC_Continue;
158844159266
}
158845159267
158846159268
158847159269
/*
158848159270
** pIdx is an index that covers all of the low-number columns used by
158849
-** pWInfo->pSelect (columns from 0 through 62). But there are columns
158850
-** in pWInfo->pSelect beyond 62. This routine tries to answer the question
158851
-** of whether pIdx covers *all* columns in the query.
158852
-**
158853
-** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
158854
-** not a covering index or if we are unable to determine if pIdx is a
158855
-** covering index.
158856
-**
158857
-** This routine is an optimization. It is always safe to return non-zero.
158858
-** But returning zero when non-zero should have been returned can lead to
158859
-** incorrect bytecode and assertion faults.
159271
+** pWInfo->pSelect (columns from 0 through 62) or an index that has
159272
+** expressions terms. Hence, we cannot determine whether or not it is
159273
+** a covering index by using the colUsed bitmasks. We have to do a search
159274
+** to see if the index is covering. This routine does that search.
159275
+**
159276
+** The return value is one of these:
159277
+**
159278
+** 0 The index is definitely not a covering index
159279
+**
159280
+** WHERE_IDX_ONLY The index is definitely a covering index
159281
+**
159282
+** WHERE_EXPRIDX The index is likely a covering index, but it is
159283
+** difficult to determine precisely because of the
159284
+** expressions that are indexed. Score it as a
159285
+** covering index, but still keep the main table open
159286
+** just in case we need it.
159287
+**
159288
+** This routine is an optimization. It is always safe to return zero.
159289
+** But returning one of the other two values when zero should have been
159290
+** returned can lead to incorrect bytecode and assertion faults.
158860159291
*/
158861159292
static SQLITE_NOINLINE u32 whereIsCoveringIndex(
158862159293
WhereInfo *pWInfo, /* The WHERE clause context */
158863159294
Index *pIdx, /* Index that is being tested */
158864159295
int iTabCur /* Cursor for the table being indexed */
158865159296
){
158866
- int i;
159297
+ int i, rc;
158867159298
struct CoveringIndexCheck ck;
158868159299
Walker w;
158869159300
if( pWInfo->pSelect==0 ){
158870159301
/* We don't have access to the full query, so we cannot check to see
158871159302
** if pIdx is covering. Assume it is not. */
158872
- return 1;
158873
- }
158874
- for(i=0; i<pIdx->nColumn; i++){
158875
- if( pIdx->aiColumn[i]>=BMS-1 ) break;
158876
- }
158877
- if( i>=pIdx->nColumn ){
158878
- /* pIdx does not index any columns greater than 62, but we know from
158879
- ** colMask that columns greater than 62 are used, so this is not a
158880
- ** covering index */
158881
- return 1;
159303
+ return 0;
159304
+ }
159305
+ if( pIdx->bHasExpr==0 ){
159306
+ for(i=0; i<pIdx->nColumn; i++){
159307
+ if( pIdx->aiColumn[i]>=BMS-1 ) break;
159308
+ }
159309
+ if( i>=pIdx->nColumn ){
159310
+ /* pIdx does not index any columns greater than 62, but we know from
159311
+ ** colMask that columns greater than 62 are used, so this is not a
159312
+ ** covering index */
159313
+ return 0;
159314
+ }
158882159315
}
158883159316
ck.pIdx = pIdx;
158884159317
ck.iTabCur = iTabCur;
159318
+ ck.bExpr = 0;
159319
+ ck.bUnidx = 0;
158885159320
memset(&w, 0, sizeof(w));
158886159321
w.xExprCallback = whereIsCoveringIndexWalkCallback;
158887159322
w.xSelectCallback = sqlite3SelectWalkNoop;
158888159323
w.u.pCovIdxCk = &ck;
158889
- w.eCode = 0;
158890159324
sqlite3WalkSelect(&w, pWInfo->pSelect);
158891
- return w.eCode;
159325
+ if( ck.bUnidx ){
159326
+ rc = 0;
159327
+ }else if( ck.bExpr ){
159328
+ rc = WHERE_EXPRIDX;
159329
+ }else{
159330
+ rc = WHERE_IDX_ONLY;
159331
+ }
159332
+ return rc;
158892159333
}
158893159334
158894159335
/*
158895159336
** Add all WhereLoop objects for a single table of the join where the table
158896159337
** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
@@ -158971,11 +159412,11 @@
158971159412
sPk.nColumn = 1;
158972159413
sPk.aiColumn = &aiColumnPk;
158973159414
sPk.aiRowLogEst = aiRowEstPk;
158974159415
sPk.onError = OE_Replace;
158975159416
sPk.pTable = pTab;
158976
- sPk.szIdxRow = pTab->szTabRow;
159417
+ sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
158977159418
sPk.idxType = SQLITE_IDXTYPE_IPK;
158978159419
aiRowEstPk[0] = pTab->nRowLogEst;
158979159420
aiRowEstPk[1] = 0;
158980159421
pFirst = pSrc->pTab->pIndex;
158981159422
if( pSrc->fg.notIndexed==0 ){
@@ -159102,18 +159543,42 @@
159102159543
pNew->nOut = rSize;
159103159544
if( rc ) break;
159104159545
}else{
159105159546
Bitmask m;
159106159547
if( pProbe->isCovering ){
159107
- pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159108159548
m = 0;
159549
+ pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159109159550
}else{
159110159551
m = pSrc->colUsed & pProbe->colNotIdxed;
159111
- if( m==TOPBIT ){
159112
- m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159552
+ pNew->wsFlags = WHERE_INDEXED;
159553
+ if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
159554
+ u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159555
+ if( isCov==0 ){
159556
+ WHERETRACE(0x200,
159557
+ ("-> %s is not a covering index"
159558
+ " according to whereIsCoveringIndex()\n", pProbe->zName));
159559
+ assert( m!=0 );
159560
+ }else{
159561
+ m = 0;
159562
+ pNew->wsFlags |= isCov;
159563
+ if( isCov & WHERE_IDX_ONLY ){
159564
+ WHERETRACE(0x200,
159565
+ ("-> %s is a covering expression index"
159566
+ " according to whereIsCoveringIndex()\n", pProbe->zName));
159567
+ }else{
159568
+ assert( isCov==WHERE_EXPRIDX );
159569
+ WHERETRACE(0x200,
159570
+ ("-> %s might be a covering expression index"
159571
+ " according to whereIsCoveringIndex()\n", pProbe->zName));
159572
+ }
159573
+ }
159574
+ }else if( m==0 ){
159575
+ WHERETRACE(0x200,
159576
+ ("-> %s a covering index according to bitmasks\n",
159577
+ pProbe->zName, m==0 ? "is" : "is not"));
159578
+ pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159113159579
}
159114
- pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
159115159580
}
159116159581
159117159582
/* Full scan via index */
159118159583
if( b
159119159584
|| !HasRowid(pTab)
@@ -159282,11 +159747,11 @@
159282159747
if( rc==SQLITE_CONSTRAINT ){
159283159748
/* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
159284159749
** that the particular combination of parameters provided is unusable.
159285159750
** Make no entries in the loop table.
159286159751
*/
159287
- WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
159752
+ WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
159288159753
return SQLITE_OK;
159289159754
}
159290159755
return rc;
159291159756
}
159292159757
@@ -159393,11 +159858,11 @@
159393159858
rc = whereLoopInsert(pBuilder, pNew);
159394159859
if( pNew->u.vtab.needFree ){
159395159860
sqlite3_free(pNew->u.vtab.idxStr);
159396159861
pNew->u.vtab.needFree = 0;
159397159862
}
159398
- WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
159863
+ WHERETRACE(0xffffffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
159399159864
*pbIn, (sqlite3_uint64)mPrereq,
159400159865
(sqlite3_uint64)(pNew->prereq & ~mPrereq)));
159401159866
159402159867
return rc;
159403159868
}
@@ -159585,11 +160050,11 @@
159585160050
return SQLITE_NOMEM_BKPT;
159586160051
}
159587160052
159588160053
/* First call xBestIndex() with all constraints usable. */
159589160054
WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
159590
- WHERETRACE(0x40, (" VirtualOne: all usable\n"));
160055
+ WHERETRACE(0x800, (" VirtualOne: all usable\n"));
159591160056
rc = whereLoopAddVirtualOne(
159592160057
pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry
159593160058
);
159594160059
if( bRetry ){
159595160060
assert( rc==SQLITE_OK );
@@ -159610,11 +160075,11 @@
159610160075
Bitmask mBestNoIn = 0;
159611160076
159612160077
/* If the plan produced by the earlier call uses an IN(...) term, call
159613160078
** xBestIndex again, this time with IN(...) terms disabled. */
159614160079
if( bIn ){
159615
- WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
160080
+ WHERETRACE(0x800, (" VirtualOne: all usable w/o IN\n"));
159616160081
rc = whereLoopAddVirtualOne(
159617160082
pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0);
159618160083
assert( bIn==0 );
159619160084
mBestNoIn = pNew->prereq & ~mPrereq;
159620160085
if( mBestNoIn==0 ){
@@ -159636,11 +160101,11 @@
159636160101
if( mThis>mPrev && mThis<mNext ) mNext = mThis;
159637160102
}
159638160103
mPrev = mNext;
159639160104
if( mNext==ALLBITS ) break;
159640160105
if( mNext==mBest || mNext==mBestNoIn ) continue;
159641
- WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
160106
+ WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
159642160107
(sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
159643160108
rc = whereLoopAddVirtualOne(
159644160109
pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
159645160110
if( pNew->prereq==mPrereq ){
159646160111
seenZero = 1;
@@ -159650,21 +160115,21 @@
159650160115
159651160116
/* If the calls to xBestIndex() in the above loop did not find a plan
159652160117
** that requires no source tables at all (i.e. one guaranteed to be
159653160118
** usable), make a call here with all source tables disabled */
159654160119
if( rc==SQLITE_OK && seenZero==0 ){
159655
- WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
160120
+ WHERETRACE(0x800, (" VirtualOne: all disabled\n"));
159656160121
rc = whereLoopAddVirtualOne(
159657160122
pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
159658160123
if( bIn==0 ) seenZeroNoIN = 1;
159659160124
}
159660160125
159661160126
/* If the calls to xBestIndex() have so far failed to find a plan
159662160127
** that requires no source tables at all and does not use an IN(...)
159663160128
** operator, make a final call to obtain one here. */
159664160129
if( rc==SQLITE_OK && seenZeroNoIN==0 ){
159665
- WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
160130
+ WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n"));
159666160131
rc = whereLoopAddVirtualOne(
159667160132
pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0);
159668160133
}
159669160134
}
159670160135
@@ -159716,11 +160181,11 @@
159716160181
int i, j;
159717160182
159718160183
sSubBuild = *pBuilder;
159719160184
sSubBuild.pOrSet = &sCur;
159720160185
159721
- WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
160186
+ WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
159722160187
for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
159723160188
if( (pOrTerm->eOperator & WO_AND)!=0 ){
159724160189
sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
159725160190
}else if( pOrTerm->leftCursor==iCur ){
159726160191
tempWC.pWInfo = pWC->pWInfo;
@@ -159733,13 +160198,13 @@
159733160198
}else{
159734160199
continue;
159735160200
}
159736160201
sCur.n = 0;
159737160202
#ifdef WHERETRACE_ENABLED
159738
- WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
160203
+ WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
159739160204
(int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
159740
- if( sqlite3WhereTrace & 0x400 ){
160205
+ if( sqlite3WhereTrace & 0x20000 ){
159741160206
sqlite3WhereClausePrint(sSubBuild.pWC);
159742160207
}
159743160208
#endif
159744160209
#ifndef SQLITE_OMIT_VIRTUALTABLE
159745160210
if( IsVirtual(pItem->pTab) ){
@@ -159797,11 +160262,11 @@
159797160262
pNew->rRun = sSum.a[i].rRun + 1;
159798160263
pNew->nOut = sSum.a[i].nOut;
159799160264
pNew->prereq = sSum.a[i].prereq;
159800160265
rc = whereLoopInsert(pBuilder, pNew);
159801160266
}
159802
- WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
160267
+ WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
159803160268
}
159804160269
}
159805160270
return rc;
159806160271
}
159807160272
@@ -160145,12 +160610,12 @@
160145160610
if( iColumn>=XN_ROWID ){
160146160611
if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
160147160612
if( pOBExpr->iTable!=iCur ) continue;
160148160613
if( pOBExpr->iColumn!=iColumn ) continue;
160149160614
}else{
160150
- Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
160151
- if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
160615
+ Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
160616
+ if( sqlite3ExprCompareSkip(pOBExpr, pIxExpr, iCur) ){
160152160617
continue;
160153160618
}
160154160619
}
160155160620
if( iColumn!=XN_ROWID ){
160156160621
pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
@@ -160278,41 +160743,60 @@
160278160743
** Return the cost of sorting nRow rows, assuming that the keys have
160279160744
** nOrderby columns and that the first nSorted columns are already in
160280160745
** order.
160281160746
*/
160282160747
static LogEst whereSortingCost(
160283
- WhereInfo *pWInfo,
160284
- LogEst nRow,
160285
- int nOrderBy,
160286
- int nSorted
160748
+ WhereInfo *pWInfo, /* Query planning context */
160749
+ LogEst nRow, /* Estimated number of rows to sort */
160750
+ int nOrderBy, /* Number of ORDER BY clause terms */
160751
+ int nSorted /* Number of initial ORDER BY terms naturally in order */
160287160752
){
160288
- /* TUNING: Estimated cost of a full external sort, where N is
160753
+ /* Estimated cost of a full external sort, where N is
160289160754
** the number of rows to sort is:
160290160755
**
160291
- ** cost = (3.0 * N * log(N)).
160756
+ ** cost = (K * N * log(N)).
160292160757
**
160293160758
** Or, if the order-by clause has X terms but only the last Y
160294160759
** terms are out of order, then block-sorting will reduce the
160295160760
** sorting cost to:
160296160761
**
160297
- ** cost = (3.0 * N * log(N)) * (Y/X)
160762
+ ** cost = (K * N * log(N)) * (Y/X)
160298160763
**
160299
- ** The (Y/X) term is implemented using stack variable rScale
160300
- ** below.
160764
+ ** The constant K is at least 2.0 but will be larger if there are a
160765
+ ** large number of columns to be sorted, as the sorting time is
160766
+ ** proportional to the amount of content to be sorted. The algorithm
160767
+ ** does not currently distinguish between fat columns (BLOBs and TEXTs)
160768
+ ** and skinny columns (INTs). It just uses the number of columns as
160769
+ ** an approximation for the row width.
160770
+ **
160771
+ ** And extra factor of 2.0 or 3.0 is added to the sorting cost if the sort
160772
+ ** is built using OP_IdxInsert and OP_Sort rather than with OP_SorterInsert.
160301160773
*/
160302
- LogEst rScale, rSortCost;
160303
- assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
160304
- rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
160305
- rSortCost = nRow + rScale + 16;
160774
+ LogEst rSortCost, nCol;
160775
+ assert( pWInfo->pSelect!=0 );
160776
+ assert( pWInfo->pSelect->pEList!=0 );
160777
+ /* TUNING: sorting cost proportional to the number of output columns: */
160778
+ nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
160779
+ rSortCost = nRow + nCol;
160780
+ if( nSorted>0 ){
160781
+ /* Scale the result by (Y/X) */
160782
+ rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
160783
+ }
160306160784
160307160785
/* Multiple by log(M) where M is the number of output rows.
160308160786
** Use the LIMIT for M if it is smaller. Or if this sort is for
160309160787
** a DISTINCT operator, M will be the number of distinct output
160310160788
** rows, so fudge it downwards a bit.
160311160789
*/
160312
- if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
160313
- nRow = pWInfo->iLimit;
160790
+ if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
160791
+ rSortCost += 10; /* TUNING: Extra 2.0x if using LIMIT */
160792
+ if( nSorted!=0 ){
160793
+ rSortCost += 6; /* TUNING: Extra 1.5x if also using partial sort */
160794
+ }
160795
+ if( pWInfo->iLimit<nRow ){
160796
+ nRow = pWInfo->iLimit;
160797
+ }
160314160798
}else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
160315160799
/* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
160316160800
** reduces the number of output rows by a factor of 2 */
160317160801
if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
160318160802
}
@@ -160460,15 +160944,15 @@
160460160944
if( aSortCost[isOrdered]==0 ){
160461160945
aSortCost[isOrdered] = whereSortingCost(
160462160946
pWInfo, nRowEst, nOrderBy, isOrdered
160463160947
);
160464160948
}
160465
- /* TUNING: Add a small extra penalty (5) to sorting as an
160949
+ /* TUNING: Add a small extra penalty (3) to sorting as an
160466160950
** extra encouragment to the query planner to select a plan
160467160951
** where the rows emerge in the correct order without any sorting
160468160952
** required. */
160469
- rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
160953
+ rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 3;
160470160954
160471160955
WHERETRACE(0x002,
160472160956
("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
160473160957
aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
160474160958
rUnsorted, rCost));
@@ -160812,11 +161296,11 @@
160812161296
if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
160813161297
#ifdef SQLITE_DEBUG
160814161298
pLoop->cId = '0';
160815161299
#endif
160816161300
#ifdef WHERETRACE_ENABLED
160817
- if( sqlite3WhereTrace ){
161301
+ if( sqlite3WhereTrace & 0x02 ){
160818161302
sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
160819161303
}
160820161304
#endif
160821161305
return 1;
160822161306
}
@@ -160942,11 +161426,11 @@
160942161426
break;
160943161427
}
160944161428
}
160945161429
}
160946161430
if( pTerm<pEnd ) continue;
160947
- WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
161431
+ WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
160948161432
notReady &= ~pLoop->maskSelf;
160949161433
for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
160950161434
if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
160951161435
pTerm->wtFlags |= TERM_CODED;
160952161436
}
@@ -161002,11 +161486,11 @@
161002161486
&& (pTab->tabFlags & TF_HasStat1)!=0
161003161487
){
161004161488
testcase( pItem->fg.jointype & JT_LEFT );
161005161489
pLoop->wsFlags |= WHERE_BLOOMFILTER;
161006161490
pLoop->wsFlags &= ~WHERE_IDX_ONLY;
161007
- WHERETRACE(0xffff, (
161491
+ WHERETRACE(0xffffffff, (
161008161492
"-> use Bloom-filter on loop %c because there are ~%.1e "
161009161493
"lookups into %s which has only ~%.1e rows\n",
161010161494
pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
161011161495
(double)sqlite3LogEstToInt(pTab->nRowLogEst)));
161012161496
}
@@ -161015,17 +161499,17 @@
161015161499
}
161016161500
}
161017161501
161018161502
/*
161019161503
** This is an sqlite3ParserAddCleanup() callback that is invoked to
161020
-** free the Parse->pIdxExpr list when the Parse object is destroyed.
161504
+** free the Parse->pIdxEpr list when the Parse object is destroyed.
161021161505
*/
161022161506
static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
161023161507
Parse *pParse = (Parse*)pObject;
161024
- while( pParse->pIdxExpr!=0 ){
161025
- IndexedExpr *p = pParse->pIdxExpr;
161026
- pParse->pIdxExpr = p->pIENext;
161508
+ while( pParse->pIdxEpr!=0 ){
161509
+ IndexedExpr *p = pParse->pIdxEpr;
161510
+ pParse->pIdxEpr = p->pIENext;
161027161511
sqlite3ExprDelete(db, p->pExpr);
161028161512
sqlite3DbFreeNN(db, p);
161029161513
}
161030161514
}
161031161515
@@ -161033,17 +161517,17 @@
161033161517
** The index pIdx is used by a query and contains one or more expressions.
161034161518
** In other words pIdx is an index on an expression. iIdxCur is the cursor
161035161519
** number for the index and iDataCur is the cursor number for the corresponding
161036161520
** table.
161037161521
**
161038
-** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
161522
+** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
161039161523
** each of the expressions in the index so that the expression code generator
161040161524
** will know to replace occurrences of the indexed expression with
161041161525
** references to the corresponding column of the index.
161042161526
*/
161043161527
static SQLITE_NOINLINE void whereAddIndexedExpr(
161044
- Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
161528
+ Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
161045161529
Index *pIdx, /* The index-on-expression that contains the expressions */
161046161530
int iIdxCur, /* Cursor number for pIdx */
161047161531
SrcItem *pTabItem /* The FROM clause entry for the table */
161048161532
){
161049161533
int i;
@@ -161068,20 +161552,20 @@
161068161552
continue;
161069161553
}
161070161554
if( sqlite3ExprIsConstant(pExpr) ) continue;
161071161555
p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
161072161556
if( p==0 ) break;
161073
- p->pIENext = pParse->pIdxExpr;
161557
+ p->pIENext = pParse->pIdxEpr;
161074161558
p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
161075161559
p->iDataCur = pTabItem->iCursor;
161076161560
p->iIdxCur = iIdxCur;
161077161561
p->iIdxCol = i;
161078161562
p->bMaybeNullRow = bMaybeNullRow;
161079161563
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
161080161564
p->zIdxName = pIdx->zName;
161081161565
#endif
161082
- pParse->pIdxExpr = p;
161566
+ pParse->pIdxEpr = p;
161083161567
if( p->pIENext==0 ){
161084161568
sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161085161569
}
161086161570
}
161087161571
}
@@ -161369,30 +161853,30 @@
161369161853
}
161370161854
}
161371161855
161372161856
/* Construct the WhereLoop objects */
161373161857
#if defined(WHERETRACE_ENABLED)
161374
- if( sqlite3WhereTrace & 0xffff ){
161858
+ if( sqlite3WhereTrace & 0xffffffff ){
161375161859
sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
161376161860
if( wctrlFlags & WHERE_USE_LIMIT ){
161377161861
sqlite3DebugPrintf(", limit: %d", iAuxArg);
161378161862
}
161379161863
sqlite3DebugPrintf(")\n");
161380
- if( sqlite3WhereTrace & 0x100 ){
161864
+ if( sqlite3WhereTrace & 0x8000 ){
161381161865
Select sSelect;
161382161866
memset(&sSelect, 0, sizeof(sSelect));
161383161867
sSelect.selFlags = SF_WhereBegin;
161384161868
sSelect.pSrc = pTabList;
161385161869
sSelect.pWhere = pWhere;
161386161870
sSelect.pOrderBy = pOrderBy;
161387161871
sSelect.pEList = pResultSet;
161388161872
sqlite3TreeViewSelect(0, &sSelect, 0);
161389161873
}
161390
- }
161391
- if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
161392
- sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
161393
- sqlite3WhereClausePrint(sWLB.pWC);
161874
+ if( sqlite3WhereTrace & 0x4000 ){ /* Display all WHERE clause terms */
161875
+ sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
161876
+ sqlite3WhereClausePrint(sWLB.pWC);
161877
+ }
161394161878
}
161395161879
#endif
161396161880
161397161881
if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
161398161882
rc = whereLoopAddAll(&sWLB);
@@ -161404,11 +161888,11 @@
161404161888
** changed based on STAT4 information while computing subsequent loops,
161405161889
** then we need to rerun the whole loop building process so that all
161406161890
** loops will be built using the revised truthProb values. */
161407161891
if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
161408161892
WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
161409
- WHERETRACE(0xffff,
161893
+ WHERETRACE(0xffffffff,
161410161894
("**** Redo all loop computations due to"
161411161895
" TERM_HIGHTRUTH changes ****\n"));
161412161896
while( pWInfo->pLoops ){
161413161897
WhereLoop *p = pWInfo->pLoops;
161414161898
pWInfo->pLoops = p->pNextLoop;
@@ -161490,15 +161974,15 @@
161490161974
){
161491161975
whereCheckIfBloomFilterIsUseful(pWInfo);
161492161976
}
161493161977
161494161978
#if defined(WHERETRACE_ENABLED)
161495
- if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
161979
+ if( sqlite3WhereTrace & 0x4000 ){ /* Display all terms of the WHERE clause */
161496161980
sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
161497161981
sqlite3WhereClausePrint(sWLB.pWC);
161498161982
}
161499
- WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
161983
+ WHERETRACE(0xffffffff,("*** Optimizer Finished ***\n"));
161500161984
#endif
161501161985
pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
161502161986
161503161987
/* If the caller is an UPDATE or DELETE statement that is requesting
161504161988
** to use a one-pass algorithm, determine if this is appropriate.
@@ -162028,11 +162512,11 @@
162028162512
last = iEnd;
162029162513
}else{
162030162514
last = pWInfo->iEndWhere;
162031162515
}
162032162516
if( pIdx->bHasExpr ){
162033
- IndexedExpr *p = pParse->pIdxExpr;
162517
+ IndexedExpr *p = pParse->pIdxEpr;
162034162518
while( p ){
162035162519
if( p->iIdxCur==pLevel->iIdxCur ){
162036162520
p->iDataCur = -1;
162037162521
p->iIdxCur = -1;
162038162522
}
@@ -163199,11 +163683,11 @@
163199163683
}
163200163684
163201163685
pSub = sqlite3SelectNew(
163202163686
pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
163203163687
);
163204
- SELECTTRACE(1,pParse,pSub,
163688
+ TREETRACE(0x40,pParse,pSub,
163205163689
("New window-function subquery in FROM clause of (%u/%p)\n",
163206163690
p->selId, p));
163207163691
p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
163208163692
assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
163209163693
** of sqlite3DbMallocRawNN() called from
@@ -176360,10 +176844,13 @@
176360176844
int iNew = *(int*)pArg;
176361176845
*(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
176362176846
if( iNew>=0 && iNew<=255 ){
176363176847
sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
176364176848
}
176849
+ rc = SQLITE_OK;
176850
+ }else if( op==SQLITE_FCNTL_RESET_CACHE ){
176851
+ sqlite3BtreeClearCache(pBtree);
176365176852
rc = SQLITE_OK;
176366176853
}else{
176367176854
int nSave = db->busyHandler.nBusy;
176368176855
rc = sqlite3OsFileControl(fd, op, pArg);
176369176856
db->busyHandler.nBusy = nSave;
@@ -213942,28 +214429,28 @@
213942214429
if( !pCsr->isAgg ){
213943214430
sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
213944214431
}
213945214432
break;
213946214433
case 4: /* ncell */
213947
- sqlite3_result_int(ctx, pCsr->nCell);
214434
+ sqlite3_result_int64(ctx, pCsr->nCell);
213948214435
break;
213949214436
case 5: /* payload */
213950
- sqlite3_result_int(ctx, pCsr->nPayload);
214437
+ sqlite3_result_int64(ctx, pCsr->nPayload);
213951214438
break;
213952214439
case 6: /* unused */
213953
- sqlite3_result_int(ctx, pCsr->nUnused);
214440
+ sqlite3_result_int64(ctx, pCsr->nUnused);
213954214441
break;
213955214442
case 7: /* mx_payload */
213956
- sqlite3_result_int(ctx, pCsr->nMxPayload);
214443
+ sqlite3_result_int64(ctx, pCsr->nMxPayload);
213957214444
break;
213958214445
case 8: /* pgoffset */
213959214446
if( !pCsr->isAgg ){
213960214447
sqlite3_result_int64(ctx, pCsr->iOffset);
213961214448
}
213962214449
break;
213963214450
case 9: /* pgsize */
213964
- sqlite3_result_int(ctx, pCsr->szPage);
214451
+ sqlite3_result_int64(ctx, pCsr->szPage);
213965214452
break;
213966214453
case 10: { /* schema */
213967214454
sqlite3 *db = sqlite3_context_db_handle(ctx);
213968214455
int iDb = pCsr->iDb;
213969214456
sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
@@ -238535,11 +239022,11 @@
238535239022
int nArg, /* Number of args */
238536239023
sqlite3_value **apUnused /* Function arguments */
238537239024
){
238538239025
assert( nArg==0 );
238539239026
UNUSED_PARAM2(nArg, apUnused);
238540
- sqlite3_result_text(pCtx, "fts5: 2022-11-16 12:10:08 89c459e766ea7e9165d0beeb124708b955a4950d0f4792f457465d71b158d318", -1, SQLITE_TRANSIENT);
239027
+ sqlite3_result_text(pCtx, "fts5: 2022-12-03 19:04:09 1a61c500add4a2bfe80c0c691d559cfca166dc5f8262651a58da7ec16a51d430", -1, SQLITE_TRANSIENT);
238541239028
}
238542239029
238543239030
/*
238544239031
** Return true if zName is the extension on one of the shadow tables used
238545239032
** by this module.
238546239033
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.40.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -450,13 +450,13 @@
450 **
451 ** See also: [sqlite3_libversion()],
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.40.0"
456 #define SQLITE_VERSION_NUMBER 3040000
457 #define SQLITE_SOURCE_ID "2022-11-16 19:57:21 5689f0d9ad1be532b274508938b25ff0d63027b8cc31f796dfaa2cca71d53642"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -1496,10 +1496,16 @@
1496 ** by clients within the current process, only within other processes.
1497 ** </ul>
1498 **
1499 ** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1500 ** Used by the cksmvfs VFS module only.
 
 
 
 
 
 
1501 ** </ul>
1502 */
1503 #define SQLITE_FCNTL_LOCKSTATE 1
1504 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1505 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1538,10 +1544,11 @@
1538 #define SQLITE_FCNTL_CKPT_DONE 37
1539 #define SQLITE_FCNTL_RESERVE_BYTES 38
1540 #define SQLITE_FCNTL_CKPT_START 39
1541 #define SQLITE_FCNTL_EXTERNAL_READER 40
1542 #define SQLITE_FCNTL_CKSM_FILE 41
 
1543
1544 /* deprecated names */
1545 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1546 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1547 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -5846,20 +5853,10 @@
5846 ** such a conversion is possible without loss of information (in other
5847 ** words, if the value is a string that looks like a number)
5848 ** then the conversion is performed. Otherwise no conversion occurs.
5849 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5850 **
5851 ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5852 ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5853 ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5854 ** returns something other than SQLITE_TEXT, then the return value from
5855 ** sqlite3_value_encoding(X) is meaningless. ^Calls to
5856 ** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5857 ** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5858 ** sqlite3_value_bytes16(X) might change the encoding of the value X and
5859 ** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5860 **
5861 ** ^Within the [xUpdate] method of a [virtual table], the
5862 ** sqlite3_value_nochange(X) interface returns true if and only if
5863 ** the column corresponding to X is unchanged by the UPDATE operation
5864 ** that the xUpdate method call was invoked to implement and if
5865 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5920,10 +5917,31 @@
5920 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5921 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5922 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5923 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5924 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5925 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5926
5927 /*
5928 ** CAPI3REF: Finding The Subtype Of SQL Values
5929 ** METHOD: sqlite3_value
@@ -14526,18 +14544,41 @@
14526 #endif
14527 #if defined(SQLITE_DEBUG) \
14528 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
14529 || defined(SQLITE_ENABLE_TREETRACE))
14530 # define TREETRACE_ENABLED 1
14531 # define SELECTTRACE(K,P,S,X) \
14532 if(sqlite3TreeTrace&(K)) \
14533 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14534 sqlite3DebugPrintf X
14535 #else
14536 # define SELECTTRACE(K,P,S,X)
14537 # define TREETRACE_ENABLED 0
14538 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14539
14540 /*
14541 ** Macros for "wheretrace"
14542 */
14543 SQLITE_PRIVATE u32 sqlite3WhereTrace;
@@ -14546,10 +14587,40 @@
14546 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14547 # define WHERETRACE_ENABLED 1
14548 #else
14549 # define WHERETRACE(K,X)
14550 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14551
14552
14553 /*
14554 ** An instance of the following structure is used to store the busy-handler
14555 ** callback for a given sqlite handle.
@@ -15711,10 +15782,12 @@
15711 #ifndef SQLITE_OMIT_WAL
15712 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15713 #endif
15714
15715 SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
 
 
15716
15717 /*
15718 ** If we are not using shared cache, then there is no need to
15719 ** use mutexes to access the BtShared structures. So make the
15720 ** Enter and Leave procedures no-ops.
@@ -18106,20 +18179,19 @@
18106 struct AggInfo {
18107 u8 directMode; /* Direct rendering mode means take data directly
18108 ** from source tables rather than from accumulators */
18109 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
18110 ** than the source table */
 
18111 int sortingIdx; /* Cursor number of the sorting index */
18112 int sortingIdxPTab; /* Cursor number of pseudo-table */
18113 int nSortingColumn; /* Number of columns in the sorting index */
18114 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
18115 ExprList *pGroupBy; /* The group by clause */
18116 struct AggInfo_col { /* For each column used in source tables */
18117 Table *pTab; /* Source table */
18118 Expr *pCExpr; /* The original expression */
18119 int iTable; /* Cursor number of the source table */
18120 int iMem; /* Memory location that acts as accumulator */
18121 i16 iColumn; /* Column number within the source table */
18122 i16 iSorterColumn; /* Column number in the sorting index */
18123 } *aCol;
18124 int nColumn; /* Number of used entries in aCol[] */
18125 int nAccumulator; /* Number of columns that show through to the output.
@@ -18126,18 +18198,28 @@
18126 ** Additional columns are used only as parameters to
18127 ** aggregate functions */
18128 struct AggInfo_func { /* For each aggregate function */
18129 Expr *pFExpr; /* Expression encoding the function */
18130 FuncDef *pFunc; /* The aggregate function implementation */
18131 int iMem; /* Memory location that acts as accumulator */
18132 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
18133 int iDistAddr; /* Address of OP_OpenEphemeral */
18134 } *aFunc;
18135 int nFunc; /* Number of entries in aFunc[] */
18136 u32 selId; /* Select to which this AggInfo belongs */
18137 };
18138
 
 
 
 
 
 
 
 
 
 
 
18139 /*
18140 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18141 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18142 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18143 ** it uses less memory in the Expr object, which is a big memory user
@@ -19046,11 +19128,11 @@
19046 ** of the base register during check-constraint eval */
19047 int nLabel; /* The *negative* of the number of labels used */
19048 int nLabelAlloc; /* Number of slots in aLabel */
19049 int *aLabel; /* Space to hold the labels */
19050 ExprList *pConstExpr;/* Constant expressions */
19051 IndexedExpr *pIdxExpr;/* List of expressions used by active indexes */
19052 Token constraintName;/* Name of the constraint currently being parsed */
19053 yDbMask writeMask; /* Start a write transaction on these databases */
19054 yDbMask cookieMask; /* Bitmask of schema verified databases */
19055 int regRowid; /* Register holding rowid of CREATE TABLE entry */
19056 int regRoot; /* Register holding root page number for new objects */
@@ -20393,10 +20475,13 @@
20393 SQLITE_PRIVATE const char *sqlite3ErrName(int);
20394 #endif
20395
20396 #ifndef SQLITE_OMIT_DESERIALIZE
20397 SQLITE_PRIVATE int sqlite3MemdbInit(void);
 
 
 
20398 #endif
20399
20400 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
20401 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
20402 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
@@ -20889,10 +20974,14 @@
20889 #endif
20890
20891 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
20892 SQLITE_PRIVATE int sqlite3KvvfsInit(void);
20893 #endif
 
 
 
 
20894
20895 #endif /* SQLITEINT_H */
20896
20897 /************** End of sqliteInt.h *******************************************/
20898 /************** Begin file os_common.h ***************************************/
@@ -20931,105 +21020,10 @@
20931 ** Macros for performance tracing. Normally turned off. Only works
20932 ** on i486 hardware.
20933 */
20934 #ifdef SQLITE_PERFORMANCE_TRACE
20935
20936 /*
20937 ** hwtime.h contains inline assembler code for implementing
20938 ** high-performance timing routines.
20939 */
20940 /************** Include hwtime.h in the middle of os_common.h ****************/
20941 /************** Begin file hwtime.h ******************************************/
20942 /*
20943 ** 2008 May 27
20944 **
20945 ** The author disclaims copyright to this source code. In place of
20946 ** a legal notice, here is a blessing:
20947 **
20948 ** May you do good and not evil.
20949 ** May you find forgiveness for yourself and forgive others.
20950 ** May you share freely, never taking more than you give.
20951 **
20952 ******************************************************************************
20953 **
20954 ** This file contains inline asm code for retrieving "high-performance"
20955 ** counters for x86 and x86_64 class CPUs.
20956 */
20957 #ifndef SQLITE_HWTIME_H
20958 #define SQLITE_HWTIME_H
20959
20960 /*
20961 ** The following routine only works on pentium-class (or newer) processors.
20962 ** It uses the RDTSC opcode to read the cycle count value out of the
20963 ** processor and returns that value. This can be used for high-res
20964 ** profiling.
20965 */
20966 #if !defined(__STRICT_ANSI__) && \
20967 (defined(__GNUC__) || defined(_MSC_VER)) && \
20968 (defined(i386) || defined(__i386__) || defined(_M_IX86))
20969
20970 #if defined(__GNUC__)
20971
20972 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20973 unsigned int lo, hi;
20974 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
20975 return (sqlite_uint64)hi << 32 | lo;
20976 }
20977
20978 #elif defined(_MSC_VER)
20979
20980 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
20981 __asm {
20982 rdtsc
20983 ret ; return value at EDX:EAX
20984 }
20985 }
20986
20987 #endif
20988
20989 #elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
20990
20991 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20992 unsigned long val;
20993 __asm__ __volatile__ ("rdtsc" : "=A" (val));
20994 return val;
20995 }
20996
20997 #elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
20998
20999 __inline__ sqlite_uint64 sqlite3Hwtime(void){
21000 unsigned long long retval;
21001 unsigned long junk;
21002 __asm__ __volatile__ ("\n\
21003 1: mftbu %1\n\
21004 mftb %L0\n\
21005 mftbu %0\n\
21006 cmpw %0,%1\n\
21007 bne 1b"
21008 : "=r" (retval), "=r" (junk));
21009 return retval;
21010 }
21011
21012 #else
21013
21014 /*
21015 ** asm() is needed for hardware timing support. Without asm(),
21016 ** disable the sqlite3Hwtime() routine.
21017 **
21018 ** sqlite3Hwtime() is only used for some obscure debugging
21019 ** and analysis configurations, not in any deliverable, so this
21020 ** should not be a great loss.
21021 */
21022 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
21023
21024 #endif
21025
21026 #endif /* !defined(SQLITE_HWTIME_H) */
21027
21028 /************** End of hwtime.h **********************************************/
21029 /************** Continuing where we left off in os_common.h ******************/
21030
21031 static sqlite_uint64 g_start;
21032 static sqlite_uint64 g_elapsed;
21033 #define TIMER_START g_start=sqlite3Hwtime()
21034 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21035 #define TIMER_ELAPSED g_elapsed
@@ -29200,11 +29194,11 @@
29200 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
29201 **
29202 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
29203 ** This provides a 256-byte safety margin for defense against 32-bit
29204 ** signed integer overflow bugs when computing memory allocation sizes.
29205 ** Parnoid applications might want to reduce the maximum allocation size
29206 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
29207 ** or even smaller would be reasonable upper bounds on the size of a memory
29208 ** allocations for most applications.
29209 */
29210 #ifndef SQLITE_MAX_ALLOCATION_SIZE
@@ -29714,13 +29708,18 @@
29714 ** SQL statement. Make a copy of this phrase in space obtained form
29715 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
29716 */
29717 SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
29718 int n;
 
 
 
 
 
29719 while( sqlite3Isspace(zStart[0]) ) zStart++;
29720 n = (int)(zEnd - zStart);
29721 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
29722 return sqlite3DbStrNDup(db, zStart, n);
29723 }
29724
29725 /*
29726 ** Free any prior content in *pz and replace it with a copy of zNew.
@@ -31698,11 +31697,11 @@
31698 if( pExpr==0 ){
31699 sqlite3TreeViewLine(pView, "nil");
31700 sqlite3TreeViewPop(&pView);
31701 return;
31702 }
31703 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
31704 StrAccum x;
31705 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
31706 sqlite3_str_appendf(&x, " fg.af=%x.%c",
31707 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
31708 if( ExprHasProperty(pExpr, EP_OuterON) ){
@@ -31715,10 +31714,13 @@
31715 sqlite3_str_appendf(&x, " DDL");
31716 }
31717 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
31718 sqlite3_str_appendf(&x, " IMMUTABLE");
31719 }
 
 
 
31720 sqlite3StrAccumFinish(&x);
31721 }else{
31722 zFlgs[0] = 0;
31723 }
31724 switch( pExpr->op ){
@@ -35193,10 +35195,106 @@
35193 i += pIn[i+1];
35194 }while( i<mx );
35195 return 0;
35196 }
35197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35198 /************** End of util.c ************************************************/
35199 /************** Begin file hash.c ********************************************/
35200 /*
35201 ** 2001 September 22
35202 **
@@ -35727,11 +35825,13 @@
35727 int isJournal; /* True if this is a journal file */
35728 unsigned int nJrnl; /* Space allocated for aJrnl[] */
35729 char *aJrnl; /* Journal content */
35730 int szPage; /* Last known page size */
35731 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
 
35732 };
 
35733
35734 /*
35735 ** Methods for KVVfsFile
35736 */
35737 static int kvvfsClose(sqlite3_file*);
@@ -36168,10 +36268,11 @@
36168 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36169
36170 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36171 pFile->isJournal ? "journal" : "db"));
36172 sqlite3_free(pFile->aJrnl);
 
36173 return SQLITE_OK;
36174 }
36175
36176 /*
36177 ** Read from the -journal file.
@@ -36216,11 +36317,11 @@
36216 ){
36217 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36218 unsigned int pgno;
36219 int got, n;
36220 char zKey[30];
36221 char aData[133073];
36222 assert( iOfst>=0 );
36223 assert( iAmt>=0 );
36224 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36225 if( iOfst+iAmt>=512 ){
36226 if( (iOfst % iAmt)!=0 ){
@@ -36233,19 +36334,20 @@
36233 pgno = 1 + iOfst/iAmt;
36234 }else{
36235 pgno = 1;
36236 }
36237 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36238 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
 
36239 if( got<0 ){
36240 n = 0;
36241 }else{
36242 aData[got] = 0;
36243 if( iOfst+iAmt<512 ){
36244 int k = iOfst+iAmt;
36245 aData[k*2] = 0;
36246 n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36247 if( n>=iOfst+iAmt ){
36248 memcpy(zBuf, &aData[2000+iOfst], iAmt);
36249 n = iAmt;
36250 }else{
36251 n = 0;
@@ -36300,11 +36402,11 @@
36300 sqlite_int64 iOfst
36301 ){
36302 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36303 unsigned int pgno;
36304 char zKey[30];
36305 char aData[131073];
36306 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36307 assert( iAmt>=512 && iAmt<=65536 );
36308 assert( (iAmt & (iAmt-1))==0 );
36309 assert( pFile->szPage<0 || pFile->szPage==iAmt );
36310 pFile->szPage = iAmt;
@@ -36508,10 +36610,14 @@
36508 }
36509 if( zName[0]=='s' ){
36510 pFile->zClass = "session";
36511 }else{
36512 pFile->zClass = "local";
 
 
 
 
36513 }
36514 pFile->aJrnl = 0;
36515 pFile->nJrnl = 0;
36516 pFile->szPage = -1;
36517 pFile->szDb = -1;
@@ -43326,11 +43432,11 @@
43326 ** requested from the underlying operating system, a number which
43327 ** might be greater than or equal to the argument, but not less
43328 ** than the argument.
43329 */
43330 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
43331 #if OS_VXWORKS
43332 struct timespec sp;
43333
43334 sp.tv_sec = microseconds / 1000000;
43335 sp.tv_nsec = (microseconds % 1000000) * 1000;
43336 nanosleep(&sp, NULL);
@@ -51816,10 +51922,17 @@
51816 sqlite3_free(pData);
51817 }
51818 sqlite3_mutex_leave(db->mutex);
51819 return rc;
51820 }
 
 
 
 
 
 
 
51821
51822 /*
51823 ** This routine is called when the extension is loaded.
51824 ** Register the new VFS.
51825 */
@@ -62136,11 +62249,15 @@
62136 ** The return value to this routine is always safe to use with
62137 ** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
62138 */
62139 SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
62140 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
62141 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
 
 
 
 
62142 }
62143
62144 /*
62145 ** Return the VFS structure for the pager.
62146 */
@@ -67719,13 +67836,13 @@
67719 ** within an expression that is an argument to another macro
67720 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
67721 ** So, this macro is defined instead.
67722 */
67723 #ifndef SQLITE_OMIT_AUTOVACUUM
67724 #define ISAUTOVACUUM (pBt->autoVacuum)
67725 #else
67726 #define ISAUTOVACUUM 0
67727 #endif
67728
67729
67730 /*
67731 ** This structure is passed around through all the sanity checking routines
@@ -69973,66 +70090,71 @@
69973 ** and initialize fields of the MemPage structure accordingly.
69974 **
69975 ** Only the following combinations are supported. Anything different
69976 ** indicates a corrupt database files:
69977 **
69978 ** PTF_ZERODATA
69979 ** PTF_ZERODATA | PTF_LEAF
69980 ** PTF_LEAFDATA | PTF_INTKEY
69981 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
69982 */
69983 static int decodeFlags(MemPage *pPage, int flagByte){
69984 BtShared *pBt; /* A copy of pPage->pBt */
69985
69986 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
69987 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69988 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
69989 flagByte &= ~PTF_LEAF;
69990 pPage->childPtrSize = 4-4*pPage->leaf;
69991 pBt = pPage->pBt;
69992 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
69993 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
69994 ** interior table b-tree page. */
69995 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
69996 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
69997 ** leaf table b-tree page. */
69998 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
69999 pPage->intKey = 1;
70000 if( pPage->leaf ){
70001 pPage->intKeyLeaf = 1;
70002 pPage->xCellSize = cellSizePtrTableLeaf;
70003 pPage->xParseCell = btreeParseCellPtr;
 
 
 
 
 
 
 
 
 
 
70004 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70005 pPage->intKeyLeaf = 0;
70006 pPage->xCellSize = cellSizePtrNoPayload;
70007 pPage->xParseCell = btreeParseCellPtrNoPayload;
70008 }
70009 pPage->maxLocal = pBt->maxLeaf;
70010 pPage->minLocal = pBt->minLeaf;
70011 }else if( flagByte==PTF_ZERODATA ){
70012 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
70013 ** interior index b-tree page. */
70014 assert( (PTF_ZERODATA)==2 );
70015 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
70016 ** leaf index b-tree page. */
70017 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
70018 pPage->intKey = 0;
70019 pPage->intKeyLeaf = 0;
70020 pPage->xCellSize = cellSizePtr;
70021 pPage->xParseCell = btreeParseCellPtrIndex;
70022 pPage->maxLocal = pBt->maxLocal;
70023 pPage->minLocal = pBt->minLocal;
70024 }else{
70025 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
70026 ** an error. */
70027 pPage->intKey = 0;
70028 pPage->intKeyLeaf = 0;
70029 pPage->xCellSize = cellSizePtr;
70030 pPage->xParseCell = btreeParseCellPtrIndex;
70031 return SQLITE_CORRUPT_PAGE(pPage);
70032 }
70033 pPage->max1bytePayload = pBt->max1bytePayload;
70034 return SQLITE_OK;
70035 }
70036
70037 /*
70038 ** Compute the amount of freespace on the page. In other words, fill
@@ -73568,13 +73690,29 @@
73568
73569 /* Move the cursor to the last entry in the table. Return SQLITE_OK
73570 ** on success. Set *pRes to 0 if the cursor actually points to something
73571 ** or set *pRes to 1 if the table is empty.
73572 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73573 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
73574 int rc;
73575
73576 assert( cursorOwnsBtShared(pCur) );
73577 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
73578
73579 /* If the cursor already points to the last entry, this is a no-op. */
73580 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
@@ -73591,27 +73729,11 @@
73591 assert( pCur->pPage->leaf );
73592 #endif
73593 *pRes = 0;
73594 return SQLITE_OK;
73595 }
73596
73597 rc = moveToRoot(pCur);
73598 if( rc==SQLITE_OK ){
73599 assert( pCur->eState==CURSOR_VALID );
73600 *pRes = 0;
73601 rc = moveToRightmost(pCur);
73602 if( rc==SQLITE_OK ){
73603 pCur->curFlags |= BTCF_AtLast;
73604 }else{
73605 pCur->curFlags &= ~BTCF_AtLast;
73606 }
73607 }else if( rc==SQLITE_EMPTY ){
73608 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
73609 *pRes = 1;
73610 rc = SQLITE_OK;
73611 }
73612 return rc;
73613 }
73614
73615 /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
73616 ** table near the key intKey. Return a success code.
73617 **
@@ -74674,11 +74796,11 @@
74674 }
74675
74676 /* If the database supports auto-vacuum, write an entry in the pointer-map
74677 ** to indicate that the page is free.
74678 */
74679 if( ISAUTOVACUUM ){
74680 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
74681 if( rc ) goto freepage_out;
74682 }
74683
74684 /* Now manipulate the actual database free-list structure. There are two
@@ -75114,28 +75236,24 @@
75114 ** pTemp is not null. Regardless of pTemp, allocate a new entry
75115 ** in pPage->apOvfl[] and make it point to the cell content (either
75116 ** in pTemp or the original pCell) and also record its index.
75117 ** Allocating a new entry in pPage->aCell[] implies that
75118 ** pPage->nOverflow is incremented.
75119 **
75120 ** *pRC must be SQLITE_OK when this routine is called.
75121 */
75122 static void insertCell(
75123 MemPage *pPage, /* Page into which we are copying */
75124 int i, /* New cell becomes the i-th cell of the page */
75125 u8 *pCell, /* Content of the new cell */
75126 int sz, /* Bytes of content in pCell */
75127 u8 *pTemp, /* Temp storage space for pCell, if needed */
75128 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
75129 int *pRC /* Read and write return code from here */
75130 ){
75131 int idx = 0; /* Where to write new cell content in data[] */
75132 int j; /* Loop counter */
75133 u8 *data; /* The content of the whole page */
75134 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
75135
75136 assert( *pRC==SQLITE_OK );
75137 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
75138 assert( MX_CELL(pPage->pBt)<=10921 );
75139 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
75140 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
75141 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
@@ -75166,18 +75284,17 @@
75166 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
75167 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
75168 }else{
75169 int rc = sqlite3PagerWrite(pPage->pDbPage);
75170 if( rc!=SQLITE_OK ){
75171 *pRC = rc;
75172 return;
75173 }
75174 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75175 data = pPage->aData;
75176 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
75177 rc = allocateSpace(pPage, sz, &idx);
75178 if( rc ){ *pRC = rc; return; }
75179 /* The allocateSpace() routine guarantees the following properties
75180 ** if it returns successfully */
75181 assert( idx >= 0 );
75182 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
75183 assert( idx+sz <= (int)pPage->pBt->usableSize );
@@ -75200,17 +75317,20 @@
75200 /* increment the cell count */
75201 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
75202 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
75203 #ifndef SQLITE_OMIT_AUTOVACUUM
75204 if( pPage->pBt->autoVacuum ){
 
75205 /* The cell may contain a pointer to an overflow page. If so, write
75206 ** the entry for the overflow page into the pointer map.
75207 */
75208 ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
 
75209 }
75210 #endif
75211 }
 
75212 }
75213
75214 /*
75215 ** The following parameters determine how many adjacent pages get involved
75216 ** in a balancing operation. NN is the number of neighbors on either side
@@ -75307,18 +75427,20 @@
75307 /*
75308 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
75309 ** computed.
75310 */
75311 static void populateCellCache(CellArray *p, int idx, int N){
 
 
75312 assert( idx>=0 && idx+N<=p->nCell );
75313 while( N>0 ){
75314 assert( p->apCell[idx]!=0 );
75315 if( p->szCell[idx]==0 ){
75316 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
75317 }else{
75318 assert( CORRUPT_DB ||
75319 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
75320 }
75321 idx++;
75322 N--;
75323 }
75324 }
@@ -75516,12 +75638,12 @@
75516 u8 * const pEnd = &aData[pPg->pBt->usableSize];
75517 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
75518 int nRet = 0;
75519 int i;
75520 int iEnd = iFirst + nCell;
75521 u8 *pFree = 0;
75522 int szFree = 0;
75523
75524 for(i=iFirst; i<iEnd; i++){
75525 u8 *pCell = pCArray->apCell[i];
75526 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
75527 int sz;
@@ -75538,10 +75660,13 @@
75538 szFree = sz;
75539 if( pFree+sz>pEnd ){
75540 return 0;
75541 }
75542 }else{
 
 
 
75543 pFree = pCell;
75544 szFree += sz;
75545 }
75546 nRet++;
75547 }
@@ -75745,11 +75870,11 @@
75745 ** of the parent page are still manipulated by thh code below.
75746 ** That is Ok, at this point the parent page is guaranteed to
75747 ** be marked as dirty. Returning an error code will cause a
75748 ** rollback, undoing any changes made to the parent page.
75749 */
75750 if( ISAUTOVACUUM ){
75751 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
75752 if( szCell>pNew->minLocal ){
75753 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
75754 }
75755 }
@@ -75773,12 +75898,12 @@
75773 pStop = &pCell[9];
75774 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
75775
75776 /* Insert the new divider cell into pParent. */
75777 if( rc==SQLITE_OK ){
75778 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
75779 0, pPage->pgno, &rc);
75780 }
75781
75782 /* Set the right-child pointer of pParent to point to the new page. */
75783 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
75784
@@ -75883,11 +76008,11 @@
75883 }
75884
75885 /* If this is an auto-vacuum database, update the pointer-map entries
75886 ** for any b-tree or overflow pages that pTo now contains the pointers to.
75887 */
75888 if( ISAUTOVACUUM ){
75889 *pRC = setChildPtrmaps(pTo);
75890 }
75891 }
75892 }
75893
@@ -76307,19 +76432,21 @@
76307
76308 r = cntNew[i-1] - 1;
76309 d = r + 1 - leafData;
76310 (void)cachedCellSize(&b, d);
76311 do{
 
76312 assert( d<nMaxCells );
76313 assert( r<nMaxCells );
76314 (void)cachedCellSize(&b, r);
 
76315 if( szRight!=0
76316 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
76317 break;
76318 }
76319 szRight += b.szCell[d] + 2;
76320 szLeft -= b.szCell[r] + 2;
76321 cntNew[i-1] = r;
76322 r--;
76323 d--;
76324 }while( r>=0 );
76325 szNew[i] = szRight;
@@ -76369,11 +76496,11 @@
76369 apNew[i] = pNew;
76370 nNew++;
76371 cntOld[i] = b.nCell;
76372
76373 /* Set the pointer-map entry for the new sibling page. */
76374 if( ISAUTOVACUUM ){
76375 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
76376 if( rc!=SQLITE_OK ){
76377 goto balance_cleanup;
76378 }
76379 }
@@ -76462,11 +76589,11 @@
76462 ** If the sibling pages are not leaves, then the pointer map entry
76463 ** associated with the right-child of each sibling may also need to be
76464 ** updated. This happens below, after the sibling pages have been
76465 ** populated, not here.
76466 */
76467 if( ISAUTOVACUUM ){
76468 MemPage *pOld;
76469 MemPage *pNew = pOld = apNew[0];
76470 int cntOldNext = pNew->nCell + pNew->nOverflow;
76471 int iNew = 0;
76472 int iOld = 0;
@@ -76559,11 +76686,11 @@
76559 pSrcEnd = b.apEnd[k];
76560 if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){
76561 rc = SQLITE_CORRUPT_BKPT;
76562 goto balance_cleanup;
76563 }
76564 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
76565 if( rc!=SQLITE_OK ) goto balance_cleanup;
76566 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76567 }
76568
76569 /* Now update the actual sibling pages. The order in which they are updated
@@ -76655,11 +76782,11 @@
76655 - apNew[0]->nCell*2)
76656 || rc!=SQLITE_OK
76657 );
76658 copyNodeContent(apNew[0], pParent, &rc);
76659 freePage(apNew[0], &rc);
76660 }else if( ISAUTOVACUUM && !leafCorrection ){
76661 /* Fix the pointer map entries associated with the right-child of each
76662 ** sibling page. All other pointer map entries have already been taken
76663 ** care of. */
76664 for(i=0; i<nNew; i++){
76665 u32 key = get4byte(&apNew[i]->aData[8]);
@@ -76676,11 +76803,11 @@
76676 for(i=nNew; i<nOld; i++){
76677 freePage(apOld[i], &rc);
76678 }
76679
76680 #if 0
76681 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
76682 /* The ptrmapCheckPages() contains assert() statements that verify that
76683 ** all pointer map pages are set correctly. This is helpful while
76684 ** debugging. This is usually disabled because a corrupt database may
76685 ** cause an assert() statement to fail. */
76686 ptrmapCheckPages(apNew, nNew);
@@ -76738,11 +76865,11 @@
76738 */
76739 rc = sqlite3PagerWrite(pRoot->pDbPage);
76740 if( rc==SQLITE_OK ){
76741 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
76742 copyNodeContent(pRoot, pChild, &rc);
76743 if( ISAUTOVACUUM ){
76744 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
76745 }
76746 }
76747 if( rc ){
76748 *ppChild = 0;
@@ -77070,11 +77197,10 @@
77070 int loc = seekResult; /* -1: before desired location +1: after */
77071 int szNew = 0;
77072 int idx;
77073 MemPage *pPage;
77074 Btree *p = pCur->pBtree;
77075 BtShared *pBt = p->pBt;
77076 unsigned char *oldCell;
77077 unsigned char *newCell = 0;
77078
77079 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
77080 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
@@ -77089,11 +77215,11 @@
77089 ** that the cursor is already where it needs to be and returns without
77090 ** doing any work. To avoid thwarting these optimizations, it is important
77091 ** not to clear the cursor here.
77092 */
77093 if( pCur->curFlags & BTCF_Multiple ){
77094 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
77095 if( rc ) return rc;
77096 if( loc && pCur->iPage<0 ){
77097 /* This can only happen if the schema is corrupt such that there is more
77098 ** than one table or index with the same root page as used by the cursor.
77099 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
@@ -77113,12 +77239,12 @@
77113 if( rc && rc!=SQLITE_EMPTY ) return rc;
77114 }
77115
77116 assert( cursorOwnsBtShared(pCur) );
77117 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
77118 && pBt->inTransaction==TRANS_WRITE
77119 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
77120 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
77121
77122 /* Assert that the caller has been consistent. If this cursor was opened
77123 ** expecting an index b-tree, then the caller should be inserting blob
77124 ** keys with no associated data. If the cursor was opened expecting an
@@ -77231,30 +77357,32 @@
77231
77232 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
77233 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
77234 loc==0 ? "overwrite" : "new entry"));
77235 assert( pPage->isInit || CORRUPT_DB );
77236 newCell = pBt->pTmpSpace;
77237 assert( newCell!=0 );
 
77238 if( flags & BTREE_PREFORMAT ){
77239 rc = SQLITE_OK;
77240 szNew = pBt->nPreformatSize;
77241 if( szNew<4 ) szNew = 4;
77242 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
77243 CellInfo info;
77244 pPage->xParseCell(pPage, newCell, &info);
77245 if( info.nPayload!=info.nLocal ){
77246 Pgno ovfl = get4byte(&newCell[szNew-4]);
77247 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
 
77248 }
77249 }
77250 }else{
77251 rc = fillInCell(pPage, newCell, pX, &szNew);
 
77252 }
77253 if( rc ) goto end_insert;
77254 assert( szNew==pPage->xCellSize(pPage, newCell) );
77255 assert( szNew <= MX_CELL_SIZE(pBt) );
77256 idx = pCur->ix;
77257 if( loc==0 ){
77258 CellInfo info;
77259 assert( idx>=0 );
77260 if( idx>=pPage->nCell ){
@@ -77270,11 +77398,11 @@
77270 }
77271 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
77272 testcase( pCur->curFlags & BTCF_ValidOvfl );
77273 invalidateOverflowCache(pCur);
77274 if( info.nSize==szNew && info.nLocal==info.nPayload
77275 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
77276 ){
77277 /* Overwrite the old cell with the new if they are the same size.
77278 ** We could also try to do this if the old cell is smaller, then add
77279 ** the leftover space to the free list. But experiments show that
77280 ** doing that is no faster then skipping this optimization and just
@@ -77300,11 +77428,11 @@
77300 idx = ++pCur->ix;
77301 pCur->curFlags &= ~BTCF_ValidNKey;
77302 }else{
77303 assert( pPage->leaf );
77304 }
77305 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
77306 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
77307 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
77308
77309 /* If no error has occurred and pPage has an overflow cell, call balance()
77310 ** to redistribute the cells within the tree. Since balance() may move
@@ -77373,11 +77501,10 @@
77373 ** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
77374 **
77375 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
77376 */
77377 SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
77378 int rc = SQLITE_OK;
77379 BtShared *pBt = pDest->pBt;
77380 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
77381 const u8 *aIn; /* Pointer to next input buffer */
77382 u32 nIn; /* Size of input buffer aIn[] */
77383 u32 nRem; /* Bytes of data still to copy */
@@ -77396,11 +77523,13 @@
77396 }
77397 nRem = pSrc->info.nPayload;
77398 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
77399 memcpy(aOut, aIn, nIn);
77400 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
 
77401 }else{
 
77402 Pager *pSrcPager = pSrc->pBt->pPager;
77403 u8 *pPgnoOut = 0;
77404 Pgno ovflIn = 0;
77405 DbPage *pPageIn = 0;
77406 MemPage *pPageOut = 0;
@@ -77448,11 +77577,11 @@
77448 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
77449 Pgno pgnoNew;
77450 MemPage *pNew = 0;
77451 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
77452 put4byte(pPgnoOut, pgnoNew);
77453 if( ISAUTOVACUUM && pPageOut ){
77454 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
77455 }
77456 releasePage(pPageOut);
77457 pPageOut = pNew;
77458 if( pPageOut ){
@@ -77464,13 +77593,12 @@
77464 }
77465 }while( nRem>0 && rc==SQLITE_OK );
77466
77467 releasePage(pPageOut);
77468 sqlite3PagerUnref(pPageIn);
 
77469 }
77470
77471 return rc;
77472 }
77473
77474 /*
77475 ** Delete the entry that the cursor is pointing to.
77476 **
@@ -77621,11 +77749,11 @@
77621 assert( MX_CELL_SIZE(pBt) >= nCell );
77622 pTmp = pBt->pTmpSpace;
77623 assert( pTmp!=0 );
77624 rc = sqlite3PagerWrite(pLeaf->pDbPage);
77625 if( rc==SQLITE_OK ){
77626 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
77627 }
77628 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
77629 if( rc ) return rc;
77630 }
77631
@@ -79144,10 +79272,21 @@
79144
79145 /*
79146 ** Return the size of the header added to each page by this module.
79147 */
79148 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
 
 
 
 
 
 
 
 
 
 
 
79149
79150 #if !defined(SQLITE_OMIT_SHARED_CACHE)
79151 /*
79152 ** Return true if the Btree passed as the only argument is sharable.
79153 */
@@ -83384,11 +83523,11 @@
83384 assert( n!=P4_INT32 && n!=P4_VTAB );
83385 assert( n<=0 );
83386 if( p->db->mallocFailed ){
83387 freeP4(p->db, n, pP4);
83388 }else{
83389 assert( pP4!=0 );
83390 assert( p->nOp>0 );
83391 pOp = &p->aOp[p->nOp-1];
83392 assert( pOp->p4type==P4_NOTUSED );
83393 pOp->p4type = n;
83394 pOp->p4.p = pP4;
@@ -87760,11 +87899,14 @@
87760 void (*xDel)(void *),
87761 unsigned char enc
87762 ){
87763 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87764 assert( xDel!=SQLITE_DYNAMIC );
87765 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
 
 
 
87766 if( n>0x7fffffff ){
87767 (void)invokeValueDestructor(z, xDel, pCtx);
87768 }else{
87769 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
87770 }
@@ -87775,29 +87917,29 @@
87775 const void *z,
87776 int n,
87777 void (*xDel)(void *)
87778 ){
87779 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87780 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
87781 }
87782 SQLITE_API void sqlite3_result_text16be(
87783 sqlite3_context *pCtx,
87784 const void *z,
87785 int n,
87786 void (*xDel)(void *)
87787 ){
87788 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87789 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
87790 }
87791 SQLITE_API void sqlite3_result_text16le(
87792 sqlite3_context *pCtx,
87793 const void *z,
87794 int n,
87795 void (*xDel)(void *)
87796 ){
87797 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87798 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
87799 }
87800 #endif /* SQLITE_OMIT_UTF16 */
87801 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
87802 Mem *pOut = pCtx->pOut;
87803 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
@@ -88858,22 +89000,25 @@
88858 sqlite3_uint64 nData,
88859 void (*xDel)(void*),
88860 unsigned char enc
88861 ){
88862 assert( xDel!=SQLITE_DYNAMIC );
88863 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
 
 
 
88864 return bindText(pStmt, i, zData, nData, xDel, enc);
88865 }
88866 #ifndef SQLITE_OMIT_UTF16
88867 SQLITE_API int sqlite3_bind_text16(
88868 sqlite3_stmt *pStmt,
88869 int i,
88870 const void *zData,
88871 int nData,
88872 void (*xDel)(void*)
88873 ){
88874 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
88875 }
88876 #endif /* SQLITE_OMIT_UTF16 */
88877 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
88878 int rc;
88879 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
@@ -90242,20 +90387,10 @@
90242 #else
90243 # define REGISTER_TRACE(R,M)
90244 #endif
90245
90246
90247 #ifdef VDBE_PROFILE
90248
90249 /*
90250 ** hwtime.h contains inline assembler code for implementing
90251 ** high-performance timing routines.
90252 */
90253 /* #include "hwtime.h" */
90254
90255 #endif
90256
90257 #ifndef NDEBUG
90258 /*
90259 ** This function is only called from within an assert() expression. It
90260 ** checks that the sqlite3.nTransaction variable is correctly set to
90261 ** the number of non-transaction savepoints currently in the
@@ -95203,10 +95338,11 @@
95203 x.nZero = pData->u.nZero;
95204 }else{
95205 x.nZero = 0;
95206 }
95207 x.pKey = 0;
 
95208 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
95209 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
95210 seekResult
95211 );
95212 pC->deferredMoveto = 0;
@@ -105097,11 +105233,11 @@
105097 pExpr = pExpr->pLeft;
105098 assert( pExpr!=0 );
105099 }
105100 op = pExpr->op;
105101 if( op==TK_REGISTER ) op = pExpr->op2;
105102 if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
105103 assert( ExprUseYTab(pExpr) );
105104 assert( pExpr->y.pTab!=0 );
105105 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
105106 }
105107 if( op==TK_SELECT ){
@@ -105217,11 +105353,13 @@
105217 CollSeq *pColl = 0;
105218 const Expr *p = pExpr;
105219 while( p ){
105220 int op = p->op;
105221 if( op==TK_REGISTER ) op = p->op2;
105222 if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
 
 
105223 int j;
105224 assert( ExprUseYTab(p) );
105225 assert( p->y.pTab!=0 );
105226 if( (j = p->iColumn)>=0 ){
105227 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
@@ -109081,11 +109219,11 @@
109081 }
109082 return target;
109083 }
109084
109085 /*
109086 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109087 ** If it is, then resolve the expression by reading from the index and
109088 ** return the register into which the value has been read. If pExpr is
109089 ** not an indexed expression, then return negative.
109090 */
109091 static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
@@ -109093,11 +109231,11 @@
109093 Expr *pExpr, /* The expression to potentially bypass */
109094 int target /* Where to store the result of the expression */
109095 ){
109096 IndexedExpr *p;
109097 Vdbe *v;
109098 for(p=pParse->pIdxExpr; p; p=p->pIENext){
109099 int iDataCur = p->iDataCur;
109100 if( iDataCur<0 ) continue;
109101 if( pParse->iSelfTab ){
109102 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109103 iDataCur = -1;
@@ -109113,14 +109251,14 @@
109113 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109114 VdbeCoverage(v);
109115 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109116 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109117 sqlite3VdbeGoto(v, 0);
109118 p = pParse->pIdxExpr;
109119 pParse->pIdxExpr = 0;
109120 sqlite3ExprCode(pParse, pExpr, target);
109121 pParse->pIdxExpr = p;
109122 sqlite3VdbeJumpHere(v, addr+2);
109123 }else{
109124 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109125 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109126 }
@@ -109155,11 +109293,11 @@
109155 assert( v!=0 );
109156
109157 expr_code_doover:
109158 if( pExpr==0 ){
109159 op = TK_NULL;
109160 }else if( pParse->pIdxExpr!=0
109161 && !ExprHasProperty(pExpr, EP_Leaf)
109162 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109163 ){
109164 return r1;
109165 }else{
@@ -109172,26 +109310,32 @@
109172 struct AggInfo_col *pCol;
109173 assert( pAggInfo!=0 );
109174 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109175 pCol = &pAggInfo->aCol[pExpr->iAgg];
109176 if( !pAggInfo->directMode ){
109177 assert( pCol->iMem>0 );
109178 return pCol->iMem;
109179 }else if( pAggInfo->useSortingIdx ){
109180 Table *pTab = pCol->pTab;
109181 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109182 pCol->iSorterColumn, target);
109183 if( pCol->iColumn<0 ){
 
 
109184 VdbeComment((v,"%s.rowid",pTab->zName));
109185 }else if( ALWAYS(pTab!=0) ){
109186 VdbeComment((v,"%s.%s",
109187 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
109188 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
109189 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
109190 }
109191 }
109192 return target;
 
 
 
 
 
109193 }
109194 /* Otherwise, fall thru into the TK_COLUMN case */
109195 /* no break */ deliberate_fall_through
109196 }
109197 case TK_COLUMN: {
@@ -109485,11 +109629,11 @@
109485 || NEVER(pExpr->iAgg>=pInfo->nFunc)
109486 ){
109487 assert( !ExprHasProperty(pExpr, EP_IntValue) );
109488 sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr);
109489 }else{
109490 return pInfo->aFunc[pExpr->iAgg].iMem;
109491 }
109492 break;
109493 }
109494 case TK_FUNCTION: {
109495 ExprList *pFarg; /* List of function arguments */
@@ -109774,11 +109918,11 @@
109774 u8 okConstFactor = pParse->okConstFactor;
109775 AggInfo *pAggInfo = pExpr->pAggInfo;
109776 if( pAggInfo ){
109777 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109778 if( !pAggInfo->directMode ){
109779 inReg = pAggInfo->aCol[pExpr->iAgg].iMem;
109780 break;
109781 }
109782 if( pExpr->pAggInfo->useSortingIdx ){
109783 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109784 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
@@ -111285,10 +111429,76 @@
111285 &pInfo->nFunc,
111286 &i
111287 );
111288 return i;
111289 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111290
111291 /*
111292 ** This is the xExprCallback for a tree walker. It is used to
111293 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
111294 ** for additional information.
@@ -111299,11 +111509,40 @@
111299 Parse *pParse = pNC->pParse;
111300 SrcList *pSrcList = pNC->pSrcList;
111301 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
111302
111303 assert( pNC->ncFlags & NC_UAggInfo );
 
111304 switch( pExpr->op ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111305 case TK_IF_NULL_ROW:
111306 case TK_AGG_COLUMN:
111307 case TK_COLUMN: {
111308 testcase( pExpr->op==TK_AGG_COLUMN );
111309 testcase( pExpr->op==TK_COLUMN );
@@ -111311,71 +111550,13 @@
111311 /* Check to see if the column is in one of the tables in the FROM
111312 ** clause of the aggregate query */
111313 if( ALWAYS(pSrcList!=0) ){
111314 SrcItem *pItem = pSrcList->a;
111315 for(i=0; i<pSrcList->nSrc; i++, pItem++){
111316 struct AggInfo_col *pCol;
111317 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
111318 if( pExpr->iTable==pItem->iCursor ){
111319 /* If we reach this point, it means that pExpr refers to a table
111320 ** that is in the FROM clause of the aggregate query.
111321 **
111322 ** Make an entry for the column in pAggInfo->aCol[] if there
111323 ** is not an entry there already.
111324 */
111325 int k;
111326 pCol = pAggInfo->aCol;
111327 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
111328 if( pCol->iTable==pExpr->iTable
111329 && pCol->iColumn==pExpr->iColumn
111330 && pExpr->op!=TK_IF_NULL_ROW
111331 ){
111332 break;
111333 }
111334 }
111335 if( (k>=pAggInfo->nColumn)
111336 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
111337 ){
111338 pCol = &pAggInfo->aCol[k];
111339 assert( ExprUseYTab(pExpr) );
111340 pCol->pTab = pExpr->y.pTab;
111341 pCol->iTable = pExpr->iTable;
111342 pCol->iColumn = pExpr->iColumn;
111343 pCol->iMem = ++pParse->nMem;
111344 pCol->iSorterColumn = -1;
111345 pCol->pCExpr = pExpr;
111346 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
111347 int j, n;
111348 ExprList *pGB = pAggInfo->pGroupBy;
111349 struct ExprList_item *pTerm = pGB->a;
111350 n = pGB->nExpr;
111351 for(j=0; j<n; j++, pTerm++){
111352 Expr *pE = pTerm->pExpr;
111353 if( pE->op==TK_COLUMN
111354 && pE->iTable==pExpr->iTable
111355 && pE->iColumn==pExpr->iColumn
111356 ){
111357 pCol->iSorterColumn = j;
111358 break;
111359 }
111360 }
111361 }
111362 if( pCol->iSorterColumn<0 ){
111363 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
111364 }
111365 }
111366 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
111367 ** because it was there before or because we just created it).
111368 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
111369 ** pAggInfo->aCol[] entry.
111370 */
111371 ExprSetVVAProperty(pExpr, EP_NoReduce);
111372 pExpr->pAggInfo = pAggInfo;
111373 if( pExpr->op==TK_COLUMN ){
111374 pExpr->op = TK_AGG_COLUMN;
111375 }
111376 pExpr->iAgg = (i16)k;
111377 break;
111378 } /* endif pExpr->iTable==pItem->iCursor */
111379 } /* end loop over pSrcList */
111380 }
111381 return WRC_Prune;
@@ -111401,11 +111582,10 @@
111401 i = addAggInfoFunc(pParse->db, pAggInfo);
111402 if( i>=0 ){
111403 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
111404 pItem = &pAggInfo->aFunc[i];
111405 pItem->pFExpr = pExpr;
111406 pItem->iMem = ++pParse->nMem;
111407 assert( ExprUseUToken(pExpr) );
111408 pItem->pFunc = sqlite3FindFunction(pParse->db,
111409 pExpr->u.zToken,
111410 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
111411 if( pExpr->flags & EP_Distinct ){
@@ -126002,21 +126182,19 @@
126002 default:
126003 return;
126004 }
126005 ans = log(x)/b;
126006 }else{
126007 ans = log(x);
126008 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
126009 case 1:
126010 /* Convert from natural logarithm to log base 10 */
126011 ans /= M_LN10;
126012 break;
126013 case 2:
126014 /* Convert from natural logarithm to log base 2 */
126015 ans /= M_LN2;
126016 break;
126017 default:
 
126018 break;
126019 }
126020 }
126021 sqlite3_result_double(context, ans);
126022 }
@@ -129565,10 +129743,11 @@
129565 /* no break */ deliberate_fall_through
129566 case OE_Rollback:
129567 case OE_Fail: {
129568 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
129569 pCol->zCnName);
 
129570 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
129571 onError, iReg);
129572 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
129573 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
129574 VdbeCoverage(v);
@@ -132296,11 +132475,11 @@
132296 0,
132297 0,
132298 #endif
132299 sqlite3_db_name,
132300 /* Version 3.40.0 and later */
132301 sqlite3_value_type
132302 };
132303
132304 /* True if x is the directory separator character
132305 */
132306 #if SQLITE_OS_WIN
@@ -139355,11 +139534,11 @@
139355 #endif
139356
139357 if( pParse->colNamesSet ) return;
139358 /* Column names are determined by the left-most term of a compound select */
139359 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
139360 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
139361 pTabList = pSelect->pSrc;
139362 pEList = pSelect->pEList;
139363 assert( v!=0 );
139364 assert( pTabList!=0 );
139365 pParse->colNamesSet = 1;
@@ -140141,11 +140320,11 @@
140141 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
140142 assert( !pPrior->pLimit );
140143 pPrior->iLimit = p->iLimit;
140144 pPrior->iOffset = p->iOffset;
140145 pPrior->pLimit = p->pLimit;
140146 SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL left...\n"));
140147 rc = sqlite3Select(pParse, pPrior, &dest);
140148 pPrior->pLimit = 0;
140149 if( rc ){
140150 goto multi_select_end;
140151 }
@@ -140159,11 +140338,11 @@
140159 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
140160 p->iLimit, p->iOffset+1, p->iOffset);
140161 }
140162 }
140163 ExplainQueryPlan((pParse, 1, "UNION ALL"));
140164 SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL right...\n"));
140165 rc = sqlite3Select(pParse, p, &dest);
140166 testcase( rc!=SQLITE_OK );
140167 pDelete = p->pPrior;
140168 p->pPrior = pPrior;
140169 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
@@ -140212,11 +140391,11 @@
140212
140213 /* Code the SELECT statements to our left
140214 */
140215 assert( !pPrior->pOrderBy );
140216 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
140217 SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
140218 rc = sqlite3Select(pParse, pPrior, &uniondest);
140219 if( rc ){
140220 goto multi_select_end;
140221 }
140222
@@ -140232,11 +140411,11 @@
140232 pLimit = p->pLimit;
140233 p->pLimit = 0;
140234 uniondest.eDest = op;
140235 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
140236 sqlite3SelectOpName(p->op)));
140237 SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
140238 rc = sqlite3Select(pParse, p, &uniondest);
140239 testcase( rc!=SQLITE_OK );
140240 assert( p->pOrderBy==0 );
140241 pDelete = p->pPrior;
140242 p->pPrior = pPrior;
@@ -140293,11 +140472,11 @@
140293 assert( p->pEList );
140294
140295 /* Code the SELECTs to our left into temporary table "tab1".
140296 */
140297 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
140298 SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT left...\n"));
140299 rc = sqlite3Select(pParse, pPrior, &intersectdest);
140300 if( rc ){
140301 goto multi_select_end;
140302 }
140303
@@ -140310,11 +140489,11 @@
140310 pLimit = p->pLimit;
140311 p->pLimit = 0;
140312 intersectdest.iSDParm = tab2;
140313 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
140314 sqlite3SelectOpName(p->op)));
140315 SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT right...\n"));
140316 rc = sqlite3Select(pParse, p, &intersectdest);
140317 testcase( rc!=SQLITE_OK );
140318 pDelete = p->pPrior;
140319 p->pPrior = pPrior;
140320 if( p->nSelectRow>pPrior->nSelectRow ){
@@ -141313,10 +141492,38 @@
141313 while( pSel->pPrior ){
141314 pSel = pSel->pPrior;
141315 }
141316 return pSel->pEList;
141317 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141318
141319 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
141320 /*
141321 ** This routine attempts to flatten subqueries as a performance optimization.
141322 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -141417,11 +141624,12 @@
141417 ** (17f) the subquery must not be the RHS of a LEFT JOIN.
141418 ** (17g) either the subquery is the first element of the outer
141419 ** query or there are no RIGHT or FULL JOINs in any arm
141420 ** of the subquery. (This is a duplicate of condition (27b).)
141421 ** (17h) The corresponding result set expressions in all arms of the
141422 ** compound must have the same affinity.
 
141423 **
141424 ** The parent and sub-query may contain WHERE clauses. Subject to
141425 ** rules (11), (13) and (14), they may also contain ORDER BY,
141426 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
141427 ** operator other than UNION ALL because all the other compound
@@ -141636,23 +141844,11 @@
141636
141637 /* Restriction (23) */
141638 if( (p->selFlags & SF_Recursive) ) return 0;
141639
141640 /* Restriction (17h) */
141641 for(ii=0; ii<pSub->pEList->nExpr; ii++){
141642 char aff;
141643 assert( pSub->pEList->a[ii].pExpr!=0 );
141644 aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
141645 for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
141646 assert( pSub1->pEList!=0 );
141647 assert( pSub1->pEList->nExpr>ii );
141648 assert( pSub1->pEList->a[ii].pExpr!=0 );
141649 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
141650 return 0;
141651 }
141652 }
141653 }
141654
141655 if( pSrc->nSrc>1 ){
141656 if( pParse->nSelect>500 ) return 0;
141657 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
141658 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
@@ -141659,11 +141855,11 @@
141659 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
141660 }
141661 }
141662
141663 /***** If we reach this point, flattening is permitted. *****/
141664 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
141665 pSub->selId, pSub, iFrom));
141666
141667 /* Authorize the subquery */
141668 pParse->zAuthContext = pSubitem->zName;
141669 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
@@ -141738,11 +141934,11 @@
141738 }
141739 pNew->pPrior = pPrior;
141740 if( pPrior ) pPrior->pNext = pNew;
141741 pNew->pNext = p;
141742 p->pPrior = pNew;
141743 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
141744 " creates %u as peer\n",pNew->selId));
141745 }
141746 assert( pSubitem->pSelect==0 );
141747 }
141748 sqlite3DbFree(db, aCsrMap);
@@ -141918,12 +142114,12 @@
141918 sqlite3AggInfoPersistWalkerInit(&w, pParse);
141919 sqlite3WalkSelect(&w,pSub1);
141920 sqlite3SelectDelete(db, pSub1);
141921
141922 #if TREETRACE_ENABLED
141923 if( sqlite3TreeTrace & 0x100 ){
141924 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
141925 sqlite3TreeViewSelect(0, p, 0);
141926 }
141927 #endif
141928
141929 return 1;
@@ -142293,16 +142489,18 @@
142293 **
142294 ** (7) The inner query is a Common Table Expression (CTE) that should
142295 ** be materialized. (This restriction is implemented in the calling
142296 ** routine.)
142297 **
142298 ** (8) The subquery may not be a compound that uses UNION, INTERSECT,
142299 ** or EXCEPT. (We could, perhaps, relax this restriction to allow
142300 ** this case if none of the comparisons operators between left and
142301 ** right arms of the compound use a collation other than BINARY.
142302 ** But it is a lot of work to check that case for an obscure and
142303 ** minor optimization, so we omit it for now.)
 
 
142304 **
142305 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
142306 ** terms are duplicated into the subquery.
142307 */
142308 static int pushDownWhereTerms(
@@ -142315,24 +142513,48 @@
142315 int nChng = 0;
142316 if( pWhere==0 ) return 0;
142317 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
142318 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
142319
142320 #ifndef SQLITE_OMIT_WINDOWFUNC
142321 if( pSubq->pPrior ){
142322 Select *pSel;
 
142323 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142324 u8 op = pSel->op;
142325 assert( op==TK_ALL || op==TK_SELECT
142326 || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
142327 if( op!=TK_ALL && op!=TK_SELECT ) return 0; /* restriction (8) */
 
 
 
142328 if( pSel->pWin ) return 0; /* restriction (6b) */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142329 }
142330 }else{
 
142331 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
 
142332 }
142333 #endif
142334
142335 #ifdef SQLITE_DEBUG
142336 /* Only the first term of a compound can have a WITH clause. But make
142337 ** sure no other terms are marked SF_Recursive in case something changes
142338 ** in the future.
@@ -143332,12 +143554,12 @@
143332 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
143333 p->selFlags |= SF_ComplexResult;
143334 }
143335 }
143336 #if TREETRACE_ENABLED
143337 if( sqlite3TreeTrace & 0x100 ){
143338 SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
143339 sqlite3TreeViewSelect(0, p, 0);
143340 }
143341 #endif
143342 return WRC_Continue;
143343 }
@@ -143467,10 +143689,177 @@
143467 if( pParse->nErr ) return;
143468 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
143469 if( pParse->nErr ) return;
143470 sqlite3SelectAddTypeInfo(pParse, p);
143471 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143472
143473 /*
143474 ** Reset the aggregate accumulator.
143475 **
143476 ** The aggregate accumulator is a set of memory cells that hold
@@ -143481,28 +143870,17 @@
143481 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
143482 Vdbe *v = pParse->pVdbe;
143483 int i;
143484 struct AggInfo_func *pFunc;
143485 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
 
143486 assert( pParse->db->pParse==pParse );
143487 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
143488 if( nReg==0 ) return;
143489 if( pParse->nErr ) return;
143490 #ifdef SQLITE_DEBUG
143491 /* Verify that all AggInfo registers are within the range specified by
143492 ** AggInfo.mnReg..AggInfo.mxReg */
143493 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
143494 for(i=0; i<pAggInfo->nColumn; i++){
143495 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
143496 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
143497 }
143498 for(i=0; i<pAggInfo->nFunc; i++){
143499 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
143500 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
143501 }
143502 #endif
143503 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
143504 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
143505 if( pFunc->iDistinct>=0 ){
143506 Expr *pE = pFunc->pFExpr;
143507 assert( ExprUseXList(pE) );
143508 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
@@ -143530,19 +143908,20 @@
143530 struct AggInfo_func *pF;
143531 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
143532 ExprList *pList;
143533 assert( ExprUseXList(pF->pFExpr) );
143534 pList = pF->pFExpr->x.pList;
143535 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
 
143536 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
143537 }
143538 }
143539
143540
143541 /*
143542 ** Update the accumulator memory cells for an aggregate based on
143543 ** the current cursor position.
143544 **
143545 ** If regAcc is non-zero and there are no min() or max() aggregates
143546 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
143547 ** registers if register regAcc contains 0. The caller will take care
143548 ** of setting and clearing regAcc.
@@ -143558,10 +143937,12 @@
143558 int regHit = 0;
143559 int addrHitTest = 0;
143560 struct AggInfo_func *pF;
143561 struct AggInfo_col *pC;
143562
 
 
143563 pAggInfo->directMode = 1;
143564 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
143565 int nArg;
143566 int addrNext = 0;
143567 int regAgg;
@@ -143618,11 +143999,11 @@
143618 pColl = pParse->db->pDfltColl;
143619 }
143620 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
143621 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
143622 }
143623 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
143624 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
143625 sqlite3VdbeChangeP5(v, (u8)nArg);
143626 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
143627 if( addrNext ){
143628 sqlite3VdbeResolveLabel(v, addrNext);
@@ -143633,11 +144014,11 @@
143633 }
143634 if( regHit ){
143635 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
143636 }
143637 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
143638 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
143639 }
143640
143641 pAggInfo->directMode = 0;
143642 if( addrHitTest ){
143643 sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
@@ -143729,11 +144110,11 @@
143729 sWalker.xExprCallback = havingToWhereExprCb;
143730 sWalker.u.pSelect = p;
143731 sqlite3WalkExpr(&sWalker, p->pHaving);
143732 #if TREETRACE_ENABLED
143733 if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
143734 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
143735 sqlite3TreeViewSelect(0, p, 0);
143736 }
143737 #endif
143738 }
143739
@@ -143861,12 +144242,12 @@
143861 }
143862 p->pEList->a[0].pExpr = pExpr;
143863 p->selFlags &= ~SF_Aggregate;
143864
143865 #if TREETRACE_ENABLED
143866 if( sqlite3TreeTrace & 0x400 ){
143867 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
143868 sqlite3TreeViewSelect(0, p, 0);
143869 }
143870 #endif
143871 return 1;
143872 }
@@ -143938,12 +144319,12 @@
143938 return 1;
143939 }
143940 assert( db->mallocFailed==0 );
143941 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
143942 #if TREETRACE_ENABLED
143943 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
143944 if( sqlite3TreeTrace & 0x10100 ){
143945 if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
143946 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
143947 __FILE__, __LINE__);
143948 }
143949 sqlite3ShowSelect(p);
@@ -143959,12 +144340,12 @@
143959 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
143960 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
143961 /* All of these destinations are also able to ignore the ORDER BY clause */
143962 if( p->pOrderBy ){
143963 #if TREETRACE_ENABLED
143964 SELECTTRACE(1,pParse,p, ("dropping superfluous ORDER BY:\n"));
143965 if( sqlite3TreeTrace & 0x100 ){
143966 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
143967 }
143968 #endif
143969 sqlite3ParserAddCleanup(pParse,
143970 (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
@@ -143980,12 +144361,12 @@
143980 goto select_end;
143981 }
143982 assert( db->mallocFailed==0 );
143983 assert( p->pEList!=0 );
143984 #if TREETRACE_ENABLED
143985 if( sqlite3TreeTrace & 0x104 ){
143986 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
143987 sqlite3TreeViewSelect(0, p, 0);
143988 }
143989 #endif
143990
143991 /* If the SF_UFSrcCheck flag is set, then this function is being called
@@ -144022,12 +144403,12 @@
144022 if( sqlite3WindowRewrite(pParse, p) ){
144023 assert( pParse->nErr );
144024 goto select_end;
144025 }
144026 #if TREETRACE_ENABLED
144027 if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
144028 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
144029 sqlite3TreeViewSelect(0, p, 0);
144030 }
144031 #endif
144032 #endif /* SQLITE_OMIT_WINDOWFUNC */
144033 pTabList = p->pSrc;
@@ -144054,11 +144435,11 @@
144054 */
144055 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
144056 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
144057 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
144058 ){
144059 SELECTTRACE(0x100,pParse,p,
144060 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
144061 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
144062 assert( pItem->iCursor>=0 );
144063 unsetJoinExpr(p->pWhere, pItem->iCursor,
144064 pTabList->a[0].fg.jointype & JT_LTORJ);
@@ -144110,11 +144491,11 @@
144110 && pSub->pLimit==0 /* Condition (1) */
144111 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
144112 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
144113 && OptimizationEnabled(db, SQLITE_OmitOrderBy)
144114 ){
144115 SELECTTRACE(0x100,pParse,p,
144116 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
144117 sqlite3ParserAddCleanup(pParse,
144118 (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
144119 pSub->pOrderBy);
144120 pSub->pOrderBy = 0;
@@ -144165,12 +144546,12 @@
144165 ** procedure.
144166 */
144167 if( p->pPrior ){
144168 rc = multiSelect(pParse, p, pDest);
144169 #if TREETRACE_ENABLED
144170 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
144171 if( (sqlite3TreeTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
144172 sqlite3TreeViewSelect(0, p, 0);
144173 }
144174 #endif
144175 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
144176 return rc;
@@ -144186,17 +144567,17 @@
144186 && p->pWhere->op==TK_AND
144187 && OptimizationEnabled(db, SQLITE_PropagateConst)
144188 && propagateConstants(pParse, p)
144189 ){
144190 #if TREETRACE_ENABLED
144191 if( sqlite3TreeTrace & 0x100 ){
144192 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
144193 sqlite3TreeViewSelect(0, p, 0);
144194 }
144195 #endif
144196 }else{
144197 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
144198 }
144199
144200 #ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
144201 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
144202 && countOfViewOptimization(pParse, p)
@@ -144265,19 +144646,19 @@
144265 && (pItem->fg.isCte==0
144266 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
144267 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
144268 ){
144269 #if TREETRACE_ENABLED
144270 if( sqlite3TreeTrace & 0x100 ){
144271 SELECTTRACE(0x100,pParse,p,
144272 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
144273 sqlite3TreeViewSelect(0, p, 0);
144274 }
144275 #endif
144276 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
144277 }else{
144278 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
144279 }
144280
144281 zSavedAuthContext = pParse->zAuthContext;
144282 pParse->zAuthContext = pItem->zName;
144283
@@ -144388,12 +144769,12 @@
144388 pGroupBy = p->pGroupBy;
144389 pHaving = p->pHaving;
144390 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
144391
144392 #if TREETRACE_ENABLED
144393 if( sqlite3TreeTrace & 0x400 ){
144394 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
144395 sqlite3TreeViewSelect(0, p, 0);
144396 }
144397 #endif
144398
144399 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
@@ -144425,12 +144806,12 @@
144425 ** original setting of the SF_Distinct flag, not the current setting */
144426 assert( sDistinct.isTnct );
144427 sDistinct.isTnct = 2;
144428
144429 #if TREETRACE_ENABLED
144430 if( sqlite3TreeTrace & 0x400 ){
144431 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
144432 sqlite3TreeViewSelect(0, p, 0);
144433 }
144434 #endif
144435 }
144436
@@ -144512,11 +144893,11 @@
144512 #endif
144513 assert( WHERE_USE_LIMIT==SF_FixedLimit );
144514
144515
144516 /* Begin the database scan. */
144517 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
144518 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
144519 p->pEList, p, wctrlFlags, p->nSelectRow);
144520 if( pWInfo==0 ) goto select_end;
144521 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
144522 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
@@ -144529,11 +144910,11 @@
144529 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
144530 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
144531 sSort.pOrderBy = 0;
144532 }
144533 }
144534 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
144535
144536 /* If sorting index that was created by a prior OP_OpenEphemeral
144537 ** instruction ended up not being needed, then change the OP_OpenEphemeral
144538 ** into an OP_Noop.
144539 */
@@ -144568,11 +144949,11 @@
144568 sqlite3WhereContinueLabel(pWInfo),
144569 sqlite3WhereBreakLabel(pWInfo));
144570
144571 /* End the database scan loop.
144572 */
144573 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
144574 sqlite3WhereEnd(pWInfo);
144575 }
144576 }else{
144577 /* This case when there exist aggregate functions or a GROUP BY clause
144578 ** or both */
@@ -144654,11 +145035,10 @@
144654 memset(&sNC, 0, sizeof(sNC));
144655 sNC.pParse = pParse;
144656 sNC.pSrcList = pTabList;
144657 sNC.uNC.pAggInfo = pAggInfo;
144658 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
144659 pAggInfo->mnReg = pParse->nMem+1;
144660 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
144661 pAggInfo->pGroupBy = pGroupBy;
144662 sqlite3ExprAnalyzeAggList(&sNC, pEList);
144663 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
144664 if( pHaving ){
@@ -144675,49 +145055,21 @@
144675 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
144676 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
144677 }else{
144678 minMaxFlag = WHERE_ORDERBY_NORMAL;
144679 }
144680 for(i=0; i<pAggInfo->nFunc; i++){
144681 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
144682 assert( ExprUseXList(pExpr) );
144683 sNC.ncFlags |= NC_InAggFunc;
144684 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
144685 #ifndef SQLITE_OMIT_WINDOWFUNC
144686 assert( !IsWindowFunc(pExpr) );
144687 if( ExprHasProperty(pExpr, EP_WinFunc) ){
144688 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
144689 }
144690 #endif
144691 sNC.ncFlags &= ~NC_InAggFunc;
144692 }
144693 pAggInfo->mxReg = pParse->nMem;
144694 if( db->mallocFailed ) goto select_end;
144695 #if TREETRACE_ENABLED
144696 if( sqlite3TreeTrace & 0x400 ){
144697 int ii;
144698 SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
144699 sqlite3TreeViewSelect(0, p, 0);
144700 if( minMaxFlag ){
144701 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
144702 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
144703 }
144704 for(ii=0; ii<pAggInfo->nColumn; ii++){
144705 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
144706 sqlite3DebugPrintf(
144707 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
144708 " iSorterColumn=%d\n",
144709 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
144710 pCol->iTable, pCol->iColumn, pCol->iMem,
144711 pCol->iSorterColumn);
144712 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
144713 }
144714 for(ii=0; ii<pAggInfo->nFunc; ii++){
144715 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
144716 ii, pAggInfo->aFunc[ii].iMem);
144717 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
144718 }
144719 }
144720 #endif
144721
144722
144723 /* Processing for aggregates with GROUP BY is very different and
@@ -144782,21 +145134,25 @@
144782 ** This might involve two separate loops with an OP_Sort in between, or
144783 ** it might be a single loop that uses an index to extract information
144784 ** in the right order to begin with.
144785 */
144786 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
144787 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
144788 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
144789 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
144790 | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
144791 );
144792 if( pWInfo==0 ){
144793 sqlite3ExprListDelete(db, pDistinct);
144794 goto select_end;
144795 }
 
 
 
 
144796 eDist = sqlite3WhereIsDistinct(pWInfo);
144797 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
144798 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
144799 /* The optimizer is able to deliver rows in group by order so
144800 ** we do not have to sort. The OP_OpenEphemeral table will be
144801 ** cancelled later because we still need to use the pKeyInfo
144802 */
@@ -144841,19 +145197,36 @@
144841 regRecord = sqlite3GetTempReg(pParse);
144842 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
144843 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
144844 sqlite3ReleaseTempReg(pParse, regRecord);
144845 sqlite3ReleaseTempRange(pParse, regBase, nCol);
144846 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
144847 sqlite3WhereEnd(pWInfo);
144848 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
144849 sortOut = sqlite3GetTempReg(pParse);
144850 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
144851 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
144852 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
144853 pAggInfo->useSortingIdx = 1;
144854 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144855
144856 /* If the index or temporary table used by the GROUP BY sort
144857 ** will naturally deliver rows in the order required by the ORDER BY
144858 ** clause, cancel the ephemeral table open coded earlier.
144859 **
@@ -144919,11 +145292,11 @@
144919 */
144920 if( groupBySort ){
144921 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
144922 VdbeCoverage(v);
144923 }else{
144924 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
144925 sqlite3WhereEnd(pWInfo);
144926 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
144927 }
144928 sqlite3ExprListDelete(db, pDistinct);
144929
@@ -145029,11 +145402,12 @@
145029 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
145030 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, (int)iRoot, iDb, 1);
145031 if( pKeyInfo ){
145032 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
145033 }
145034 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
 
145035 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
145036 explainSimpleCount(pParse, pTab, pBest);
145037 }else{
145038 int regAcc = 0; /* "populate accumulators" flag */
145039 ExprList *pDistinct = 0;
@@ -145065,10 +145439,11 @@
145065 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
145066 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
145067 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
145068 distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
145069 }
 
145070
145071 /* This case runs if the aggregate has no GROUP BY clause. The
145072 ** processing is much simpler since there is only a single row
145073 ** of output.
145074 */
@@ -145081,17 +145456,17 @@
145081 ** be an appropriate ORDER BY expression for the optimization.
145082 */
145083 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
145084 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
145085
145086 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
145087 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
145088 pDistinct, p, minMaxFlag|distFlag, 0);
145089 if( pWInfo==0 ){
145090 goto select_end;
145091 }
145092 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
145093 eDist = sqlite3WhereIsDistinct(pWInfo);
145094 updateAccumulator(pParse, regAcc, pAggInfo, eDist);
145095 if( eDist!=WHERE_DISTINCT_NOOP ){
145096 struct AggInfo_func *pF = pAggInfo->aFunc;
145097 if( pF ){
@@ -145101,11 +145476,11 @@
145101
145102 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
145103 if( minMaxFlag ){
145104 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
145105 }
145106 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
145107 sqlite3WhereEnd(pWInfo);
145108 finalizeAggFunctions(pParse, pAggInfo);
145109 }
145110
145111 sSort.pOrderBy = 0;
@@ -145148,11 +145523,11 @@
145148 sqlite3ExprListDelete(db, pMinMaxOrderBy);
145149 #ifdef SQLITE_DEBUG
145150 if( pAggInfo && !db->mallocFailed ){
145151 for(i=0; i<pAggInfo->nColumn; i++){
145152 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
145153 assert( pExpr!=0 );
145154 assert( pExpr->pAggInfo==pAggInfo );
145155 assert( pExpr->iAgg==i );
145156 }
145157 for(i=0; i<pAggInfo->nFunc; i++){
145158 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
@@ -145162,12 +145537,12 @@
145162 }
145163 }
145164 #endif
145165
145166 #if TREETRACE_ENABLED
145167 SELECTTRACE(0x1,pParse,p,("end processing\n"));
145168 if( (sqlite3TreeTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
145169 sqlite3TreeViewSelect(0, p, 0);
145170 }
145171 #endif
145172 ExplainQueryPlanPop(pParse);
145173 return rc;
@@ -145437,11 +145812,11 @@
145437 while( p ){
145438 Trigger *pTrig = (Trigger *)sqliteHashData(p);
145439 if( pTrig->pTabSchema==pTab->pSchema
145440 && pTrig->table
145441 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
145442 && pTrig->pTabSchema!=pTmpSchema
145443 ){
145444 pTrig->pNext = pList;
145445 pList = pTrig;
145446 }else if( pTrig->op==TK_RETURNING ){
145447 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -150944,10 +151319,11 @@
150944 #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
150945 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
150946 #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
150947 #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
150948 #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
 
150949
150950 #endif /* !defined(SQLITE_WHEREINT_H) */
150951
150952 /************** End of whereInt.h ********************************************/
150953 /************** Continuing where we left off in wherecode.c ******************/
@@ -151289,11 +151665,11 @@
151289 pTerm->wtFlags |= TERM_LIKECOND;
151290 }else{
151291 pTerm->wtFlags |= TERM_CODED;
151292 }
151293 #ifdef WHERETRACE_ENABLED
151294 if( sqlite3WhereTrace & 0x20000 ){
151295 sqlite3DebugPrintf("DISABLE-");
151296 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
151297 }
151298 #endif
151299 if( pTerm->iParent<0 ) break;
@@ -152276,17 +152652,19 @@
152276 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
152277 iCur = pTabItem->iCursor;
152278 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
152279 bRev = (pWInfo->revMask>>iLevel)&1;
152280 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
152281 #if WHERETRACE_ENABLED /* 0x20800 */
152282 if( sqlite3WhereTrace & 0x800 ){
152283 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
152284 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
152285 sqlite3WhereLoopPrint(pLoop, pWC);
 
 
152286 }
152287 if( sqlite3WhereTrace & 0x20000 ){
152288 if( iLevel==0 ){
152289 sqlite3DebugPrintf("WHERE clause being coded:\n");
152290 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
152291 }
152292 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
@@ -153206,11 +153584,11 @@
153206 pAndExpr->pLeft = pOrExpr;
153207 pOrExpr = pAndExpr;
153208 }
153209 /* Loop through table entries that match term pOrTerm. */
153210 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
153211 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
153212 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
153213 WHERE_OR_SUBCLAUSE, iCovCur);
153214 assert( pSubWInfo || pParse->nErr );
153215 if( pSubWInfo ){
153216 WhereLoop *pSubLoop;
@@ -153443,16 +153821,16 @@
153443 VdbeCoverageIf(v, (x&1)==1);
153444 VdbeCoverageIf(v, (x&1)==0);
153445 }
153446 #endif
153447 }
153448 #ifdef WHERETRACE_ENABLED /* 0xffff */
153449 if( sqlite3WhereTrace ){
153450 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
153451 pWC->nTerm-j, pTerm, iLoop));
153452 }
153453 if( sqlite3WhereTrace & 0x800 ){
153454 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
153455 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
153456 }
153457 #endif
153458 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
@@ -153477,12 +153855,12 @@
153477 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
153478 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
153479 if( pTerm->leftCursor!=iCur ) continue;
153480 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
153481 pE = pTerm->pExpr;
153482 #ifdef WHERETRACE_ENABLED /* 0x800 */
153483 if( sqlite3WhereTrace & 0x800 ){
153484 sqlite3DebugPrintf("Coding transitive constraint:\n");
153485 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
153486 }
153487 #endif
153488 assert( !ExprHasProperty(pE, EP_OuterON) );
@@ -153593,17 +153971,17 @@
153593 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
153594 pTerm->wtFlags |= TERM_CODED;
153595 }
153596 }
153597
153598 #if WHERETRACE_ENABLED /* 0x20800 */
153599 if( sqlite3WhereTrace & 0x20000 ){
153600 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
153601 iLevel);
153602 sqlite3WhereClausePrint(pWC);
153603 }
153604 if( sqlite3WhereTrace & 0x800 ){
153605 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
153606 iLevel, (u64)pLevel->notReady);
153607 }
153608 #endif
153609 return pLevel->notReady;
@@ -156259,11 +156637,11 @@
156259 ** are no-ops.
156260 */
156261 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
156262 static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
156263 int i;
156264 if( !sqlite3WhereTrace ) return;
156265 for(i=0; i<p->nConstraint; i++){
156266 sqlite3DebugPrintf(
156267 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
156268 i,
156269 p->aConstraint[i].iColumn,
@@ -156279,11 +156657,11 @@
156279 p->aOrderBy[i].desc);
156280 }
156281 }
156282 static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
156283 int i;
156284 if( !sqlite3WhereTrace ) return;
156285 for(i=0; i<p->nConstraint; i++){
156286 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
156287 i,
156288 p->aConstraintUsage[i].argvIndex,
156289 p->aConstraintUsage[i].omit);
@@ -157296,11 +157674,11 @@
157296 ** using the method described in the header comment for this function. */
157297 if( nDiff!=1 || pUpper==0 || pLower==0 ){
157298 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
157299 pLoop->nOut -= nAdjust;
157300 *pbDone = 1;
157301 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
157302 nLower, nUpper, nAdjust*-1, pLoop->nOut));
157303 }
157304
157305 }else{
157306 assert( *pbDone==0 );
@@ -157474,11 +157852,11 @@
157474 nNew = 10; assert( 10==sqlite3LogEst(2) );
157475 }
157476 if( nNew<nOut ){
157477 nOut = nNew;
157478 }
157479 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
157480 (u32)iLower, (u32)iUpper, nOut));
157481 }
157482 }else{
157483 int bDone = 0;
157484 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
@@ -157507,11 +157885,11 @@
157507 nOut -= (pLower!=0) + (pUpper!=0);
157508 if( nNew<10 ) nNew = 10;
157509 if( nNew<nOut ) nOut = nNew;
157510 #if defined(WHERETRACE_ENABLED)
157511 if( pLoop->nOut>nOut ){
157512 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
157513 pLoop->nOut, nOut));
157514 }
157515 #endif
157516 pLoop->nOut = (LogEst)nOut;
157517 return rc;
@@ -157572,11 +157950,11 @@
157572 if( rc!=SQLITE_OK ) return rc;
157573 if( bOk==0 ) return SQLITE_NOTFOUND;
157574 pBuilder->nRecValid = nEq;
157575
157576 whereKeyStats(pParse, p, pRec, 0, a);
157577 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
157578 p->zName, nEq-1, (int)a[1]));
157579 *pnRow = a[1];
157580
157581 return rc;
157582 }
@@ -157622,11 +158000,11 @@
157622 }
157623
157624 if( rc==SQLITE_OK ){
157625 if( nRowEst > nRow0 ) nRowEst = nRow0;
157626 *pnRow = nRowEst;
157627 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
157628 }
157629 assert( pBuilder->nRecValid==nRecValid );
157630 return rc;
157631 }
157632 #endif /* SQLITE_ENABLE_STAT4 */
@@ -157731,11 +158109,11 @@
157731 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
157732 }else{
157733 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
157734 }
157735 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
157736 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
157737 int i;
157738 for(i=0; i<p->nLTerm; i++){
157739 sqlite3WhereTermPrint(p->aLTerm[i], i);
157740 }
157741 }
@@ -158609,11 +158987,11 @@
158609 ** to be true for half or more of the rows in the table.
158610 ** See tag-202002240-1 */
158611 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
158612 ){
158613 #if WHERETRACE_ENABLED /* 0x01 */
158614 if( sqlite3WhereTrace & 0x01 ){
158615 sqlite3DebugPrintf(
158616 "STAT4 determines term has low selectivity:\n");
158617 sqlite3WhereTermPrint(pTerm, 999);
158618 }
158619 #endif
@@ -158646,13 +159024,21 @@
158646 /* Set rCostIdx to the cost of visiting selected rows in index. Add
158647 ** it to pNew->rRun, which is currently set to the cost of the index
158648 ** seek only. Then, if this is a non-covering index, add the cost of
158649 ** visiting the rows in the main table. */
158650 assert( pSrc->pTab->szTabRow>0 );
158651 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
 
 
 
 
 
 
 
 
158652 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
158653 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
158654 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
158655 }
158656 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
158657
158658 nOutUnadjusted = pNew->nOut;
@@ -158800,21 +159186,44 @@
158800 return 1;
158801 }
158802 }
158803 return 0;
158804 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158805
158806 /*
158807 ** Structure passed to the whereIsCoveringIndex Walker callback.
158808 */
 
158809 struct CoveringIndexCheck {
158810 Index *pIdx; /* The index */
158811 int iTabCur; /* Cursor number for the corresponding table */
 
 
158812 };
158813
158814 /*
158815 ** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
158816 **
158817 ** If the Expr node references the table with cursor pCk->iTabCur, then
158818 ** make sure that column is covered by the index pCk->pIdx. We know that
158819 ** all columns less than 63 (really BMS-1) are covered, so we don't need
158820 ** to check them. But we do need to check any column at 63 or greater.
@@ -158822,75 +159231,107 @@
158822 ** If the index does not cover the column, then set pWalk->eCode to
158823 ** non-zero and return WRC_Abort to stop the search.
158824 **
158825 ** If this node does not disprove that the index can be a covering index,
158826 ** then just return WRC_Continue, to continue the search.
 
 
 
158827 */
158828 static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
158829 int i; /* Loop counter */
158830 const Index *pIdx; /* The index of interest */
158831 const i16 *aiColumn; /* Columns contained in the index */
158832 u16 nColumn; /* Number of columns in the index */
158833 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
158834 if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
158835 if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
158836 pIdx = pWalk->u.pCovIdxCk->pIdx;
158837 aiColumn = pIdx->aiColumn;
158838 nColumn = pIdx->nColumn;
158839 for(i=0; i<nColumn; i++){
158840 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
158841 }
158842 pWalk->eCode = 1;
158843 return WRC_Abort;
 
 
 
 
 
 
 
 
 
 
158844 }
158845
158846
158847 /*
158848 ** pIdx is an index that covers all of the low-number columns used by
158849 ** pWInfo->pSelect (columns from 0 through 62). But there are columns
158850 ** in pWInfo->pSelect beyond 62. This routine tries to answer the question
158851 ** of whether pIdx covers *all* columns in the query.
158852 **
158853 ** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
158854 ** not a covering index or if we are unable to determine if pIdx is a
158855 ** covering index.
158856 **
158857 ** This routine is an optimization. It is always safe to return non-zero.
158858 ** But returning zero when non-zero should have been returned can lead to
158859 ** incorrect bytecode and assertion faults.
 
 
 
 
 
 
 
 
 
158860 */
158861 static SQLITE_NOINLINE u32 whereIsCoveringIndex(
158862 WhereInfo *pWInfo, /* The WHERE clause context */
158863 Index *pIdx, /* Index that is being tested */
158864 int iTabCur /* Cursor for the table being indexed */
158865 ){
158866 int i;
158867 struct CoveringIndexCheck ck;
158868 Walker w;
158869 if( pWInfo->pSelect==0 ){
158870 /* We don't have access to the full query, so we cannot check to see
158871 ** if pIdx is covering. Assume it is not. */
158872 return 1;
158873 }
158874 for(i=0; i<pIdx->nColumn; i++){
158875 if( pIdx->aiColumn[i]>=BMS-1 ) break;
158876 }
158877 if( i>=pIdx->nColumn ){
158878 /* pIdx does not index any columns greater than 62, but we know from
158879 ** colMask that columns greater than 62 are used, so this is not a
158880 ** covering index */
158881 return 1;
 
 
158882 }
158883 ck.pIdx = pIdx;
158884 ck.iTabCur = iTabCur;
 
 
158885 memset(&w, 0, sizeof(w));
158886 w.xExprCallback = whereIsCoveringIndexWalkCallback;
158887 w.xSelectCallback = sqlite3SelectWalkNoop;
158888 w.u.pCovIdxCk = &ck;
158889 w.eCode = 0;
158890 sqlite3WalkSelect(&w, pWInfo->pSelect);
158891 return w.eCode;
 
 
 
 
 
 
 
158892 }
158893
158894 /*
158895 ** Add all WhereLoop objects for a single table of the join where the table
158896 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
@@ -158971,11 +159412,11 @@
158971 sPk.nColumn = 1;
158972 sPk.aiColumn = &aiColumnPk;
158973 sPk.aiRowLogEst = aiRowEstPk;
158974 sPk.onError = OE_Replace;
158975 sPk.pTable = pTab;
158976 sPk.szIdxRow = pTab->szTabRow;
158977 sPk.idxType = SQLITE_IDXTYPE_IPK;
158978 aiRowEstPk[0] = pTab->nRowLogEst;
158979 aiRowEstPk[1] = 0;
158980 pFirst = pSrc->pTab->pIndex;
158981 if( pSrc->fg.notIndexed==0 ){
@@ -159102,18 +159543,42 @@
159102 pNew->nOut = rSize;
159103 if( rc ) break;
159104 }else{
159105 Bitmask m;
159106 if( pProbe->isCovering ){
159107 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159108 m = 0;
 
159109 }else{
159110 m = pSrc->colUsed & pProbe->colNotIdxed;
159111 if( m==TOPBIT ){
159112 m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159113 }
159114 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
159115 }
159116
159117 /* Full scan via index */
159118 if( b
159119 || !HasRowid(pTab)
@@ -159282,11 +159747,11 @@
159282 if( rc==SQLITE_CONSTRAINT ){
159283 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
159284 ** that the particular combination of parameters provided is unusable.
159285 ** Make no entries in the loop table.
159286 */
159287 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
159288 return SQLITE_OK;
159289 }
159290 return rc;
159291 }
159292
@@ -159393,11 +159858,11 @@
159393 rc = whereLoopInsert(pBuilder, pNew);
159394 if( pNew->u.vtab.needFree ){
159395 sqlite3_free(pNew->u.vtab.idxStr);
159396 pNew->u.vtab.needFree = 0;
159397 }
159398 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
159399 *pbIn, (sqlite3_uint64)mPrereq,
159400 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
159401
159402 return rc;
159403 }
@@ -159585,11 +160050,11 @@
159585 return SQLITE_NOMEM_BKPT;
159586 }
159587
159588 /* First call xBestIndex() with all constraints usable. */
159589 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
159590 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
159591 rc = whereLoopAddVirtualOne(
159592 pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry
159593 );
159594 if( bRetry ){
159595 assert( rc==SQLITE_OK );
@@ -159610,11 +160075,11 @@
159610 Bitmask mBestNoIn = 0;
159611
159612 /* If the plan produced by the earlier call uses an IN(...) term, call
159613 ** xBestIndex again, this time with IN(...) terms disabled. */
159614 if( bIn ){
159615 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
159616 rc = whereLoopAddVirtualOne(
159617 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0);
159618 assert( bIn==0 );
159619 mBestNoIn = pNew->prereq & ~mPrereq;
159620 if( mBestNoIn==0 ){
@@ -159636,11 +160101,11 @@
159636 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
159637 }
159638 mPrev = mNext;
159639 if( mNext==ALLBITS ) break;
159640 if( mNext==mBest || mNext==mBestNoIn ) continue;
159641 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
159642 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
159643 rc = whereLoopAddVirtualOne(
159644 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
159645 if( pNew->prereq==mPrereq ){
159646 seenZero = 1;
@@ -159650,21 +160115,21 @@
159650
159651 /* If the calls to xBestIndex() in the above loop did not find a plan
159652 ** that requires no source tables at all (i.e. one guaranteed to be
159653 ** usable), make a call here with all source tables disabled */
159654 if( rc==SQLITE_OK && seenZero==0 ){
159655 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
159656 rc = whereLoopAddVirtualOne(
159657 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
159658 if( bIn==0 ) seenZeroNoIN = 1;
159659 }
159660
159661 /* If the calls to xBestIndex() have so far failed to find a plan
159662 ** that requires no source tables at all and does not use an IN(...)
159663 ** operator, make a final call to obtain one here. */
159664 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
159665 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
159666 rc = whereLoopAddVirtualOne(
159667 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0);
159668 }
159669 }
159670
@@ -159716,11 +160181,11 @@
159716 int i, j;
159717
159718 sSubBuild = *pBuilder;
159719 sSubBuild.pOrSet = &sCur;
159720
159721 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
159722 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
159723 if( (pOrTerm->eOperator & WO_AND)!=0 ){
159724 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
159725 }else if( pOrTerm->leftCursor==iCur ){
159726 tempWC.pWInfo = pWC->pWInfo;
@@ -159733,13 +160198,13 @@
159733 }else{
159734 continue;
159735 }
159736 sCur.n = 0;
159737 #ifdef WHERETRACE_ENABLED
159738 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
159739 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
159740 if( sqlite3WhereTrace & 0x400 ){
159741 sqlite3WhereClausePrint(sSubBuild.pWC);
159742 }
159743 #endif
159744 #ifndef SQLITE_OMIT_VIRTUALTABLE
159745 if( IsVirtual(pItem->pTab) ){
@@ -159797,11 +160262,11 @@
159797 pNew->rRun = sSum.a[i].rRun + 1;
159798 pNew->nOut = sSum.a[i].nOut;
159799 pNew->prereq = sSum.a[i].prereq;
159800 rc = whereLoopInsert(pBuilder, pNew);
159801 }
159802 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
159803 }
159804 }
159805 return rc;
159806 }
159807
@@ -160145,12 +160610,12 @@
160145 if( iColumn>=XN_ROWID ){
160146 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
160147 if( pOBExpr->iTable!=iCur ) continue;
160148 if( pOBExpr->iColumn!=iColumn ) continue;
160149 }else{
160150 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
160151 if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
160152 continue;
160153 }
160154 }
160155 if( iColumn!=XN_ROWID ){
160156 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
@@ -160278,41 +160743,60 @@
160278 ** Return the cost of sorting nRow rows, assuming that the keys have
160279 ** nOrderby columns and that the first nSorted columns are already in
160280 ** order.
160281 */
160282 static LogEst whereSortingCost(
160283 WhereInfo *pWInfo,
160284 LogEst nRow,
160285 int nOrderBy,
160286 int nSorted
160287 ){
160288 /* TUNING: Estimated cost of a full external sort, where N is
160289 ** the number of rows to sort is:
160290 **
160291 ** cost = (3.0 * N * log(N)).
160292 **
160293 ** Or, if the order-by clause has X terms but only the last Y
160294 ** terms are out of order, then block-sorting will reduce the
160295 ** sorting cost to:
160296 **
160297 ** cost = (3.0 * N * log(N)) * (Y/X)
160298 **
160299 ** The (Y/X) term is implemented using stack variable rScale
160300 ** below.
 
 
 
 
 
 
 
160301 */
160302 LogEst rScale, rSortCost;
160303 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
160304 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
160305 rSortCost = nRow + rScale + 16;
 
 
 
 
 
 
160306
160307 /* Multiple by log(M) where M is the number of output rows.
160308 ** Use the LIMIT for M if it is smaller. Or if this sort is for
160309 ** a DISTINCT operator, M will be the number of distinct output
160310 ** rows, so fudge it downwards a bit.
160311 */
160312 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
160313 nRow = pWInfo->iLimit;
 
 
 
 
 
 
160314 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
160315 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
160316 ** reduces the number of output rows by a factor of 2 */
160317 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
160318 }
@@ -160460,15 +160944,15 @@
160460 if( aSortCost[isOrdered]==0 ){
160461 aSortCost[isOrdered] = whereSortingCost(
160462 pWInfo, nRowEst, nOrderBy, isOrdered
160463 );
160464 }
160465 /* TUNING: Add a small extra penalty (5) to sorting as an
160466 ** extra encouragment to the query planner to select a plan
160467 ** where the rows emerge in the correct order without any sorting
160468 ** required. */
160469 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
160470
160471 WHERETRACE(0x002,
160472 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
160473 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
160474 rUnsorted, rCost));
@@ -160812,11 +161296,11 @@
160812 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
160813 #ifdef SQLITE_DEBUG
160814 pLoop->cId = '0';
160815 #endif
160816 #ifdef WHERETRACE_ENABLED
160817 if( sqlite3WhereTrace ){
160818 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
160819 }
160820 #endif
160821 return 1;
160822 }
@@ -160942,11 +161426,11 @@
160942 break;
160943 }
160944 }
160945 }
160946 if( pTerm<pEnd ) continue;
160947 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
160948 notReady &= ~pLoop->maskSelf;
160949 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
160950 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
160951 pTerm->wtFlags |= TERM_CODED;
160952 }
@@ -161002,11 +161486,11 @@
161002 && (pTab->tabFlags & TF_HasStat1)!=0
161003 ){
161004 testcase( pItem->fg.jointype & JT_LEFT );
161005 pLoop->wsFlags |= WHERE_BLOOMFILTER;
161006 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
161007 WHERETRACE(0xffff, (
161008 "-> use Bloom-filter on loop %c because there are ~%.1e "
161009 "lookups into %s which has only ~%.1e rows\n",
161010 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
161011 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
161012 }
@@ -161015,17 +161499,17 @@
161015 }
161016 }
161017
161018 /*
161019 ** This is an sqlite3ParserAddCleanup() callback that is invoked to
161020 ** free the Parse->pIdxExpr list when the Parse object is destroyed.
161021 */
161022 static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
161023 Parse *pParse = (Parse*)pObject;
161024 while( pParse->pIdxExpr!=0 ){
161025 IndexedExpr *p = pParse->pIdxExpr;
161026 pParse->pIdxExpr = p->pIENext;
161027 sqlite3ExprDelete(db, p->pExpr);
161028 sqlite3DbFreeNN(db, p);
161029 }
161030 }
161031
@@ -161033,17 +161517,17 @@
161033 ** The index pIdx is used by a query and contains one or more expressions.
161034 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
161035 ** number for the index and iDataCur is the cursor number for the corresponding
161036 ** table.
161037 **
161038 ** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
161039 ** each of the expressions in the index so that the expression code generator
161040 ** will know to replace occurrences of the indexed expression with
161041 ** references to the corresponding column of the index.
161042 */
161043 static SQLITE_NOINLINE void whereAddIndexedExpr(
161044 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
161045 Index *pIdx, /* The index-on-expression that contains the expressions */
161046 int iIdxCur, /* Cursor number for pIdx */
161047 SrcItem *pTabItem /* The FROM clause entry for the table */
161048 ){
161049 int i;
@@ -161068,20 +161552,20 @@
161068 continue;
161069 }
161070 if( sqlite3ExprIsConstant(pExpr) ) continue;
161071 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
161072 if( p==0 ) break;
161073 p->pIENext = pParse->pIdxExpr;
161074 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
161075 p->iDataCur = pTabItem->iCursor;
161076 p->iIdxCur = iIdxCur;
161077 p->iIdxCol = i;
161078 p->bMaybeNullRow = bMaybeNullRow;
161079 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
161080 p->zIdxName = pIdx->zName;
161081 #endif
161082 pParse->pIdxExpr = p;
161083 if( p->pIENext==0 ){
161084 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161085 }
161086 }
161087 }
@@ -161369,30 +161853,30 @@
161369 }
161370 }
161371
161372 /* Construct the WhereLoop objects */
161373 #if defined(WHERETRACE_ENABLED)
161374 if( sqlite3WhereTrace & 0xffff ){
161375 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
161376 if( wctrlFlags & WHERE_USE_LIMIT ){
161377 sqlite3DebugPrintf(", limit: %d", iAuxArg);
161378 }
161379 sqlite3DebugPrintf(")\n");
161380 if( sqlite3WhereTrace & 0x100 ){
161381 Select sSelect;
161382 memset(&sSelect, 0, sizeof(sSelect));
161383 sSelect.selFlags = SF_WhereBegin;
161384 sSelect.pSrc = pTabList;
161385 sSelect.pWhere = pWhere;
161386 sSelect.pOrderBy = pOrderBy;
161387 sSelect.pEList = pResultSet;
161388 sqlite3TreeViewSelect(0, &sSelect, 0);
161389 }
161390 }
161391 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
161392 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
161393 sqlite3WhereClausePrint(sWLB.pWC);
161394 }
161395 #endif
161396
161397 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
161398 rc = whereLoopAddAll(&sWLB);
@@ -161404,11 +161888,11 @@
161404 ** changed based on STAT4 information while computing subsequent loops,
161405 ** then we need to rerun the whole loop building process so that all
161406 ** loops will be built using the revised truthProb values. */
161407 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
161408 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
161409 WHERETRACE(0xffff,
161410 ("**** Redo all loop computations due to"
161411 " TERM_HIGHTRUTH changes ****\n"));
161412 while( pWInfo->pLoops ){
161413 WhereLoop *p = pWInfo->pLoops;
161414 pWInfo->pLoops = p->pNextLoop;
@@ -161490,15 +161974,15 @@
161490 ){
161491 whereCheckIfBloomFilterIsUseful(pWInfo);
161492 }
161493
161494 #if defined(WHERETRACE_ENABLED)
161495 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
161496 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
161497 sqlite3WhereClausePrint(sWLB.pWC);
161498 }
161499 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
161500 #endif
161501 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
161502
161503 /* If the caller is an UPDATE or DELETE statement that is requesting
161504 ** to use a one-pass algorithm, determine if this is appropriate.
@@ -162028,11 +162512,11 @@
162028 last = iEnd;
162029 }else{
162030 last = pWInfo->iEndWhere;
162031 }
162032 if( pIdx->bHasExpr ){
162033 IndexedExpr *p = pParse->pIdxExpr;
162034 while( p ){
162035 if( p->iIdxCur==pLevel->iIdxCur ){
162036 p->iDataCur = -1;
162037 p->iIdxCur = -1;
162038 }
@@ -163199,11 +163683,11 @@
163199 }
163200
163201 pSub = sqlite3SelectNew(
163202 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
163203 );
163204 SELECTTRACE(1,pParse,pSub,
163205 ("New window-function subquery in FROM clause of (%u/%p)\n",
163206 p->selId, p));
163207 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
163208 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
163209 ** of sqlite3DbMallocRawNN() called from
@@ -176360,10 +176844,13 @@
176360 int iNew = *(int*)pArg;
176361 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
176362 if( iNew>=0 && iNew<=255 ){
176363 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
176364 }
 
 
 
176365 rc = SQLITE_OK;
176366 }else{
176367 int nSave = db->busyHandler.nBusy;
176368 rc = sqlite3OsFileControl(fd, op, pArg);
176369 db->busyHandler.nBusy = nSave;
@@ -213942,28 +214429,28 @@
213942 if( !pCsr->isAgg ){
213943 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
213944 }
213945 break;
213946 case 4: /* ncell */
213947 sqlite3_result_int(ctx, pCsr->nCell);
213948 break;
213949 case 5: /* payload */
213950 sqlite3_result_int(ctx, pCsr->nPayload);
213951 break;
213952 case 6: /* unused */
213953 sqlite3_result_int(ctx, pCsr->nUnused);
213954 break;
213955 case 7: /* mx_payload */
213956 sqlite3_result_int(ctx, pCsr->nMxPayload);
213957 break;
213958 case 8: /* pgoffset */
213959 if( !pCsr->isAgg ){
213960 sqlite3_result_int64(ctx, pCsr->iOffset);
213961 }
213962 break;
213963 case 9: /* pgsize */
213964 sqlite3_result_int(ctx, pCsr->szPage);
213965 break;
213966 case 10: { /* schema */
213967 sqlite3 *db = sqlite3_context_db_handle(ctx);
213968 int iDb = pCsr->iDb;
213969 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
@@ -238535,11 +239022,11 @@
238535 int nArg, /* Number of args */
238536 sqlite3_value **apUnused /* Function arguments */
238537 ){
238538 assert( nArg==0 );
238539 UNUSED_PARAM2(nArg, apUnused);
238540 sqlite3_result_text(pCtx, "fts5: 2022-11-16 12:10:08 89c459e766ea7e9165d0beeb124708b955a4950d0f4792f457465d71b158d318", -1, SQLITE_TRANSIENT);
238541 }
238542
238543 /*
238544 ** Return true if zName is the extension on one of the shadow tables used
238545 ** by this module.
238546
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.41.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -450,13 +450,13 @@
450 **
451 ** See also: [sqlite3_libversion()],
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.41.0"
456 #define SQLITE_VERSION_NUMBER 3041000
457 #define SQLITE_SOURCE_ID "2022-12-05 02:52:37 1b779afa3ed2f35a110e460fc6ed13cba744db85b9924149ab028b100d1e1e12"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -1496,10 +1496,16 @@
1496 ** by clients within the current process, only within other processes.
1497 ** </ul>
1498 **
1499 ** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1500 ** Used by the cksmvfs VFS module only.
1501 **
1502 ** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1503 ** If there is currently no transaction open on the database, and the
1504 ** database is not a temp db, then this file-control purges the contents
1505 ** of the in-memory page cache. If there is an open transaction, or if
1506 ** the db is a temp-db, it is a no-op, not an error.
1507 ** </ul>
1508 */
1509 #define SQLITE_FCNTL_LOCKSTATE 1
1510 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1511 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1538,10 +1544,11 @@
1544 #define SQLITE_FCNTL_CKPT_DONE 37
1545 #define SQLITE_FCNTL_RESERVE_BYTES 38
1546 #define SQLITE_FCNTL_CKPT_START 39
1547 #define SQLITE_FCNTL_EXTERNAL_READER 40
1548 #define SQLITE_FCNTL_CKSM_FILE 41
1549 #define SQLITE_FCNTL_RESET_CACHE 42
1550
1551 /* deprecated names */
1552 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1553 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1554 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -5846,20 +5853,10 @@
5853 ** such a conversion is possible without loss of information (in other
5854 ** words, if the value is a string that looks like a number)
5855 ** then the conversion is performed. Otherwise no conversion occurs.
5856 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5857 **
 
 
 
 
 
 
 
 
 
 
5858 ** ^Within the [xUpdate] method of a [virtual table], the
5859 ** sqlite3_value_nochange(X) interface returns true if and only if
5860 ** the column corresponding to X is unchanged by the UPDATE operation
5861 ** that the xUpdate method call was invoked to implement and if
5862 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5920,10 +5917,31 @@
5917 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5918 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5919 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5920 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5921 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5922
5923 /*
5924 ** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
5925 ** METHOD: sqlite3_value
5926 **
5927 ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5928 ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
5929 ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5930 ** returns something other than SQLITE_TEXT, then the return value from
5931 ** sqlite3_value_encoding(X) is meaningless. ^Calls to
5932 ** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
5933 ** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
5934 ** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
5935 ** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5936 **
5937 ** This routine is intended for used by applications that test and validate
5938 ** the SQLite implementation. This routine is inquiring about the opaque
5939 ** internal state of an [sqlite3_value] object. Ordinary applications should
5940 ** not need to know what the internal state of an sqlite3_value object is and
5941 ** hence should not need to use this interface.
5942 */
5943 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5944
5945 /*
5946 ** CAPI3REF: Finding The Subtype Of SQL Values
5947 ** METHOD: sqlite3_value
@@ -14526,18 +14544,41 @@
14544 #endif
14545 #if defined(SQLITE_DEBUG) \
14546 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
14547 || defined(SQLITE_ENABLE_TREETRACE))
14548 # define TREETRACE_ENABLED 1
14549 # define TREETRACE(K,P,S,X) \
14550 if(sqlite3TreeTrace&(K)) \
14551 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14552 sqlite3DebugPrintf X
14553 #else
14554 # define TREETRACE(K,P,S,X)
14555 # define TREETRACE_ENABLED 0
14556 #endif
14557
14558 /* TREETRACE flag meanings:
14559 **
14560 ** 0x00000001 Beginning and end of SELECT processing
14561 ** 0x00000002 WHERE clause processing
14562 ** 0x00000004 Query flattener
14563 ** 0x00000008 Result-set wildcard expansion
14564 ** 0x00000010 Query name resolution
14565 ** 0x00000020 Aggregate analysis
14566 ** 0x00000040 Window functions
14567 ** 0x00000080 Generated column names
14568 ** 0x00000100 Move HAVING terms into WHERE
14569 ** 0x00000200 Count-of-view optimization
14570 ** 0x00000400 Compound SELECT processing
14571 ** 0x00000800 Drop superfluous ORDER BY
14572 ** 0x00001000 LEFT JOIN simplifies to JOIN
14573 ** 0x00002000 Constant propagation
14574 ** 0x00004000 Push-down optimization
14575 ** 0x00008000 After all FROM-clause analysis
14576 ** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
14577 ** 0x00020000 Transform DISTINCT into GROUP BY
14578 ** 0x00040000 SELECT tree dump after all code has been generated
14579 */
14580
14581 /*
14582 ** Macros for "wheretrace"
14583 */
14584 SQLITE_PRIVATE u32 sqlite3WhereTrace;
@@ -14546,10 +14587,40 @@
14587 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14588 # define WHERETRACE_ENABLED 1
14589 #else
14590 # define WHERETRACE(K,X)
14591 #endif
14592
14593 /*
14594 ** Bits for the sqlite3WhereTrace mask:
14595 **
14596 ** (---any--) Top-level block structure
14597 ** 0x-------F High-level debug messages
14598 ** 0x----FFF- More detail
14599 ** 0xFFFF---- Low-level debug messages
14600 **
14601 ** 0x00000001 Code generation
14602 ** 0x00000002 Solver
14603 ** 0x00000004 Solver costs
14604 ** 0x00000008 WhereLoop inserts
14605 **
14606 ** 0x00000010 Display sqlite3_index_info xBestIndex calls
14607 ** 0x00000020 Range an equality scan metrics
14608 ** 0x00000040 IN operator decisions
14609 ** 0x00000080 WhereLoop cost adjustements
14610 ** 0x00000100
14611 ** 0x00000200 Covering index decisions
14612 ** 0x00000400 OR optimization
14613 ** 0x00000800 Index scanner
14614 ** 0x00001000 More details associated with code generation
14615 ** 0x00002000
14616 ** 0x00004000 Show all WHERE terms at key points
14617 ** 0x00008000 Show the full SELECT statement at key places
14618 **
14619 ** 0x00010000 Show more detail when printing WHERE terms
14620 ** 0x00020000 Show WHERE terms returned from whereScanNext()
14621 */
14622
14623
14624 /*
14625 ** An instance of the following structure is used to store the busy-handler
14626 ** callback for a given sqlite handle.
@@ -15711,10 +15782,12 @@
15782 #ifndef SQLITE_OMIT_WAL
15783 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15784 #endif
15785
15786 SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
15787
15788 SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree*);
15789
15790 /*
15791 ** If we are not using shared cache, then there is no need to
15792 ** use mutexes to access the BtShared structures. So make the
15793 ** Enter and Leave procedures no-ops.
@@ -18106,20 +18179,19 @@
18179 struct AggInfo {
18180 u8 directMode; /* Direct rendering mode means take data directly
18181 ** from source tables rather than from accumulators */
18182 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
18183 ** than the source table */
18184 u16 nSortingColumn; /* Number of columns in the sorting index */
18185 int sortingIdx; /* Cursor number of the sorting index */
18186 int sortingIdxPTab; /* Cursor number of pseudo-table */
18187 int iFirstReg; /* First register in range for aCol[] and aFunc[] */
 
18188 ExprList *pGroupBy; /* The group by clause */
18189 struct AggInfo_col { /* For each column used in source tables */
18190 Table *pTab; /* Source table */
18191 Expr *pCExpr; /* The original expression */
18192 int iTable; /* Cursor number of the source table */
 
18193 i16 iColumn; /* Column number within the source table */
18194 i16 iSorterColumn; /* Column number in the sorting index */
18195 } *aCol;
18196 int nColumn; /* Number of used entries in aCol[] */
18197 int nAccumulator; /* Number of columns that show through to the output.
@@ -18126,18 +18198,28 @@
18198 ** Additional columns are used only as parameters to
18199 ** aggregate functions */
18200 struct AggInfo_func { /* For each aggregate function */
18201 Expr *pFExpr; /* Expression encoding the function */
18202 FuncDef *pFunc; /* The aggregate function implementation */
 
18203 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
18204 int iDistAddr; /* Address of OP_OpenEphemeral */
18205 } *aFunc;
18206 int nFunc; /* Number of entries in aFunc[] */
18207 u32 selId; /* Select to which this AggInfo belongs */
18208 };
18209
18210 /*
18211 ** Macros to compute aCol[] and aFunc[] register numbers.
18212 **
18213 ** These macros should not be used prior to the call to
18214 ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18215 ** The assert()s that are part of this macro verify that constraint.
18216 */
18217 #define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18218 #define AggInfoFuncReg(A,I) \
18219 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18220
18221 /*
18222 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18223 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18224 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18225 ** it uses less memory in the Expr object, which is a big memory user
@@ -19046,11 +19128,11 @@
19128 ** of the base register during check-constraint eval */
19129 int nLabel; /* The *negative* of the number of labels used */
19130 int nLabelAlloc; /* Number of slots in aLabel */
19131 int *aLabel; /* Space to hold the labels */
19132 ExprList *pConstExpr;/* Constant expressions */
19133 IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
19134 Token constraintName;/* Name of the constraint currently being parsed */
19135 yDbMask writeMask; /* Start a write transaction on these databases */
19136 yDbMask cookieMask; /* Bitmask of schema verified databases */
19137 int regRowid; /* Register holding rowid of CREATE TABLE entry */
19138 int regRoot; /* Register holding root page number for new objects */
@@ -20393,10 +20475,13 @@
20475 SQLITE_PRIVATE const char *sqlite3ErrName(int);
20476 #endif
20477
20478 #ifndef SQLITE_OMIT_DESERIALIZE
20479 SQLITE_PRIVATE int sqlite3MemdbInit(void);
20480 SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs*);
20481 #else
20482 # define sqlite3IsMemdb(X) 0
20483 #endif
20484
20485 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
20486 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
20487 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
@@ -20889,10 +20974,14 @@
20974 #endif
20975
20976 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
20977 SQLITE_PRIVATE int sqlite3KvvfsInit(void);
20978 #endif
20979
20980 #if defined(VDBE_PROFILE) || defined(SQLITE_PERFORMANCE_TRACE)
20981 SQLITE_PRIVATE sqlite3_uint64 sqlite3Hwtime(void);
20982 #endif
20983
20984 #endif /* SQLITEINT_H */
20985
20986 /************** End of sqliteInt.h *******************************************/
20987 /************** Begin file os_common.h ***************************************/
@@ -20931,105 +21020,10 @@
21020 ** Macros for performance tracing. Normally turned off. Only works
21021 ** on i486 hardware.
21022 */
21023 #ifdef SQLITE_PERFORMANCE_TRACE
21024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21025 static sqlite_uint64 g_start;
21026 static sqlite_uint64 g_elapsed;
21027 #define TIMER_START g_start=sqlite3Hwtime()
21028 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21029 #define TIMER_ELAPSED g_elapsed
@@ -29200,11 +29194,11 @@
29194 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
29195 **
29196 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
29197 ** This provides a 256-byte safety margin for defense against 32-bit
29198 ** signed integer overflow bugs when computing memory allocation sizes.
29199 ** Paranoid applications might want to reduce the maximum allocation size
29200 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
29201 ** or even smaller would be reasonable upper bounds on the size of a memory
29202 ** allocations for most applications.
29203 */
29204 #ifndef SQLITE_MAX_ALLOCATION_SIZE
@@ -29714,13 +29708,18 @@
29708 ** SQL statement. Make a copy of this phrase in space obtained form
29709 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
29710 */
29711 SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
29712 int n;
29713 #ifdef SQLITE_DEBUG
29714 /* Because of the way the parser works, the span is guaranteed to contain
29715 ** at least one non-space character */
29716 for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]<zEnd ); }
29717 #endif
29718 while( sqlite3Isspace(zStart[0]) ) zStart++;
29719 n = (int)(zEnd - zStart);
29720 while( sqlite3Isspace(zStart[n-1]) ) n--;
29721 return sqlite3DbStrNDup(db, zStart, n);
29722 }
29723
29724 /*
29725 ** Free any prior content in *pz and replace it with a copy of zNew.
@@ -31698,11 +31697,11 @@
31697 if( pExpr==0 ){
31698 sqlite3TreeViewLine(pView, "nil");
31699 sqlite3TreeViewPop(&pView);
31700 return;
31701 }
31702 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){
31703 StrAccum x;
31704 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
31705 sqlite3_str_appendf(&x, " fg.af=%x.%c",
31706 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
31707 if( ExprHasProperty(pExpr, EP_OuterON) ){
@@ -31715,10 +31714,13 @@
31714 sqlite3_str_appendf(&x, " DDL");
31715 }
31716 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
31717 sqlite3_str_appendf(&x, " IMMUTABLE");
31718 }
31719 if( pExpr->pAggInfo!=0 ){
31720 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg);
31721 }
31722 sqlite3StrAccumFinish(&x);
31723 }else{
31724 zFlgs[0] = 0;
31725 }
31726 switch( pExpr->op ){
@@ -35193,10 +35195,106 @@
35195 i += pIn[i+1];
35196 }while( i<mx );
35197 return 0;
35198 }
35199
35200 /*
35201 ** High-resolution hardware timer used for debugging and testing only.
35202 */
35203 #if defined(VDBE_PROFILE) || defined(SQLITE_PERFORMANCE_TRACE)
35204 /************** Include hwtime.h in the middle of util.c *********************/
35205 /************** Begin file hwtime.h ******************************************/
35206 /*
35207 ** 2008 May 27
35208 **
35209 ** The author disclaims copyright to this source code. In place of
35210 ** a legal notice, here is a blessing:
35211 **
35212 ** May you do good and not evil.
35213 ** May you find forgiveness for yourself and forgive others.
35214 ** May you share freely, never taking more than you give.
35215 **
35216 ******************************************************************************
35217 **
35218 ** This file contains inline asm code for retrieving "high-performance"
35219 ** counters for x86 and x86_64 class CPUs.
35220 */
35221 #ifndef SQLITE_HWTIME_H
35222 #define SQLITE_HWTIME_H
35223
35224 /*
35225 ** The following routine only works on pentium-class (or newer) processors.
35226 ** It uses the RDTSC opcode to read the cycle count value out of the
35227 ** processor and returns that value. This can be used for high-res
35228 ** profiling.
35229 */
35230 #if !defined(__STRICT_ANSI__) && \
35231 (defined(__GNUC__) || defined(_MSC_VER)) && \
35232 (defined(i386) || defined(__i386__) || defined(_M_IX86))
35233
35234 #if defined(__GNUC__)
35235
35236 __inline__ sqlite_uint64 sqlite3Hwtime(void){
35237 unsigned int lo, hi;
35238 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
35239 return (sqlite_uint64)hi << 32 | lo;
35240 }
35241
35242 #elif defined(_MSC_VER)
35243
35244 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
35245 __asm {
35246 rdtsc
35247 ret ; return value at EDX:EAX
35248 }
35249 }
35250
35251 #endif
35252
35253 #elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
35254
35255 __inline__ sqlite_uint64 sqlite3Hwtime(void){
35256 unsigned long val;
35257 __asm__ __volatile__ ("rdtsc" : "=A" (val));
35258 return val;
35259 }
35260
35261 #elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
35262
35263 __inline__ sqlite_uint64 sqlite3Hwtime(void){
35264 unsigned long long retval;
35265 unsigned long junk;
35266 __asm__ __volatile__ ("\n\
35267 1: mftbu %1\n\
35268 mftb %L0\n\
35269 mftbu %0\n\
35270 cmpw %0,%1\n\
35271 bne 1b"
35272 : "=r" (retval), "=r" (junk));
35273 return retval;
35274 }
35275
35276 #else
35277
35278 /*
35279 ** asm() is needed for hardware timing support. Without asm(),
35280 ** disable the sqlite3Hwtime() routine.
35281 **
35282 ** sqlite3Hwtime() is only used for some obscure debugging
35283 ** and analysis configurations, not in any deliverable, so this
35284 ** should not be a great loss.
35285 */
35286 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
35287
35288 #endif
35289
35290 #endif /* !defined(SQLITE_HWTIME_H) */
35291
35292 /************** End of hwtime.h **********************************************/
35293 /************** Continuing where we left off in util.c ***********************/
35294 #endif
35295
35296 /************** End of util.c ************************************************/
35297 /************** Begin file hash.c ********************************************/
35298 /*
35299 ** 2001 September 22
35300 **
@@ -35727,11 +35825,13 @@
35825 int isJournal; /* True if this is a journal file */
35826 unsigned int nJrnl; /* Space allocated for aJrnl[] */
35827 char *aJrnl; /* Journal content */
35828 int szPage; /* Last known page size */
35829 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35830 char *aData; /* Buffer to hold page data */
35831 };
35832 #define SQLITE_KVOS_SZ 133073
35833
35834 /*
35835 ** Methods for KVVfsFile
35836 */
35837 static int kvvfsClose(sqlite3_file*);
@@ -36168,10 +36268,11 @@
36268 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36269
36270 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36271 pFile->isJournal ? "journal" : "db"));
36272 sqlite3_free(pFile->aJrnl);
36273 sqlite3_free(pFile->aData);
36274 return SQLITE_OK;
36275 }
36276
36277 /*
36278 ** Read from the -journal file.
@@ -36216,11 +36317,11 @@
36317 ){
36318 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36319 unsigned int pgno;
36320 int got, n;
36321 char zKey[30];
36322 char *aData = pFile->aData;
36323 assert( iOfst>=0 );
36324 assert( iAmt>=0 );
36325 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36326 if( iOfst+iAmt>=512 ){
36327 if( (iOfst % iAmt)!=0 ){
@@ -36233,19 +36334,20 @@
36334 pgno = 1 + iOfst/iAmt;
36335 }else{
36336 pgno = 1;
36337 }
36338 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36339 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
36340 aData, SQLITE_KVOS_SZ-1);
36341 if( got<0 ){
36342 n = 0;
36343 }else{
36344 aData[got] = 0;
36345 if( iOfst+iAmt<512 ){
36346 int k = iOfst+iAmt;
36347 aData[k*2] = 0;
36348 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
36349 if( n>=iOfst+iAmt ){
36350 memcpy(zBuf, &aData[2000+iOfst], iAmt);
36351 n = iAmt;
36352 }else{
36353 n = 0;
@@ -36300,11 +36402,11 @@
36402 sqlite_int64 iOfst
36403 ){
36404 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36405 unsigned int pgno;
36406 char zKey[30];
36407 char *aData = pFile->aData;
36408 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36409 assert( iAmt>=512 && iAmt<=65536 );
36410 assert( (iAmt & (iAmt-1))==0 );
36411 assert( pFile->szPage<0 || pFile->szPage==iAmt );
36412 pFile->szPage = iAmt;
@@ -36508,10 +36610,14 @@
36610 }
36611 if( zName[0]=='s' ){
36612 pFile->zClass = "session";
36613 }else{
36614 pFile->zClass = "local";
36615 }
36616 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
36617 if( pFile->aData==0 ){
36618 return SQLITE_NOMEM;
36619 }
36620 pFile->aJrnl = 0;
36621 pFile->nJrnl = 0;
36622 pFile->szPage = -1;
36623 pFile->szDb = -1;
@@ -43326,11 +43432,11 @@
43432 ** requested from the underlying operating system, a number which
43433 ** might be greater than or equal to the argument, but not less
43434 ** than the argument.
43435 */
43436 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
43437 #if OS_VXWORKS || _POSIX_C_SOURCE >= 199309L
43438 struct timespec sp;
43439
43440 sp.tv_sec = microseconds / 1000000;
43441 sp.tv_nsec = (microseconds % 1000000) * 1000;
43442 nanosleep(&sp, NULL);
@@ -51816,10 +51922,17 @@
51922 sqlite3_free(pData);
51923 }
51924 sqlite3_mutex_leave(db->mutex);
51925 return rc;
51926 }
51927
51928 /*
51929 ** Return true if the VFS is the memvfs.
51930 */
51931 SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
51932 return pVfs==&memdb_vfs;
51933 }
51934
51935 /*
51936 ** This routine is called when the extension is loaded.
51937 ** Register the new VFS.
51938 */
@@ -62136,11 +62249,15 @@
62249 ** The return value to this routine is always safe to use with
62250 ** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
62251 */
62252 SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
62253 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
62254 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
62255 return &zFake[4];
62256 }else{
62257 return pPager->zFilename;
62258 }
62259 }
62260
62261 /*
62262 ** Return the VFS structure for the pager.
62263 */
@@ -67719,13 +67836,13 @@
67836 ** within an expression that is an argument to another macro
67837 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
67838 ** So, this macro is defined instead.
67839 */
67840 #ifndef SQLITE_OMIT_AUTOVACUUM
67841 #define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
67842 #else
67843 #define ISAUTOVACUUM(pBt) 0
67844 #endif
67845
67846
67847 /*
67848 ** This structure is passed around through all the sanity checking routines
@@ -69973,66 +70090,71 @@
70090 ** and initialize fields of the MemPage structure accordingly.
70091 **
70092 ** Only the following combinations are supported. Anything different
70093 ** indicates a corrupt database files:
70094 **
70095 ** PTF_ZERODATA (0x02, 2)
70096 ** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
70097 ** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
70098 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
70099 */
70100 static int decodeFlags(MemPage *pPage, int flagByte){
70101 BtShared *pBt; /* A copy of pPage->pBt */
70102
70103 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
70104 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 
 
 
70105 pBt = pPage->pBt;
70106 pPage->max1bytePayload = pBt->max1bytePayload;
70107 if( flagByte>=(PTF_ZERODATA | PTF_LEAF) ){
70108 pPage->childPtrSize = 0;
70109 pPage->leaf = 1;
70110 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF) ){
 
 
 
 
70111 pPage->intKeyLeaf = 1;
70112 pPage->xCellSize = cellSizePtrTableLeaf;
70113 pPage->xParseCell = btreeParseCellPtr;
70114 pPage->intKey = 1;
70115 pPage->maxLocal = pBt->maxLeaf;
70116 pPage->minLocal = pBt->minLeaf;
70117 }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){
70118 pPage->intKey = 0;
70119 pPage->intKeyLeaf = 0;
70120 pPage->xCellSize = cellSizePtr;
70121 pPage->xParseCell = btreeParseCellPtrIndex;
70122 pPage->maxLocal = pBt->maxLocal;
70123 pPage->minLocal = pBt->minLocal;
70124 }else{
70125 pPage->intKey = 0;
70126 pPage->intKeyLeaf = 0;
70127 pPage->xCellSize = cellSizePtr;
70128 pPage->xParseCell = btreeParseCellPtrIndex;
70129 return SQLITE_CORRUPT_PAGE(pPage);
70130 }
70131 }else{
70132 pPage->childPtrSize = 4;
70133 pPage->leaf = 0;
70134 if( flagByte==(PTF_ZERODATA) ){
70135 pPage->intKey = 0;
70136 pPage->intKeyLeaf = 0;
70137 pPage->xCellSize = cellSizePtr;
70138 pPage->xParseCell = btreeParseCellPtrIndex;
70139 pPage->maxLocal = pBt->maxLocal;
70140 pPage->minLocal = pBt->minLocal;
70141 }else if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
70142 pPage->intKeyLeaf = 0;
70143 pPage->xCellSize = cellSizePtrNoPayload;
70144 pPage->xParseCell = btreeParseCellPtrNoPayload;
70145 pPage->intKey = 1;
70146 pPage->maxLocal = pBt->maxLeaf;
70147 pPage->minLocal = pBt->minLeaf;
70148 }else{
70149 pPage->intKey = 0;
70150 pPage->intKeyLeaf = 0;
70151 pPage->xCellSize = cellSizePtr;
70152 pPage->xParseCell = btreeParseCellPtrIndex;
70153 return SQLITE_CORRUPT_PAGE(pPage);
70154 }
70155 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70156 return SQLITE_OK;
70157 }
70158
70159 /*
70160 ** Compute the amount of freespace on the page. In other words, fill
@@ -73568,13 +73690,29 @@
73690
73691 /* Move the cursor to the last entry in the table. Return SQLITE_OK
73692 ** on success. Set *pRes to 0 if the cursor actually points to something
73693 ** or set *pRes to 1 if the table is empty.
73694 */
73695 static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){
73696 int rc = moveToRoot(pCur);
73697 if( rc==SQLITE_OK ){
73698 assert( pCur->eState==CURSOR_VALID );
73699 *pRes = 0;
73700 rc = moveToRightmost(pCur);
73701 if( rc==SQLITE_OK ){
73702 pCur->curFlags |= BTCF_AtLast;
73703 }else{
73704 pCur->curFlags &= ~BTCF_AtLast;
73705 }
73706 }else if( rc==SQLITE_EMPTY ){
73707 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
73708 *pRes = 1;
73709 rc = SQLITE_OK;
73710 }
73711 return rc;
73712 }
73713 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 
 
73714 assert( cursorOwnsBtShared(pCur) );
73715 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
73716
73717 /* If the cursor already points to the last entry, this is a no-op. */
73718 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
@@ -73591,27 +73729,11 @@
73729 assert( pCur->pPage->leaf );
73730 #endif
73731 *pRes = 0;
73732 return SQLITE_OK;
73733 }
73734 return btreeLast(pCur, pRes);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73735 }
73736
73737 /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
73738 ** table near the key intKey. Return a success code.
73739 **
@@ -74674,11 +74796,11 @@
74796 }
74797
74798 /* If the database supports auto-vacuum, write an entry in the pointer-map
74799 ** to indicate that the page is free.
74800 */
74801 if( ISAUTOVACUUM(pBt) ){
74802 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
74803 if( rc ) goto freepage_out;
74804 }
74805
74806 /* Now manipulate the actual database free-list structure. There are two
@@ -75114,28 +75236,24 @@
75236 ** pTemp is not null. Regardless of pTemp, allocate a new entry
75237 ** in pPage->apOvfl[] and make it point to the cell content (either
75238 ** in pTemp or the original pCell) and also record its index.
75239 ** Allocating a new entry in pPage->aCell[] implies that
75240 ** pPage->nOverflow is incremented.
 
 
75241 */
75242 static int insertCell(
75243 MemPage *pPage, /* Page into which we are copying */
75244 int i, /* New cell becomes the i-th cell of the page */
75245 u8 *pCell, /* Content of the new cell */
75246 int sz, /* Bytes of content in pCell */
75247 u8 *pTemp, /* Temp storage space for pCell, if needed */
75248 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
 
75249 ){
75250 int idx = 0; /* Where to write new cell content in data[] */
75251 int j; /* Loop counter */
75252 u8 *data; /* The content of the whole page */
75253 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
75254
 
75255 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
75256 assert( MX_CELL(pPage->pBt)<=10921 );
75257 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
75258 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
75259 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
@@ -75166,18 +75284,17 @@
75284 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
75285 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
75286 }else{
75287 int rc = sqlite3PagerWrite(pPage->pDbPage);
75288 if( rc!=SQLITE_OK ){
75289 return rc;
 
75290 }
75291 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75292 data = pPage->aData;
75293 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
75294 rc = allocateSpace(pPage, sz, &idx);
75295 if( rc ){ return rc; }
75296 /* The allocateSpace() routine guarantees the following properties
75297 ** if it returns successfully */
75298 assert( idx >= 0 );
75299 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
75300 assert( idx+sz <= (int)pPage->pBt->usableSize );
@@ -75200,17 +75317,20 @@
75317 /* increment the cell count */
75318 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
75319 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
75320 #ifndef SQLITE_OMIT_AUTOVACUUM
75321 if( pPage->pBt->autoVacuum ){
75322 int rc2 = SQLITE_OK;
75323 /* The cell may contain a pointer to an overflow page. If so, write
75324 ** the entry for the overflow page into the pointer map.
75325 */
75326 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
75327 if( rc2 ) return rc2;
75328 }
75329 #endif
75330 }
75331 return SQLITE_OK;
75332 }
75333
75334 /*
75335 ** The following parameters determine how many adjacent pages get involved
75336 ** in a balancing operation. NN is the number of neighbors on either side
@@ -75307,18 +75427,20 @@
75427 /*
75428 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
75429 ** computed.
75430 */
75431 static void populateCellCache(CellArray *p, int idx, int N){
75432 MemPage *pRef = p->pRef;
75433 u16 *szCell = p->szCell;
75434 assert( idx>=0 && idx+N<=p->nCell );
75435 while( N>0 ){
75436 assert( p->apCell[idx]!=0 );
75437 if( szCell[idx]==0 ){
75438 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
75439 }else{
75440 assert( CORRUPT_DB ||
75441 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
75442 }
75443 idx++;
75444 N--;
75445 }
75446 }
@@ -75516,12 +75638,12 @@
75638 u8 * const pEnd = &aData[pPg->pBt->usableSize];
75639 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
75640 int nRet = 0;
75641 int i;
75642 int iEnd = iFirst + nCell;
75643 u8 *pFree = 0; /* \__ Parameters for pending call to */
75644 int szFree = 0; /* / freeSpace() */
75645
75646 for(i=iFirst; i<iEnd; i++){
75647 u8 *pCell = pCArray->apCell[i];
75648 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
75649 int sz;
@@ -75538,10 +75660,13 @@
75660 szFree = sz;
75661 if( pFree+sz>pEnd ){
75662 return 0;
75663 }
75664 }else{
75665 /* The current cell is adjacent to and before the pFree cell.
75666 ** Combine the two regions into one to reduce the number of calls
75667 ** to freeSpace(). */
75668 pFree = pCell;
75669 szFree += sz;
75670 }
75671 nRet++;
75672 }
@@ -75745,11 +75870,11 @@
75870 ** of the parent page are still manipulated by thh code below.
75871 ** That is Ok, at this point the parent page is guaranteed to
75872 ** be marked as dirty. Returning an error code will cause a
75873 ** rollback, undoing any changes made to the parent page.
75874 */
75875 if( ISAUTOVACUUM(pBt) ){
75876 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
75877 if( szCell>pNew->minLocal ){
75878 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
75879 }
75880 }
@@ -75773,12 +75898,12 @@
75898 pStop = &pCell[9];
75899 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
75900
75901 /* Insert the new divider cell into pParent. */
75902 if( rc==SQLITE_OK ){
75903 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
75904 0, pPage->pgno);
75905 }
75906
75907 /* Set the right-child pointer of pParent to point to the new page. */
75908 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
75909
@@ -75883,11 +76008,11 @@
76008 }
76009
76010 /* If this is an auto-vacuum database, update the pointer-map entries
76011 ** for any b-tree or overflow pages that pTo now contains the pointers to.
76012 */
76013 if( ISAUTOVACUUM(pBt) ){
76014 *pRC = setChildPtrmaps(pTo);
76015 }
76016 }
76017 }
76018
@@ -76307,19 +76432,21 @@
76432
76433 r = cntNew[i-1] - 1;
76434 d = r + 1 - leafData;
76435 (void)cachedCellSize(&b, d);
76436 do{
76437 int szR, szD;
76438 assert( d<nMaxCells );
76439 assert( r<nMaxCells );
76440 szR = cachedCellSize(&b, r);
76441 szD = b.szCell[d];
76442 if( szRight!=0
76443 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
76444 break;
76445 }
76446 szRight += szD + 2;
76447 szLeft -= szR + 2;
76448 cntNew[i-1] = r;
76449 r--;
76450 d--;
76451 }while( r>=0 );
76452 szNew[i] = szRight;
@@ -76369,11 +76496,11 @@
76496 apNew[i] = pNew;
76497 nNew++;
76498 cntOld[i] = b.nCell;
76499
76500 /* Set the pointer-map entry for the new sibling page. */
76501 if( ISAUTOVACUUM(pBt) ){
76502 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
76503 if( rc!=SQLITE_OK ){
76504 goto balance_cleanup;
76505 }
76506 }
@@ -76462,11 +76589,11 @@
76589 ** If the sibling pages are not leaves, then the pointer map entry
76590 ** associated with the right-child of each sibling may also need to be
76591 ** updated. This happens below, after the sibling pages have been
76592 ** populated, not here.
76593 */
76594 if( ISAUTOVACUUM(pBt) ){
76595 MemPage *pOld;
76596 MemPage *pNew = pOld = apNew[0];
76597 int cntOldNext = pNew->nCell + pNew->nOverflow;
76598 int iNew = 0;
76599 int iOld = 0;
@@ -76559,11 +76686,11 @@
76686 pSrcEnd = b.apEnd[k];
76687 if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){
76688 rc = SQLITE_CORRUPT_BKPT;
76689 goto balance_cleanup;
76690 }
76691 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
76692 if( rc!=SQLITE_OK ) goto balance_cleanup;
76693 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76694 }
76695
76696 /* Now update the actual sibling pages. The order in which they are updated
@@ -76655,11 +76782,11 @@
76782 - apNew[0]->nCell*2)
76783 || rc!=SQLITE_OK
76784 );
76785 copyNodeContent(apNew[0], pParent, &rc);
76786 freePage(apNew[0], &rc);
76787 }else if( ISAUTOVACUUM(pBt) && !leafCorrection ){
76788 /* Fix the pointer map entries associated with the right-child of each
76789 ** sibling page. All other pointer map entries have already been taken
76790 ** care of. */
76791 for(i=0; i<nNew; i++){
76792 u32 key = get4byte(&apNew[i]->aData[8]);
@@ -76676,11 +76803,11 @@
76803 for(i=nNew; i<nOld; i++){
76804 freePage(apOld[i], &rc);
76805 }
76806
76807 #if 0
76808 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
76809 /* The ptrmapCheckPages() contains assert() statements that verify that
76810 ** all pointer map pages are set correctly. This is helpful while
76811 ** debugging. This is usually disabled because a corrupt database may
76812 ** cause an assert() statement to fail. */
76813 ptrmapCheckPages(apNew, nNew);
@@ -76738,11 +76865,11 @@
76865 */
76866 rc = sqlite3PagerWrite(pRoot->pDbPage);
76867 if( rc==SQLITE_OK ){
76868 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
76869 copyNodeContent(pRoot, pChild, &rc);
76870 if( ISAUTOVACUUM(pBt) ){
76871 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
76872 }
76873 }
76874 if( rc ){
76875 *ppChild = 0;
@@ -77070,11 +77197,10 @@
77197 int loc = seekResult; /* -1: before desired location +1: after */
77198 int szNew = 0;
77199 int idx;
77200 MemPage *pPage;
77201 Btree *p = pCur->pBtree;
 
77202 unsigned char *oldCell;
77203 unsigned char *newCell = 0;
77204
77205 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
77206 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
@@ -77089,11 +77215,11 @@
77215 ** that the cursor is already where it needs to be and returns without
77216 ** doing any work. To avoid thwarting these optimizations, it is important
77217 ** not to clear the cursor here.
77218 */
77219 if( pCur->curFlags & BTCF_Multiple ){
77220 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
77221 if( rc ) return rc;
77222 if( loc && pCur->iPage<0 ){
77223 /* This can only happen if the schema is corrupt such that there is more
77224 ** than one table or index with the same root page as used by the cursor.
77225 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
@@ -77113,12 +77239,12 @@
77239 if( rc && rc!=SQLITE_EMPTY ) return rc;
77240 }
77241
77242 assert( cursorOwnsBtShared(pCur) );
77243 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
77244 && p->pBt->inTransaction==TRANS_WRITE
77245 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
77246 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
77247
77248 /* Assert that the caller has been consistent. If this cursor was opened
77249 ** expecting an index b-tree, then the caller should be inserting blob
77250 ** keys with no associated data. If the cursor was opened expecting an
@@ -77231,30 +77357,32 @@
77357
77358 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
77359 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
77360 loc==0 ? "overwrite" : "new entry"));
77361 assert( pPage->isInit || CORRUPT_DB );
77362 newCell = p->pBt->pTmpSpace;
77363 assert( newCell!=0 );
77364 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
77365 if( flags & BTREE_PREFORMAT ){
77366 rc = SQLITE_OK;
77367 szNew = p->pBt->nPreformatSize;
77368 if( szNew<4 ) szNew = 4;
77369 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
77370 CellInfo info;
77371 pPage->xParseCell(pPage, newCell, &info);
77372 if( info.nPayload!=info.nLocal ){
77373 Pgno ovfl = get4byte(&newCell[szNew-4]);
77374 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
77375 if( NEVER(rc) ) goto end_insert;
77376 }
77377 }
77378 }else{
77379 rc = fillInCell(pPage, newCell, pX, &szNew);
77380 if( rc ) goto end_insert;
77381 }
 
77382 assert( szNew==pPage->xCellSize(pPage, newCell) );
77383 assert( szNew <= MX_CELL_SIZE(p->pBt) );
77384 idx = pCur->ix;
77385 if( loc==0 ){
77386 CellInfo info;
77387 assert( idx>=0 );
77388 if( idx>=pPage->nCell ){
@@ -77270,11 +77398,11 @@
77398 }
77399 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
77400 testcase( pCur->curFlags & BTCF_ValidOvfl );
77401 invalidateOverflowCache(pCur);
77402 if( info.nSize==szNew && info.nLocal==info.nPayload
77403 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
77404 ){
77405 /* Overwrite the old cell with the new if they are the same size.
77406 ** We could also try to do this if the old cell is smaller, then add
77407 ** the leftover space to the free list. But experiments show that
77408 ** doing that is no faster then skipping this optimization and just
@@ -77300,11 +77428,11 @@
77428 idx = ++pCur->ix;
77429 pCur->curFlags &= ~BTCF_ValidNKey;
77430 }else{
77431 assert( pPage->leaf );
77432 }
77433 rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
77434 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
77435 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
77436
77437 /* If no error has occurred and pPage has an overflow cell, call balance()
77438 ** to redistribute the cells within the tree. Since balance() may move
@@ -77373,11 +77501,10 @@
77501 ** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
77502 **
77503 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
77504 */
77505 SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
 
77506 BtShared *pBt = pDest->pBt;
77507 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
77508 const u8 *aIn; /* Pointer to next input buffer */
77509 u32 nIn; /* Size of input buffer aIn[] */
77510 u32 nRem; /* Bytes of data still to copy */
@@ -77396,11 +77523,13 @@
77523 }
77524 nRem = pSrc->info.nPayload;
77525 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
77526 memcpy(aOut, aIn, nIn);
77527 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
77528 return SQLITE_OK;
77529 }else{
77530 int rc = SQLITE_OK;
77531 Pager *pSrcPager = pSrc->pBt->pPager;
77532 u8 *pPgnoOut = 0;
77533 Pgno ovflIn = 0;
77534 DbPage *pPageIn = 0;
77535 MemPage *pPageOut = 0;
@@ -77448,11 +77577,11 @@
77577 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
77578 Pgno pgnoNew;
77579 MemPage *pNew = 0;
77580 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
77581 put4byte(pPgnoOut, pgnoNew);
77582 if( ISAUTOVACUUM(pBt) && pPageOut ){
77583 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
77584 }
77585 releasePage(pPageOut);
77586 pPageOut = pNew;
77587 if( pPageOut ){
@@ -77464,13 +77593,12 @@
77593 }
77594 }while( nRem>0 && rc==SQLITE_OK );
77595
77596 releasePage(pPageOut);
77597 sqlite3PagerUnref(pPageIn);
77598 return rc;
77599 }
 
 
77600 }
77601
77602 /*
77603 ** Delete the entry that the cursor is pointing to.
77604 **
@@ -77621,11 +77749,11 @@
77749 assert( MX_CELL_SIZE(pBt) >= nCell );
77750 pTmp = pBt->pTmpSpace;
77751 assert( pTmp!=0 );
77752 rc = sqlite3PagerWrite(pLeaf->pDbPage);
77753 if( rc==SQLITE_OK ){
77754 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
77755 }
77756 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
77757 if( rc ) return rc;
77758 }
77759
@@ -79144,10 +79272,21 @@
79272
79273 /*
79274 ** Return the size of the header added to each page by this module.
79275 */
79276 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
79277
79278 /*
79279 ** If no transaction is active and the database is not a temp-db, clear
79280 ** the in-memory pager cache.
79281 */
79282 SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree *p){
79283 BtShared *pBt = p->pBt;
79284 if( pBt->inTransaction==TRANS_NONE ){
79285 sqlite3PagerClearCache(pBt->pPager);
79286 }
79287 }
79288
79289 #if !defined(SQLITE_OMIT_SHARED_CACHE)
79290 /*
79291 ** Return true if the Btree passed as the only argument is sharable.
79292 */
@@ -83384,11 +83523,11 @@
83523 assert( n!=P4_INT32 && n!=P4_VTAB );
83524 assert( n<=0 );
83525 if( p->db->mallocFailed ){
83526 freeP4(p->db, n, pP4);
83527 }else{
83528 assert( pP4!=0 || n==P4_DYNAMIC );
83529 assert( p->nOp>0 );
83530 pOp = &p->aOp[p->nOp-1];
83531 assert( pOp->p4type==P4_NOTUSED );
83532 pOp->p4type = n;
83533 pOp->p4.p = pP4;
@@ -87760,11 +87899,14 @@
87899 void (*xDel)(void *),
87900 unsigned char enc
87901 ){
87902 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87903 assert( xDel!=SQLITE_DYNAMIC );
87904 if( enc!=SQLITE_UTF8 ){
87905 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
87906 n &= ~(u64)1;
87907 }
87908 if( n>0x7fffffff ){
87909 (void)invokeValueDestructor(z, xDel, pCtx);
87910 }else{
87911 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
87912 }
@@ -87775,29 +87917,29 @@
87917 const void *z,
87918 int n,
87919 void (*xDel)(void *)
87920 ){
87921 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87922 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel);
87923 }
87924 SQLITE_API void sqlite3_result_text16be(
87925 sqlite3_context *pCtx,
87926 const void *z,
87927 int n,
87928 void (*xDel)(void *)
87929 ){
87930 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87931 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel);
87932 }
87933 SQLITE_API void sqlite3_result_text16le(
87934 sqlite3_context *pCtx,
87935 const void *z,
87936 int n,
87937 void (*xDel)(void *)
87938 ){
87939 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87940 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel);
87941 }
87942 #endif /* SQLITE_OMIT_UTF16 */
87943 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
87944 Mem *pOut = pCtx->pOut;
87945 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
@@ -88858,22 +89000,25 @@
89000 sqlite3_uint64 nData,
89001 void (*xDel)(void*),
89002 unsigned char enc
89003 ){
89004 assert( xDel!=SQLITE_DYNAMIC );
89005 if( enc!=SQLITE_UTF8 ){
89006 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
89007 nData &= ~(u16)1;
89008 }
89009 return bindText(pStmt, i, zData, nData, xDel, enc);
89010 }
89011 #ifndef SQLITE_OMIT_UTF16
89012 SQLITE_API int sqlite3_bind_text16(
89013 sqlite3_stmt *pStmt,
89014 int i,
89015 const void *zData,
89016 int n,
89017 void (*xDel)(void*)
89018 ){
89019 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE);
89020 }
89021 #endif /* SQLITE_OMIT_UTF16 */
89022 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
89023 int rc;
89024 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
@@ -90242,20 +90387,10 @@
90387 #else
90388 # define REGISTER_TRACE(R,M)
90389 #endif
90390
90391
 
 
 
 
 
 
 
 
 
 
90392 #ifndef NDEBUG
90393 /*
90394 ** This function is only called from within an assert() expression. It
90395 ** checks that the sqlite3.nTransaction variable is correctly set to
90396 ** the number of non-transaction savepoints currently in the
@@ -95203,10 +95338,11 @@
95338 x.nZero = pData->u.nZero;
95339 }else{
95340 x.nZero = 0;
95341 }
95342 x.pKey = 0;
95343 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
95344 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
95345 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
95346 seekResult
95347 );
95348 pC->deferredMoveto = 0;
@@ -105097,11 +105233,11 @@
105233 pExpr = pExpr->pLeft;
105234 assert( pExpr!=0 );
105235 }
105236 op = pExpr->op;
105237 if( op==TK_REGISTER ) op = pExpr->op2;
105238 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
105239 assert( ExprUseYTab(pExpr) );
105240 assert( pExpr->y.pTab!=0 );
105241 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
105242 }
105243 if( op==TK_SELECT ){
@@ -105217,11 +105353,13 @@
105353 CollSeq *pColl = 0;
105354 const Expr *p = pExpr;
105355 while( p ){
105356 int op = p->op;
105357 if( op==TK_REGISTER ) op = p->op2;
105358 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
105359 || op==TK_COLUMN || op==TK_TRIGGER
105360 ){
105361 int j;
105362 assert( ExprUseYTab(p) );
105363 assert( p->y.pTab!=0 );
105364 if( (j = p->iColumn)>=0 ){
105365 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
@@ -109081,11 +109219,11 @@
109219 }
109220 return target;
109221 }
109222
109223 /*
109224 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
109225 ** If it is, then resolve the expression by reading from the index and
109226 ** return the register into which the value has been read. If pExpr is
109227 ** not an indexed expression, then return negative.
109228 */
109229 static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
@@ -109093,11 +109231,11 @@
109231 Expr *pExpr, /* The expression to potentially bypass */
109232 int target /* Where to store the result of the expression */
109233 ){
109234 IndexedExpr *p;
109235 Vdbe *v;
109236 for(p=pParse->pIdxEpr; p; p=p->pIENext){
109237 int iDataCur = p->iDataCur;
109238 if( iDataCur<0 ) continue;
109239 if( pParse->iSelfTab ){
109240 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109241 iDataCur = -1;
@@ -109113,14 +109251,14 @@
109251 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109252 VdbeCoverage(v);
109253 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109254 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109255 sqlite3VdbeGoto(v, 0);
109256 p = pParse->pIdxEpr;
109257 pParse->pIdxEpr = 0;
109258 sqlite3ExprCode(pParse, pExpr, target);
109259 pParse->pIdxEpr = p;
109260 sqlite3VdbeJumpHere(v, addr+2);
109261 }else{
109262 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109263 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109264 }
@@ -109155,11 +109293,11 @@
109293 assert( v!=0 );
109294
109295 expr_code_doover:
109296 if( pExpr==0 ){
109297 op = TK_NULL;
109298 }else if( pParse->pIdxEpr!=0
109299 && !ExprHasProperty(pExpr, EP_Leaf)
109300 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109301 ){
109302 return r1;
109303 }else{
@@ -109172,26 +109310,32 @@
109310 struct AggInfo_col *pCol;
109311 assert( pAggInfo!=0 );
109312 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109313 pCol = &pAggInfo->aCol[pExpr->iAgg];
109314 if( !pAggInfo->directMode ){
109315 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
 
109316 }else if( pAggInfo->useSortingIdx ){
109317 Table *pTab = pCol->pTab;
109318 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109319 pCol->iSorterColumn, target);
109320 if( pTab==0 ){
109321 /* No comment added */
109322 }else if( pCol->iColumn<0 ){
109323 VdbeComment((v,"%s.rowid",pTab->zName));
109324 }else{
109325 VdbeComment((v,"%s.%s",
109326 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
109327 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
109328 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
109329 }
109330 }
109331 return target;
109332 }else if( pExpr->y.pTab==0 ){
109333 /* This case happens when the argument to an aggregate function
109334 ** is rewritten by aggregateConvertIndexedExprRefToColumn() */
109335 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
109336 return target;
109337 }
109338 /* Otherwise, fall thru into the TK_COLUMN case */
109339 /* no break */ deliberate_fall_through
109340 }
109341 case TK_COLUMN: {
@@ -109485,11 +109629,11 @@
109629 || NEVER(pExpr->iAgg>=pInfo->nFunc)
109630 ){
109631 assert( !ExprHasProperty(pExpr, EP_IntValue) );
109632 sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr);
109633 }else{
109634 return AggInfoFuncReg(pInfo, pExpr->iAgg);
109635 }
109636 break;
109637 }
109638 case TK_FUNCTION: {
109639 ExprList *pFarg; /* List of function arguments */
@@ -109774,11 +109918,11 @@
109918 u8 okConstFactor = pParse->okConstFactor;
109919 AggInfo *pAggInfo = pExpr->pAggInfo;
109920 if( pAggInfo ){
109921 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109922 if( !pAggInfo->directMode ){
109923 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
109924 break;
109925 }
109926 if( pExpr->pAggInfo->useSortingIdx ){
109927 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109928 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
@@ -111285,10 +111429,76 @@
111429 &pInfo->nFunc,
111430 &i
111431 );
111432 return i;
111433 }
111434
111435 /*
111436 ** Search the AggInfo object for an aCol[] entry that has iTable and iColumn.
111437 ** Return the index in aCol[] of the entry that describes that column.
111438 **
111439 ** If no prior entry is found, create a new one and return -1. The
111440 ** new column will have an idex of pAggInfo->nColumn-1.
111441 */
111442 static void findOrCreateAggInfoColumn(
111443 Parse *pParse, /* Parsing context */
111444 AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */
111445 Expr *pExpr /* Expr describing the column to find or insert */
111446 ){
111447 struct AggInfo_col *pCol;
111448 int k;
111449
111450 assert( pAggInfo->iFirstReg==0 );
111451 pCol = pAggInfo->aCol;
111452 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
111453 if( pCol->iTable==pExpr->iTable
111454 && pCol->iColumn==pExpr->iColumn
111455 && pExpr->op!=TK_IF_NULL_ROW
111456 ){
111457 goto fix_up_expr;
111458 }
111459 }
111460 k = addAggInfoColumn(pParse->db, pAggInfo);
111461 if( k<0 ){
111462 /* OOM on resize */
111463 assert( pParse->db->mallocFailed );
111464 return;
111465 }
111466 pCol = &pAggInfo->aCol[k];
111467 assert( ExprUseYTab(pExpr) );
111468 pCol->pTab = pExpr->y.pTab;
111469 pCol->iTable = pExpr->iTable;
111470 pCol->iColumn = pExpr->iColumn;
111471 pCol->iSorterColumn = -1;
111472 pCol->pCExpr = pExpr;
111473 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
111474 int j, n;
111475 ExprList *pGB = pAggInfo->pGroupBy;
111476 struct ExprList_item *pTerm = pGB->a;
111477 n = pGB->nExpr;
111478 for(j=0; j<n; j++, pTerm++){
111479 Expr *pE = pTerm->pExpr;
111480 if( pE->op==TK_COLUMN
111481 && pE->iTable==pExpr->iTable
111482 && pE->iColumn==pExpr->iColumn
111483 ){
111484 pCol->iSorterColumn = j;
111485 break;
111486 }
111487 }
111488 }
111489 if( pCol->iSorterColumn<0 ){
111490 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
111491 }
111492 fix_up_expr:
111493 ExprSetVVAProperty(pExpr, EP_NoReduce);
111494 pExpr->pAggInfo = pAggInfo;
111495 if( pExpr->op==TK_COLUMN ){
111496 pExpr->op = TK_AGG_COLUMN;
111497 }
111498 pExpr->iAgg = (i16)k;
111499 }
111500
111501 /*
111502 ** This is the xExprCallback for a tree walker. It is used to
111503 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
111504 ** for additional information.
@@ -111299,11 +111509,40 @@
111509 Parse *pParse = pNC->pParse;
111510 SrcList *pSrcList = pNC->pSrcList;
111511 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
111512
111513 assert( pNC->ncFlags & NC_UAggInfo );
111514 assert( pAggInfo->iFirstReg==0 );
111515 switch( pExpr->op ){
111516 default: {
111517 IndexedExpr *pIEpr;
111518 Expr tmp;
111519 assert( pParse->iSelfTab==0 );
111520 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
111521 if( pParse->pIdxEpr==0 ) break;
111522 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
111523 int iDataCur = pIEpr->iDataCur;
111524 if( iDataCur<0 ) continue;
111525 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
111526 }
111527 if( pIEpr==0 ) break;
111528 if( NEVER(!ExprUseYTab(pExpr)) ) break;
111529
111530 /* If we reach this point, it means that expression pExpr can be
111531 ** translated into a reference to an index column as described by
111532 ** pIEpr.
111533 */
111534 memset(&tmp, 0, sizeof(tmp));
111535 tmp.op = TK_AGG_COLUMN;
111536 tmp.iTable = pIEpr->iIdxCur;
111537 tmp.iColumn = pIEpr->iIdxCol;
111538 findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp);
111539 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
111540 pExpr->pAggInfo = pAggInfo;
111541 pExpr->iAgg = tmp.iAgg;
111542 return WRC_Prune;
111543 }
111544 case TK_IF_NULL_ROW:
111545 case TK_AGG_COLUMN:
111546 case TK_COLUMN: {
111547 testcase( pExpr->op==TK_AGG_COLUMN );
111548 testcase( pExpr->op==TK_COLUMN );
@@ -111311,71 +111550,13 @@
111550 /* Check to see if the column is in one of the tables in the FROM
111551 ** clause of the aggregate query */
111552 if( ALWAYS(pSrcList!=0) ){
111553 SrcItem *pItem = pSrcList->a;
111554 for(i=0; i<pSrcList->nSrc; i++, pItem++){
 
111555 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
111556 if( pExpr->iTable==pItem->iCursor ){
111557 findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111558 break;
111559 } /* endif pExpr->iTable==pItem->iCursor */
111560 } /* end loop over pSrcList */
111561 }
111562 return WRC_Prune;
@@ -111401,11 +111582,10 @@
111582 i = addAggInfoFunc(pParse->db, pAggInfo);
111583 if( i>=0 ){
111584 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
111585 pItem = &pAggInfo->aFunc[i];
111586 pItem->pFExpr = pExpr;
 
111587 assert( ExprUseUToken(pExpr) );
111588 pItem->pFunc = sqlite3FindFunction(pParse->db,
111589 pExpr->u.zToken,
111590 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
111591 if( pExpr->flags & EP_Distinct ){
@@ -126002,21 +126182,19 @@
126182 default:
126183 return;
126184 }
126185 ans = log(x)/b;
126186 }else{
 
126187 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
126188 case 1:
126189 ans = log10(x);
 
126190 break;
126191 case 2:
126192 ans = log2(x);
 
126193 break;
126194 default:
126195 ans = log(x);
126196 break;
126197 }
126198 }
126199 sqlite3_result_double(context, ans);
126200 }
@@ -129565,10 +129743,11 @@
129743 /* no break */ deliberate_fall_through
129744 case OE_Rollback:
129745 case OE_Fail: {
129746 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
129747 pCol->zCnName);
129748 testcase( zMsg==0 && db->mallocFailed==0 );
129749 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
129750 onError, iReg);
129751 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
129752 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
129753 VdbeCoverage(v);
@@ -132296,11 +132475,11 @@
132475 0,
132476 0,
132477 #endif
132478 sqlite3_db_name,
132479 /* Version 3.40.0 and later */
132480 sqlite3_value_encoding
132481 };
132482
132483 /* True if x is the directory separator character
132484 */
132485 #if SQLITE_OS_WIN
@@ -139355,11 +139534,11 @@
139534 #endif
139535
139536 if( pParse->colNamesSet ) return;
139537 /* Column names are determined by the left-most term of a compound select */
139538 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
139539 TREETRACE(0x80,pParse,pSelect,("generating column names\n"));
139540 pTabList = pSelect->pSrc;
139541 pEList = pSelect->pEList;
139542 assert( v!=0 );
139543 assert( pTabList!=0 );
139544 pParse->colNamesSet = 1;
@@ -140141,11 +140320,11 @@
140320 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
140321 assert( !pPrior->pLimit );
140322 pPrior->iLimit = p->iLimit;
140323 pPrior->iOffset = p->iOffset;
140324 pPrior->pLimit = p->pLimit;
140325 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
140326 rc = sqlite3Select(pParse, pPrior, &dest);
140327 pPrior->pLimit = 0;
140328 if( rc ){
140329 goto multi_select_end;
140330 }
@@ -140159,11 +140338,11 @@
140338 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
140339 p->iLimit, p->iOffset+1, p->iOffset);
140340 }
140341 }
140342 ExplainQueryPlan((pParse, 1, "UNION ALL"));
140343 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
140344 rc = sqlite3Select(pParse, p, &dest);
140345 testcase( rc!=SQLITE_OK );
140346 pDelete = p->pPrior;
140347 p->pPrior = pPrior;
140348 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
@@ -140212,11 +140391,11 @@
140391
140392 /* Code the SELECT statements to our left
140393 */
140394 assert( !pPrior->pOrderBy );
140395 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
140396 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
140397 rc = sqlite3Select(pParse, pPrior, &uniondest);
140398 if( rc ){
140399 goto multi_select_end;
140400 }
140401
@@ -140232,11 +140411,11 @@
140411 pLimit = p->pLimit;
140412 p->pLimit = 0;
140413 uniondest.eDest = op;
140414 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
140415 sqlite3SelectOpName(p->op)));
140416 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
140417 rc = sqlite3Select(pParse, p, &uniondest);
140418 testcase( rc!=SQLITE_OK );
140419 assert( p->pOrderBy==0 );
140420 pDelete = p->pPrior;
140421 p->pPrior = pPrior;
@@ -140293,11 +140472,11 @@
140472 assert( p->pEList );
140473
140474 /* Code the SELECTs to our left into temporary table "tab1".
140475 */
140476 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
140477 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
140478 rc = sqlite3Select(pParse, pPrior, &intersectdest);
140479 if( rc ){
140480 goto multi_select_end;
140481 }
140482
@@ -140310,11 +140489,11 @@
140489 pLimit = p->pLimit;
140490 p->pLimit = 0;
140491 intersectdest.iSDParm = tab2;
140492 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
140493 sqlite3SelectOpName(p->op)));
140494 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
140495 rc = sqlite3Select(pParse, p, &intersectdest);
140496 testcase( rc!=SQLITE_OK );
140497 pDelete = p->pPrior;
140498 p->pPrior = pPrior;
140499 if( p->nSelectRow>pPrior->nSelectRow ){
@@ -141313,10 +141492,38 @@
141492 while( pSel->pPrior ){
141493 pSel = pSel->pPrior;
141494 }
141495 return pSel->pEList;
141496 }
141497
141498 /*
141499 ** Return true if any of the result-set columns in the compound query
141500 ** have incompatible affinities on one or more arms of the compound.
141501 */
141502 static int compoundHasDifferentAffinities(Select *p){
141503 int ii;
141504 ExprList *pList;
141505 assert( p!=0 );
141506 assert( p->pEList!=0 );
141507 assert( p->pPrior!=0 );
141508 pList = p->pEList;
141509 for(ii=0; ii<pList->nExpr; ii++){
141510 char aff;
141511 Select *pSub1;
141512 assert( pList->a[ii].pExpr!=0 );
141513 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
141514 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
141515 assert( pSub1->pEList!=0 );
141516 assert( pSub1->pEList->nExpr>ii );
141517 assert( pSub1->pEList->a[ii].pExpr!=0 );
141518 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
141519 return 1;
141520 }
141521 }
141522 }
141523 return 0;
141524 }
141525
141526 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
141527 /*
141528 ** This routine attempts to flatten subqueries as a performance optimization.
141529 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -141417,11 +141624,12 @@
141624 ** (17f) the subquery must not be the RHS of a LEFT JOIN.
141625 ** (17g) either the subquery is the first element of the outer
141626 ** query or there are no RIGHT or FULL JOINs in any arm
141627 ** of the subquery. (This is a duplicate of condition (27b).)
141628 ** (17h) The corresponding result set expressions in all arms of the
141629 ** compound must have the same affinity. (See restriction (9)
141630 ** on the push-down optimization.)
141631 **
141632 ** The parent and sub-query may contain WHERE clauses. Subject to
141633 ** rules (11), (13) and (14), they may also contain ORDER BY,
141634 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
141635 ** operator other than UNION ALL because all the other compound
@@ -141636,23 +141844,11 @@
141844
141845 /* Restriction (23) */
141846 if( (p->selFlags & SF_Recursive) ) return 0;
141847
141848 /* Restriction (17h) */
141849 if( compoundHasDifferentAffinities(pSub) ) return 0;
 
 
 
 
 
 
 
 
 
 
 
 
141850
141851 if( pSrc->nSrc>1 ){
141852 if( pParse->nSelect>500 ) return 0;
141853 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
141854 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
@@ -141659,11 +141855,11 @@
141855 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
141856 }
141857 }
141858
141859 /***** If we reach this point, flattening is permitted. *****/
141860 TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n",
141861 pSub->selId, pSub, iFrom));
141862
141863 /* Authorize the subquery */
141864 pParse->zAuthContext = pSubitem->zName;
141865 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
@@ -141738,11 +141934,11 @@
141934 }
141935 pNew->pPrior = pPrior;
141936 if( pPrior ) pPrior->pNext = pNew;
141937 pNew->pNext = p;
141938 p->pPrior = pNew;
141939 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
141940 " creates %u as peer\n",pNew->selId));
141941 }
141942 assert( pSubitem->pSelect==0 );
141943 }
141944 sqlite3DbFree(db, aCsrMap);
@@ -141918,12 +142114,12 @@
142114 sqlite3AggInfoPersistWalkerInit(&w, pParse);
142115 sqlite3WalkSelect(&w,pSub1);
142116 sqlite3SelectDelete(db, pSub1);
142117
142118 #if TREETRACE_ENABLED
142119 if( sqlite3TreeTrace & 0x4 ){
142120 TREETRACE(0x4,pParse,p,("After flattening:\n"));
142121 sqlite3TreeViewSelect(0, p, 0);
142122 }
142123 #endif
142124
142125 return 1;
@@ -142293,16 +142489,18 @@
142489 **
142490 ** (7) The inner query is a Common Table Expression (CTE) that should
142491 ** be materialized. (This restriction is implemented in the calling
142492 ** routine.)
142493 **
142494 ** (8) If the subquery is a compound that uses UNION, INTERSECT,
142495 ** or EXCEPT, then all of the result set columns for all arms of
142496 ** the compound must use the BINARY collating sequence.
142497 **
142498 ** (9) If the subquery is a compound, then all arms of the compound must
142499 ** have the same affinity. (This is the same as restriction (17h)
142500 ** for query flattening.)
142501 **
142502 **
142503 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
142504 ** terms are duplicated into the subquery.
142505 */
142506 static int pushDownWhereTerms(
@@ -142315,24 +142513,48 @@
142513 int nChng = 0;
142514 if( pWhere==0 ) return 0;
142515 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
142516 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
142517
 
142518 if( pSubq->pPrior ){
142519 Select *pSel;
142520 int notUnionAll = 0;
142521 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142522 u8 op = pSel->op;
142523 assert( op==TK_ALL || op==TK_SELECT
142524 || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT );
142525 if( op!=TK_ALL && op!=TK_SELECT ){
142526 notUnionAll = 1;
142527 }
142528 #ifndef SQLITE_OMIT_WINDOWFUNC
142529 if( pSel->pWin ) return 0; /* restriction (6b) */
142530 #endif
142531 }
142532 if( compoundHasDifferentAffinities(pSubq) ){
142533 return 0; /* restriction (9) */
142534 }
142535 if( notUnionAll ){
142536 /* If any of the compound arms are connected using UNION, INTERSECT,
142537 ** or EXCEPT, then we must ensure that none of the columns use a
142538 ** non-BINARY collating sequence. */
142539 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
142540 int ii;
142541 const ExprList *pList = pSel->pEList;
142542 assert( pList!=0 );
142543 for(ii=0; ii<pList->nExpr; ii++){
142544 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
142545 if( !sqlite3IsBinary(pColl) ){
142546 return 0; /* Restriction (8) */
142547 }
142548 }
142549 }
142550 }
142551 }else{
142552 #ifndef SQLITE_OMIT_WINDOWFUNC
142553 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
142554 #endif
142555 }
 
142556
142557 #ifdef SQLITE_DEBUG
142558 /* Only the first term of a compound can have a WITH clause. But make
142559 ** sure no other terms are marked SF_Recursive in case something changes
142560 ** in the future.
@@ -143332,12 +143554,12 @@
143554 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
143555 p->selFlags |= SF_ComplexResult;
143556 }
143557 }
143558 #if TREETRACE_ENABLED
143559 if( sqlite3TreeTrace & 0x8 ){
143560 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
143561 sqlite3TreeViewSelect(0, p, 0);
143562 }
143563 #endif
143564 return WRC_Continue;
143565 }
@@ -143467,10 +143689,177 @@
143689 if( pParse->nErr ) return;
143690 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
143691 if( pParse->nErr ) return;
143692 sqlite3SelectAddTypeInfo(pParse, p);
143693 }
143694
143695 #if TREETRACE_ENABLED
143696 /*
143697 ** Display all information about an AggInfo object
143698 */
143699 static void printAggInfo(AggInfo *pAggInfo){
143700 int ii;
143701 for(ii=0; ii<pAggInfo->nColumn; ii++){
143702 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
143703 sqlite3DebugPrintf(
143704 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
143705 " iSorterColumn=%d %s\n",
143706 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
143707 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
143708 pCol->iSorterColumn,
143709 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
143710 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
143711 }
143712 for(ii=0; ii<pAggInfo->nFunc; ii++){
143713 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
143714 ii, AggInfoFuncReg(pAggInfo,ii));
143715 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
143716 }
143717 }
143718 #endif /* TREETRACE_ENABLED */
143719
143720 /*
143721 ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
143722 ** entries for columns that are arguments to aggregate functions but which
143723 ** are not otherwise used.
143724 **
143725 ** The aCol[] entries in AggInfo prior to nAccumulator are columns that
143726 ** are referenced outside of aggregate functions. These might be columns
143727 ** that are part of the GROUP by clause, for example. Other database engines
143728 ** would throw an error if there is a column reference that is not in the
143729 ** GROUP BY clause and that is not part of an aggregate function argument.
143730 ** But SQLite allows this.
143731 **
143732 ** The aCol[] entries beginning with the aCol[nAccumulator] and following
143733 ** are column references that are used exclusively as arguments to
143734 ** aggregate functions. This routine is responsible for computing
143735 ** (or recomputing) those aCol[] entries.
143736 */
143737 static void analyzeAggFuncArgs(
143738 AggInfo *pAggInfo,
143739 NameContext *pNC
143740 ){
143741 int i;
143742 assert( pAggInfo!=0 );
143743 assert( pAggInfo->iFirstReg==0 );
143744 pNC->ncFlags |= NC_InAggFunc;
143745 for(i=0; i<pAggInfo->nFunc; i++){
143746 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
143747 assert( ExprUseXList(pExpr) );
143748 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
143749 #ifndef SQLITE_OMIT_WINDOWFUNC
143750 assert( !IsWindowFunc(pExpr) );
143751 if( ExprHasProperty(pExpr, EP_WinFunc) ){
143752 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
143753 }
143754 #endif
143755 }
143756 pNC->ncFlags &= ~NC_InAggFunc;
143757 }
143758
143759 /*
143760 ** An index on expressions is being used in the inner loop of an
143761 ** aggregate query with a GROUP BY clause. This routine attempts
143762 ** to adjust the AggInfo object to take advantage of index and to
143763 ** perhaps use the index as a covering index.
143764 **
143765 */
143766 static void optimizeAggregateUseOfIndexedExpr(
143767 Parse *pParse, /* Parsing context */
143768 Select *pSelect, /* The SELECT statement being processed */
143769 AggInfo *pAggInfo, /* The aggregate info */
143770 NameContext *pNC /* Name context used to resolve agg-func args */
143771 ){
143772 assert( pAggInfo->iFirstReg==0 );
143773 pAggInfo->nColumn = pAggInfo->nAccumulator;
143774 if( ALWAYS(pAggInfo->nSortingColumn>0) ){
143775 if( pAggInfo->nColumn==0 ){
143776 pAggInfo->nSortingColumn = 0;
143777 }else{
143778 pAggInfo->nSortingColumn =
143779 pAggInfo->aCol[pAggInfo->nColumn-1].iSorterColumn+1;
143780 }
143781 }
143782 analyzeAggFuncArgs(pAggInfo, pNC);
143783 #if TREETRACE_ENABLED
143784 if( sqlite3TreeTrace & 0x20 ){
143785 IndexedExpr *pIEpr;
143786 TREETRACE(0x20, pParse, pSelect,
143787 ("AggInfo (possibly) adjusted for Indexed Exprs\n"));
143788 sqlite3TreeViewSelect(0, pSelect, 0);
143789 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
143790 printf("data-cursor=%d index={%d,%d}\n",
143791 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
143792 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
143793 }
143794 printAggInfo(pAggInfo);
143795 }
143796 #else
143797 UNUSED_PARAMETER(pSelect);
143798 UNUSED_PARAMETER(pParse);
143799 #endif
143800 }
143801
143802 /*
143803 ** Walker callback for aggregateConvertIndexedExprRefToColumn().
143804 */
143805 static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){
143806 AggInfo *pAggInfo;
143807 struct AggInfo_col *pCol;
143808 UNUSED_PARAMETER(pWalker);
143809 if( pExpr->pAggInfo==0 ) return WRC_Continue;
143810 if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
143811 if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
143812 if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
143813 pAggInfo = pExpr->pAggInfo;
143814 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
143815 pCol = &pAggInfo->aCol[pExpr->iAgg];
143816 pExpr->op = TK_AGG_COLUMN;
143817 pExpr->iTable = pCol->iTable;
143818 pExpr->iColumn = pCol->iColumn;
143819 return WRC_Prune;
143820 }
143821
143822 /*
143823 ** Convert every pAggInfo->aFunc[].pExpr such that any node within
143824 ** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
143825 ** opcode.
143826 */
143827 static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){
143828 int i;
143829 Walker w;
143830 memset(&w, 0, sizeof(w));
143831 w.xExprCallback = aggregateIdxEprRefToColCallback;
143832 for(i=0; i<pAggInfo->nFunc; i++){
143833 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
143834 }
143835 }
143836
143837
143838 /*
143839 ** Allocate a block of registers so that there is one register for each
143840 ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
143841 ** register in this block is stored in pAggInfo->iFirstReg.
143842 **
143843 ** This routine may only be called once for each AggInfo object. Prior
143844 ** to calling this routine:
143845 **
143846 ** * The aCol[] and aFunc[] arrays may be modified
143847 ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used
143848 **
143849 ** After clling this routine:
143850 **
143851 ** * The aCol[] and aFunc[] arrays are fixed
143852 ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used
143853 **
143854 */
143855 static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){
143856 assert( pAggInfo!=0 );
143857 assert( pAggInfo->iFirstReg==0 );
143858 pAggInfo->iFirstReg = pParse->nMem + 1;
143859 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
143860 }
143861
143862 /*
143863 ** Reset the aggregate accumulator.
143864 **
143865 ** The aggregate accumulator is a set of memory cells that hold
@@ -143481,28 +143870,17 @@
143870 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
143871 Vdbe *v = pParse->pVdbe;
143872 int i;
143873 struct AggInfo_func *pFunc;
143874 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
143875 assert( pAggInfo->iFirstReg>0 );
143876 assert( pParse->db->pParse==pParse );
143877 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
143878 if( nReg==0 ) return;
143879 if( pParse->nErr ) return;
143880 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg,
143881 pAggInfo->iFirstReg+nReg-1);
 
 
 
 
 
 
 
 
 
 
 
 
143882 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
143883 if( pFunc->iDistinct>=0 ){
143884 Expr *pE = pFunc->pFExpr;
143885 assert( ExprUseXList(pE) );
143886 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
@@ -143530,19 +143908,20 @@
143908 struct AggInfo_func *pF;
143909 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
143910 ExprList *pList;
143911 assert( ExprUseXList(pF->pFExpr) );
143912 pList = pF->pFExpr->x.pList;
143913 sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i),
143914 pList ? pList->nExpr : 0);
143915 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
143916 }
143917 }
143918
143919
143920 /*
143921 ** Generate code that will update the accumulator memory cells for an
143922 ** aggregate based on the current cursor position.
143923 **
143924 ** If regAcc is non-zero and there are no min() or max() aggregates
143925 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
143926 ** registers if register regAcc contains 0. The caller will take care
143927 ** of setting and clearing regAcc.
@@ -143558,10 +143937,12 @@
143937 int regHit = 0;
143938 int addrHitTest = 0;
143939 struct AggInfo_func *pF;
143940 struct AggInfo_col *pC;
143941
143942 assert( pAggInfo->iFirstReg>0 );
143943 if( pParse->nErr ) return;
143944 pAggInfo->directMode = 1;
143945 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
143946 int nArg;
143947 int addrNext = 0;
143948 int regAgg;
@@ -143618,11 +143999,11 @@
143999 pColl = pParse->db->pDfltColl;
144000 }
144001 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
144002 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
144003 }
144004 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i));
144005 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
144006 sqlite3VdbeChangeP5(v, (u8)nArg);
144007 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
144008 if( addrNext ){
144009 sqlite3VdbeResolveLabel(v, addrNext);
@@ -143633,11 +144014,11 @@
144014 }
144015 if( regHit ){
144016 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
144017 }
144018 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
144019 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
144020 }
144021
144022 pAggInfo->directMode = 0;
144023 if( addrHitTest ){
144024 sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
@@ -143729,11 +144110,11 @@
144110 sWalker.xExprCallback = havingToWhereExprCb;
144111 sWalker.u.pSelect = p;
144112 sqlite3WalkExpr(&sWalker, p->pHaving);
144113 #if TREETRACE_ENABLED
144114 if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
144115 TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
144116 sqlite3TreeViewSelect(0, p, 0);
144117 }
144118 #endif
144119 }
144120
@@ -143861,12 +144242,12 @@
144242 }
144243 p->pEList->a[0].pExpr = pExpr;
144244 p->selFlags &= ~SF_Aggregate;
144245
144246 #if TREETRACE_ENABLED
144247 if( sqlite3TreeTrace & 0x200 ){
144248 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
144249 sqlite3TreeViewSelect(0, p, 0);
144250 }
144251 #endif
144252 return 1;
144253 }
@@ -143938,12 +144319,12 @@
144319 return 1;
144320 }
144321 assert( db->mallocFailed==0 );
144322 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
144323 #if TREETRACE_ENABLED
144324 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
144325 if( sqlite3TreeTrace & 0x10000 ){
144326 if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
144327 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
144328 __FILE__, __LINE__);
144329 }
144330 sqlite3ShowSelect(p);
@@ -143959,12 +144340,12 @@
144340 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
144341 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
144342 /* All of these destinations are also able to ignore the ORDER BY clause */
144343 if( p->pOrderBy ){
144344 #if TREETRACE_ENABLED
144345 TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
144346 if( sqlite3TreeTrace & 0x800 ){
144347 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
144348 }
144349 #endif
144350 sqlite3ParserAddCleanup(pParse,
144351 (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
@@ -143980,12 +144361,12 @@
144361 goto select_end;
144362 }
144363 assert( db->mallocFailed==0 );
144364 assert( p->pEList!=0 );
144365 #if TREETRACE_ENABLED
144366 if( sqlite3TreeTrace & 0x10 ){
144367 TREETRACE(0x10,pParse,p, ("after name resolution:\n"));
144368 sqlite3TreeViewSelect(0, p, 0);
144369 }
144370 #endif
144371
144372 /* If the SF_UFSrcCheck flag is set, then this function is being called
@@ -144022,12 +144403,12 @@
144403 if( sqlite3WindowRewrite(pParse, p) ){
144404 assert( pParse->nErr );
144405 goto select_end;
144406 }
144407 #if TREETRACE_ENABLED
144408 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
144409 TREETRACE(0x40,pParse,p, ("after window rewrite:\n"));
144410 sqlite3TreeViewSelect(0, p, 0);
144411 }
144412 #endif
144413 #endif /* SQLITE_OMIT_WINDOWFUNC */
144414 pTabList = p->pSrc;
@@ -144054,11 +144435,11 @@
144435 */
144436 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
144437 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
144438 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
144439 ){
144440 TREETRACE(0x1000,pParse,p,
144441 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
144442 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
144443 assert( pItem->iCursor>=0 );
144444 unsetJoinExpr(p->pWhere, pItem->iCursor,
144445 pTabList->a[0].fg.jointype & JT_LTORJ);
@@ -144110,11 +144491,11 @@
144491 && pSub->pLimit==0 /* Condition (1) */
144492 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
144493 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
144494 && OptimizationEnabled(db, SQLITE_OmitOrderBy)
144495 ){
144496 TREETRACE(0x800,pParse,p,
144497 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
144498 sqlite3ParserAddCleanup(pParse,
144499 (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
144500 pSub->pOrderBy);
144501 pSub->pOrderBy = 0;
@@ -144165,12 +144546,12 @@
144546 ** procedure.
144547 */
144548 if( p->pPrior ){
144549 rc = multiSelect(pParse, p, pDest);
144550 #if TREETRACE_ENABLED
144551 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
144552 if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)==0 ){
144553 sqlite3TreeViewSelect(0, p, 0);
144554 }
144555 #endif
144556 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
144557 return rc;
@@ -144186,17 +144567,17 @@
144567 && p->pWhere->op==TK_AND
144568 && OptimizationEnabled(db, SQLITE_PropagateConst)
144569 && propagateConstants(pParse, p)
144570 ){
144571 #if TREETRACE_ENABLED
144572 if( sqlite3TreeTrace & 0x2000 ){
144573 TREETRACE(0x2000,pParse,p,("After constant propagation:\n"));
144574 sqlite3TreeViewSelect(0, p, 0);
144575 }
144576 #endif
144577 }else{
144578 TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n"));
144579 }
144580
144581 #ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
144582 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
144583 && countOfViewOptimization(pParse, p)
@@ -144265,19 +144646,19 @@
144646 && (pItem->fg.isCte==0
144647 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
144648 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
144649 ){
144650 #if TREETRACE_ENABLED
144651 if( sqlite3TreeTrace & 0x4000 ){
144652 TREETRACE(0x4000,pParse,p,
144653 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
144654 sqlite3TreeViewSelect(0, p, 0);
144655 }
144656 #endif
144657 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
144658 }else{
144659 TREETRACE(0x4000,pParse,p,("Push-down not possible\n"));
144660 }
144661
144662 zSavedAuthContext = pParse->zAuthContext;
144663 pParse->zAuthContext = pItem->zName;
144664
@@ -144388,12 +144769,12 @@
144769 pGroupBy = p->pGroupBy;
144770 pHaving = p->pHaving;
144771 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
144772
144773 #if TREETRACE_ENABLED
144774 if( sqlite3TreeTrace & 0x8000 ){
144775 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
144776 sqlite3TreeViewSelect(0, p, 0);
144777 }
144778 #endif
144779
144780 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
@@ -144425,12 +144806,12 @@
144806 ** original setting of the SF_Distinct flag, not the current setting */
144807 assert( sDistinct.isTnct );
144808 sDistinct.isTnct = 2;
144809
144810 #if TREETRACE_ENABLED
144811 if( sqlite3TreeTrace & 0x20000 ){
144812 TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
144813 sqlite3TreeViewSelect(0, p, 0);
144814 }
144815 #endif
144816 }
144817
@@ -144512,11 +144893,11 @@
144893 #endif
144894 assert( WHERE_USE_LIMIT==SF_FixedLimit );
144895
144896
144897 /* Begin the database scan. */
144898 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
144899 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
144900 p->pEList, p, wctrlFlags, p->nSelectRow);
144901 if( pWInfo==0 ) goto select_end;
144902 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
144903 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
@@ -144529,11 +144910,11 @@
144910 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
144911 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
144912 sSort.pOrderBy = 0;
144913 }
144914 }
144915 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
144916
144917 /* If sorting index that was created by a prior OP_OpenEphemeral
144918 ** instruction ended up not being needed, then change the OP_OpenEphemeral
144919 ** into an OP_Noop.
144920 */
@@ -144568,11 +144949,11 @@
144949 sqlite3WhereContinueLabel(pWInfo),
144950 sqlite3WhereBreakLabel(pWInfo));
144951
144952 /* End the database scan loop.
144953 */
144954 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
144955 sqlite3WhereEnd(pWInfo);
144956 }
144957 }else{
144958 /* This case when there exist aggregate functions or a GROUP BY clause
144959 ** or both */
@@ -144654,11 +145035,10 @@
145035 memset(&sNC, 0, sizeof(sNC));
145036 sNC.pParse = pParse;
145037 sNC.pSrcList = pTabList;
145038 sNC.uNC.pAggInfo = pAggInfo;
145039 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
 
145040 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
145041 pAggInfo->pGroupBy = pGroupBy;
145042 sqlite3ExprAnalyzeAggList(&sNC, pEList);
145043 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
145044 if( pHaving ){
@@ -144675,49 +145055,21 @@
145055 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
145056 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
145057 }else{
145058 minMaxFlag = WHERE_ORDERBY_NORMAL;
145059 }
145060 analyzeAggFuncArgs(pAggInfo, &sNC);
 
 
 
 
 
 
 
 
 
 
 
 
 
145061 if( db->mallocFailed ) goto select_end;
145062 #if TREETRACE_ENABLED
145063 if( sqlite3TreeTrace & 0x20 ){
145064 TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
 
145065 sqlite3TreeViewSelect(0, p, 0);
145066 if( minMaxFlag ){
145067 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
145068 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
145069 }
145070 printAggInfo(pAggInfo);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145071 }
145072 #endif
145073
145074
145075 /* Processing for aggregates with GROUP BY is very different and
@@ -144782,21 +145134,25 @@
145134 ** This might involve two separate loops with an OP_Sort in between, or
145135 ** it might be a single loop that uses an index to extract information
145136 ** in the right order to begin with.
145137 */
145138 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
145139 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
145140 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
145141 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
145142 | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
145143 );
145144 if( pWInfo==0 ){
145145 sqlite3ExprListDelete(db, pDistinct);
145146 goto select_end;
145147 }
145148 if( pParse->pIdxEpr ){
145149 optimizeAggregateUseOfIndexedExpr(pParse, p, pAggInfo, &sNC);
145150 }
145151 assignAggregateRegisters(pParse, pAggInfo);
145152 eDist = sqlite3WhereIsDistinct(pWInfo);
145153 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
145154 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
145155 /* The optimizer is able to deliver rows in group by order so
145156 ** we do not have to sort. The OP_OpenEphemeral table will be
145157 ** cancelled later because we still need to use the pKeyInfo
145158 */
@@ -144841,19 +145197,36 @@
145197 regRecord = sqlite3GetTempReg(pParse);
145198 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
145199 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
145200 sqlite3ReleaseTempReg(pParse, regRecord);
145201 sqlite3ReleaseTempRange(pParse, regBase, nCol);
145202 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
145203 sqlite3WhereEnd(pWInfo);
145204 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
145205 sortOut = sqlite3GetTempReg(pParse);
145206 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
145207 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
145208 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
145209 pAggInfo->useSortingIdx = 1;
145210 }
145211
145212 /* If there entries in pAgggInfo->aFunc[] that contain subexpressions
145213 ** that are indexed (and that were previously identified and tagged
145214 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
145215 ** must now be converted into a TK_AGG_COLUMN node so that the value
145216 ** is correctly pulled from the index rather than being recomputed. */
145217 if( pParse->pIdxEpr ){
145218 aggregateConvertIndexedExprRefToColumn(pAggInfo);
145219 #if TREETRACE_ENABLED
145220 if( sqlite3TreeTrace & 0x20 ){
145221 TREETRACE(0x20, pParse, p,
145222 ("AggInfo function expressions converted to reference index\n"));
145223 sqlite3TreeViewSelect(0, p, 0);
145224 printAggInfo(pAggInfo);
145225 }
145226 #endif
145227 }
145228
145229 /* If the index or temporary table used by the GROUP BY sort
145230 ** will naturally deliver rows in the order required by the ORDER BY
145231 ** clause, cancel the ephemeral table open coded earlier.
145232 **
@@ -144919,11 +145292,11 @@
145292 */
145293 if( groupBySort ){
145294 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
145295 VdbeCoverage(v);
145296 }else{
145297 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
145298 sqlite3WhereEnd(pWInfo);
145299 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
145300 }
145301 sqlite3ExprListDelete(db, pDistinct);
145302
@@ -145029,11 +145402,12 @@
145402 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
145403 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, (int)iRoot, iDb, 1);
145404 if( pKeyInfo ){
145405 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
145406 }
145407 assignAggregateRegisters(pParse, pAggInfo);
145408 sqlite3VdbeAddOp2(v, OP_Count, iCsr, AggInfoFuncReg(pAggInfo,0));
145409 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
145410 explainSimpleCount(pParse, pTab, pBest);
145411 }else{
145412 int regAcc = 0; /* "populate accumulators" flag */
145413 ExprList *pDistinct = 0;
@@ -145065,10 +145439,11 @@
145439 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
145440 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
145441 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
145442 distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
145443 }
145444 assignAggregateRegisters(pParse, pAggInfo);
145445
145446 /* This case runs if the aggregate has no GROUP BY clause. The
145447 ** processing is much simpler since there is only a single row
145448 ** of output.
145449 */
@@ -145081,17 +145456,17 @@
145456 ** be an appropriate ORDER BY expression for the optimization.
145457 */
145458 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
145459 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
145460
145461 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
145462 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
145463 pDistinct, p, minMaxFlag|distFlag, 0);
145464 if( pWInfo==0 ){
145465 goto select_end;
145466 }
145467 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
145468 eDist = sqlite3WhereIsDistinct(pWInfo);
145469 updateAccumulator(pParse, regAcc, pAggInfo, eDist);
145470 if( eDist!=WHERE_DISTINCT_NOOP ){
145471 struct AggInfo_func *pF = pAggInfo->aFunc;
145472 if( pF ){
@@ -145101,11 +145476,11 @@
145476
145477 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
145478 if( minMaxFlag ){
145479 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
145480 }
145481 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
145482 sqlite3WhereEnd(pWInfo);
145483 finalizeAggFunctions(pParse, pAggInfo);
145484 }
145485
145486 sSort.pOrderBy = 0;
@@ -145148,11 +145523,11 @@
145523 sqlite3ExprListDelete(db, pMinMaxOrderBy);
145524 #ifdef SQLITE_DEBUG
145525 if( pAggInfo && !db->mallocFailed ){
145526 for(i=0; i<pAggInfo->nColumn; i++){
145527 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
145528 if( pExpr==0 ) continue;
145529 assert( pExpr->pAggInfo==pAggInfo );
145530 assert( pExpr->iAgg==i );
145531 }
145532 for(i=0; i<pAggInfo->nFunc; i++){
145533 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
@@ -145162,12 +145537,12 @@
145537 }
145538 }
145539 #endif
145540
145541 #if TREETRACE_ENABLED
145542 TREETRACE(0x1,pParse,p,("end processing\n"));
145543 if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
145544 sqlite3TreeViewSelect(0, p, 0);
145545 }
145546 #endif
145547 ExplainQueryPlanPop(pParse);
145548 return rc;
@@ -145437,11 +145812,11 @@
145812 while( p ){
145813 Trigger *pTrig = (Trigger *)sqliteHashData(p);
145814 if( pTrig->pTabSchema==pTab->pSchema
145815 && pTrig->table
145816 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
145817 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
145818 ){
145819 pTrig->pNext = pList;
145820 pList = pTrig;
145821 }else if( pTrig->op==TK_RETURNING ){
145822 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -150944,10 +151319,11 @@
151319 #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
151320 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
151321 #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
151322 #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
151323 #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
151324 #define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
151325
151326 #endif /* !defined(SQLITE_WHEREINT_H) */
151327
151328 /************** End of whereInt.h ********************************************/
151329 /************** Continuing where we left off in wherecode.c ******************/
@@ -151289,11 +151665,11 @@
151665 pTerm->wtFlags |= TERM_LIKECOND;
151666 }else{
151667 pTerm->wtFlags |= TERM_CODED;
151668 }
151669 #ifdef WHERETRACE_ENABLED
151670 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
151671 sqlite3DebugPrintf("DISABLE-");
151672 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
151673 }
151674 #endif
151675 if( pTerm->iParent<0 ) break;
@@ -152276,17 +152652,19 @@
152652 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
152653 iCur = pTabItem->iCursor;
152654 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
152655 bRev = (pWInfo->revMask>>iLevel)&1;
152656 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
152657 #if WHERETRACE_ENABLED /* 0x4001 */
152658 if( sqlite3WhereTrace & 0x1 ){
152659 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
152660 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
152661 if( sqlite3WhereTrace & 0x1000 ){
152662 sqlite3WhereLoopPrint(pLoop, pWC);
152663 }
152664 }
152665 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
152666 if( iLevel==0 ){
152667 sqlite3DebugPrintf("WHERE clause being coded:\n");
152668 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
152669 }
152670 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
@@ -153206,11 +153584,11 @@
153584 pAndExpr->pLeft = pOrExpr;
153585 pOrExpr = pAndExpr;
153586 }
153587 /* Loop through table entries that match term pOrTerm. */
153588 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
153589 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
153590 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
153591 WHERE_OR_SUBCLAUSE, iCovCur);
153592 assert( pSubWInfo || pParse->nErr );
153593 if( pSubWInfo ){
153594 WhereLoop *pSubLoop;
@@ -153443,16 +153821,16 @@
153821 VdbeCoverageIf(v, (x&1)==1);
153822 VdbeCoverageIf(v, (x&1)==0);
153823 }
153824 #endif
153825 }
153826 #ifdef WHERETRACE_ENABLED /* 0xffffffff */
153827 if( sqlite3WhereTrace ){
153828 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
153829 pWC->nTerm-j, pTerm, iLoop));
153830 }
153831 if( sqlite3WhereTrace & 0x4000 ){
153832 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
153833 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
153834 }
153835 #endif
153836 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
@@ -153477,12 +153855,12 @@
153855 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
153856 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
153857 if( pTerm->leftCursor!=iCur ) continue;
153858 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
153859 pE = pTerm->pExpr;
153860 #ifdef WHERETRACE_ENABLED /* 0x4001 */
153861 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
153862 sqlite3DebugPrintf("Coding transitive constraint:\n");
153863 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
153864 }
153865 #endif
153866 assert( !ExprHasProperty(pE, EP_OuterON) );
@@ -153593,17 +153971,17 @@
153971 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
153972 pTerm->wtFlags |= TERM_CODED;
153973 }
153974 }
153975
153976 #if WHERETRACE_ENABLED /* 0x4001 */
153977 if( sqlite3WhereTrace & 0x4000 ){
153978 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
153979 iLevel);
153980 sqlite3WhereClausePrint(pWC);
153981 }
153982 if( sqlite3WhereTrace & 0x1 ){
153983 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
153984 iLevel, (u64)pLevel->notReady);
153985 }
153986 #endif
153987 return pLevel->notReady;
@@ -156259,11 +156637,11 @@
156637 ** are no-ops.
156638 */
156639 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
156640 static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
156641 int i;
156642 if( (sqlite3WhereTrace & 0x10)==0 ) return;
156643 for(i=0; i<p->nConstraint; i++){
156644 sqlite3DebugPrintf(
156645 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
156646 i,
156647 p->aConstraint[i].iColumn,
@@ -156279,11 +156657,11 @@
156657 p->aOrderBy[i].desc);
156658 }
156659 }
156660 static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
156661 int i;
156662 if( (sqlite3WhereTrace & 0x10)==0 ) return;
156663 for(i=0; i<p->nConstraint; i++){
156664 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
156665 i,
156666 p->aConstraintUsage[i].argvIndex,
156667 p->aConstraintUsage[i].omit);
@@ -157296,11 +157674,11 @@
157674 ** using the method described in the header comment for this function. */
157675 if( nDiff!=1 || pUpper==0 || pLower==0 ){
157676 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
157677 pLoop->nOut -= nAdjust;
157678 *pbDone = 1;
157679 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
157680 nLower, nUpper, nAdjust*-1, pLoop->nOut));
157681 }
157682
157683 }else{
157684 assert( *pbDone==0 );
@@ -157474,11 +157852,11 @@
157852 nNew = 10; assert( 10==sqlite3LogEst(2) );
157853 }
157854 if( nNew<nOut ){
157855 nOut = nNew;
157856 }
157857 WHERETRACE(0x20, ("STAT4 range scan: %u..%u est=%d\n",
157858 (u32)iLower, (u32)iUpper, nOut));
157859 }
157860 }else{
157861 int bDone = 0;
157862 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
@@ -157507,11 +157885,11 @@
157885 nOut -= (pLower!=0) + (pUpper!=0);
157886 if( nNew<10 ) nNew = 10;
157887 if( nNew<nOut ) nOut = nNew;
157888 #if defined(WHERETRACE_ENABLED)
157889 if( pLoop->nOut>nOut ){
157890 WHERETRACE(0x20,("Range scan lowers nOut from %d to %d\n",
157891 pLoop->nOut, nOut));
157892 }
157893 #endif
157894 pLoop->nOut = (LogEst)nOut;
157895 return rc;
@@ -157572,11 +157950,11 @@
157950 if( rc!=SQLITE_OK ) return rc;
157951 if( bOk==0 ) return SQLITE_NOTFOUND;
157952 pBuilder->nRecValid = nEq;
157953
157954 whereKeyStats(pParse, p, pRec, 0, a);
157955 WHERETRACE(0x20,("equality scan regions %s(%d): %d\n",
157956 p->zName, nEq-1, (int)a[1]));
157957 *pnRow = a[1];
157958
157959 return rc;
157960 }
@@ -157622,11 +158000,11 @@
158000 }
158001
158002 if( rc==SQLITE_OK ){
158003 if( nRowEst > nRow0 ) nRowEst = nRow0;
158004 *pnRow = nRowEst;
158005 WHERETRACE(0x20,("IN row estimate: est=%d\n", nRowEst));
158006 }
158007 assert( pBuilder->nRecValid==nRecValid );
158008 return rc;
158009 }
158010 #endif /* SQLITE_ENABLE_STAT4 */
@@ -157731,11 +158109,11 @@
158109 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
158110 }else{
158111 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
158112 }
158113 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
158114 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
158115 int i;
158116 for(i=0; i<p->nLTerm; i++){
158117 sqlite3WhereTermPrint(p->aLTerm[i], i);
158118 }
158119 }
@@ -158609,11 +158987,11 @@
158987 ** to be true for half or more of the rows in the table.
158988 ** See tag-202002240-1 */
158989 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
158990 ){
158991 #if WHERETRACE_ENABLED /* 0x01 */
158992 if( sqlite3WhereTrace & 0x20 ){
158993 sqlite3DebugPrintf(
158994 "STAT4 determines term has low selectivity:\n");
158995 sqlite3WhereTermPrint(pTerm, 999);
158996 }
158997 #endif
@@ -158646,13 +159024,21 @@
159024 /* Set rCostIdx to the cost of visiting selected rows in index. Add
159025 ** it to pNew->rRun, which is currently set to the cost of the index
159026 ** seek only. Then, if this is a non-covering index, add the cost of
159027 ** visiting the rows in the main table. */
159028 assert( pSrc->pTab->szTabRow>0 );
159029 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
159030 /* The pProbe->szIdxRow is low for an IPK table since the interior
159031 ** pages are small. Thuse szIdxRow gives a good estimate of seek cost.
159032 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
159033 ** under-estimate the scanning cost. */
159034 rCostIdx = pNew->nOut + 16;
159035 }else{
159036 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
159037 }
159038 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
159039 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
159040 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
159041 }
159042 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
159043
159044 nOutUnadjusted = pNew->nOut;
@@ -158800,21 +159186,44 @@
159186 return 1;
159187 }
159188 }
159189 return 0;
159190 }
159191
159192 /*
159193 ** pIdx is an index containing expressions. Check it see if any of the
159194 ** expressions in the index match the pExpr expression.
159195 */
159196 static int exprIsCoveredByIndex(
159197 const Expr *pExpr,
159198 const Index *pIdx,
159199 int iTabCur
159200 ){
159201 int i;
159202 for(i=0; i<pIdx->nColumn; i++){
159203 if( pIdx->aiColumn[i]==XN_EXPR
159204 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
159205 ){
159206 return 1;
159207 }
159208 }
159209 return 0;
159210 }
159211
159212 /*
159213 ** Structure passed to the whereIsCoveringIndex Walker callback.
159214 */
159215 typedef struct CoveringIndexCheck CoveringIndexCheck;
159216 struct CoveringIndexCheck {
159217 Index *pIdx; /* The index */
159218 int iTabCur; /* Cursor number for the corresponding table */
159219 u8 bExpr; /* Uses an indexed expression */
159220 u8 bUnidx; /* Uses an unindexed column not within an indexed expr */
159221 };
159222
159223 /*
159224 ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
159225 **
159226 ** If the Expr node references the table with cursor pCk->iTabCur, then
159227 ** make sure that column is covered by the index pCk->pIdx. We know that
159228 ** all columns less than 63 (really BMS-1) are covered, so we don't need
159229 ** to check them. But we do need to check any column at 63 or greater.
@@ -158822,75 +159231,107 @@
159231 ** If the index does not cover the column, then set pWalk->eCode to
159232 ** non-zero and return WRC_Abort to stop the search.
159233 **
159234 ** If this node does not disprove that the index can be a covering index,
159235 ** then just return WRC_Continue, to continue the search.
159236 **
159237 ** If pCk->pIdx contains indexed expressions and one of those expressions
159238 ** matches pExpr, then prune the search.
159239 */
159240 static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
159241 int i; /* Loop counter */
159242 const Index *pIdx; /* The index of interest */
159243 const i16 *aiColumn; /* Columns contained in the index */
159244 u16 nColumn; /* Number of columns in the index */
159245 CoveringIndexCheck *pCk; /* Info about this search */
159246
159247 pCk = pWalk->u.pCovIdxCk;
159248 pIdx = pCk->pIdx;
159249 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
159250 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
159251 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
159252 pIdx = pWalk->u.pCovIdxCk->pIdx;
159253 aiColumn = pIdx->aiColumn;
159254 nColumn = pIdx->nColumn;
159255 for(i=0; i<nColumn; i++){
159256 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
159257 }
159258 pCk->bUnidx = 1;
159259 return WRC_Abort;
159260 }else if( pIdx->bHasExpr
159261 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
159262 pCk->bExpr = 1;
159263 return WRC_Prune;
159264 }
159265 return WRC_Continue;
159266 }
159267
159268
159269 /*
159270 ** pIdx is an index that covers all of the low-number columns used by
159271 ** pWInfo->pSelect (columns from 0 through 62) or an index that has
159272 ** expressions terms. Hence, we cannot determine whether or not it is
159273 ** a covering index by using the colUsed bitmasks. We have to do a search
159274 ** to see if the index is covering. This routine does that search.
159275 **
159276 ** The return value is one of these:
159277 **
159278 ** 0 The index is definitely not a covering index
159279 **
159280 ** WHERE_IDX_ONLY The index is definitely a covering index
159281 **
159282 ** WHERE_EXPRIDX The index is likely a covering index, but it is
159283 ** difficult to determine precisely because of the
159284 ** expressions that are indexed. Score it as a
159285 ** covering index, but still keep the main table open
159286 ** just in case we need it.
159287 **
159288 ** This routine is an optimization. It is always safe to return zero.
159289 ** But returning one of the other two values when zero should have been
159290 ** returned can lead to incorrect bytecode and assertion faults.
159291 */
159292 static SQLITE_NOINLINE u32 whereIsCoveringIndex(
159293 WhereInfo *pWInfo, /* The WHERE clause context */
159294 Index *pIdx, /* Index that is being tested */
159295 int iTabCur /* Cursor for the table being indexed */
159296 ){
159297 int i, rc;
159298 struct CoveringIndexCheck ck;
159299 Walker w;
159300 if( pWInfo->pSelect==0 ){
159301 /* We don't have access to the full query, so we cannot check to see
159302 ** if pIdx is covering. Assume it is not. */
159303 return 0;
159304 }
159305 if( pIdx->bHasExpr==0 ){
159306 for(i=0; i<pIdx->nColumn; i++){
159307 if( pIdx->aiColumn[i]>=BMS-1 ) break;
159308 }
159309 if( i>=pIdx->nColumn ){
159310 /* pIdx does not index any columns greater than 62, but we know from
159311 ** colMask that columns greater than 62 are used, so this is not a
159312 ** covering index */
159313 return 0;
159314 }
159315 }
159316 ck.pIdx = pIdx;
159317 ck.iTabCur = iTabCur;
159318 ck.bExpr = 0;
159319 ck.bUnidx = 0;
159320 memset(&w, 0, sizeof(w));
159321 w.xExprCallback = whereIsCoveringIndexWalkCallback;
159322 w.xSelectCallback = sqlite3SelectWalkNoop;
159323 w.u.pCovIdxCk = &ck;
 
159324 sqlite3WalkSelect(&w, pWInfo->pSelect);
159325 if( ck.bUnidx ){
159326 rc = 0;
159327 }else if( ck.bExpr ){
159328 rc = WHERE_EXPRIDX;
159329 }else{
159330 rc = WHERE_IDX_ONLY;
159331 }
159332 return rc;
159333 }
159334
159335 /*
159336 ** Add all WhereLoop objects for a single table of the join where the table
159337 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
@@ -158971,11 +159412,11 @@
159412 sPk.nColumn = 1;
159413 sPk.aiColumn = &aiColumnPk;
159414 sPk.aiRowLogEst = aiRowEstPk;
159415 sPk.onError = OE_Replace;
159416 sPk.pTable = pTab;
159417 sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
159418 sPk.idxType = SQLITE_IDXTYPE_IPK;
159419 aiRowEstPk[0] = pTab->nRowLogEst;
159420 aiRowEstPk[1] = 0;
159421 pFirst = pSrc->pTab->pIndex;
159422 if( pSrc->fg.notIndexed==0 ){
@@ -159102,18 +159543,42 @@
159543 pNew->nOut = rSize;
159544 if( rc ) break;
159545 }else{
159546 Bitmask m;
159547 if( pProbe->isCovering ){
 
159548 m = 0;
159549 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159550 }else{
159551 m = pSrc->colUsed & pProbe->colNotIdxed;
159552 pNew->wsFlags = WHERE_INDEXED;
159553 if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
159554 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159555 if( isCov==0 ){
159556 WHERETRACE(0x200,
159557 ("-> %s is not a covering index"
159558 " according to whereIsCoveringIndex()\n", pProbe->zName));
159559 assert( m!=0 );
159560 }else{
159561 m = 0;
159562 pNew->wsFlags |= isCov;
159563 if( isCov & WHERE_IDX_ONLY ){
159564 WHERETRACE(0x200,
159565 ("-> %s is a covering expression index"
159566 " according to whereIsCoveringIndex()\n", pProbe->zName));
159567 }else{
159568 assert( isCov==WHERE_EXPRIDX );
159569 WHERETRACE(0x200,
159570 ("-> %s might be a covering expression index"
159571 " according to whereIsCoveringIndex()\n", pProbe->zName));
159572 }
159573 }
159574 }else if( m==0 ){
159575 WHERETRACE(0x200,
159576 ("-> %s a covering index according to bitmasks\n",
159577 pProbe->zName, m==0 ? "is" : "is not"));
159578 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159579 }
 
159580 }
159581
159582 /* Full scan via index */
159583 if( b
159584 || !HasRowid(pTab)
@@ -159282,11 +159747,11 @@
159747 if( rc==SQLITE_CONSTRAINT ){
159748 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
159749 ** that the particular combination of parameters provided is unusable.
159750 ** Make no entries in the loop table.
159751 */
159752 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
159753 return SQLITE_OK;
159754 }
159755 return rc;
159756 }
159757
@@ -159393,11 +159858,11 @@
159858 rc = whereLoopInsert(pBuilder, pNew);
159859 if( pNew->u.vtab.needFree ){
159860 sqlite3_free(pNew->u.vtab.idxStr);
159861 pNew->u.vtab.needFree = 0;
159862 }
159863 WHERETRACE(0xffffffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
159864 *pbIn, (sqlite3_uint64)mPrereq,
159865 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
159866
159867 return rc;
159868 }
@@ -159585,11 +160050,11 @@
160050 return SQLITE_NOMEM_BKPT;
160051 }
160052
160053 /* First call xBestIndex() with all constraints usable. */
160054 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
160055 WHERETRACE(0x800, (" VirtualOne: all usable\n"));
160056 rc = whereLoopAddVirtualOne(
160057 pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry
160058 );
160059 if( bRetry ){
160060 assert( rc==SQLITE_OK );
@@ -159610,11 +160075,11 @@
160075 Bitmask mBestNoIn = 0;
160076
160077 /* If the plan produced by the earlier call uses an IN(...) term, call
160078 ** xBestIndex again, this time with IN(...) terms disabled. */
160079 if( bIn ){
160080 WHERETRACE(0x800, (" VirtualOne: all usable w/o IN\n"));
160081 rc = whereLoopAddVirtualOne(
160082 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0);
160083 assert( bIn==0 );
160084 mBestNoIn = pNew->prereq & ~mPrereq;
160085 if( mBestNoIn==0 ){
@@ -159636,11 +160101,11 @@
160101 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
160102 }
160103 mPrev = mNext;
160104 if( mNext==ALLBITS ) break;
160105 if( mNext==mBest || mNext==mBestNoIn ) continue;
160106 WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
160107 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
160108 rc = whereLoopAddVirtualOne(
160109 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
160110 if( pNew->prereq==mPrereq ){
160111 seenZero = 1;
@@ -159650,21 +160115,21 @@
160115
160116 /* If the calls to xBestIndex() in the above loop did not find a plan
160117 ** that requires no source tables at all (i.e. one guaranteed to be
160118 ** usable), make a call here with all source tables disabled */
160119 if( rc==SQLITE_OK && seenZero==0 ){
160120 WHERETRACE(0x800, (" VirtualOne: all disabled\n"));
160121 rc = whereLoopAddVirtualOne(
160122 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
160123 if( bIn==0 ) seenZeroNoIN = 1;
160124 }
160125
160126 /* If the calls to xBestIndex() have so far failed to find a plan
160127 ** that requires no source tables at all and does not use an IN(...)
160128 ** operator, make a final call to obtain one here. */
160129 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
160130 WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n"));
160131 rc = whereLoopAddVirtualOne(
160132 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0);
160133 }
160134 }
160135
@@ -159716,11 +160181,11 @@
160181 int i, j;
160182
160183 sSubBuild = *pBuilder;
160184 sSubBuild.pOrSet = &sCur;
160185
160186 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
160187 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
160188 if( (pOrTerm->eOperator & WO_AND)!=0 ){
160189 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
160190 }else if( pOrTerm->leftCursor==iCur ){
160191 tempWC.pWInfo = pWC->pWInfo;
@@ -159733,13 +160198,13 @@
160198 }else{
160199 continue;
160200 }
160201 sCur.n = 0;
160202 #ifdef WHERETRACE_ENABLED
160203 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
160204 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
160205 if( sqlite3WhereTrace & 0x20000 ){
160206 sqlite3WhereClausePrint(sSubBuild.pWC);
160207 }
160208 #endif
160209 #ifndef SQLITE_OMIT_VIRTUALTABLE
160210 if( IsVirtual(pItem->pTab) ){
@@ -159797,11 +160262,11 @@
160262 pNew->rRun = sSum.a[i].rRun + 1;
160263 pNew->nOut = sSum.a[i].nOut;
160264 pNew->prereq = sSum.a[i].prereq;
160265 rc = whereLoopInsert(pBuilder, pNew);
160266 }
160267 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
160268 }
160269 }
160270 return rc;
160271 }
160272
@@ -160145,12 +160610,12 @@
160610 if( iColumn>=XN_ROWID ){
160611 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
160612 if( pOBExpr->iTable!=iCur ) continue;
160613 if( pOBExpr->iColumn!=iColumn ) continue;
160614 }else{
160615 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
160616 if( sqlite3ExprCompareSkip(pOBExpr, pIxExpr, iCur) ){
160617 continue;
160618 }
160619 }
160620 if( iColumn!=XN_ROWID ){
160621 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
@@ -160278,41 +160743,60 @@
160743 ** Return the cost of sorting nRow rows, assuming that the keys have
160744 ** nOrderby columns and that the first nSorted columns are already in
160745 ** order.
160746 */
160747 static LogEst whereSortingCost(
160748 WhereInfo *pWInfo, /* Query planning context */
160749 LogEst nRow, /* Estimated number of rows to sort */
160750 int nOrderBy, /* Number of ORDER BY clause terms */
160751 int nSorted /* Number of initial ORDER BY terms naturally in order */
160752 ){
160753 /* Estimated cost of a full external sort, where N is
160754 ** the number of rows to sort is:
160755 **
160756 ** cost = (K * N * log(N)).
160757 **
160758 ** Or, if the order-by clause has X terms but only the last Y
160759 ** terms are out of order, then block-sorting will reduce the
160760 ** sorting cost to:
160761 **
160762 ** cost = (K * N * log(N)) * (Y/X)
160763 **
160764 ** The constant K is at least 2.0 but will be larger if there are a
160765 ** large number of columns to be sorted, as the sorting time is
160766 ** proportional to the amount of content to be sorted. The algorithm
160767 ** does not currently distinguish between fat columns (BLOBs and TEXTs)
160768 ** and skinny columns (INTs). It just uses the number of columns as
160769 ** an approximation for the row width.
160770 **
160771 ** And extra factor of 2.0 or 3.0 is added to the sorting cost if the sort
160772 ** is built using OP_IdxInsert and OP_Sort rather than with OP_SorterInsert.
160773 */
160774 LogEst rSortCost, nCol;
160775 assert( pWInfo->pSelect!=0 );
160776 assert( pWInfo->pSelect->pEList!=0 );
160777 /* TUNING: sorting cost proportional to the number of output columns: */
160778 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
160779 rSortCost = nRow + nCol;
160780 if( nSorted>0 ){
160781 /* Scale the result by (Y/X) */
160782 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
160783 }
160784
160785 /* Multiple by log(M) where M is the number of output rows.
160786 ** Use the LIMIT for M if it is smaller. Or if this sort is for
160787 ** a DISTINCT operator, M will be the number of distinct output
160788 ** rows, so fudge it downwards a bit.
160789 */
160790 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
160791 rSortCost += 10; /* TUNING: Extra 2.0x if using LIMIT */
160792 if( nSorted!=0 ){
160793 rSortCost += 6; /* TUNING: Extra 1.5x if also using partial sort */
160794 }
160795 if( pWInfo->iLimit<nRow ){
160796 nRow = pWInfo->iLimit;
160797 }
160798 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
160799 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
160800 ** reduces the number of output rows by a factor of 2 */
160801 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
160802 }
@@ -160460,15 +160944,15 @@
160944 if( aSortCost[isOrdered]==0 ){
160945 aSortCost[isOrdered] = whereSortingCost(
160946 pWInfo, nRowEst, nOrderBy, isOrdered
160947 );
160948 }
160949 /* TUNING: Add a small extra penalty (3) to sorting as an
160950 ** extra encouragment to the query planner to select a plan
160951 ** where the rows emerge in the correct order without any sorting
160952 ** required. */
160953 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 3;
160954
160955 WHERETRACE(0x002,
160956 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
160957 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
160958 rUnsorted, rCost));
@@ -160812,11 +161296,11 @@
161296 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
161297 #ifdef SQLITE_DEBUG
161298 pLoop->cId = '0';
161299 #endif
161300 #ifdef WHERETRACE_ENABLED
161301 if( sqlite3WhereTrace & 0x02 ){
161302 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
161303 }
161304 #endif
161305 return 1;
161306 }
@@ -160942,11 +161426,11 @@
161426 break;
161427 }
161428 }
161429 }
161430 if( pTerm<pEnd ) continue;
161431 WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
161432 notReady &= ~pLoop->maskSelf;
161433 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
161434 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
161435 pTerm->wtFlags |= TERM_CODED;
161436 }
@@ -161002,11 +161486,11 @@
161486 && (pTab->tabFlags & TF_HasStat1)!=0
161487 ){
161488 testcase( pItem->fg.jointype & JT_LEFT );
161489 pLoop->wsFlags |= WHERE_BLOOMFILTER;
161490 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
161491 WHERETRACE(0xffffffff, (
161492 "-> use Bloom-filter on loop %c because there are ~%.1e "
161493 "lookups into %s which has only ~%.1e rows\n",
161494 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
161495 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
161496 }
@@ -161015,17 +161499,17 @@
161499 }
161500 }
161501
161502 /*
161503 ** This is an sqlite3ParserAddCleanup() callback that is invoked to
161504 ** free the Parse->pIdxEpr list when the Parse object is destroyed.
161505 */
161506 static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
161507 Parse *pParse = (Parse*)pObject;
161508 while( pParse->pIdxEpr!=0 ){
161509 IndexedExpr *p = pParse->pIdxEpr;
161510 pParse->pIdxEpr = p->pIENext;
161511 sqlite3ExprDelete(db, p->pExpr);
161512 sqlite3DbFreeNN(db, p);
161513 }
161514 }
161515
@@ -161033,17 +161517,17 @@
161517 ** The index pIdx is used by a query and contains one or more expressions.
161518 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
161519 ** number for the index and iDataCur is the cursor number for the corresponding
161520 ** table.
161521 **
161522 ** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
161523 ** each of the expressions in the index so that the expression code generator
161524 ** will know to replace occurrences of the indexed expression with
161525 ** references to the corresponding column of the index.
161526 */
161527 static SQLITE_NOINLINE void whereAddIndexedExpr(
161528 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
161529 Index *pIdx, /* The index-on-expression that contains the expressions */
161530 int iIdxCur, /* Cursor number for pIdx */
161531 SrcItem *pTabItem /* The FROM clause entry for the table */
161532 ){
161533 int i;
@@ -161068,20 +161552,20 @@
161552 continue;
161553 }
161554 if( sqlite3ExprIsConstant(pExpr) ) continue;
161555 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
161556 if( p==0 ) break;
161557 p->pIENext = pParse->pIdxEpr;
161558 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
161559 p->iDataCur = pTabItem->iCursor;
161560 p->iIdxCur = iIdxCur;
161561 p->iIdxCol = i;
161562 p->bMaybeNullRow = bMaybeNullRow;
161563 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
161564 p->zIdxName = pIdx->zName;
161565 #endif
161566 pParse->pIdxEpr = p;
161567 if( p->pIENext==0 ){
161568 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161569 }
161570 }
161571 }
@@ -161369,30 +161853,30 @@
161853 }
161854 }
161855
161856 /* Construct the WhereLoop objects */
161857 #if defined(WHERETRACE_ENABLED)
161858 if( sqlite3WhereTrace & 0xffffffff ){
161859 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
161860 if( wctrlFlags & WHERE_USE_LIMIT ){
161861 sqlite3DebugPrintf(", limit: %d", iAuxArg);
161862 }
161863 sqlite3DebugPrintf(")\n");
161864 if( sqlite3WhereTrace & 0x8000 ){
161865 Select sSelect;
161866 memset(&sSelect, 0, sizeof(sSelect));
161867 sSelect.selFlags = SF_WhereBegin;
161868 sSelect.pSrc = pTabList;
161869 sSelect.pWhere = pWhere;
161870 sSelect.pOrderBy = pOrderBy;
161871 sSelect.pEList = pResultSet;
161872 sqlite3TreeViewSelect(0, &sSelect, 0);
161873 }
161874 if( sqlite3WhereTrace & 0x4000 ){ /* Display all WHERE clause terms */
161875 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
161876 sqlite3WhereClausePrint(sWLB.pWC);
161877 }
161878 }
161879 #endif
161880
161881 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
161882 rc = whereLoopAddAll(&sWLB);
@@ -161404,11 +161888,11 @@
161888 ** changed based on STAT4 information while computing subsequent loops,
161889 ** then we need to rerun the whole loop building process so that all
161890 ** loops will be built using the revised truthProb values. */
161891 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
161892 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
161893 WHERETRACE(0xffffffff,
161894 ("**** Redo all loop computations due to"
161895 " TERM_HIGHTRUTH changes ****\n"));
161896 while( pWInfo->pLoops ){
161897 WhereLoop *p = pWInfo->pLoops;
161898 pWInfo->pLoops = p->pNextLoop;
@@ -161490,15 +161974,15 @@
161974 ){
161975 whereCheckIfBloomFilterIsUseful(pWInfo);
161976 }
161977
161978 #if defined(WHERETRACE_ENABLED)
161979 if( sqlite3WhereTrace & 0x4000 ){ /* Display all terms of the WHERE clause */
161980 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
161981 sqlite3WhereClausePrint(sWLB.pWC);
161982 }
161983 WHERETRACE(0xffffffff,("*** Optimizer Finished ***\n"));
161984 #endif
161985 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
161986
161987 /* If the caller is an UPDATE or DELETE statement that is requesting
161988 ** to use a one-pass algorithm, determine if this is appropriate.
@@ -162028,11 +162512,11 @@
162512 last = iEnd;
162513 }else{
162514 last = pWInfo->iEndWhere;
162515 }
162516 if( pIdx->bHasExpr ){
162517 IndexedExpr *p = pParse->pIdxEpr;
162518 while( p ){
162519 if( p->iIdxCur==pLevel->iIdxCur ){
162520 p->iDataCur = -1;
162521 p->iIdxCur = -1;
162522 }
@@ -163199,11 +163683,11 @@
163683 }
163684
163685 pSub = sqlite3SelectNew(
163686 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
163687 );
163688 TREETRACE(0x40,pParse,pSub,
163689 ("New window-function subquery in FROM clause of (%u/%p)\n",
163690 p->selId, p));
163691 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
163692 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
163693 ** of sqlite3DbMallocRawNN() called from
@@ -176360,10 +176844,13 @@
176844 int iNew = *(int*)pArg;
176845 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
176846 if( iNew>=0 && iNew<=255 ){
176847 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
176848 }
176849 rc = SQLITE_OK;
176850 }else if( op==SQLITE_FCNTL_RESET_CACHE ){
176851 sqlite3BtreeClearCache(pBtree);
176852 rc = SQLITE_OK;
176853 }else{
176854 int nSave = db->busyHandler.nBusy;
176855 rc = sqlite3OsFileControl(fd, op, pArg);
176856 db->busyHandler.nBusy = nSave;
@@ -213942,28 +214429,28 @@
214429 if( !pCsr->isAgg ){
214430 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
214431 }
214432 break;
214433 case 4: /* ncell */
214434 sqlite3_result_int64(ctx, pCsr->nCell);
214435 break;
214436 case 5: /* payload */
214437 sqlite3_result_int64(ctx, pCsr->nPayload);
214438 break;
214439 case 6: /* unused */
214440 sqlite3_result_int64(ctx, pCsr->nUnused);
214441 break;
214442 case 7: /* mx_payload */
214443 sqlite3_result_int64(ctx, pCsr->nMxPayload);
214444 break;
214445 case 8: /* pgoffset */
214446 if( !pCsr->isAgg ){
214447 sqlite3_result_int64(ctx, pCsr->iOffset);
214448 }
214449 break;
214450 case 9: /* pgsize */
214451 sqlite3_result_int64(ctx, pCsr->szPage);
214452 break;
214453 case 10: { /* schema */
214454 sqlite3 *db = sqlite3_context_db_handle(ctx);
214455 int iDb = pCsr->iDb;
214456 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
@@ -238535,11 +239022,11 @@
239022 int nArg, /* Number of args */
239023 sqlite3_value **apUnused /* Function arguments */
239024 ){
239025 assert( nArg==0 );
239026 UNUSED_PARAM2(nArg, apUnused);
239027 sqlite3_result_text(pCtx, "fts5: 2022-12-03 19:04:09 1a61c500add4a2bfe80c0c691d559cfca166dc5f8262651a58da7ec16a51d430", -1, SQLITE_TRANSIENT);
239028 }
239029
239030 /*
239031 ** Return true if zName is the extension on one of the shadow tables used
239032 ** by this module.
239033
+31 -13
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144144
**
145145
** See also: [sqlite3_libversion()],
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149
-#define SQLITE_VERSION "3.40.0"
150
-#define SQLITE_VERSION_NUMBER 3040000
151
-#define SQLITE_SOURCE_ID "2022-11-16 19:57:21 5689f0d9ad1be532b274508938b25ff0d63027b8cc31f796dfaa2cca71d53642"
149
+#define SQLITE_VERSION "3.41.0"
150
+#define SQLITE_VERSION_NUMBER 3041000
151
+#define SQLITE_SOURCE_ID "2022-12-05 02:52:37 1b779afa3ed2f35a110e460fc6ed13cba744db85b9924149ab028b100d1e1e12"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -1190,10 +1190,16 @@
11901190
** by clients within the current process, only within other processes.
11911191
** </ul>
11921192
**
11931193
** <li>[[SQLITE_FCNTL_CKSM_FILE]]
11941194
** Used by the cksmvfs VFS module only.
1195
+**
1196
+** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1197
+** If there is currently no transaction open on the database, and the
1198
+** database is not a temp db, then this file-control purges the contents
1199
+** of the in-memory page cache. If there is an open transaction, or if
1200
+** the db is a temp-db, it is a no-op, not an error.
11951201
** </ul>
11961202
*/
11971203
#define SQLITE_FCNTL_LOCKSTATE 1
11981204
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
11991205
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1232,10 +1238,11 @@
12321238
#define SQLITE_FCNTL_CKPT_DONE 37
12331239
#define SQLITE_FCNTL_RESERVE_BYTES 38
12341240
#define SQLITE_FCNTL_CKPT_START 39
12351241
#define SQLITE_FCNTL_EXTERNAL_READER 40
12361242
#define SQLITE_FCNTL_CKSM_FILE 41
1243
+#define SQLITE_FCNTL_RESET_CACHE 42
12371244
12381245
/* deprecated names */
12391246
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
12401247
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
12411248
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -5540,20 +5547,10 @@
55405547
** such a conversion is possible without loss of information (in other
55415548
** words, if the value is a string that looks like a number)
55425549
** then the conversion is performed. Otherwise no conversion occurs.
55435550
** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
55445551
**
5545
-** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5546
-** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5547
-** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5548
-** returns something other than SQLITE_TEXT, then the return value from
5549
-** sqlite3_value_encoding(X) is meaningless. ^Calls to
5550
-** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5551
-** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5552
-** sqlite3_value_bytes16(X) might change the encoding of the value X and
5553
-** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5554
-**
55555552
** ^Within the [xUpdate] method of a [virtual table], the
55565553
** sqlite3_value_nochange(X) interface returns true if and only if
55575554
** the column corresponding to X is unchanged by the UPDATE operation
55585555
** that the xUpdate method call was invoked to implement and if
55595556
** and the prior [xColumn] method call that was invoked to extracted
@@ -5614,10 +5611,31 @@
56145611
SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
56155612
SQLITE_API int sqlite3_value_type(sqlite3_value*);
56165613
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
56175614
SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
56185615
SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5616
+
5617
+/*
5618
+** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
5619
+** METHOD: sqlite3_value
5620
+**
5621
+** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5622
+** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
5623
+** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5624
+** returns something other than SQLITE_TEXT, then the return value from
5625
+** sqlite3_value_encoding(X) is meaningless. ^Calls to
5626
+** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
5627
+** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
5628
+** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
5629
+** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5630
+**
5631
+** This routine is intended for used by applications that test and validate
5632
+** the SQLite implementation. This routine is inquiring about the opaque
5633
+** internal state of an [sqlite3_value] object. Ordinary applications should
5634
+** not need to know what the internal state of an sqlite3_value object is and
5635
+** hence should not need to use this interface.
5636
+*/
56195637
SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
56205638
56215639
/*
56225640
** CAPI3REF: Finding The Subtype Of SQL Values
56235641
** METHOD: sqlite3_value
56245642
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144 **
145 ** See also: [sqlite3_libversion()],
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.40.0"
150 #define SQLITE_VERSION_NUMBER 3040000
151 #define SQLITE_SOURCE_ID "2022-11-16 19:57:21 5689f0d9ad1be532b274508938b25ff0d63027b8cc31f796dfaa2cca71d53642"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -1190,10 +1190,16 @@
1190 ** by clients within the current process, only within other processes.
1191 ** </ul>
1192 **
1193 ** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1194 ** Used by the cksmvfs VFS module only.
 
 
 
 
 
 
1195 ** </ul>
1196 */
1197 #define SQLITE_FCNTL_LOCKSTATE 1
1198 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1199 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1232,10 +1238,11 @@
1232 #define SQLITE_FCNTL_CKPT_DONE 37
1233 #define SQLITE_FCNTL_RESERVE_BYTES 38
1234 #define SQLITE_FCNTL_CKPT_START 39
1235 #define SQLITE_FCNTL_EXTERNAL_READER 40
1236 #define SQLITE_FCNTL_CKSM_FILE 41
 
1237
1238 /* deprecated names */
1239 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1240 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1241 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -5540,20 +5547,10 @@
5540 ** such a conversion is possible without loss of information (in other
5541 ** words, if the value is a string that looks like a number)
5542 ** then the conversion is performed. Otherwise no conversion occurs.
5543 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5544 **
5545 ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5546 ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5547 ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5548 ** returns something other than SQLITE_TEXT, then the return value from
5549 ** sqlite3_value_encoding(X) is meaningless. ^Calls to
5550 ** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5551 ** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5552 ** sqlite3_value_bytes16(X) might change the encoding of the value X and
5553 ** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5554 **
5555 ** ^Within the [xUpdate] method of a [virtual table], the
5556 ** sqlite3_value_nochange(X) interface returns true if and only if
5557 ** the column corresponding to X is unchanged by the UPDATE operation
5558 ** that the xUpdate method call was invoked to implement and if
5559 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5614,10 +5611,31 @@
5614 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5615 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5616 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5617 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5618 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5619 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5620
5621 /*
5622 ** CAPI3REF: Finding The Subtype Of SQL Values
5623 ** METHOD: sqlite3_value
5624
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -144,13 +144,13 @@
144 **
145 ** See also: [sqlite3_libversion()],
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.41.0"
150 #define SQLITE_VERSION_NUMBER 3041000
151 #define SQLITE_SOURCE_ID "2022-12-05 02:52:37 1b779afa3ed2f35a110e460fc6ed13cba744db85b9924149ab028b100d1e1e12"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -1190,10 +1190,16 @@
1190 ** by clients within the current process, only within other processes.
1191 ** </ul>
1192 **
1193 ** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1194 ** Used by the cksmvfs VFS module only.
1195 **
1196 ** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1197 ** If there is currently no transaction open on the database, and the
1198 ** database is not a temp db, then this file-control purges the contents
1199 ** of the in-memory page cache. If there is an open transaction, or if
1200 ** the db is a temp-db, it is a no-op, not an error.
1201 ** </ul>
1202 */
1203 #define SQLITE_FCNTL_LOCKSTATE 1
1204 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1205 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1232,10 +1238,11 @@
1238 #define SQLITE_FCNTL_CKPT_DONE 37
1239 #define SQLITE_FCNTL_RESERVE_BYTES 38
1240 #define SQLITE_FCNTL_CKPT_START 39
1241 #define SQLITE_FCNTL_EXTERNAL_READER 40
1242 #define SQLITE_FCNTL_CKSM_FILE 41
1243 #define SQLITE_FCNTL_RESET_CACHE 42
1244
1245 /* deprecated names */
1246 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1247 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1248 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -5540,20 +5547,10 @@
5547 ** such a conversion is possible without loss of information (in other
5548 ** words, if the value is a string that looks like a number)
5549 ** then the conversion is performed. Otherwise no conversion occurs.
5550 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5551 **
 
 
 
 
 
 
 
 
 
 
5552 ** ^Within the [xUpdate] method of a [virtual table], the
5553 ** sqlite3_value_nochange(X) interface returns true if and only if
5554 ** the column corresponding to X is unchanged by the UPDATE operation
5555 ** that the xUpdate method call was invoked to implement and if
5556 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5614,10 +5611,31 @@
5611 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5612 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5613 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5614 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5615 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5616
5617 /*
5618 ** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
5619 ** METHOD: sqlite3_value
5620 **
5621 ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5622 ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
5623 ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5624 ** returns something other than SQLITE_TEXT, then the return value from
5625 ** sqlite3_value_encoding(X) is meaningless. ^Calls to
5626 ** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
5627 ** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
5628 ** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
5629 ** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5630 **
5631 ** This routine is intended for used by applications that test and validate
5632 ** the SQLite implementation. This routine is inquiring about the opaque
5633 ** internal state of an [sqlite3_value] object. Ordinary applications should
5634 ** not need to know what the internal state of an sqlite3_value object is and
5635 ** hence should not need to use this interface.
5636 */
5637 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5638
5639 /*
5640 ** CAPI3REF: Finding The Subtype Of SQL Values
5641 ** METHOD: sqlite3_value
5642

Keyboard Shortcuts

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