@cryptotaxi247 / netdata-1 / commits / 0daaa91b4

Sqlite upgrade to version 3.46.1 (#18772)

* Update to sqlite version 3.46.1 * Use new recommendation to run optimize

Stelios Fragkakis committed Oct 15, 2024 at 09:46 UTC 0daaa91b4e61c6a99844e4276197f88ed9f132ea
3 files changed +5266 -3471
src/database/sqlite/sqlite3.c
+5188 -3447
@@ -1,6 +1,6 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 -** version 3.45.3. By combining all the individual C code files into this
3 +** version 3.46.1. 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
@@ -18,7 +18,7 @@
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 -** 8653b758870e6ef0c98d46b3ace27849054a.
21 +** c9c2ab54ba1f5f46360f1b4f35d849cd3f08.
22 */
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
@@ -467,9 +467,9 @@ extern "C" {
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 -#define SQLITE_VERSION "3.45.3"
471 -#define SQLITE_VERSION_NUMBER 3045003
472 -#define SQLITE_SOURCE_ID "2024-04-15 13:34:05 8653b758870e6ef0c98d46b3ace27849054af85da891eb121e9aaa537f1e8355"
470 +#define SQLITE_VERSION "3.46.1"
471 +#define SQLITE_VERSION_NUMBER 3046001
472 +#define SQLITE_SOURCE_ID "2024-08-13 09:16:08 c9c2ab54ba1f5f46360f1b4f35d849cd3f080e6fc2b6c60e91b16c63f69a1e33"
473
474 /*
475 ** CAPI3REF: Run-Time Library Version Numbers
@@ -1085,11 +1085,11 @@ struct sqlite3_file {
1085 ** </ul>
1086 ** xLock() upgrades the database file lock. In other words, xLock() moves the
1087 ** database file lock in the direction NONE toward EXCLUSIVE. The argument to
1088 -** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1088 +** xLock() is always one of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1089 ** SQLITE_LOCK_NONE. If the database file lock is already at or above the
1090 ** requested lock, then the call to xLock() is a no-op.
1091 ** xUnlock() downgrades the database file lock to either SHARED or NONE.
1092 -* If the lock is already at or below the requested lock state, then the call
1092 +** If the lock is already at or below the requested lock state, then the call
1093 ** to xUnlock() is a no-op.
1094 ** The xCheckReservedLock() method checks whether any database connection,
1095 ** either in this process or in some other process, is holding a RESERVED,
@@ -3626,8 +3626,8 @@ SQLITE_API int sqlite3_set_authorizer(
3626 #define SQLITE_RECURSIVE 33 /* NULL NULL */
3627
3628 /*
3629 -** CAPI3REF: Tracing And Profiling Functions
3630 -** METHOD: sqlite3
3629 +** CAPI3REF: Deprecated Tracing And Profiling Functions
3630 +** DEPRECATED
3631 **
3632 ** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3633 ** instead of the routines described here.
@@ -7208,6 +7208,12 @@ SQLITE_API int sqlite3_autovacuum_pages(
7208 ** The exceptions defined in this paragraph might change in a future
7209 ** release of SQLite.
7210 **
7211 +** Whether the update hook is invoked before or after the
7212 +** corresponding change is currently unspecified and may differ
7213 +** depending on the type of change. Do not rely on the order of the
7214 +** hook call with regards to the final result of the operation which
7215 +** triggers the hook.
7216 +**
7217 ** The update hook implementation must not do anything that will modify
7218 ** the database connection that invoked the update hook. Any actions
7219 ** to modify the database connection must be deferred until after the
@@ -8678,7 +8684,7 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8684 ** The sqlite3_keyword_count() interface returns the number of distinct
8685 ** keywords understood by SQLite.
8686 **
8681 -** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8687 +** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8688 ** makes *Z point to that keyword expressed as UTF8 and writes the number
8689 ** of bytes in the keyword into *L. The string that *Z points to is not
8690 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
@@ -10257,24 +10263,45 @@ SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10263 ** <li value="2"><p>
10264 ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
10265 ** that the query planner does not need the rows returned in any particular
10260 -** order, as long as rows with the same values in all "aOrderBy" columns
10261 -** are adjacent.)^ ^(Furthermore, only a single row for each particular
10262 -** combination of values in the columns identified by the "aOrderBy" field
10263 -** needs to be returned.)^ ^It is always ok for two or more rows with the same
10264 -** values in all "aOrderBy" columns to be returned, as long as all such rows
10265 -** are adjacent. ^The virtual table may, if it chooses, omit extra rows
10266 -** that have the same value for all columns identified by "aOrderBy".
10267 -** ^However omitting the extra rows is optional.
10266 +** order, as long as rows with the same values in all columns identified
10267 +** by "aOrderBy" are adjacent.)^ ^(Furthermore, when two or more rows
10268 +** contain the same values for all columns identified by "colUsed", all but
10269 +** one such row may optionally be omitted from the result.)^
10270 +** The virtual table is not required to omit rows that are duplicates
10271 +** over the "colUsed" columns, but if the virtual table can do that without
10272 +** too much extra effort, it could potentially help the query to run faster.
10273 ** This mode is used for a DISTINCT query.
10274 ** <li value="3"><p>
10270 -** ^(If the sqlite3_vtab_distinct() interface returns 3, that means
10271 -** that the query planner needs only distinct rows but it does need the
10272 -** rows to be sorted.)^ ^The virtual table implementation is free to omit
10273 -** rows that are identical in all aOrderBy columns, if it wants to, but
10274 -** it is not required to omit any rows. This mode is used for queries
10275 +** ^(If the sqlite3_vtab_distinct() interface returns 3, that means the
10276 +** virtual table must return rows in the order defined by "aOrderBy" as
10277 +** if the sqlite3_vtab_distinct() interface had returned 0. However if
10278 +** two or more rows in the result have the same values for all columns
10279 +** identified by "colUsed", then all but one such row may optionally be
10280 +** omitted.)^ Like when the return value is 2, the virtual table
10281 +** is not required to omit rows that are duplicates over the "colUsed"
10282 +** columns, but if the virtual table can do that without
10283 +** too much extra effort, it could potentially help the query to run faster.
10284 +** This mode is used for queries
10285 ** that have both DISTINCT and ORDER BY clauses.
10286 ** </ol>
10287 **
10288 +** <p>The following table summarizes the conditions under which the
10289 +** virtual table is allowed to set the "orderByConsumed" flag based on
10290 +** the value returned by sqlite3_vtab_distinct(). This table is a
10291 +** restatement of the previous four paragraphs:
10292 +**
10293 +** <table border=1 cellspacing=0 cellpadding=10 width="90%">
10294 +** <tr>
10295 +** <td valign="top">sqlite3_vtab_distinct() return value
10296 +** <td valign="top">Rows are returned in aOrderBy order
10297 +** <td valign="top">Rows with the same value in all aOrderBy columns are adjacent
10298 +** <td valign="top">Duplicates over all colUsed columns may be omitted
10299 +** <tr><td>0<td>yes<td>yes<td>no
10300 +** <tr><td>1<td>no<td>yes<td>no
10301 +** <tr><td>2<td>no<td>yes<td>yes
10302 +** <tr><td>3<td>yes<td>yes<td>yes
10303 +** </table>
10304 +**
10305 ** ^For the purposes of comparing virtual table output values to see if the
10306 ** values are same value for sorting purposes, two NULL values are considered
10307 ** to be the same. In other words, the comparison operator is "IS"
@@ -12319,6 +12346,30 @@ SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const c
12346 */
12347 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12348
12349 +/*
12350 +** CAPI3REF: Add A Single Change To A Changegroup
12351 +** METHOD: sqlite3_changegroup
12352 +**
12353 +** This function adds the single change currently indicated by the iterator
12354 +** passed as the second argument to the changegroup object. The rules for
12355 +** adding the change are just as described for [sqlite3changegroup_add()].
12356 +**
12357 +** If the change is successfully added to the changegroup, SQLITE_OK is
12358 +** returned. Otherwise, an SQLite error code is returned.
12359 +**
12360 +** The iterator must point to a valid entry when this function is called.
12361 +** If it does not, SQLITE_ERROR is returned and no change is added to the
12362 +** changegroup. Additionally, the iterator must not have been opened with
12363 +** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12364 +** returned.
12365 +*/
12366 +SQLITE_API int sqlite3changegroup_add_change(
12367 + sqlite3_changegroup*,
12368 + sqlite3_changeset_iter*
12369 +);
12370 +
12371 +
12372 +
12373 /*
12374 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12375 ** METHOD: sqlite3_changegroup
@@ -13123,8 +13174,8 @@ struct Fts5PhraseIter {
13174 ** EXTENSION API FUNCTIONS
13175 **
13176 ** xUserData(pFts):
13126 -** Return a copy of the context pointer the extension function was
13127 -** registered with.
13177 +** Return a copy of the pUserData pointer passed to the xCreateFunction()
13178 +** API when the extension function was registered.
13179 **
13180 ** xColumnTotalSize(pFts, iCol, pnToken):
13181 ** If parameter iCol is less than zero, set output variable *pnToken
@@ -14322,6 +14373,8 @@ struct fts5_api {
14373 # define SQLITE_OMIT_ALTERTABLE
14374 #endif
14375
14376 +#define SQLITE_DIGIT_SEPARATOR '_'
14377 +
14378 /*
14379 ** Return true (non-zero) if the input is an integer that is too large
14380 ** to fit in 32-bits. This macro is used inside of various testcase()
@@ -14614,8 +14667,8 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
14667 #define TK_TRUEFALSE 170
14668 #define TK_ISNOT 171
14669 #define TK_FUNCTION 172
14617 -#define TK_UMINUS 173
14618 -#define TK_UPLUS 174
14670 +#define TK_UPLUS 173
14671 +#define TK_UMINUS 174
14672 #define TK_TRUTH 175
14673 #define TK_REGISTER 176
14674 #define TK_VECTOR 177
@@ -14624,8 +14677,9 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
14677 #define TK_ASTERISK 180
14678 #define TK_SPAN 181
14679 #define TK_ERROR 182
14627 -#define TK_SPACE 183
14628 -#define TK_ILLEGAL 184
14680 +#define TK_QNUMBER 183
14681 +#define TK_SPACE 184
14682 +#define TK_ILLEGAL 185
14683
14684 /************** End of parse.h ***********************************************/
14685 /************** Continuing where we left off in sqliteInt.h ******************/
@@ -14887,7 +14941,7 @@ typedef INT16_TYPE LogEst;
14941 # define SQLITE_PTRSIZE __SIZEOF_POINTER__
14942 # elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14943 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
14890 - (defined(__APPLE__) && defined(__POWERPC__)) || \
14944 + (defined(__APPLE__) && defined(__ppc__)) || \
14945 (defined(__TOS_AIX__) && !defined(__64BIT__))
14946 # define SQLITE_PTRSIZE 4
14947 # else
@@ -15155,7 +15209,7 @@ SQLITE_PRIVATE u32 sqlite3WhereTrace;
15209 ** 0x00000010 Display sqlite3_index_info xBestIndex calls
15210 ** 0x00000020 Range an equality scan metrics
15211 ** 0x00000040 IN operator decisions
15158 -** 0x00000080 WhereLoop cost adjustements
15212 +** 0x00000080 WhereLoop cost adjustments
15213 ** 0x00000100
15214 ** 0x00000200 Covering index decisions
15215 ** 0x00000400 OR optimization
@@ -16304,6 +16358,7 @@ SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
16358 sqlite3 *db, /* Database connection that is running the check */
16359 Btree *p, /* The btree to be checked */
16360 Pgno *aRoot, /* An array of root pages numbers for individual trees */
16361 + sqlite3_value *aCnt, /* OUT: entry counts for each btree in aRoot[] */
16362 int nRoot, /* Number of entries in aRoot[] */
16363 int mxErr, /* Stop reporting errors after this many */
16364 int *pnErr, /* OUT: Write number of errors seen to this variable */
@@ -16574,12 +16629,12 @@ typedef struct VdbeOpList VdbeOpList;
16629 #define OP_Vacuum 5
16630 #define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
16631 #define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */
16577 -#define OP_Init 8 /* jump, synopsis: Start at P2 */
16632 +#define OP_Init 8 /* jump0, synopsis: Start at P2 */
16633 #define OP_Goto 9 /* jump */
16634 #define OP_Gosub 10 /* jump */
16580 -#define OP_InitCoroutine 11 /* jump */
16581 -#define OP_Yield 12 /* jump */
16582 -#define OP_MustBeInt 13 /* jump */
16635 +#define OP_InitCoroutine 11 /* jump0 */
16636 +#define OP_Yield 12 /* jump0 */
16637 +#define OP_MustBeInt 13 /* jump0 */
16638 #define OP_Jump 14 /* jump */
16639 #define OP_Once 15 /* jump */
16640 #define OP_If 16 /* jump */
@@ -16587,22 +16642,22 @@ typedef struct VdbeOpList VdbeOpList;
16642 #define OP_IsType 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */
16643 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
16644 #define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
16590 -#define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */
16591 -#define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */
16592 -#define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */
16593 -#define OP_SeekGT 24 /* jump, synopsis: key=r[P3@P4] */
16645 +#define OP_SeekLT 21 /* jump0, synopsis: key=r[P3@P4] */
16646 +#define OP_SeekLE 22 /* jump0, synopsis: key=r[P3@P4] */
16647 +#define OP_SeekGE 23 /* jump0, synopsis: key=r[P3@P4] */
16648 +#define OP_SeekGT 24 /* jump0, synopsis: key=r[P3@P4] */
16649 #define OP_IfNotOpen 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */
16650 #define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */
16651 #define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */
16652 #define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */
16653 #define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */
16599 -#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */
16654 +#define OP_SeekRowid 30 /* jump0, synopsis: intkey=r[P3] */
16655 #define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */
16601 -#define OP_Last 32 /* jump */
16602 -#define OP_IfSmaller 33 /* jump */
16656 +#define OP_Last 32 /* jump0 */
16657 +#define OP_IfSizeBetween 33 /* jump */
16658 #define OP_SorterSort 34 /* jump */
16659 #define OP_Sort 35 /* jump */
16605 -#define OP_Rewind 36 /* jump */
16660 +#define OP_Rewind 36 /* jump0 */
16661 #define OP_SorterNext 37 /* jump */
16662 #define OP_Prev 38 /* jump */
16663 #define OP_Next 39 /* jump */
@@ -16614,7 +16669,7 @@ typedef struct VdbeOpList VdbeOpList;
16669 #define OP_IdxGE 45 /* jump, synopsis: key=r[P3@P4] */
16670 #define OP_RowSetRead 46 /* jump, synopsis: r[P3]=rowset(P1) */
16671 #define OP_RowSetTest 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
16617 -#define OP_Program 48 /* jump */
16672 +#define OP_Program 48 /* jump0 */
16673 #define OP_FkIfZero 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
16674 #define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
16675 #define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
@@ -16644,7 +16699,7 @@ typedef struct VdbeOpList VdbeOpList;
16699 #define OP_Null 75 /* synopsis: r[P2..P3]=NULL */
16700 #define OP_SoftNull 76 /* synopsis: r[P1]=NULL */
16701 #define OP_Blob 77 /* synopsis: r[P2]=P4 (len=P1) */
16647 -#define OP_Variable 78 /* synopsis: r[P2]=parameter(P1,P4) */
16702 +#define OP_Variable 78 /* synopsis: r[P2]=parameter(P1) */
16703 #define OP_Move 79 /* synopsis: r[P2@P3]=r[P1@P3] */
16704 #define OP_Copy 80 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
16705 #define OP_SCopy 81 /* synopsis: r[P2]=r[P1] */
@@ -16768,14 +16823,15 @@ typedef struct VdbeOpList VdbeOpList;
16823 #define OPFLG_OUT2 0x10 /* out2: P2 is an output */
16824 #define OPFLG_OUT3 0x20 /* out3: P3 is an output */
16825 #define OPFLG_NCYCLE 0x40 /* ncycle:Cycles count against P1 */
16826 +#define OPFLG_JUMP0 0x80 /* jump0: P2 might be zero */
16827 #define OPFLG_INITIALIZER {\
16828 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
16773 -/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
16774 -/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\
16775 -/* 24 */ 0x49, 0x01, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,\
16776 -/* 32 */ 0x41, 0x01, 0x41, 0x41, 0x41, 0x01, 0x41, 0x41,\
16829 +/* 8 */ 0x81, 0x01, 0x01, 0x81, 0x83, 0x83, 0x01, 0x01,\
16830 +/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0xc9, 0xc9, 0xc9,\
16831 +/* 24 */ 0xc9, 0x01, 0x49, 0x49, 0x49, 0x49, 0xc9, 0x49,\
16832 +/* 32 */ 0xc1, 0x01, 0x41, 0x41, 0xc1, 0x01, 0x41, 0x41,\
16833 /* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
16778 -/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16834 +/* 48 */ 0x81, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16835 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\
16836 /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
16837 /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
@@ -16935,6 +16991,8 @@ SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
16991 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
16992 SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*);
16993
16994 +SQLITE_PRIVATE void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val);
16995 +
16996 SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
16997 #ifdef SQLITE_ENABLE_BYTECODE_VTAB
16998 SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
@@ -17522,6 +17580,10 @@ struct FuncDefHash {
17580 };
17581 #define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
17582
17583 +#if defined(SQLITE_USER_AUTHENTICATION)
17584 +# warning "The SQLITE_USER_AUTHENTICATION extension is deprecated. \
17585 + See ext/userauth/user-auth.txt for details."
17586 +#endif
17587 #ifdef SQLITE_USER_AUTHENTICATION
17588 /*
17589 ** Information held in the "sqlite3" database connection object and used
@@ -17825,7 +17887,7 @@ struct sqlite3 {
17887 #define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */
17888 #define SQLITE_Stat4 0x00000800 /* Use STAT4 data */
17889 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
17828 -#define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17890 +#define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */
17891 #define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */
17892 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17893 #define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */
@@ -18398,8 +18460,7 @@ struct Table {
18460 #define TF_HasStored 0x00000040 /* Has one or more STORED columns */
18461 #define TF_HasGenerated 0x00000060 /* Combo: HasVirtual + HasStored */
18462 #define TF_WithoutRowid 0x00000080 /* No rowid. PRIMARY KEY is the key */
18401 -#define TF_StatsUsed 0x00000100 /* Query planner decisions affected by
18402 - ** Index.aiRowLogEst[] values */
18463 +#define TF_MaybeReanalyze 0x00000100 /* Maybe run ANALYZE on this table */
18464 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18465 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18466 #define TF_HasNotNull 0x00000800 /* Contains NOT NULL constraints */
@@ -19199,10 +19260,12 @@ struct IdList {
19260 **
19261 ** Union member validity:
19262 **
19202 -** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
19203 -** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
19204 -** u2.pIBIndex fg.isIndexedBy && !fg.isCte
19205 -** u2.pCteUse fg.isCte && !fg.isIndexedBy
19263 +** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
19264 +** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
19265 +** u1.nRow !fg.isTabFunc && !fg.isIndexedBy
19266 +**
19267 +** u2.pIBIndex fg.isIndexedBy && !fg.isCte
19268 +** u2.pCteUse fg.isCte && !fg.isIndexedBy
19269 */
19270 struct SrcItem {
19271 Schema *pSchema; /* Schema to which this item is fixed */
@@ -19230,6 +19293,7 @@ struct SrcItem {
19293 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19294 unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */
19295 unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
19296 + unsigned rowidUsed :1; /* The ROWID of this table is referenced */
19297 } fg;
19298 int iCursor; /* The VDBE cursor number used to access this table */
19299 union {
@@ -19240,6 +19304,7 @@ struct SrcItem {
19304 union {
19305 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
19306 ExprList *pFuncArg; /* Arguments to table-valued-function */
19307 + u32 nRow; /* Number of rows in a VALUES clause */
19308 } u1;
19309 union {
19310 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
@@ -19304,7 +19369,7 @@ struct SrcList {
19369 #define WHERE_AGG_DISTINCT 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
19370 #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
19371 #define WHERE_RIGHT_JOIN 0x1000 /* Processing a RIGHT JOIN */
19307 - /* 0x2000 not currently used */
19372 +#define WHERE_KEEP_ALL_JOINS 0x2000 /* Do not do the omit-noop-join opt */
19373 #define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
19374 /* 0x8000 not currently used */
19375
@@ -19497,11 +19562,12 @@ struct Select {
19562 #define SF_View 0x0200000 /* SELECT statement is a view */
19563 #define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
19564 #define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
19500 -#define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
19565 +#define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
19566 #define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
19567 #define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
19568 #define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
19569 #define SF_UpdateFrom 0x10000000 /* Query originates with UPDATE FROM */
19570 +#define SF_Correlated 0x20000000 /* True if references the outer context */
19571
19572 /* True if S exists and has SF_NestedFrom */
19573 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
@@ -19741,6 +19807,7 @@ struct Parse {
19807 u8 disableLookaside; /* Number of times lookaside has been disabled */
19808 u8 prepFlags; /* SQLITE_PREPARE_* flags */
19809 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
19810 + u8 bHasWith; /* True if statement contains WITH */
19811 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
19812 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
19813 #endif
@@ -20420,6 +20487,9 @@ struct Window {
20487 ** due to the SQLITE_SUBTYPE flag */
20488 };
20489
20490 +SQLITE_PRIVATE Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow);
20491 +SQLITE_PRIVATE void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal);
20492 +
20493 #ifndef SQLITE_OMIT_WINDOWFUNC
20494 SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
20495 SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
@@ -20737,6 +20807,7 @@ SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
20807 SQLITE_PRIVATE void sqlite3Dequote(char*);
20808 SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
20809 SQLITE_PRIVATE void sqlite3DequoteToken(Token*);
20810 +SQLITE_PRIVATE void sqlite3DequoteNumber(Parse*, Expr*);
20811 SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
20812 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
20813 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*);
@@ -20767,7 +20838,7 @@ SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*)
20838 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
20839 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
20840 SQLITE_PRIVATE void sqlite3ExprDeleteGeneric(sqlite3*,void*);
20770 -SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse*, Expr*);
20841 +SQLITE_PRIVATE int sqlite3ExprDeferredDelete(Parse*, Expr*);
20842 SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
20843 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
20844 SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
@@ -20990,12 +21061,10 @@ SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
21061 SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char*);
21062 SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
21063 SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
20993 -SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
20994 -SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
21064 +SQLITE_PRIVATE int sqlite3ExprIsConstant(Parse*,Expr*);
21065 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
21066 SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
20997 -SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
20998 -SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int);
21067 +SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int);
21068 #ifdef SQLITE_ENABLE_CURSOR_HINTS
21069 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
21070 #endif
@@ -21180,7 +21249,9 @@ SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
21249 SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
21250 SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3*);
21251 SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
21252 +#if !defined(SQLITE_OMIT_BLOB_LITERAL)
21253 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
21254 +#endif
21255 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
21256 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
21257
@@ -24227,13 +24298,14 @@ struct DateTime {
24298 int tz; /* Timezone offset in minutes */
24299 double s; /* Seconds */
24300 char validJD; /* True (1) if iJD is valid */
24230 - char rawS; /* Raw numeric value stored in s */
24301 char validYMD; /* True (1) if Y,M,D are valid */
24302 char validHMS; /* True (1) if h,m,s are valid */
24233 - char validTZ; /* True (1) if tz is valid */
24234 - char tzSet; /* Timezone was set explicitly */
24235 - char isError; /* An overflow has occurred */
24236 - char useSubsec; /* Display subsecond precision */
24303 + char nFloor; /* Days to implement "floor" */
24304 + unsigned rawS : 1; /* Raw numeric value stored in s */
24305 + unsigned isError : 1; /* An overflow has occurred */
24306 + unsigned useSubsec : 1; /* Display subsecond precision */
24307 + unsigned isUtc : 1; /* Time is known to be UTC */
24308 + unsigned isLocal : 1; /* Time is known to be localtime */
24309 };
24310
24311
@@ -24331,6 +24403,8 @@ static int parseTimezone(const char *zDate, DateTime *p){
24403 sgn = +1;
24404 }else if( c=='Z' || c=='z' ){
24405 zDate++;
24406 + p->isLocal = 0;
24407 + p->isUtc = 1;
24408 goto zulu_time;
24409 }else{
24410 return c!=0;
@@ -24343,7 +24417,6 @@ static int parseTimezone(const char *zDate, DateTime *p){
24417 p->tz = sgn*(nMn + nHr*60);
24418 zulu_time:
24419 while( sqlite3Isspace(*zDate) ){ zDate++; }
24346 - p->tzSet = 1;
24420 return *zDate!=0;
24421 }
24422
@@ -24387,7 +24460,6 @@ static int parseHhMmSs(const char *zDate, DateTime *p){
24460 p->m = m;
24461 p->s = s + ms;
24462 if( parseTimezone(zDate, p) ) return 1;
24390 - p->validTZ = (p->tz!=0)?1:0;
24463 return 0;
24464 }
24465
@@ -24434,15 +24506,40 @@ static void computeJD(DateTime *p){
24506 p->validJD = 1;
24507 if( p->validHMS ){
24508 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
24437 - if( p->validTZ ){
24509 + if( p->tz ){
24510 p->iJD -= p->tz*60000;
24511 p->validYMD = 0;
24512 p->validHMS = 0;
24441 - p->validTZ = 0;
24513 + p->tz = 0;
24514 + p->isUtc = 1;
24515 + p->isLocal = 0;
24516 }
24517 }
24518 }
24519
24520 +/*
24521 +** Given the YYYY-MM-DD information current in p, determine if there
24522 +** is day-of-month overflow and set nFloor to the number of days that
24523 +** would need to be subtracted from the date in order to bring the
24524 +** date back to the end of the month.
24525 +*/
24526 +static void computeFloor(DateTime *p){
24527 + assert( p->validYMD || p->isError );
24528 + assert( p->D>=0 && p->D<=31 );
24529 + assert( p->M>=0 && p->M<=12 );
24530 + if( p->D<=28 ){
24531 + p->nFloor = 0;
24532 + }else if( (1<<p->M) & 0x15aa ){
24533 + p->nFloor = 0;
24534 + }else if( p->M!=2 ){
24535 + p->nFloor = (p->D==31);
24536 + }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){
24537 + p->nFloor = p->D - 28;
24538 + }else{
24539 + p->nFloor = p->D - 29;
24540 + }
24541 +}
24542 +
24543 /*
24544 ** Parse dates of the form
24545 **
@@ -24481,12 +24578,16 @@ static int parseYyyyMmDd(const char *zDate, DateTime *p){
24578 p->Y = neg ? -Y : Y;
24579 p->M = M;
24580 p->D = D;
24484 - if( p->validTZ ){
24581 + computeFloor(p);
24582 + if( p->tz ){
24583 computeJD(p);
24584 }
24585 return 0;
24586 }
24587
24588 +
24589 +static void clearYMD_HMS_TZ(DateTime *p); /* Forward declaration */
24590 +
24591 /*
24592 ** Set the time to the current time reported by the VFS.
24593 **
@@ -24496,6 +24597,9 @@ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
24597 p->iJD = sqlite3StmtCurrentTime(context);
24598 if( p->iJD>0 ){
24599 p->validJD = 1;
24600 + p->isUtc = 1;
24601 + p->isLocal = 0;
24602 + clearYMD_HMS_TZ(p);
24603 return 0;
24604 }else{
24605 return 1;
@@ -24634,7 +24738,7 @@ static void computeYMD_HMS(DateTime *p){
24738 static void clearYMD_HMS_TZ(DateTime *p){
24739 p->validYMD = 0;
24740 p->validHMS = 0;
24637 - p->validTZ = 0;
24741 + p->tz = 0;
24742 }
24743
24744 #ifndef SQLITE_OMIT_LOCALTIME
@@ -24766,7 +24870,7 @@ static int toLocaltime(
24870 p->validHMS = 1;
24871 p->validJD = 0;
24872 p->rawS = 0;
24769 - p->validTZ = 0;
24873 + p->tz = 0;
24874 p->isError = 0;
24875 return SQLITE_OK;
24876 }
@@ -24786,12 +24890,12 @@ static const struct {
24890 float rLimit; /* Maximum NNN value for this transform */
24891 float rXform; /* Constant used for this transform */
24892 } aXformType[] = {
24789 - { 6, "second", 4.6427e+14, 1.0 },
24790 - { 6, "minute", 7.7379e+12, 60.0 },
24791 - { 4, "hour", 1.2897e+11, 3600.0 },
24792 - { 3, "day", 5373485.0, 86400.0 },
24793 - { 5, "month", 176546.0, 2592000.0 },
24794 - { 4, "year", 14713.0, 31536000.0 },
24893 + /* 0 */ { 6, "second", 4.6427e+14, 1.0 },
24894 + /* 1 */ { 6, "minute", 7.7379e+12, 60.0 },
24895 + /* 2 */ { 4, "hour", 1.2897e+11, 3600.0 },
24896 + /* 3 */ { 3, "day", 5373485.0, 86400.0 },
24897 + /* 4 */ { 5, "month", 176546.0, 30.0*86400.0 },
24898 + /* 5 */ { 4, "year", 14713.0, 365.0*86400.0 },
24899 };
24900
24901 /*
@@ -24823,14 +24927,20 @@ static void autoAdjustDate(DateTime *p){
24927 ** NNN.NNNN seconds
24928 ** NNN months
24929 ** NNN years
24930 +** +/-YYYY-MM-DD HH:MM:SS.SSS
24931 +** ceiling
24932 +** floor
24933 ** start of month
24934 ** start of year
24935 ** start of week
24936 ** start of day
24937 ** weekday N
24938 ** unixepoch
24939 +** auto
24940 ** localtime
24941 ** utc
24942 +** subsec
24943 +** subsecond
24944 **
24945 ** Return 0 on success and 1 if there is any kind of error. If the error
24946 ** is in a system call (i.e. localtime()), then an error message is written
@@ -24861,6 +24971,37 @@ static int parseModifier(
24971 }
24972 break;
24973 }
24974 + case 'c': {
24975 + /*
24976 + ** ceiling
24977 + **
24978 + ** Resolve day-of-month overflow by rolling forward into the next
24979 + ** month. As this is the default action, this modifier is really
24980 + ** a no-op that is only included for symmetry. See "floor".
24981 + */
24982 + if( sqlite3_stricmp(z, "ceiling")==0 ){
24983 + computeJD(p);
24984 + clearYMD_HMS_TZ(p);
24985 + rc = 0;
24986 + p->nFloor = 0;
24987 + }
24988 + break;
24989 + }
24990 + case 'f': {
24991 + /*
24992 + ** floor
24993 + **
24994 + ** Resolve day-of-month overflow by rolling back to the end of the
24995 + ** previous month.
24996 + */
24997 + if( sqlite3_stricmp(z, "floor")==0 ){
24998 + computeJD(p);
24999 + p->iJD -= p->nFloor*86400000;
25000 + clearYMD_HMS_TZ(p);
25001 + rc = 0;
25002 + }
25003 + break;
25004 + }
25005 case 'j': {
25006 /*
25007 ** julianday
@@ -24887,7 +25028,9 @@ static int parseModifier(
25028 ** show local time.
25029 */
25030 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
24890 - rc = toLocaltime(p, pCtx);
25031 + rc = p->isLocal ? SQLITE_OK : toLocaltime(p, pCtx);
25032 + p->isUtc = 0;
25033 + p->isLocal = 1;
25034 }
25035 break;
25036 }
@@ -24912,7 +25055,7 @@ static int parseModifier(
25055 }
25056 #ifndef SQLITE_OMIT_LOCALTIME
25057 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
24915 - if( p->tzSet==0 ){
25058 + if( p->isUtc==0 ){
25059 i64 iOrigJD; /* Original localtime */
25060 i64 iGuess; /* Guess at the corresponding utc time */
25061 int cnt = 0; /* Safety to prevent infinite loop */
@@ -24935,7 +25078,8 @@ static int parseModifier(
25078 memset(p, 0, sizeof(*p));
25079 p->iJD = iGuess;
25080 p->validJD = 1;
24938 - p->tzSet = 1;
25081 + p->isUtc = 1;
25082 + p->isLocal = 0;
25083 }
25084 rc = SQLITE_OK;
25085 }
@@ -24955,7 +25099,7 @@ static int parseModifier(
25099 && r>=0.0 && r<7.0 && (n=(int)r)==r ){
25100 sqlite3_int64 Z;
25101 computeYMD_HMS(p);
24958 - p->validTZ = 0;
25102 + p->tz = 0;
25103 p->validJD = 0;
25104 computeJD(p);
25105 Z = ((p->iJD + 129600000)/86400000) % 7;
@@ -24995,7 +25139,7 @@ static int parseModifier(
25139 p->h = p->m = 0;
25140 p->s = 0.0;
25141 p->rawS = 0;
24998 - p->validTZ = 0;
25142 + p->tz = 0;
25143 p->validJD = 0;
25144 if( sqlite3_stricmp(z,"month")==0 ){
25145 p->D = 1;
@@ -25066,6 +25210,7 @@ static int parseModifier(
25210 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25211 p->Y += x;
25212 p->M -= x*12;
25213 + computeFloor(p);
25214 computeJD(p);
25215 p->validHMS = 0;
25216 p->validYMD = 0;
@@ -25112,11 +25257,12 @@ static int parseModifier(
25257 z += n;
25258 while( sqlite3Isspace(*z) ) z++;
25259 n = sqlite3Strlen30(z);
25115 - if( n>10 || n<3 ) break;
25260 + if( n<3 || n>10 ) break;
25261 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
25262 computeJD(p);
25263 assert( rc==1 );
25264 rRounder = r<0 ? -0.5 : +0.5;
25265 + p->nFloor = 0;
25266 for(i=0; i<ArraySize(aXformType); i++){
25267 if( aXformType[i].nName==n
25268 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
@@ -25124,21 +25270,24 @@ static int parseModifier(
25270 ){
25271 switch( i ){
25272 case 4: { /* Special processing to add months */
25127 - assert( strcmp(aXformType[i].zName,"month")==0 );
25273 + assert( strcmp(aXformType[4].zName,"month")==0 );
25274 computeYMD_HMS(p);
25275 p->M += (int)r;
25276 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25277 p->Y += x;
25278 p->M -= x*12;
25279 + computeFloor(p);
25280 p->validJD = 0;
25281 r -= (int)r;
25282 break;
25283 }
25284 case 5: { /* Special processing to add years */
25285 int y = (int)r;
25139 - assert( strcmp(aXformType[i].zName,"year")==0 );
25286 + assert( strcmp(aXformType[5].zName,"year")==0 );
25287 computeYMD_HMS(p);
25288 + assert( p->M>=0 && p->M<=12 );
25289 p->Y += y;
25290 + computeFloor(p);
25291 p->validJD = 0;
25292 r -= (int)r;
25293 break;
@@ -25392,22 +25541,83 @@ static void dateFunc(
25541 }
25542 }
25543
25544 +/*
25545 +** Compute the number of days after the most recent January 1.
25546 +**
25547 +** In other words, compute the zero-based day number for the
25548 +** current year:
25549 +**
25550 +** Jan01 = 0, Jan02 = 1, ..., Jan31 = 30, Feb01 = 31, ...
25551 +** Dec31 = 364 or 365.
25552 +*/
25553 +static int daysAfterJan01(DateTime *pDate){
25554 + DateTime jan01 = *pDate;
25555 + assert( jan01.validYMD );
25556 + assert( jan01.validHMS );
25557 + assert( pDate->validJD );
25558 + jan01.validJD = 0;
25559 + jan01.M = 1;
25560 + jan01.D = 1;
25561 + computeJD(&jan01);
25562 + return (int)((pDate->iJD-jan01.iJD+43200000)/86400000);
25563 +}
25564 +
25565 +/*
25566 +** Return the number of days after the most recent Monday.
25567 +**
25568 +** In other words, return the day of the week according
25569 +** to this code:
25570 +**
25571 +** 0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday.
25572 +*/
25573 +static int daysAfterMonday(DateTime *pDate){
25574 + assert( pDate->validJD );
25575 + return (int)((pDate->iJD+43200000)/86400000) % 7;
25576 +}
25577 +
25578 +/*
25579 +** Return the number of days after the most recent Sunday.
25580 +**
25581 +** In other words, return the day of the week according
25582 +** to this code:
25583 +**
25584 +** 0=Sunday, 1=Monday, 2=Tues, ..., 6=Saturday
25585 +*/
25586 +static int daysAfterSunday(DateTime *pDate){
25587 + assert( pDate->validJD );
25588 + return (int)((pDate->iJD+129600000)/86400000) % 7;
25589 +}
25590 +
25591 /*
25592 ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
25593 **
25594 ** Return a string described by FORMAT. Conversions as follows:
25595 **
25400 -** %d day of month
25596 +** %d day of month 01-31
25597 +** %e day of month 1-31
25598 ** %f ** fractional seconds SS.SSS
25599 +** %F ISO date. YYYY-MM-DD
25600 +** %G ISO year corresponding to %V 0000-9999.
25601 +** %g 2-digit ISO year corresponding to %V 00-99
25602 ** %H hour 00-24
25403 -** %j day of year 000-366
25603 +** %k hour 0-24 (leading zero converted to space)
25604 +** %I hour 01-12
25605 +** %j day of year 001-366
25606 ** %J ** julian day number
25607 +** %l hour 1-12 (leading zero converted to space)
25608 ** %m month 01-12
25609 ** %M minute 00-59
25610 +** %p "am" or "pm"
25611 +** %P "AM" or "PM"
25612 +** %R time as HH:MM
25613 ** %s seconds since 1970-01-01
25614 ** %S seconds 00-59
25409 -** %w day of week 0-6 Sunday==0
25410 -** %W week of year 00-53
25615 +** %T time as HH:MM:SS
25616 +** %u day of week 1-7 Monday==1, Sunday==7
25617 +** %w day of week 0-6 Sunday==0, Monday==1
25618 +** %U week of year 00-53 (First Sunday is start of week 01)
25619 +** %V week of year 01-53 (First week containing Thursday is week 01)
25620 +** %W week of year 00-53 (First Monday is start of week 01)
25621 ** %Y year 0000-9999
25622 ** %% %
25623 */
@@ -25444,7 +25654,7 @@ static void strftimeFunc(
25654 sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D);
25655 break;
25656 }
25447 - case 'f': {
25657 + case 'f': { /* Fractional seconds. (Non-standard) */
25658 double s = x.s;
25659 if( s>59.999 ) s = 59.999;
25660 sqlite3_str_appendf(&sRes, "%06.3f", s);
@@ -25454,6 +25664,21 @@ static void strftimeFunc(
25664 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D);
25665 break;
25666 }
25667 + case 'G': /* Fall thru */
25668 + case 'g': {
25669 + DateTime y = x;
25670 + assert( y.validJD );
25671 + /* Move y so that it is the Thursday in the same week as x */
25672 + y.iJD += (3 - daysAfterMonday(&x))*86400000;
25673 + y.validYMD = 0;
25674 + computeYMD(&y);
25675 + if( cf=='g' ){
25676 + sqlite3_str_appendf(&sRes, "%02d", y.Y%100);
25677 + }else{
25678 + sqlite3_str_appendf(&sRes, "%04d", y.Y);
25679 + }
25680 + break;
25681 + }
25682 case 'H':
25683 case 'k': {
25684 sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h);
@@ -25467,25 +25692,11 @@ static void strftimeFunc(
25692 sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h);
25693 break;
25694 }
25470 - case 'W': /* Fall thru */
25471 - case 'j': {
25472 - int nDay; /* Number of days since 1st day of year */
25473 - DateTime y = x;
25474 - y.validJD = 0;
25475 - y.M = 1;
25476 - y.D = 1;
25477 - computeJD(&y);
25478 - nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
25479 - if( cf=='W' ){
25480 - int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
25481 - wd = (int)(((x.iJD+43200000)/86400000)%7);
25482 - sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
25483 - }else{
25484 - sqlite3_str_appendf(&sRes,"%03d",nDay+1);
25485 - }
25695 + case 'j': { /* Day of year. Jan01==1, Jan02==2, and so forth */
25696 + sqlite3_str_appendf(&sRes,"%03d",daysAfterJan01(&x)+1);
25697 break;
25698 }
25488 - case 'J': {
25699 + case 'J': { /* Julian day number. (Non-standard) */
25700 sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0);
25701 break;
25702 }
@@ -25528,13 +25739,33 @@ static void strftimeFunc(
25739 sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s);
25740 break;
25741 }
25531 - case 'u': /* Fall thru */
25532 - case 'w': {
25533 - char c = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
25742 + case 'u': /* Day of week. 1 to 7. Monday==1, Sunday==7 */
25743 + case 'w': { /* Day of week. 0 to 6. Sunday==0, Monday==1 */
25744 + char c = (char)daysAfterSunday(&x) + '0';
25745 if( c=='0' && cf=='u' ) c = '7';
25746 sqlite3_str_appendchar(&sRes, 1, c);
25747 break;
25748 }
25749 + case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */
25750 + sqlite3_str_appendf(&sRes,"%02d",
25751 + (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7);
25752 + break;
25753 + }
25754 + case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */
25755 + DateTime y = x;
25756 + /* Adjust y so that is the Thursday in the same week as x */
25757 + assert( y.validJD );
25758 + y.iJD += (3 - daysAfterMonday(&x))*86400000;
25759 + y.validYMD = 0;
25760 + computeYMD(&y);
25761 + sqlite3_str_appendf(&sRes,"%02d", daysAfterJan01(&y)/7+1);
25762 + break;
25763 + }
25764 + case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */
25765 + sqlite3_str_appendf(&sRes,"%02d",
25766 + (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7);
25767 + break;
25768 + }
25769 case 'Y': {
25770 sqlite3_str_appendf(&sRes,"%04d",x.Y);
25771 break;
@@ -25681,9 +25912,7 @@ static void timediffFunc(
25912 d1.iJD = d2.iJD - d1.iJD;
25913 d1.iJD += (u64)1486995408 * (u64)100000;
25914 }
25684 - d1.validYMD = 0;
25685 - d1.validHMS = 0;
25686 - d1.validTZ = 0;
25915 + clearYMD_HMS_TZ(&d1);
25916 computeYMD_HMS(&d1);
25917 sqlite3StrAccumInit(&sRes, 0, 0, 0, 100);
25918 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f",
@@ -25752,6 +25981,36 @@ static void currentTimeFunc(
25981 }
25982 #endif
25983
25984 +#if !defined(SQLITE_OMIT_DATETIME_FUNCS) && defined(SQLITE_DEBUG)
25985 +/*
25986 +** datedebug(...)
25987 +**
25988 +** This routine returns JSON that describes the internal DateTime object.
25989 +** Used for debugging and testing only. Subject to change.
25990 +*/
25991 +static void datedebugFunc(
25992 + sqlite3_context *context,
25993 + int argc,
25994 + sqlite3_value **argv
25995 +){
25996 + DateTime x;
25997 + if( isDate(context, argc, argv, &x)==0 ){
25998 + char *zJson;
25999 + zJson = sqlite3_mprintf(
26000 + "{iJD:%lld,Y:%d,M:%d,D:%d,h:%d,m:%d,tz:%d,"
26001 + "s:%.3f,validJD:%d,validYMS:%d,validHMS:%d,"
26002 + "nFloor:%d,rawS:%d,isError:%d,useSubsec:%d,"
26003 + "isUtc:%d,isLocal:%d}",
26004 + x.iJD, x.Y, x.M, x.D, x.h, x.m, x.tz,
26005 + x.s, x.validJD, x.validYMD, x.validHMS,
26006 + x.nFloor, x.rawS, x.isError, x.useSubsec,
26007 + x.isUtc, x.isLocal);
26008 + sqlite3_result_text(context, zJson, -1, sqlite3_free);
26009 + }
26010 +}
26011 +#endif /* !SQLITE_OMIT_DATETIME_FUNCS && SQLITE_DEBUG */
26012 +
26013 +
26014 /*
26015 ** This function registered all of the above C functions as SQL
26016 ** functions. This should be the only routine in this file with
@@ -25767,6 +26026,9 @@ SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
26026 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
26027 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
26028 PURE_DATE(timediff, 2, 0, 0, timediffFunc ),
26029 +#ifdef SQLITE_DEBUG
26030 + PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ),
26031 +#endif
26032 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
26033 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
26034 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
@@ -30182,6 +30444,24 @@ static void sqlite3MallocAlarm(int nByte){
30444 sqlite3_mutex_enter(mem0.mutex);
30445 }
30446
30447 +#ifdef SQLITE_DEBUG
30448 +/*
30449 +** This routine is called whenever an out-of-memory condition is seen,
30450 +** It's only purpose to to serve as a breakpoint for gdb or similar
30451 +** code debuggers when working on out-of-memory conditions, for example
30452 +** caused by PRAGMA hard_heap_limit=N.
30453 +*/
30454 +static SQLITE_NOINLINE void test_oom_breakpoint(u64 n){
30455 + static u64 nOomFault = 0;
30456 + nOomFault += n;
30457 + /* The assert() is never reached in a human lifetime. It is here mostly
30458 + ** to prevent code optimizers from optimizing out this function. */
30459 + assert( (nOomFault>>32) < 0xffffffff );
30460 +}
30461 +#else
30462 +# define test_oom_breakpoint(X) /* No-op for production builds */
30463 +#endif
30464 +
30465 /*
30466 ** Do a memory allocation with statistics and alarms. Assume the
30467 ** lock is already held.
@@ -30208,6 +30488,7 @@ static void mallocWithAlarm(int n, void **pp){
30488 if( mem0.hardLimit ){
30489 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
30490 if( nUsed >= mem0.hardLimit - nFull ){
30491 + test_oom_breakpoint(1);
30492 *pp = 0;
30493 return;
30494 }
@@ -30496,6 +30777,7 @@ SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
30777 sqlite3MallocAlarm(nDiff);
30778 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
30779 sqlite3_mutex_leave(mem0.mutex);
30780 + test_oom_breakpoint(1);
30781 return 0;
30782 }
30783 }
@@ -31398,13 +31680,14 @@ SQLITE_API void sqlite3_str_vappendf(
31680 }
31681
31682 exp = s.iDP-1;
31401 - if( xtype==etGENERIC && precision>0 ) precision--;
31683
31684 /*
31685 ** If the field type is etGENERIC, then convert to either etEXP
31686 ** or etFLOAT, as appropriate.
31687 */
31688 if( xtype==etGENERIC ){
31689 + assert( precision>0 );
31690 + precision--;
31691 flag_rtz = !flag_alternateform;
31692 if( exp<-4 || exp>precision ){
31693 xtype = etEXP;
@@ -31720,9 +32003,13 @@ SQLITE_API void sqlite3_str_vappendf(
32003 sqlite3_str_appendall(pAccum, pItem->zAlias);
32004 }else{
32005 Select *pSel = pItem->pSelect;
31723 - assert( pSel!=0 );
32006 + assert( pSel!=0 ); /* Because of tag-20240424-1 */
32007 if( pSel->selFlags & SF_NestedFrom ){
32008 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
32009 + }else if( pSel->selFlags & SF_MultiValue ){
32010 + assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy );
32011 + sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
32012 + pItem->u1.nRow);
32013 }else{
32014 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
32015 }
@@ -32499,8 +32786,10 @@ SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc)
32786 x.printfFlags |= SQLITE_PRINTF_INTERNAL;
32787 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
32788 if( pItem->pTab ){
32502 - sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
32503 - pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
32789 + sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx%s",
32790 + pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab,
32791 + pItem->colUsed,
32792 + pItem->fg.rowidUsed ? "+rowid" : "");
32793 }
32794 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){
32795 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
@@ -32540,12 +32829,14 @@ SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc)
32829 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
32830 }
32831 if( pItem->pSelect ){
32832 + sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc);
32833 if( pItem->pTab ){
32834 Table *pTab = pItem->pTab;
32835 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
32836 }
32837 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
32838 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
32839 + sqlite3TreeViewPop(&pView);
32840 }
32841 if( pItem->fg.isTabFunc ){
32842 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
@@ -32649,7 +32940,7 @@ SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 m
32940 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
32941 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
32942 if( p->pLimit->pRight ){
32652 - sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
32943 + sqlite3TreeViewItem(pView, "OFFSET", 0);
32944 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
32945 sqlite3TreeViewPop(&pView);
32946 }
@@ -34950,6 +35241,44 @@ SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
35241 sqlite3Dequote(p->u.zToken);
35242 }
35243
35244 +/*
35245 +** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
35246 +** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those
35247 +** that contain '_' characters that must be removed before further processing.
35248 +*/
35249 +SQLITE_PRIVATE void sqlite3DequoteNumber(Parse *pParse, Expr *p){
35250 + assert( p!=0 || pParse->db->mallocFailed );
35251 + if( p ){
35252 + const char *pIn = p->u.zToken;
35253 + char *pOut = p->u.zToken;
35254 + int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X'));
35255 + int iValue;
35256 + assert( p->op==TK_QNUMBER );
35257 + p->op = TK_INTEGER;
35258 + do {
35259 + if( *pIn!=SQLITE_DIGIT_SEPARATOR ){
35260 + *pOut++ = *pIn;
35261 + if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT;
35262 + }else{
35263 + if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1])))
35264 + || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1])))
35265 + ){
35266 + sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken);
35267 + }
35268 + }
35269 + }while( *pIn++ );
35270 + if( bHex ) p->op = TK_INTEGER;
35271 +
35272 + /* tag-20240227-a: If after dequoting, the number is an integer that
35273 + ** fits in 32 bits, then it must be converted into EP_IntValue. Other
35274 + ** parts of the code expect this. See also tag-20240227-b. */
35275 + if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){
35276 + p->u.iValue = iValue;
35277 + p->flags |= EP_IntValue;
35278 + }
35279 + }
35280 +}
35281 +
35282 /*
35283 ** If the input token p is quoted, try to adjust the token to remove
35284 ** the quotes. This is not always possible:
@@ -36889,7 +37218,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
37218 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
37219 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
37220 /* 32 */ "Last" OpHelp(""),
36892 - /* 33 */ "IfSmaller" OpHelp(""),
37221 + /* 33 */ "IfSizeBetween" OpHelp(""),
37222 /* 34 */ "SorterSort" OpHelp(""),
37223 /* 35 */ "Sort" OpHelp(""),
37224 /* 36 */ "Rewind" OpHelp(""),
@@ -36934,7 +37263,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
37263 /* 75 */ "Null" OpHelp("r[P2..P3]=NULL"),
37264 /* 76 */ "SoftNull" OpHelp("r[P1]=NULL"),
37265 /* 77 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
36937 - /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
37266 + /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1)"),
37267 /* 79 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
37268 /* 80 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
37269 /* 81 */ "SCopy" OpHelp("r[P2]=r[P1]"),
@@ -39332,8 +39661,12 @@ static int unixLogErrorAtLine(
39661 ** available, the error message will often be an empty string. Not a
39662 ** huge problem. Incorrectly concluding that the GNU version is available
39663 ** could lead to a segfault though.
39664 + **
39665 + ** Forum post 3f13857fa4062301 reports that the Android SDK may use
39666 + ** int-type return, depending on its version.
39667 */
39336 -#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
39668 +#if (defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)) \
39669 + && !defined(ANDROID) && !defined(__ANDROID__)
39670 zErr =
39671 # endif
39672 strerror_r(iErrno, aErr, sizeof(aErr)-1);
@@ -44431,12 +44764,19 @@ static int unixOpen(
44764 rc = SQLITE_READONLY_DIRECTORY;
44765 }else if( errno!=EISDIR && isReadWrite ){
44766 /* Failed to open the file for read/write access. Try read-only. */
44767 + UnixUnusedFd *pReadonly = 0;
44768 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
44769 openFlags &= ~(O_RDWR|O_CREAT);
44770 flags |= SQLITE_OPEN_READONLY;
44771 openFlags |= O_RDONLY;
44772 isReadonly = 1;
44439 - fd = robust_open(zName, openFlags, openMode);
44773 + pReadonly = findReusableFd(zName, flags);
44774 + if( pReadonly ){
44775 + fd = pReadonly->fd;
44776 + sqlite3_free(pReadonly);
44777 + }else{
44778 + fd = robust_open(zName, openFlags, openMode);
44779 + }
44780 }
44781 }
44782 if( fd<0 ){
@@ -69887,6 +70227,7 @@ struct IntegrityCk {
70227 StrAccum errMsg; /* Accumulate the error message text here */
70228 u32 *heap; /* Min-heap used for analyzing cell coverage */
70229 sqlite3 *db; /* Database connection running the check */
70230 + i64 nRow; /* Number of rows visited in current tree */
70231 };
70232
70233 /*
@@ -70361,8 +70702,47 @@ int corruptPageError(int lineno, MemPage *p){
70702 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
70703 #endif
70704
70705 +/* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
70706 +** or if the lock tracking is disabled. This is always the value for
70707 +** release builds.
70708 +*/
70709 +#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
70710 +
70711 #ifndef SQLITE_OMIT_SHARED_CACHE
70712
70713 +#if 0
70714 +/* ^---- Change to 1 and recompile to enable shared-lock tracing
70715 +** for debugging purposes.
70716 +**
70717 +** Print all shared-cache locks on a BtShared. Debugging use only.
70718 +*/
70719 +static void sharedLockTrace(
70720 + BtShared *pBt,
70721 + const char *zMsg,
70722 + int iRoot,
70723 + int eLockType
70724 +){
70725 + BtLock *pLock;
70726 + if( iRoot>0 ){
70727 + printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK?"R":"W");
70728 + }else{
70729 + printf("%s-%p:", zMsg, pBt);
70730 + }
70731 + for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
70732 + printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
70733 + pLock->eLock==READ_LOCK ? "R" : "W");
70734 + while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
70735 + pLock = pLock->pNext;
70736 + printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK ? "R" : "W");
70737 + }
70738 + }
70739 + printf("\n");
70740 + fflush(stdout);
70741 +}
70742 +#undef SHARED_LOCK_TRACE
70743 +#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) sharedLockTrace(X,MSG,TAB,TYPE)
70744 +#endif /* Shared-lock tracing */
70745 +
70746 #ifdef SQLITE_DEBUG
70747 /*
70748 **** This function is only used as part of an assert() statement. ***
@@ -70439,6 +70819,8 @@ static int hasSharedCacheTableLock(
70819 iTab = iRoot;
70820 }
70821
70822 + SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
70823 +
70824 /* Search for the required lock. Either a write-lock on root-page iTab, a
70825 ** write-lock on the schema table, or (if the client is reading) a
70826 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
@@ -70572,6 +70954,8 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
70954 BtLock *pLock = 0;
70955 BtLock *pIter;
70956
70957 + SHARED_LOCK_TRACE(pBt,"setLock", iTable, eLock);
70958 +
70959 assert( sqlite3BtreeHoldsMutex(p) );
70960 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
70961 assert( p->db!=0 );
@@ -70639,6 +71023,8 @@ static void clearAllSharedCacheTableLocks(Btree *p){
71023 assert( p->sharable || 0==*ppIter );
71024 assert( p->inTrans>0 );
71025
71026 + SHARED_LOCK_TRACE(pBt, "clearAllLocks", 0, 0);
71027 +
71028 while( *ppIter ){
71029 BtLock *pLock = *ppIter;
71030 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
@@ -70677,6 +71063,9 @@ static void clearAllSharedCacheTableLocks(Btree *p){
71063 */
71064 static void downgradeAllSharedCacheTableLocks(Btree *p){
71065 BtShared *pBt = p->pBt;
71066 +
71067 + SHARED_LOCK_TRACE(pBt, "downgradeLocks", 0, 0);
71068 +
71069 if( pBt->pWriter==p ){
71070 BtLock *pLock;
71071 pBt->pWriter = 0;
@@ -75290,9 +75679,12 @@ static int accessPayload(
75679 if( pCur->aOverflow==0
75680 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
75681 ){
75293 - Pgno *aNew = (Pgno*)sqlite3Realloc(
75294 - pCur->aOverflow, nOvfl*2*sizeof(Pgno)
75295 - );
75682 + Pgno *aNew;
75683 + if( sqlite3FaultSim(413) ){
75684 + aNew = 0;
75685 + }else{
75686 + aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
75687 + }
75688 if( aNew==0 ){
75689 return SQLITE_NOMEM_BKPT;
75690 }else{
@@ -75302,6 +75694,12 @@ static int accessPayload(
75694 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
75695 pCur->curFlags |= BTCF_ValidOvfl;
75696 }else{
75697 + /* Sanity check the validity of the overflow page cache */
75698 + assert( pCur->aOverflow[0]==nextPage
75699 + || pCur->aOverflow[0]==0
75700 + || CORRUPT_DB );
75701 + assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 );
75702 +
75703 /* If the overflow page-list cache has been allocated and the
75704 ** entry for the first required overflow page is valid, skip
75705 ** directly to it.
@@ -75783,6 +76181,23 @@ SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
76181 return rc;
76182 }
76183
76184 +#ifdef SQLITE_DEBUG
76185 +/* The cursors is CURSOR_VALID and has BTCF_AtLast set. Verify that
76186 +** this flags are true for a consistent database.
76187 +**
76188 +** This routine is is called from within assert() statements only.
76189 +** It is an internal verification routine and does not appear in production
76190 +** builds.
76191 +*/
76192 +static int cursorIsAtLastEntry(BtCursor *pCur){
76193 + int ii;
76194 + for(ii=0; ii<pCur->iPage; ii++){
76195 + if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
76196 + }
76197 + return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
76198 +}
76199 +#endif
76200 +
76201 /* Move the cursor to the last entry in the table. Return SQLITE_OK
76202 ** on success. Set *pRes to 0 if the cursor actually points to something
76203 ** or set *pRes to 1 if the table is empty.
@@ -75811,18 +76226,7 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
76226
76227 /* If the cursor already points to the last entry, this is a no-op. */
76228 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
75814 -#ifdef SQLITE_DEBUG
75815 - /* This block serves to assert() that the cursor really does point
75816 - ** to the last entry in the b-tree. */
75817 - int ii;
75818 - for(ii=0; ii<pCur->iPage; ii++){
75819 - assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
75820 - }
75821 - assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
75822 - testcase( pCur->ix!=pCur->pPage->nCell-1 );
75823 - /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
75824 - assert( pCur->pPage->leaf );
75825 -#endif
76229 + assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB );
76230 *pRes = 0;
76231 return SQLITE_OK;
76232 }
@@ -75875,6 +76279,7 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
76279 }
76280 if( pCur->info.nKey<intKey ){
76281 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
76282 + assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB );
76283 *pRes = -1;
76284 return SQLITE_OK;
76285 }
@@ -76341,10 +76746,10 @@ SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
76746 assert( cursorOwnsBtShared(pCur) );
76747 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76748
76344 - /* Currently this interface is only called by the OP_IfSmaller
76345 - ** opcode, and it that case the cursor will always be valid and
76346 - ** will always point to a leaf node. */
76347 - if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
76749 + /* Currently this interface is only called by the OP_IfSizeBetween
76750 + ** opcode and the OP_Count opcode with P3=1. In either case,
76751 + ** the cursor will always be valid unless the btree is empty. */
76752 + if( pCur->eState!=CURSOR_VALID ) return 0;
76753 if( NEVER(pCur->pPage->leaf==0) ) return -1;
76754
76755 n = pCur->pPage->nCell;
@@ -78475,7 +78880,7 @@ static int balance_nonroot(
78880 ** table-interior, index-leaf, or index-interior).
78881 */
78882 if( pOld->aData[0]!=apOld[0]->aData[0] ){
78478 - rc = SQLITE_CORRUPT_BKPT;
78883 + rc = SQLITE_CORRUPT_PAGE(pOld);
78884 goto balance_cleanup;
78885 }
78886
@@ -78499,7 +78904,7 @@ static int balance_nonroot(
78904 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
78905 if( pOld->nOverflow>0 ){
78906 if( NEVER(limit<pOld->aiOvfl[0]) ){
78502 - rc = SQLITE_CORRUPT_BKPT;
78907 + rc = SQLITE_CORRUPT_PAGE(pOld);
78908 goto balance_cleanup;
78909 }
78910 limit = pOld->aiOvfl[0];
@@ -79142,7 +79547,7 @@ static int anotherValidCursor(BtCursor *pCur){
79547 && pOther->eState==CURSOR_VALID
79548 && pOther->pPage==pCur->pPage
79549 ){
79145 - return SQLITE_CORRUPT_BKPT;
79550 + return SQLITE_CORRUPT_PAGE(pCur->pPage);
79551 }
79552 }
79553 return SQLITE_OK;
@@ -79202,7 +79607,7 @@ static int balance(BtCursor *pCur){
79607 /* The page being written is not a root page, and there is currently
79608 ** more than one reference to it. This only happens if the page is one
79609 ** of its own ancestor pages. Corruption. */
79205 - rc = SQLITE_CORRUPT_BKPT;
79610 + rc = SQLITE_CORRUPT_PAGE(pPage);
79611 }else{
79612 MemPage * const pParent = pCur->apPage[iPage-1];
79613 int const iIdx = pCur->aiIdx[iPage-1];
@@ -79366,7 +79771,7 @@ static SQLITE_NOINLINE int btreeOverwriteOverflowCell(
79771 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
79772 if( rc ) return rc;
79773 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
79369 - rc = SQLITE_CORRUPT_BKPT;
79774 + rc = SQLITE_CORRUPT_PAGE(pPage);
79775 }else{
79776 if( iOffset+ovflPageSize<(u32)nTotal ){
79777 ovflPgno = get4byte(pPage->aData);
@@ -79394,7 +79799,7 @@ static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
79799 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
79800 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
79801 ){
79397 - return SQLITE_CORRUPT_BKPT;
79802 + return SQLITE_CORRUPT_PAGE(pPage);
79803 }
79804 if( pCur->info.nLocal==nTotal ){
79805 /* The entire cell is local */
@@ -79475,7 +79880,7 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
79880 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
79881 ** the schema was loaded. This cannot be asserted though, as a user might
79882 ** set the flag, load the schema, and then unset the flag. */
79478 - return SQLITE_CORRUPT_BKPT;
79883 + return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
79884 }
79885 }
79886
@@ -79598,7 +80003,7 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
80003 if( pPage->nFree<0 ){
80004 if( NEVER(pCur->eState>CURSOR_INVALID) ){
80005 /* ^^^^^--- due to the moveToRoot() call above */
79601 - rc = SQLITE_CORRUPT_BKPT;
80006 + rc = SQLITE_CORRUPT_PAGE(pPage);
80007 }else{
80008 rc = btreeComputeFreeSpace(pPage);
80009 }
@@ -79640,7 +80045,7 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
80045 CellInfo info;
80046 assert( idx>=0 );
80047 if( idx>=pPage->nCell ){
79643 - return SQLITE_CORRUPT_BKPT;
80048 + return SQLITE_CORRUPT_PAGE(pPage);
80049 }
80050 rc = sqlite3PagerWrite(pPage->pDbPage);
80051 if( rc ){
@@ -79667,10 +80072,10 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
80072 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
80073 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
80074 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
79670 - return SQLITE_CORRUPT_BKPT;
80075 + return SQLITE_CORRUPT_PAGE(pPage);
80076 }
80077 if( oldCell+szNew > pPage->aDataEnd ){
79673 - return SQLITE_CORRUPT_BKPT;
80078 + return SQLITE_CORRUPT_PAGE(pPage);
80079 }
80080 memcpy(oldCell, newCell, szNew);
80081 return SQLITE_OK;
@@ -79772,7 +80177,7 @@ SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64
80177 nIn = pSrc->info.nLocal;
80178 aIn = pSrc->info.pPayload;
80179 if( aIn+nIn>pSrc->pPage->aDataEnd ){
79775 - return SQLITE_CORRUPT_BKPT;
80180 + return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80181 }
80182 nRem = pSrc->info.nPayload;
80183 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
@@ -79797,7 +80202,7 @@ SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64
80202
80203 if( nRem>nIn ){
80204 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
79800 - return SQLITE_CORRUPT_BKPT;
80205 + return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80206 }
80207 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
80208 }
@@ -79893,7 +80298,7 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
80298 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
80299 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
80300 }else{
79896 - return SQLITE_CORRUPT_BKPT;
80301 + return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
80302 }
80303 }
80304 assert( pCur->eState==CURSOR_VALID );
@@ -79902,14 +80307,14 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
80307 iCellIdx = pCur->ix;
80308 pPage = pCur->pPage;
80309 if( pPage->nCell<=iCellIdx ){
79905 - return SQLITE_CORRUPT_BKPT;
80310 + return SQLITE_CORRUPT_PAGE(pPage);
80311 }
80312 pCell = findCell(pPage, iCellIdx);
80313 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
79909 - return SQLITE_CORRUPT_BKPT;
80314 + return SQLITE_CORRUPT_PAGE(pPage);
80315 }
80316 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
79912 - return SQLITE_CORRUPT_BKPT;
80317 + return SQLITE_CORRUPT_PAGE(pPage);
80318 }
80319
80320 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
@@ -80000,7 +80405,7 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
80405 n = pCur->pPage->pgno;
80406 }
80407 pCell = findCell(pLeaf, pLeaf->nCell-1);
80003 - if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
80408 + if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf);
80409 nCell = pLeaf->xCellSize(pLeaf, pCell);
80410 assert( MX_CELL_SIZE(pBt) >= nCell );
80411 pTmp = pBt->pTmpSpace;
@@ -80116,7 +80521,7 @@ static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
80521 */
80522 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
80523 if( pgnoRoot>btreePagecount(pBt) ){
80119 - return SQLITE_CORRUPT_BKPT;
80524 + return SQLITE_CORRUPT_PGNO(pgnoRoot);
80525 }
80526 pgnoRoot++;
80527
@@ -80164,7 +80569,7 @@ static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
80569 }
80570 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
80571 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
80167 - rc = SQLITE_CORRUPT_BKPT;
80572 + rc = SQLITE_CORRUPT_PGNO(pgnoRoot);
80573 }
80574 if( rc!=SQLITE_OK ){
80575 releasePage(pRoot);
@@ -80254,14 +80659,14 @@ static int clearDatabasePage(
80659
80660 assert( sqlite3_mutex_held(pBt->mutex) );
80661 if( pgno>btreePagecount(pBt) ){
80257 - return SQLITE_CORRUPT_BKPT;
80662 + return SQLITE_CORRUPT_PGNO(pgno);
80663 }
80664 rc = getAndInitPage(pBt, pgno, &pPage, 0);
80665 if( rc ) return rc;
80666 if( (pBt->openFlags & BTREE_SINGLE)==0
80667 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
80668 ){
80264 - rc = SQLITE_CORRUPT_BKPT;
80669 + rc = SQLITE_CORRUPT_PAGE(pPage);
80670 goto cleardatabasepage_out;
80671 }
80672 hdr = pPage->hdrOffset;
@@ -80365,7 +80770,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
80770 assert( p->inTrans==TRANS_WRITE );
80771 assert( iTable>=2 );
80772 if( iTable>btreePagecount(pBt) ){
80368 - return SQLITE_CORRUPT_BKPT;
80773 + return SQLITE_CORRUPT_PGNO(iTable);
80774 }
80775
80776 rc = sqlite3BtreeClearTable(p, iTable, 0);
@@ -80959,6 +81364,9 @@ static int checkTreePage(
81364 ** number of cells on the page. */
81365 nCell = get2byte(&data[hdr+3]);
81366 assert( pPage->nCell==nCell );
81367 + if( pPage->leaf || pPage->intKey==0 ){
81368 + pCheck->nRow += nCell;
81369 + }
81370
81371 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
81372 ** immediately follows the b-tree page header. */
@@ -81070,6 +81478,7 @@ static int checkTreePage(
81478 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
81479 }
81480 }
81481 + assert( heap!=0 );
81482 /* Add the freeblocks to the min-heap
81483 **
81484 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
@@ -81169,6 +81578,7 @@ SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
81578 sqlite3 *db, /* Database connection that is running the check */
81579 Btree *p, /* The btree to be checked */
81580 Pgno *aRoot, /* An array of root pages numbers for individual trees */
81581 + Mem *aCnt, /* Memory cells to write counts for each tree to */
81582 int nRoot, /* Number of entries in aRoot[] */
81583 int mxErr, /* Stop reporting errors after this many */
81584 int *pnErr, /* OUT: Write number of errors seen to this variable */
@@ -81182,7 +81592,9 @@ SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
81592 int bPartial = 0; /* True if not checking all btrees */
81593 int bCkFreelist = 1; /* True to scan the freelist */
81594 VVA_ONLY( int nRef );
81595 +
81596 assert( nRoot>0 );
81597 + assert( aCnt!=0 );
81598
81599 /* aRoot[0]==0 means this is a partial check */
81600 if( aRoot[0]==0 ){
@@ -81255,15 +81667,18 @@ SQLITE_PRIVATE int sqlite3BtreeIntegrityCheck(
81667 testcase( pBt->db->flags & SQLITE_CellSizeCk );
81668 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
81669 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
81258 - i64 notUsed;
81259 - if( aRoot[i]==0 ) continue;
81670 + sCheck.nRow = 0;
81671 + if( aRoot[i] ){
81672 + i64 notUsed;
81673 #ifndef SQLITE_OMIT_AUTOVACUUM
81261 - if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
81262 - checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
81263 - }
81674 + if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
81675 + checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
81676 + }
81677 #endif
81265 - sCheck.v0 = aRoot[i];
81266 - checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
81678 + sCheck.v0 = aRoot[i];
81679 + checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
81680 + }
81681 + sqlite3MemSetArrayInt64(aCnt, i, sCheck.nRow);
81682 }
81683 pBt->db->flags = savedDbFlags;
81684
@@ -83318,6 +83733,13 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
83733 }
83734 }
83735
83736 +/*
83737 +** Set the iIdx'th entry of array aMem[] to contain integer value val.
83738 +*/
83739 +SQLITE_PRIVATE void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val){
83740 + sqlite3VdbeMemSetInt64(&aMem[iIdx], val);
83741 +}
83742 +
83743 /* A no-op destructor */
83744 SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
83745
@@ -84006,14 +84428,20 @@ static int valueFromExpr(
84428 }
84429
84430 /* Handle negative integers in a single step. This is needed in the
84009 - ** case when the value is -9223372036854775808.
84010 - */
84011 - if( op==TK_UMINUS
84012 - && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
84013 - pExpr = pExpr->pLeft;
84014 - op = pExpr->op;
84015 - negInt = -1;
84016 - zNeg = "-";
84431 + ** case when the value is -9223372036854775808. Except - do not do this
84432 + ** for hexadecimal literals. */
84433 + if( op==TK_UMINUS ){
84434 + Expr *pLeft = pExpr->pLeft;
84435 + if( (pLeft->op==TK_INTEGER || pLeft->op==TK_FLOAT) ){
84436 + if( ExprHasProperty(pLeft, EP_IntValue)
84437 + || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
84438 + ){
84439 + pExpr = pLeft;
84440 + op = pExpr->op;
84441 + negInt = -1;
84442 + zNeg = "-";
84443 + }
84444 + }
84445 }
84446
84447 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
@@ -84022,12 +84450,26 @@ static int valueFromExpr(
84450 if( ExprHasProperty(pExpr, EP_IntValue) ){
84451 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
84452 }else{
84025 - zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
84026 - if( zVal==0 ) goto no_mem;
84027 - sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
84453 + i64 iVal;
84454 + if( op==TK_INTEGER && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
84455 + sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
84456 + }else{
84457 + zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
84458 + if( zVal==0 ) goto no_mem;
84459 + sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
84460 + }
84461 }
84029 - if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
84030 - sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
84462 + if( affinity==SQLITE_AFF_BLOB ){
84463 + if( op==TK_FLOAT ){
84464 + assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
84465 + sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8);
84466 + pVal->flags = MEM_Real;
84467 + }else if( op==TK_INTEGER ){
84468 + /* This case is required by -9223372036854775808 and other strings
84469 + ** that look like integers but cannot be handled by the
84470 + ** sqlite3DecOrHexToI64() call above. */
84471 + sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
84472 + }
84473 }else{
84474 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
84475 }
@@ -84297,17 +84739,17 @@ SQLITE_PRIVATE int sqlite3Stat4Column(
84739 sqlite3_value **ppVal /* OUT: Extracted value */
84740 ){
84741 u32 t = 0; /* a column type code */
84300 - int nHdr; /* Size of the header in the record */
84301 - int iHdr; /* Next unread header byte */
84302 - int iField; /* Next unread data byte */
84303 - int szField = 0; /* Size of the current data field */
84742 + u32 nHdr; /* Size of the header in the record */
84743 + u32 iHdr; /* Next unread header byte */
84744 + i64 iField; /* Next unread data byte */
84745 + u32 szField = 0; /* Size of the current data field */
84746 int i; /* Column index */
84747 u8 *a = (u8*)pRec; /* Typecast byte array */
84748 Mem *pMem = *ppVal; /* Write result into this Mem object */
84749
84750 assert( iCol>0 );
84751 iHdr = getVarint32(a, nHdr);
84310 - if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
84752 + if( nHdr>(u32)nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
84753 iField = nHdr;
84754 for(i=0; i<=iCol; i++){
84755 iHdr += getVarint32(&a[iHdr], t);
@@ -85342,6 +85784,15 @@ static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
85784 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
85785 pOp->p2 = aLabel[ADDR(pOp->p2)];
85786 }
85787 +
85788 + /* OPFLG_JUMP opcodes never have P2==0, though OPFLG_JUMP0 opcodes
85789 + ** might */
85790 + assert( pOp->p2>0
85791 + || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 );
85792 +
85793 + /* Jumps never go off the end of the bytecode array */
85794 + assert( pOp->p2<p->nOp
85795 + || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 );
85796 break;
85797 }
85798 }
@@ -87749,7 +88200,7 @@ SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
88200
88201 /* Check for immediate foreign key violations. */
88202 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
87752 - sqlite3VdbeCheckFk(p, 0);
88203 + (void)sqlite3VdbeCheckFk(p, 0);
88204 }
88205
88206 /* If the auto-commit flag is set and this is the only active writer
@@ -88919,17 +89370,15 @@ SQLITE_PRIVATE int sqlite3IntFloatCompare(i64 i, double r){
89370 return (x<r) ? -1 : (x>r);
89371 }else{
89372 i64 y;
88922 - double s;
89373 if( r<-9223372036854775808.0 ) return +1;
89374 if( r>=9223372036854775808.0 ) return -1;
89375 y = (i64)r;
89376 if( i<y ) return -1;
89377 if( i>y ) return +1;
88928 - s = (double)i;
88929 - testcase( doubleLt(s,r) );
88930 - testcase( doubleLt(r,s) );
88931 - testcase( doubleEq(r,s) );
88932 - return (s<r) ? -1 : (s>r);
89378 + testcase( doubleLt(((double)i),r) );
89379 + testcase( doubleLt(r,((double)i)) );
89380 + testcase( doubleEq(r,((double)i)) );
89381 + return (((double)i)<r) ? -1 : (((double)i)>r);
89382 }
89383 }
89384
@@ -89732,7 +90181,8 @@ SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff
90181 assert( iVar>0 );
90182 if( v ){
90183 Mem *pMem = &v->aVar[iVar-1];
89735 - assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
90184 + assert( (v->db->flags & SQLITE_EnableQPSG)==0
90185 + || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
90186 if( 0==(pMem->flags & MEM_Null) ){
90187 sqlite3_value *pRet = sqlite3ValueNew(v->db);
90188 if( pRet ){
@@ -89752,7 +90202,8 @@ SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff
90202 */
90203 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
90204 assert( iVar>0 );
89755 - assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
90205 + assert( (v->db->flags & SQLITE_EnableQPSG)==0
90206 + || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
90207 if( iVar>=32 ){
90208 v->expmask |= 0x80000000;
90209 }else{
@@ -92337,7 +92788,6 @@ SQLITE_API int sqlite3_stmt_scanstatus_v2(
92788 }
92789 if( flags & SQLITE_SCANSTAT_COMPLEX ){
92790 idx = iScan;
92340 - pScan = &p->aScan[idx];
92791 }else{
92792 /* If the COMPLEX flag is clear, then this function must ignore any
92793 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */
@@ -92350,6 +92800,8 @@ SQLITE_API int sqlite3_stmt_scanstatus_v2(
92800 }
92801 }
92802 if( idx>=p->nScan ) return 1;
92803 + assert( pScan==0 || pScan==&p->aScan[idx] );
92804 + pScan = &p->aScan[idx];
92805
92806 switch( iScanStatusOp ){
92807 case SQLITE_SCANSTAT_NLOOP: {
@@ -93798,7 +94250,7 @@ case OP_Return: { /* in1 */
94250 **
94251 ** See also: EndCoroutine
94252 */
93801 -case OP_InitCoroutine: { /* jump */
94253 +case OP_InitCoroutine: { /* jump0 */
94254 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94255 assert( pOp->p2>=0 && pOp->p2<p->nOp );
94256 assert( pOp->p3>=0 && pOp->p3<p->nOp );
@@ -93821,7 +94273,9 @@ jump_to_p2:
94273 **
94274 ** The instruction at the address in register P1 is a Yield.
94275 ** Jump to the P2 parameter of that Yield.
93824 -** After the jump, register P1 becomes undefined.
94276 +** After the jump, the value register P1 is left with a value
94277 +** such that subsequent OP_Yields go back to the this same
94278 +** OP_EndCoroutine instruction.
94279 **
94280 ** See also: InitCoroutine
94281 */
@@ -93833,8 +94287,8 @@ case OP_EndCoroutine: { /* in1 */
94287 pCaller = &aOp[pIn1->u.i];
94288 assert( pCaller->opcode==OP_Yield );
94289 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
94290 + pIn1->u.i = (int)(pOp - p->aOp) - 1;
94291 pOp = &aOp[pCaller->p2 - 1];
93837 - pIn1->flags = MEM_Undefined;
94292 break;
94293 }
94294
@@ -93851,7 +94305,7 @@ case OP_EndCoroutine: { /* in1 */
94305 **
94306 ** See also: InitCoroutine
94307 */
93854 -case OP_Yield: { /* in1, jump */
94308 +case OP_Yield: { /* in1, jump0 */
94309 int pcDest;
94310 pIn1 = &aMem[pOp->p1];
94311 assert( VdbeMemDynamic(pIn1)==0 );
@@ -94181,19 +94635,15 @@ case OP_Blob: { /* out2 */
94635 break;
94636 }
94637
94184 -/* Opcode: Variable P1 P2 * P4 *
94185 -** Synopsis: r[P2]=parameter(P1,P4)
94638 +/* Opcode: Variable P1 P2 * * *
94639 +** Synopsis: r[P2]=parameter(P1)
94640 **
94641 ** Transfer the values of bound parameter P1 into register P2
94188 -**
94189 -** If the parameter is named, then its name appears in P4.
94190 -** The P4 value is used by sqlite3_bind_parameter_name().
94642 */
94643 case OP_Variable: { /* out2 */
94644 Mem *pVar; /* Value being transferred */
94645
94646 assert( pOp->p1>0 && pOp->p1<=p->nVar );
94196 - assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
94647 pVar = &p->aVar[pOp->p1 - 1];
94648 if( sqlite3VdbeMemTooBig(pVar) ){
94649 goto too_big;
@@ -94714,7 +95164,7 @@ case OP_AddImm: { /* in1 */
95164 ** without data loss, then jump immediately to P2, or if P2==0
95165 ** raise an SQLITE_MISMATCH exception.
95166 */
94717 -case OP_MustBeInt: { /* jump, in1 */
95167 +case OP_MustBeInt: { /* jump0, in1 */
95168 pIn1 = &aMem[pOp->p1];
95169 if( (pIn1->flags & MEM_Int)==0 ){
95170 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
@@ -94755,7 +95205,7 @@ case OP_RealAffinity: { /* in1 */
95205 }
95206 #endif
95207
94758 -#ifndef SQLITE_OMIT_CAST
95208 +#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_ANALYZE)
95209 /* Opcode: Cast P1 P2 * * *
95210 ** Synopsis: affinity(r[P1])
95211 **
@@ -96327,11 +96777,16 @@ case OP_MakeRecord: {
96777 switch( len ){
96778 default: zPayload[7] = (u8)(v&0xff); v >>= 8;
96779 zPayload[6] = (u8)(v&0xff); v >>= 8;
96780 + /* no break */ deliberate_fall_through
96781 case 6: zPayload[5] = (u8)(v&0xff); v >>= 8;
96782 zPayload[4] = (u8)(v&0xff); v >>= 8;
96783 + /* no break */ deliberate_fall_through
96784 case 4: zPayload[3] = (u8)(v&0xff); v >>= 8;
96785 + /* no break */ deliberate_fall_through
96786 case 3: zPayload[2] = (u8)(v&0xff); v >>= 8;
96787 + /* no break */ deliberate_fall_through
96788 case 2: zPayload[1] = (u8)(v&0xff); v >>= 8;
96789 + /* no break */ deliberate_fall_through
96790 case 1: zPayload[0] = (u8)(v&0xff);
96791 }
96792 zPayload += len;
@@ -97250,7 +97705,8 @@ case OP_SequenceTest: {
97705 ** is the only cursor opcode that works with a pseudo-table.
97706 **
97707 ** P3 is the number of fields in the records that will be stored by
97253 -** the pseudo-table.
97708 +** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
97709 +** will return NULL for every column.
97710 */
97711 case OP_OpenPseudo: {
97712 VdbeCursor *pCx;
@@ -97393,10 +97849,10 @@ case OP_ColumnsUsed: {
97849 **
97850 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
97851 */
97396 -case OP_SeekLT: /* jump, in3, group, ncycle */
97397 -case OP_SeekLE: /* jump, in3, group, ncycle */
97398 -case OP_SeekGE: /* jump, in3, group, ncycle */
97399 -case OP_SeekGT: { /* jump, in3, group, ncycle */
97852 +case OP_SeekLT: /* jump0, in3, group, ncycle */
97853 +case OP_SeekLE: /* jump0, in3, group, ncycle */
97854 +case OP_SeekGE: /* jump0, in3, group, ncycle */
97855 +case OP_SeekGT: { /* jump0, in3, group, ncycle */
97856 int res; /* Comparison result */
97857 int oc; /* Opcode */
97858 VdbeCursor *pC; /* The cursor to seek */
@@ -98063,7 +98519,7 @@ case OP_Found: { /* jump, in3, ncycle */
98519 **
98520 ** See also: Found, NotFound, NoConflict, SeekRowid
98521 */
98066 -case OP_SeekRowid: { /* jump, in3, ncycle */
98522 +case OP_SeekRowid: { /* jump0, in3, ncycle */
98523 VdbeCursor *pC;
98524 BtCursor *pCrsr;
98525 int res;
@@ -98822,7 +99278,7 @@ case OP_NullRow: {
99278 ** configured to use Prev, not Next.
99279 */
99280 case OP_SeekEnd: /* ncycle */
98825 -case OP_Last: { /* jump, ncycle */
99281 +case OP_Last: { /* jump0, ncycle */
99282 VdbeCursor *pC;
99283 BtCursor *pCrsr;
99284 int res;
@@ -98856,28 +99312,38 @@ case OP_Last: { /* jump, ncycle */
99312 break;
99313 }
99314
98859 -/* Opcode: IfSmaller P1 P2 P3 * *
99315 +/* Opcode: IfSizeBetween P1 P2 P3 P4 *
99316 **
98861 -** Estimate the number of rows in the table P1. Jump to P2 if that
98862 -** estimate is less than approximately 2**(0.1*P3).
99317 +** Let N be the approximate number of rows in the table or index
99318 +** with cursor P1 and let X be 10*log2(N) if N is positive or -1
99319 +** if N is zero.
99320 +**
99321 +** Jump to P2 if X is in between P3 and P4, inclusive.
99322 */
98864 -case OP_IfSmaller: { /* jump */
99323 +case OP_IfSizeBetween: { /* jump */
99324 VdbeCursor *pC;
99325 BtCursor *pCrsr;
99326 int res;
99327 i64 sz;
99328
99329 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99330 + assert( pOp->p4type==P4_INT32 );
99331 + assert( pOp->p3>=-1 && pOp->p3<=640*2 );
99332 + assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 );
99333 pC = p->apCsr[pOp->p1];
99334 assert( pC!=0 );
99335 pCrsr = pC->uc.pCursor;
99336 assert( pCrsr );
99337 rc = sqlite3BtreeFirst(pCrsr, &res);
99338 if( rc ) goto abort_due_to_error;
98877 - if( res==0 ){
99339 + if( res!=0 ){
99340 + sz = -1; /* -Infinity encoding */
99341 + }else{
99342 sz = sqlite3BtreeRowCountEst(pCrsr);
98879 - if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
99343 + assert( sz>0 );
99344 + sz = sqlite3LogEst((u64)sz);
99345 }
99346 + res = sz>=pOp->p3 && sz<=pOp->p4.i;
99347 VdbeBranchTaken(res!=0,2);
99348 if( res ) goto jump_to_p2;
99349 break;
@@ -98930,7 +99396,7 @@ case OP_Sort: { /* jump ncycle */
99396 ** from the beginning toward the end. In other words, the cursor is
99397 ** configured to use Next, not Prev.
99398 */
98933 -case OP_Rewind: { /* jump, ncycle */
99399 +case OP_Rewind: { /* jump0, ncycle */
99400 VdbeCursor *pC;
99401 BtCursor *pCrsr;
99402 int res;
@@ -99577,11 +100043,18 @@ case OP_CreateBtree: { /* out2 */
100043 break;
100044 }
100045
99580 -/* Opcode: SqlExec * * * P4 *
100046 +/* Opcode: SqlExec P1 P2 * P4 *
100047 **
100048 ** Run the SQL statement or statements specified in the P4 string.
99583 -** Disable Auth and Trace callbacks while those statements are running if
99584 -** P1 is true.
100049 +**
100050 +** The P1 parameter is a bitmask of options:
100051 +**
100052 +** 0x0001 Disable Auth and Trace callbacks while the statements
100053 +** in P4 are running.
100054 +**
100055 +** 0x0002 Set db->nAnalysisLimit to P2 while the statements in
100056 +** P4 are running.
100057 +**
100058 */
100059 case OP_SqlExec: {
100060 char *zErr;
@@ -99589,6 +100062,7 @@ case OP_SqlExec: {
100062 sqlite3_xauth xAuth;
100063 #endif
100064 u8 mTrace;
100065 + int savedAnalysisLimit;
100066
100067 sqlite3VdbeIncrWriteCounter(p, 0);
100068 db->nSqlExec++;
@@ -99597,18 +100071,23 @@ case OP_SqlExec: {
100071 xAuth = db->xAuth;
100072 #endif
100073 mTrace = db->mTrace;
99600 - if( pOp->p1 ){
100074 + savedAnalysisLimit = db->nAnalysisLimit;
100075 + if( pOp->p1 & 0x0001 ){
100076 #ifndef SQLITE_OMIT_AUTHORIZATION
100077 db->xAuth = 0;
100078 #endif
100079 db->mTrace = 0;
100080 }
100081 + if( pOp->p1 & 0x0002 ){
100082 + db->nAnalysisLimit = pOp->p2;
100083 + }
100084 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
100085 db->nSqlExec--;
100086 #ifndef SQLITE_OMIT_AUTHORIZATION
100087 db->xAuth = xAuth;
100088 #endif
100089 db->mTrace = mTrace;
100090 + db->nAnalysisLimit = savedAnalysisLimit;
100091 if( zErr || rc ){
100092 sqlite3VdbeError(p, "%s", zErr);
100093 sqlite3_free(zErr);
@@ -99760,11 +100239,11 @@ case OP_DropTrigger: {
100239 /* Opcode: IntegrityCk P1 P2 P3 P4 P5
100240 **
100241 ** Do an analysis of the currently open database. Store in
99763 -** register P1 the text of an error message describing any problems.
99764 -** If no problems are found, store a NULL in register P1.
100242 +** register (P1+1) the text of an error message describing any problems.
100243 +** If no problems are found, store a NULL in register (P1+1).
100244 **
99766 -** The register P3 contains one less than the maximum number of allowed errors.
99767 -** At most reg(P3) errors will be reported.
100245 +** The register (P1) contains one less than the maximum number of allowed
100246 +** errors. At most reg(P1) errors will be reported.
100247 ** In other words, the analysis stops as soon as reg(P1) errors are
100248 ** seen. Reg(P1) is updated with the number of errors remaining.
100249 **
@@ -99784,19 +100263,21 @@ case OP_IntegrityCk: {
100263 Mem *pnErr; /* Register keeping track of errors remaining */
100264
100265 assert( p->bIsReader );
100266 + assert( pOp->p4type==P4_INTARRAY );
100267 nRoot = pOp->p2;
100268 aRoot = pOp->p4.ai;
100269 assert( nRoot>0 );
100270 + assert( aRoot!=0 );
100271 assert( aRoot[0]==(Pgno)nRoot );
99791 - assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
99792 - pnErr = &aMem[pOp->p3];
100272 + assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) );
100273 + pnErr = &aMem[pOp->p1];
100274 assert( (pnErr->flags & MEM_Int)!=0 );
100275 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
99795 - pIn1 = &aMem[pOp->p1];
100276 + pIn1 = &aMem[pOp->p1+1];
100277 assert( pOp->p5<db->nDb );
100278 assert( DbMaskTest(p->btreeMask, pOp->p5) );
99798 - rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
99799 - (int)pnErr->u.i+1, &nErr, &z);
100279 + rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1],
100280 + &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z);
100281 sqlite3VdbeMemSetNull(pIn1);
100282 if( nErr==0 ){
100283 assert( z==0 );
@@ -99923,7 +100404,9 @@ case OP_RowSetTest: { /* jump, in1, in3 */
100404 ** P1 contains the address of the memory cell that contains the first memory
100405 ** cell in an array of values used as arguments to the sub-program. P2
100406 ** contains the address to jump to if the sub-program throws an IGNORE
99926 -** exception using the RAISE() function. Register P3 contains the address
100407 +** exception using the RAISE() function. P2 might be zero, if there is
100408 +** no possibility that an IGNORE exception will be raised.
100409 +** Register P3 contains the address
100410 ** of a memory cell in this (the parent) VM that is used to allocate the
100411 ** memory required by the sub-vdbe at runtime.
100412 **
@@ -99931,7 +100414,7 @@ case OP_RowSetTest: { /* jump, in1, in3 */
100414 **
100415 ** If P5 is non-zero, then recursive program invocation is enabled.
100416 */
99934 -case OP_Program: { /* jump */
100417 +case OP_Program: { /* jump0 */
100418 int nMem; /* Number of memory registers for sub-program */
100419 int nByte; /* Bytes of runtime space required for sub-program */
100420 Mem *pRt; /* Register to allocate runtime space */
@@ -101480,7 +101963,7 @@ case OP_Filter: { /* jump */
101963 ** error is encountered.
101964 */
101965 case OP_Trace:
101483 -case OP_Init: { /* jump */
101966 +case OP_Init: { /* jump0 */
101967 int i;
101968 #ifndef SQLITE_OMIT_TRACE
101969 char *zTrace;
@@ -105381,10 +105864,10 @@ static int bytecodevtabColumn(
105864
105865 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
105866 case 9: /* nexec */
105384 - sqlite3_result_int(ctx, pOp->nExec);
105867 + sqlite3_result_int64(ctx, pOp->nExec);
105868 break;
105869 case 10: /* ncycle */
105387 - sqlite3_result_int(ctx, pOp->nCycle);
105870 + sqlite3_result_int64(ctx, pOp->nCycle);
105871 break;
105872 #else
105873 case 9: /* nexec */
@@ -106477,7 +106960,7 @@ static void extendFJMatch(
106960 static SQLITE_NOINLINE int isValidSchemaTableName(
106961 const char *zTab, /* Name as it appears in the SQL */
106962 Table *pTab, /* The schema table we are trying to match */
106480 - Schema *pSchema /* non-NULL if a database qualifier is present */
106963 + const char *zDb /* non-NULL if a database qualifier is present */
106964 ){
106965 const char *zLegacy;
106966 assert( pTab!=0 );
@@ -106488,7 +106971,7 @@ static SQLITE_NOINLINE int isValidSchemaTableName(
106971 if( sqlite3StrICmp(zTab+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){
106972 return 1;
106973 }
106491 - if( pSchema==0 ) return 0;
106974 + if( zDb==0 ) return 0;
106975 if( sqlite3StrICmp(zTab+7, &LEGACY_SCHEMA_TABLE[7])==0 ) return 1;
106976 if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1;
106977 }else{
@@ -106528,7 +107011,7 @@ static int lookupName(
107011 Parse *pParse, /* The parsing context */
107012 const char *zDb, /* Name of the database containing table, or NULL */
107013 const char *zTab, /* Name of table containing column, or NULL */
106531 - const char *zCol, /* Name of the column. */
107014 + const Expr *pRight, /* Name of the column. */
107015 NameContext *pNC, /* The name context used to resolve the name */
107016 Expr *pExpr /* Make this EXPR node point to the selected column */
107017 ){
@@ -106545,6 +107028,7 @@ static int lookupName(
107028 Table *pTab = 0; /* Table holding the row */
107029 Column *pCol; /* A column of pTab */
107030 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
107031 + const char *zCol = pRight->u.zToken;
107032
107033 assert( pNC ); /* the name context cannot be NULL. */
107034 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
@@ -106670,7 +107154,7 @@ static int lookupName(
107154 }
107155 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
107156 if( pTab->tnum!=1 ) continue;
106673 - if( !isValidSchemaTableName(zTab, pTab, pSchema) ) continue;
107157 + if( !isValidSchemaTableName(zTab, pTab, zDb) ) continue;
107158 }
107159 assert( ExprUseYTab(pExpr) );
107160 if( IN_RENAME_OBJECT && pItem->zAlias ){
@@ -106776,7 +107260,8 @@ static int lookupName(
107260 if( pParse->bReturning ){
107261 if( (pNC->ncFlags & NC_UBaseReg)!=0
107262 && ALWAYS(zTab==0
106779 - || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
107263 + || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0
107264 + || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))
107265 ){
107266 pExpr->iTable = op!=TK_DELETE;
107267 pTab = pParse->pTriggerTab;
@@ -106880,6 +107365,11 @@ static int lookupName(
107365 && ALWAYS(VisibleRowid(pMatch->pTab) || pMatch->fg.isNestedFrom)
107366 ){
107367 cnt = cntTab;
107368 +#if SQLITE_ALLOW_ROWID_IN_VIEW+0==2
107369 + if( pMatch->pTab!=0 && IsView(pMatch->pTab) ){
107370 + eNewExprOp = TK_NULL;
107371 + }
107372 +#endif
107373 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
107374 pExpr->affExpr = SQLITE_AFF_INTEGER;
107375 }
@@ -107033,6 +107523,10 @@ static int lookupName(
107523 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
107524 }else if( zTab ){
107525 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
107526 + }else if( cnt==0 && ExprHasProperty(pRight,EP_DblQuoted) ){
107527 + sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"
107528 + " string literal in single-quotes?",
107529 + zErr, zCol);
107530 }else{
107531 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
107532 }
@@ -107066,8 +107560,12 @@ static int lookupName(
107560 ** If a generated column is referenced, set bits for every column
107561 ** of the table.
107562 */
107069 - if( pExpr->iColumn>=0 && cnt==1 && pMatch!=0 ){
107070 - pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
107563 + if( pMatch ){
107564 + if( pExpr->iColumn>=0 ){
107565 + pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
107566 + }else{
107567 + pMatch->fg.rowidUsed = 1;
107568 + }
107569 }
107570
107571 pExpr->op = eNewExprOp;
@@ -107310,7 +107808,6 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
107808 */
107809 case TK_ID:
107810 case TK_DOT: {
107313 - const char *zColumn;
107811 const char *zTable;
107812 const char *zDb;
107813 Expr *pRight;
@@ -107319,7 +107816,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
107816 zDb = 0;
107817 zTable = 0;
107818 assert( !ExprHasProperty(pExpr, EP_IntValue) );
107322 - zColumn = pExpr->u.zToken;
107819 + pRight = pExpr;
107820 }else{
107821 Expr *pLeft = pExpr->pLeft;
107822 testcase( pNC->ncFlags & NC_IdxExpr );
@@ -107338,14 +107835,13 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
107835 }
107836 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) );
107837 zTable = pLeft->u.zToken;
107341 - zColumn = pRight->u.zToken;
107838 assert( ExprUseYTab(pExpr) );
107839 if( IN_RENAME_OBJECT ){
107840 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
107841 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
107842 }
107843 }
107348 - return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
107844 + return lookupName(pParse, zDb, zTable, pRight, pNC, pExpr);
107845 }
107846
107847 /* Resolve function names
@@ -107521,11 +108017,9 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108017 #endif
108018 }
108019 }
107524 -#ifndef SQLITE_OMIT_WINDOWFUNC
107525 - else if( ExprHasProperty(pExpr, EP_WinFunc) ){
108020 + else if( ExprHasProperty(pExpr, EP_WinFunc) || pExpr->pLeft ){
108021 is_agg = 1;
108022 }
107528 -#endif
108023 sqlite3WalkExprList(pWalker, pList);
108024 if( is_agg ){
108025 if( pExpr->pLeft ){
@@ -107595,6 +108089,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108089 testcase( pNC->ncFlags & NC_PartIdx );
108090 testcase( pNC->ncFlags & NC_IdxExpr );
108091 testcase( pNC->ncFlags & NC_GenCol );
108092 + assert( pExpr->x.pSelect );
108093 if( pNC->ncFlags & NC_SelfRef ){
108094 notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr);
108095 }else{
@@ -107603,6 +108098,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108098 assert( pNC->nRef>=nRef );
108099 if( nRef!=pNC->nRef ){
108100 ExprSetProperty(pExpr, EP_VarSelect);
108101 + pExpr->x.pSelect->selFlags |= SF_Correlated;
108102 }
108103 pNC->ncFlags |= NC_Subquery;
108104 }
@@ -108128,6 +108624,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108624 if( pOuterNC ) pOuterNC->nNestedSelect++;
108625 for(i=0; i<p->pSrc->nSrc; i++){
108626 SrcItem *pItem = &p->pSrc->a[i];
108627 + assert( pItem->zName!=0 || pItem->pSelect!=0 );/* Test of tag-20240424-1*/
108628 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108629 int nRef = pOuterNC ? pOuterNC->nRef : 0;
108630 const char *zSavedContext = pParse->zAuthContext;
@@ -108389,6 +108886,9 @@ SQLITE_PRIVATE int sqlite3ResolveExprNames(
108886 ** Resolve all names for all expression in an expression list. This is
108887 ** just like sqlite3ResolveExprNames() except that it works for an expression
108888 ** list rather than a single expression.
108889 +**
108890 +** The return value is SQLITE_OK (0) for success or SQLITE_ERROR (1) for a
108891 +** failure.
108892 */
108893 SQLITE_PRIVATE int sqlite3ResolveExprListNames(
108894 NameContext *pNC, /* Namespace to resolve expressions in. */
@@ -108397,7 +108897,7 @@ SQLITE_PRIVATE int sqlite3ResolveExprListNames(
108897 int i;
108898 int savedHasAgg = 0;
108899 Walker w;
108400 - if( pList==0 ) return WRC_Continue;
108900 + if( pList==0 ) return SQLITE_OK;
108901 w.pParse = pNC->pParse;
108902 w.xExprCallback = resolveExprStep;
108903 w.xSelectCallback = resolveSelectStep;
@@ -108411,7 +108911,7 @@ SQLITE_PRIVATE int sqlite3ResolveExprListNames(
108911 #if SQLITE_MAX_EXPR_DEPTH>0
108912 w.pParse->nHeight += pExpr->nHeight;
108913 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
108414 - return WRC_Abort;
108914 + return SQLITE_ERROR;
108915 }
108916 #endif
108917 sqlite3WalkExprNN(&w, pExpr);
@@ -108428,10 +108928,10 @@ SQLITE_PRIVATE int sqlite3ResolveExprListNames(
108928 (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108929 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108930 }
108431 - if( w.pParse->nErr>0 ) return WRC_Abort;
108931 + if( w.pParse->nErr>0 ) return SQLITE_ERROR;
108932 }
108933 pNC->ncFlags |= savedHasAgg;
108434 - return WRC_Continue;
108934 + return SQLITE_OK;
108935 }
108936
108937 /*
@@ -109434,11 +109934,12 @@ SQLITE_PRIVATE void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){
109934 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
109935 ** then the EP_DblQuoted flag is set on the expression node.
109936 **
109437 -** Special case: If op==TK_INTEGER and pToken points to a string that
109438 -** can be translated into a 32-bit integer, then the token is not
109439 -** stored in u.zToken. Instead, the integer values is written
109440 -** into u.iValue and the EP_IntValue flag is set. No extra storage
109937 +** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to
109938 +** a string that can be translated into a 32-bit integer, then the token is
109939 +** not stored in u.zToken. Instead, the integer values is written
109940 +** into u.iValue and the EP_IntValue flag is set. No extra storage
109941 ** is allocated to hold the integer text and the dequote flag is ignored.
109942 +** See also tag-20240227-b.
109943 */
109944 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
109945 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
@@ -109454,7 +109955,7 @@ SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
109955 if( pToken ){
109956 if( op!=TK_INTEGER || pToken->z==0
109957 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
109457 - nExtra = pToken->n+1;
109958 + nExtra = pToken->n+1; /* tag-20240227-a */
109959 assert( iValue>=0 );
109960 }
109961 }
@@ -109886,6 +110387,7 @@ SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n
110387 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110388 assert( p!=0 );
110389 assert( db!=0 );
110390 +exprDeleteRestart:
110391 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
110392 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
110393 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
@@ -109901,7 +110403,6 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110403 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
110404 /* The Expr.x union is never used at the same time as Expr.pRight */
110405 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
109904 - if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
110406 if( p->pRight ){
110407 assert( !ExprHasProperty(p, EP_WinFunc) );
110408 sqlite3ExprDeleteNN(db, p->pRight);
@@ -109916,6 +110417,19 @@ static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110417 }
110418 #endif
110419 }
110420 + if( p->pLeft && p->op!=TK_SELECT_COLUMN ){
110421 + Expr *pLeft = p->pLeft;
110422 + if( !ExprHasProperty(p, EP_Static)
110423 + && !ExprHasProperty(pLeft, EP_Static)
110424 + ){
110425 + /* Avoid unnecessary recursion on unary operators */
110426 + sqlite3DbNNFreeNN(db, p);
110427 + p = pLeft;
110428 + goto exprDeleteRestart;
110429 + }else{
110430 + sqlite3ExprDeleteNN(db, pLeft);
110431 + }
110432 + }
110433 }
110434 if( !ExprHasProperty(p, EP_Static) ){
110435 sqlite3DbNNFreeNN(db, p);
@@ -109948,11 +110462,11 @@ SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){
110462 **
110463 ** The pExpr might be deleted immediately on an OOM error.
110464 **
109951 -** The deferred delete is (currently) implemented by adding the
109952 -** pExpr to the pParse->pConstExpr list with a register number of 0.
110465 +** Return 0 if the delete was successfully deferred. Return non-zero
110466 +** if the delete happened immediately because of an OOM.
110467 */
109954 -SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
109955 - sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr);
110468 +SQLITE_PRIVATE int sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
110469 + return 0==sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr);
110470 }
110471
110472 /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
@@ -110388,17 +110902,19 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int fla
110902 pNewItem->iCursor = pOldItem->iCursor;
110903 pNewItem->addrFillSub = pOldItem->addrFillSub;
110904 pNewItem->regReturn = pOldItem->regReturn;
110905 + pNewItem->regResult = pOldItem->regResult;
110906 if( pNewItem->fg.isIndexedBy ){
110907 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
110908 + }else if( pNewItem->fg.isTabFunc ){
110909 + pNewItem->u1.pFuncArg =
110910 + sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
110911 + }else{
110912 + pNewItem->u1.nRow = pOldItem->u1.nRow;
110913 }
110914 pNewItem->u2 = pOldItem->u2;
110915 if( pNewItem->fg.isCte ){
110916 pNewItem->u2.pCteUse->nUse++;
110917 }
110398 - if( pNewItem->fg.isTabFunc ){
110399 - pNewItem->u1.pFuncArg =
110400 - sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
110401 - }
110918 pTab = pNewItem->pTab = pOldItem->pTab;
110919 if( pTab ){
110920 pTab->nTabRef++;
@@ -110864,6 +111380,54 @@ SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
111380 return pExpr;
111381 }
111382
111383 +/*
111384 +** pExpr is a TK_FUNCTION node. Try to determine whether or not the
111385 +** function is a constant function. A function is constant if all of
111386 +** the following are true:
111387 +**
111388 +** (1) It is a scalar function (not an aggregate or window function)
111389 +** (2) It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG
111390 +** property.
111391 +** (3) All of its arguments are constants
111392 +**
111393 +** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
111394 +** It makes no changes to pWalker->eCode if pExpr is constant. In
111395 +** every case, it returns WRC_Abort.
111396 +**
111397 +** Called as a service subroutine from exprNodeIsConstant().
111398 +*/
111399 +static SQLITE_NOINLINE int exprNodeIsConstantFunction(
111400 + Walker *pWalker,
111401 + Expr *pExpr
111402 +){
111403 + int n; /* Number of arguments */
111404 + ExprList *pList; /* List of arguments */
111405 + FuncDef *pDef; /* The function */
111406 + sqlite3 *db; /* The database */
111407 +
111408 + assert( pExpr->op==TK_FUNCTION );
111409 + if( ExprHasProperty(pExpr, EP_TokenOnly)
111410 + || (pList = pExpr->x.pList)==0
111411 + ){;
111412 + n = 0;
111413 + }else{
111414 + n = pList->nExpr;
111415 + sqlite3WalkExprList(pWalker, pList);
111416 + if( pWalker->eCode==0 ) return WRC_Abort;
111417 + }
111418 + db = pWalker->pParse->db;
111419 + pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
111420 + if( pDef==0
111421 + || pDef->xFinalize!=0
111422 + || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
111423 + || ExprHasProperty(pExpr, EP_WinFunc)
111424 + ){
111425 + pWalker->eCode = 0;
111426 + return WRC_Abort;
111427 + }
111428 + return WRC_Prune;
111429 +}
111430 +
111431
111432 /*
111433 ** These routines are Walker callbacks used to check expressions to
@@ -110892,6 +111456,7 @@ SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
111456 ** malformed schema error.
111457 */
111458 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
111459 + assert( pWalker->eCode>0 );
111460
111461 /* If pWalker->eCode is 2 then any term of the expression that comes from
111462 ** the ON or USING clauses of an outer join disqualifies the expression
@@ -110911,6 +111476,8 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
111476 ){
111477 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
111478 return WRC_Continue;
111479 + }else if( pWalker->pParse ){
111480 + return exprNodeIsConstantFunction(pWalker, pExpr);
111481 }else{
111482 pWalker->eCode = 0;
111483 return WRC_Abort;
@@ -110939,9 +111506,11 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
111506 case TK_IF_NULL_ROW:
111507 case TK_REGISTER:
111508 case TK_DOT:
111509 + case TK_RAISE:
111510 testcase( pExpr->op==TK_REGISTER );
111511 testcase( pExpr->op==TK_IF_NULL_ROW );
111512 testcase( pExpr->op==TK_DOT );
111513 + testcase( pExpr->op==TK_RAISE );
111514 pWalker->eCode = 0;
111515 return WRC_Abort;
111516 case TK_VARIABLE:
@@ -110963,15 +111532,15 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
111532 return WRC_Continue;
111533 }
111534 }
110966 -static int exprIsConst(Expr *p, int initFlag, int iCur){
111535 +static int exprIsConst(Parse *pParse, Expr *p, int initFlag){
111536 Walker w;
111537 w.eCode = initFlag;
111538 + w.pParse = pParse;
111539 w.xExprCallback = exprNodeIsConstant;
111540 w.xSelectCallback = sqlite3SelectWalkFail;
111541 #ifdef SQLITE_DEBUG
111542 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
111543 #endif
110974 - w.u.iCur = iCur;
111544 sqlite3WalkExpr(&w, p);
111545 return w.eCode;
111546 }
@@ -110983,9 +111552,15 @@ static int exprIsConst(Expr *p, int initFlag, int iCur){
111552 ** For the purposes of this function, a double-quoted string (ex: "abc")
111553 ** is considered a variable but a single-quoted string (ex: 'abc') is
111554 ** a constant.
111555 +**
111556 +** The pParse parameter may be NULL. But if it is NULL, there is no way
111557 +** to determine if function calls are constant or not, and hence all
111558 +** function calls will be considered to be non-constant. If pParse is
111559 +** not NULL, then a function call might be constant, depending on the
111560 +** function and on its parameters.
111561 */
110987 -SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
110988 - return exprIsConst(p, 1, 0);
111562 +SQLITE_PRIVATE int sqlite3ExprIsConstant(Parse *pParse, Expr *p){
111563 + return exprIsConst(pParse, p, 1);
111564 }
111565
111566 /*
@@ -111001,8 +111576,24 @@ SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
111576 ** can be added to the pParse->pConstExpr list and evaluated once when
111577 ** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
111578 */
111004 -SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
111005 - return exprIsConst(p, 2, 0);
111579 +static int sqlite3ExprIsConstantNotJoin(Parse *pParse, Expr *p){
111580 + return exprIsConst(pParse, p, 2);
111581 +}
111582 +
111583 +/*
111584 +** This routine examines sub-SELECT statements as an expression is being
111585 +** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered
111586 +** constant as long as they are uncorrelated - meaning that they do not
111587 +** contain any terms from outer contexts.
111588 +*/
111589 +static int exprSelectWalkTableConstant(Walker *pWalker, Select *pSelect){
111590 + assert( pSelect!=0 );
111591 + assert( pWalker->eCode==3 || pWalker->eCode==0 );
111592 + if( (pSelect->selFlags & SF_Correlated)!=0 ){
111593 + pWalker->eCode = 0;
111594 + return WRC_Abort;
111595 + }
111596 + return WRC_Prune;
111597 }
111598
111599 /*
@@ -111010,9 +111601,26 @@ SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
111601 ** for any single row of the table with cursor iCur. In other words, the
111602 ** expression must not refer to any non-deterministic function nor any
111603 ** table other than iCur.
111604 +**
111605 +** Consider uncorrelated subqueries to be constants if the bAllowSubq
111606 +** parameter is true.
111607 */
111014 -SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
111015 - return exprIsConst(p, 3, iCur);
111608 +static int sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq){
111609 + Walker w;
111610 + w.eCode = 3;
111611 + w.pParse = 0;
111612 + w.xExprCallback = exprNodeIsConstant;
111613 + if( bAllowSubq ){
111614 + w.xSelectCallback = exprSelectWalkTableConstant;
111615 + }else{
111616 + w.xSelectCallback = sqlite3SelectWalkFail;
111617 +#ifdef SQLITE_DEBUG
111618 + w.xSelectCallback2 = sqlite3SelectWalkAssert2;
111619 +#endif
111620 + }
111621 + w.u.iCur = iCur;
111622 + sqlite3WalkExpr(&w, p);
111623 + return w.eCode;
111624 }
111625
111626 /*
@@ -111030,7 +111638,10 @@ SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
111638 **
111639 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
111640 **
111033 -** (2) pExpr cannot use subqueries or non-deterministic functions.
111641 +** (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is
111642 +** true and the subquery is non-correlated
111643 +**
111644 +** (2b) pExpr cannot use non-deterministic functions.
111645 **
111646 ** (3) pSrc cannot be part of the left operand for a RIGHT JOIN.
111647 ** (Is there some way to relax this constraint?)
@@ -111059,7 +111670,8 @@ SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
111670 SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(
111671 Expr *pExpr, /* The constraint */
111672 const SrcList *pSrcList, /* Complete FROM clause */
111062 - int iSrc /* Which element of pSrcList to use */
111673 + int iSrc, /* Which element of pSrcList to use */
111674 + int bAllowSubq /* Allow non-correlated subqueries */
111675 ){
111676 const SrcItem *pSrc = &pSrcList->a[iSrc];
111677 if( pSrc->fg.jointype & JT_LTORJ ){
@@ -111084,7 +111696,8 @@ SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(
111696 }
111697 }
111698 }
111087 - return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
111699 + /* Rules (1), (2a), and (2b) handled by the following: */
111700 + return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq);
111701 }
111702
111703
@@ -111169,7 +111782,7 @@ SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprLi
111782 */
111783 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
111784 assert( isInit==0 || isInit==1 );
111172 - return exprIsConst(p, 4+isInit, 0);
111785 + return exprIsConst(0, p, 4+isInit);
111786 }
111787
111788 #ifdef SQLITE_ENABLE_CURSOR_HINTS
@@ -111417,13 +112030,13 @@ static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
112030 ** The argument is an IN operator with a list (not a subquery) on the
112031 ** right-hand side. Return TRUE if that list is constant.
112032 */
111420 -static int sqlite3InRhsIsConstant(Expr *pIn){
112033 +static int sqlite3InRhsIsConstant(Parse *pParse, Expr *pIn){
112034 Expr *pLHS;
112035 int res;
112036 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
112037 pLHS = pIn->pLeft;
112038 pIn->pLeft = 0;
111426 - res = sqlite3ExprIsConstant(pIn);
112039 + res = sqlite3ExprIsConstant(pParse, pIn);
112040 pIn->pLeft = pLHS;
112041 return res;
112042 }
@@ -111692,7 +112305,7 @@ SQLITE_PRIVATE int sqlite3FindInIndex(
112305 if( eType==0
112306 && (inFlags & IN_INDEX_NOOP_OK)
112307 && ExprUseXList(pX)
111695 - && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
112308 + && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2)
112309 ){
112310 pParse->nTab--; /* Back out the allocation of the unused cursor */
112311 iTab = -1; /* Cursor is not allocated */
@@ -111975,7 +112588,7 @@ SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
112588 ** this code only executes once. Because for a non-constant
112589 ** expression we need to rerun this code each time.
112590 */
111978 - if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
112591 + if( addrOnce && !sqlite3ExprIsConstant(pParse, pE2) ){
112592 sqlite3VdbeChangeToNoop(v, addrOnce-1);
112593 sqlite3VdbeChangeToNoop(v, addrOnce);
112594 ExprClearProperty(pExpr, EP_Subrtn);
@@ -113139,12 +113752,6 @@ expr_code_doover:
113752 assert( pExpr->u.zToken!=0 );
113753 assert( pExpr->u.zToken[0]!=0 );
113754 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
113142 - if( pExpr->u.zToken[1]!=0 ){
113143 - const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
113144 - assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
113145 - pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
113146 - sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
113147 - }
113755 return target;
113756 }
113757 case TK_REGISTER: {
@@ -113318,7 +113925,9 @@ expr_code_doover:
113925 }
113926 #endif
113927
113321 - if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
113928 + if( ConstFactorOk(pParse)
113929 + && sqlite3ExprIsConstantNotJoin(pParse,pExpr)
113930 + ){
113931 /* SQL functions can be expensive. So try to avoid running them
113932 ** multiple times if we know they always give the same result */
113933 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
@@ -113349,7 +113958,7 @@ expr_code_doover:
113958 }
113959
113960 for(i=0; i<nFarg; i++){
113352 - if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
113961 + if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){
113962 testcase( i==31 );
113963 constMask |= MASKBIT32(i);
113964 }
@@ -113491,8 +114100,9 @@ expr_code_doover:
114100 if( !ExprHasProperty(pExpr, EP_Collate) ){
114101 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
114102 ** "SOFT-COLLATE" that is added to constraints that are pushed down
113494 - ** from outer queries into sub-queries by the push-down optimization.
113495 - ** Clear subtypes as subtypes may not cross a subquery boundary.
114103 + ** from outer queries into sub-queries by the WHERE-clause push-down
114104 + ** optimization. Clear subtypes as subtypes may not cross a subquery
114105 + ** boundary.
114106 */
114107 assert( pExpr->pLeft );
114108 sqlite3ExprCode(pParse, pExpr->pLeft, target);
@@ -113816,7 +114426,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
114426 if( ConstFactorOk(pParse)
114427 && ALWAYS(pExpr!=0)
114428 && pExpr->op!=TK_REGISTER
113819 - && sqlite3ExprIsConstantNotJoin(pExpr)
114429 + && sqlite3ExprIsConstantNotJoin(pParse, pExpr)
114430 ){
114431 *pReg = 0;
114432 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
@@ -113880,7 +114490,7 @@ SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
114490 ** might choose to code the expression at initialization time.
114491 */
114492 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
113883 - if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
114493 + if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){
114494 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
114495 }else{
114496 sqlite3ExprCodeCopy(pParse, pExpr, target);
@@ -113939,7 +114549,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeExprList(
114549 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
114550 }
114551 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
113942 - && sqlite3ExprIsConstantNotJoin(pExpr)
114552 + && sqlite3ExprIsConstantNotJoin(pParse,pExpr)
114553 ){
114554 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
114555 }else{
@@ -115090,9 +115700,8 @@ static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
115700 && pAggInfo->aCol[iAgg].pCExpr==pExpr
115701 ){
115702 pExpr = sqlite3ExprDup(db, pExpr, 0);
115093 - if( pExpr ){
115703 + if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){
115704 pAggInfo->aCol[iAgg].pCExpr = pExpr;
115095 - sqlite3ExprDeferredDelete(pParse, pExpr);
115705 }
115706 }
115707 }else{
@@ -115101,9 +115710,8 @@ static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
115710 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
115711 ){
115712 pExpr = sqlite3ExprDup(db, pExpr, 0);
115104 - if( pExpr ){
115713 + if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){
115714 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
115106 - sqlite3ExprDeferredDelete(pParse, pExpr);
115715 }
115716 }
115717 }
@@ -116862,7 +117470,7 @@ static int renameResolveTrigger(Parse *pParse){
117470 /* ALWAYS() because if the table of the trigger does not exist, the
117471 ** error would have been hit before this point */
117472 if( ALWAYS(pParse->pTriggerTab) ){
116865 - rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
117473 + rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0;
117474 }
117475
117476 /* Resolve symbols in WHEN clause */
@@ -117804,7 +118412,12 @@ SQLITE_PRIVATE void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const T
118412 if( i==pTab->iPKey ){
118413 sqlite3VdbeAddOp2(v, OP_Null, 0, regOut);
118414 }else{
118415 + char aff = pTab->aCol[i].affinity;
118416 + if( aff==SQLITE_AFF_REAL ){
118417 + pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
118418 + }
118419 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
118420 + pTab->aCol[i].affinity = aff;
118421 }
118422 nField++;
118423 }
@@ -118723,7 +119336,7 @@ static void statGet(
119336 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
119337 sqlite3_str_appendf(&sStat, " %llu", iVal);
119338 #ifdef SQLITE_ENABLE_STAT4
118726 - assert( p->current.anEq[i] );
119339 + assert( p->current.anEq[i] || p->nRow==0 );
119340 #endif
119341 }
119342 sqlite3ResultStrAccum(context, &sStat);
@@ -118908,7 +119521,7 @@ static void analyzeOneTable(
119521
119522 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119523 int nCol; /* Number of columns in pIdx. "N" */
118911 - int addrRewind; /* Address of "OP_Rewind iIdxCur" */
119524 + int addrGotoEnd; /* Address of "OP_Rewind iIdxCur" */
119525 int addrNextRow; /* Address of "next_row:" */
119526 const char *zIdxName; /* Name of the index */
119527 int nColTest; /* Number of columns to test for changes */
@@ -118932,9 +119545,14 @@ static void analyzeOneTable(
119545 /*
119546 ** Pseudo-code for loop that calls stat_push():
119547 **
118935 - ** Rewind csr
118936 - ** if eof(csr) goto end_of_scan;
119548 ** regChng = 0
119549 + ** Rewind csr
119550 + ** if eof(csr){
119551 + ** stat_init() with count = 0;
119552 + ** goto end_of_scan;
119553 + ** }
119554 + ** count()
119555 + ** stat_init()
119556 ** goto chng_addr_0;
119557 **
119558 ** next_row:
@@ -118973,41 +119591,36 @@ static void analyzeOneTable(
119591 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
119592 VdbeComment((v, "%s", pIdx->zName));
119593
118976 - /* Invoke the stat_init() function. The arguments are:
119594 + /* Implementation of the following:
119595 **
119596 + ** regChng = 0
119597 + ** Rewind csr
119598 + ** if eof(csr){
119599 + ** stat_init() with count = 0;
119600 + ** goto end_of_scan;
119601 + ** }
119602 + ** count()
119603 + ** stat_init()
119604 + ** goto chng_addr_0;
119605 + */
119606 + assert( regTemp2==regStat+4 );
119607 + sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
119608 +
119609 + /* Arguments to stat_init():
119610 ** (1) the number of columns in the index including the rowid
119611 ** (or for a WITHOUT ROWID table, the number of PK columns),
119612 ** (2) the number of columns in the key without the rowid/pk
118981 - ** (3) estimated number of rows in the index,
118982 - */
119613 + ** (3) estimated number of rows in the index. */
119614 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat+1);
119615 assert( regRowid==regStat+2 );
119616 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
118986 -#ifdef SQLITE_ENABLE_STAT4
118987 - if( OptimizationEnabled(db, SQLITE_Stat4) ){
118988 - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp);
118989 - addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
118990 - VdbeCoverage(v);
118991 - }else
118992 -#endif
118993 - {
118994 - addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
118995 - VdbeCoverage(v);
118996 - sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp, 1);
118997 - }
118998 - assert( regTemp2==regStat+4 );
118999 - sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
119617 + sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp,
119618 + OptimizationDisabled(db, SQLITE_Stat4));
119619 sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4,
119620 &statInitFuncdef, 0);
119621 + addrGotoEnd = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
119622 + VdbeCoverage(v);
119623
119003 - /* Implementation of the following:
119004 - **
119005 - ** Rewind csr
119006 - ** if eof(csr) goto end_of_scan;
119007 - ** regChng = 0
119008 - ** goto next_push_0;
119009 - **
119010 - */
119624 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
119625 addrNextRow = sqlite3VdbeCurrentAddr(v);
119626
@@ -119114,6 +119727,12 @@ static void analyzeOneTable(
119727 }
119728
119729 /* Add the entry to the stat1 table. */
119730 + if( pIdx->pPartIdxWhere ){
119731 + /* Partial indexes might get a zero-entry in sqlite_stat1. But
119732 + ** an empty table is omitted from sqlite_stat1. */
119733 + sqlite3VdbeJumpHere(v, addrGotoEnd);
119734 + addrGotoEnd = 0;
119735 + }
119736 callStatGet(pParse, regStat, STAT_GET_STAT1, regStat1);
119737 assert( "BBB"[0]==SQLITE_AFF_TEXT );
119738 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
@@ -119137,6 +119756,13 @@ static void analyzeOneTable(
119756 int addrIsNull;
119757 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
119758
119759 + /* No STAT4 data is generated if the number of rows is zero */
119760 + if( addrGotoEnd==0 ){
119761 + sqlite3VdbeAddOp2(v, OP_Cast, regStat1, SQLITE_AFF_INTEGER);
119762 + addrGotoEnd = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
119763 + VdbeCoverage(v);
119764 + }
119765 +
119766 if( doOnce ){
119767 int mxCol = nCol;
119768 Index *pX;
@@ -119189,7 +119815,7 @@ static void analyzeOneTable(
119815 #endif /* SQLITE_ENABLE_STAT4 */
119816
119817 /* End of analysis */
119192 - sqlite3VdbeJumpHere(v, addrRewind);
119818 + if( addrGotoEnd ) sqlite3VdbeJumpHere(v, addrGotoEnd);
119819 }
119820
119821
@@ -120938,7 +121564,7 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
121564 }
121565 sqlite3VdbeAddOp0(v, OP_Halt);
121566
120941 -#if SQLITE_USER_AUTHENTICATION
121567 +#if SQLITE_USER_AUTHENTICATION && !defined(SQLITE_OMIT_SHARED_CACHE)
121568 if( pParse->nTableLock>0 && db->init.busy==0 ){
121569 sqlite3UserAuthInit(db);
121570 if( db->auth.authLevel<UAUTH_User ){
@@ -123577,20 +124203,20 @@ SQLITE_PRIVATE void sqlite3EndTable(
124203 int regRowid; /* Rowid of the next row to insert */
124204 int addrInsLoop; /* Top of the loop for inserting rows */
124205 Table *pSelTab; /* A table that describes the SELECT results */
124206 + int iCsr; /* Write cursor on the new table */
124207
124208 if( IN_SPECIAL_PARSE ){
124209 pParse->rc = SQLITE_ERROR;
124210 pParse->nErr++;
124211 return;
124212 }
124213 + iCsr = pParse->nTab++;
124214 regYield = ++pParse->nMem;
124215 regRec = ++pParse->nMem;
124216 regRowid = ++pParse->nMem;
123589 - assert(pParse->nTab==1);
124217 sqlite3MayAbort(pParse);
123591 - sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
124218 + sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->regRoot, iDb);
124219 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
123593 - pParse->nTab = 2;
124220 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
124221 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
124222 if( pParse->nErr ) return;
@@ -123611,11 +124237,11 @@ SQLITE_PRIVATE void sqlite3EndTable(
124237 VdbeCoverage(v);
124238 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
124239 sqlite3TableAffinity(v, p, 0);
123614 - sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
123615 - sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
124240 + sqlite3VdbeAddOp2(v, OP_NewRowid, iCsr, regRowid);
124241 + sqlite3VdbeAddOp3(v, OP_Insert, iCsr, regRec, regRowid);
124242 sqlite3VdbeGoto(v, addrInsLoop);
124243 sqlite3VdbeJumpHere(v, addrInsLoop);
123618 - sqlite3VdbeAddOp1(v, OP_Close, 1);
124244 + sqlite3VdbeAddOp1(v, OP_Close, iCsr);
124245 }
124246
124247 /* Compute the complete text of the CREATE statement */
@@ -123672,13 +124298,10 @@ SQLITE_PRIVATE void sqlite3EndTable(
124298 /* Test for cycles in generated columns and illegal expressions
124299 ** in CHECK constraints and in DEFAULT clauses. */
124300 if( p->tabFlags & TF_HasGenerated ){
123675 - sqlite3VdbeAddOp4(v, OP_SqlExec, 1, 0, 0,
124301 + sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
124302 sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
124303 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124304 }
123679 - sqlite3VdbeAddOp4(v, OP_SqlExec, 1, 0, 0,
123680 - sqlite3MPrintf(db, "PRAGMA \"%w\".integrity_check(%Q)",
123681 - db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124305 }
124306
124307 /* Add the table to the in-memory representation of the database.
@@ -123816,8 +124439,9 @@ create_view_fail:
124439 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
124440 /*
124441 ** The Table structure pTable is really a VIEW. Fill in the names of
123819 -** the columns of the view in the pTable structure. Return the number
123820 -** of errors. If an error is seen leave an error message in pParse->zErrMsg.
124442 +** the columns of the view in the pTable structure. Return non-zero if
124443 +** there are errors. If an error is seen an error message is left
124444 +** in pParse->zErrMsg.
124445 */
124446 static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
124447 Table *pSelTab; /* A fake table from which we get the result set */
@@ -123940,7 +124564,7 @@ static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
124564 sqlite3DeleteColumnNames(db, pTable);
124565 }
124566 #endif /* SQLITE_OMIT_VIEW */
123943 - return nErr;
124567 + return nErr + pParse->nErr;
124568 }
124569 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
124570 assert( pTable!=0 );
@@ -130238,6 +130862,8 @@ static void groupConcatValue(sqlite3_context *context){
130862 sqlite3_result_error_toobig(context);
130863 }else if( pAccum->accError==SQLITE_NOMEM ){
130864 sqlite3_result_error_nomem(context);
130865 + }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
130866 + sqlite3_result_text(context, "", 1, SQLITE_STATIC);
130867 }else{
130868 const char *zText = sqlite3_str_value(pAccum);
130869 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
@@ -132852,6 +133478,196 @@ SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
133478 # define autoIncStep(A,B,C)
133479 #endif /* SQLITE_OMIT_AUTOINCREMENT */
133480
133481 +/*
133482 +** If argument pVal is a Select object returned by an sqlite3MultiValues()
133483 +** that was able to use the co-routine optimization, finish coding the
133484 +** co-routine.
133485 +*/
133486 +SQLITE_PRIVATE void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){
133487 + if( ALWAYS(pVal) && pVal->pSrc->nSrc>0 ){
133488 + SrcItem *pItem = &pVal->pSrc->a[0];
133489 + sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->regReturn);
133490 + sqlite3VdbeJumpHere(pParse->pVdbe, pItem->addrFillSub - 1);
133491 + }
133492 +}
133493 +
133494 +/*
133495 +** Return true if all expressions in the expression-list passed as the
133496 +** only argument are constant.
133497 +*/
133498 +static int exprListIsConstant(Parse *pParse, ExprList *pRow){
133499 + int ii;
133500 + for(ii=0; ii<pRow->nExpr; ii++){
133501 + if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0;
133502 + }
133503 + return 1;
133504 +}
133505 +
133506 +/*
133507 +** Return true if all expressions in the expression-list passed as the
133508 +** only argument are both constant and have no affinity.
133509 +*/
133510 +static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){
133511 + int ii;
133512 + if( exprListIsConstant(pParse,pRow)==0 ) return 0;
133513 + for(ii=0; ii<pRow->nExpr; ii++){
133514 + Expr *pExpr = pRow->a[ii].pExpr;
133515 + assert( pExpr->op!=TK_RAISE );
133516 + assert( pExpr->affExpr==0 );
133517 + if( 0!=sqlite3ExprAffinity(pExpr) ) return 0;
133518 + }
133519 + return 1;
133520 +
133521 +}
133522 +
133523 +/*
133524 +** This function is called by the parser for the second and subsequent
133525 +** rows of a multi-row VALUES clause. Argument pLeft is the part of
133526 +** the VALUES clause already parsed, argument pRow is the vector of values
133527 +** for the new row. The Select object returned represents the complete
133528 +** VALUES clause, including the new row.
133529 +**
133530 +** There are two ways in which this may be achieved - by incremental
133531 +** coding of a co-routine (the "co-routine" method) or by returning a
133532 +** Select object equivalent to the following (the "UNION ALL" method):
133533 +**
133534 +** "pLeft UNION ALL SELECT pRow"
133535 +**
133536 +** If the VALUES clause contains a lot of rows, this compound Select
133537 +** object may consume a lot of memory.
133538 +**
133539 +** When the co-routine method is used, each row that will be returned
133540 +** by the VALUES clause is coded into part of a co-routine as it is
133541 +** passed to this function. The returned Select object is equivalent to:
133542 +**
133543 +** SELECT * FROM (
133544 +** Select object to read co-routine
133545 +** )
133546 +**
133547 +** The co-routine method is used in most cases. Exceptions are:
133548 +**
133549 +** a) If the current statement has a WITH clause. This is to avoid
133550 +** statements like:
133551 +**
133552 +** WITH cte AS ( VALUES('x'), ('y') ... )
133553 +** SELECT * FROM cte AS a, cte AS b;
133554 +**
133555 +** This will not work, as the co-routine uses a hard-coded register
133556 +** for its OP_Yield instructions, and so it is not possible for two
133557 +** cursors to iterate through it concurrently.
133558 +**
133559 +** b) The schema is currently being parsed (i.e. the VALUES clause is part
133560 +** of a schema item like a VIEW or TRIGGER). In this case there is no VM
133561 +** being generated when parsing is taking place, and so generating
133562 +** a co-routine is not possible.
133563 +**
133564 +** c) There are non-constant expressions in the VALUES clause (e.g.
133565 +** the VALUES clause is part of a correlated sub-query).
133566 +**
133567 +** d) One or more of the values in the first row of the VALUES clause
133568 +** has an affinity (i.e. is a CAST expression). This causes problems
133569 +** because the complex rules SQLite uses (see function
133570 +** sqlite3SubqueryColumnTypes() in select.c) to determine the effective
133571 +** affinity of such a column for all rows require access to all values in
133572 +** the column simultaneously.
133573 +*/
133574 +SQLITE_PRIVATE Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){
133575 +
133576 + if( pParse->bHasWith /* condition (a) above */
133577 + || pParse->db->init.busy /* condition (b) above */
133578 + || exprListIsConstant(pParse,pRow)==0 /* condition (c) above */
133579 + || (pLeft->pSrc->nSrc==0 &&
133580 + exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */
133581 + || IN_SPECIAL_PARSE
133582 + ){
133583 + /* The co-routine method cannot be used. Fall back to UNION ALL. */
133584 + Select *pSelect = 0;
133585 + int f = SF_Values | SF_MultiValue;
133586 + if( pLeft->pSrc->nSrc ){
133587 + sqlite3MultiValuesEnd(pParse, pLeft);
133588 + f = SF_Values;
133589 + }else if( pLeft->pPrior ){
133590 + /* In this case set the SF_MultiValue flag only if it was set on pLeft */
133591 + f = (f & pLeft->selFlags);
133592 + }
133593 + pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0);
133594 + pLeft->selFlags &= ~SF_MultiValue;
133595 + if( pSelect ){
133596 + pSelect->op = TK_ALL;
133597 + pSelect->pPrior = pLeft;
133598 + pLeft = pSelect;
133599 + }
133600 + }else{
133601 + SrcItem *p = 0; /* SrcItem that reads from co-routine */
133602 +
133603 + if( pLeft->pSrc->nSrc==0 ){
133604 + /* Co-routine has not yet been started and the special Select object
133605 + ** that accesses the co-routine has not yet been created. This block
133606 + ** does both those things. */
133607 + Vdbe *v = sqlite3GetVdbe(pParse);
133608 + Select *pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0);
133609 +
133610 + /* Ensure the database schema has been read. This is to ensure we have
133611 + ** the correct text encoding. */
133612 + if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ){
133613 + sqlite3ReadSchema(pParse);
133614 + }
133615 +
133616 + if( pRet ){
133617 + SelectDest dest;
133618 + pRet->pSrc->nSrc = 1;
133619 + pRet->pPrior = pLeft->pPrior;
133620 + pRet->op = pLeft->op;
133621 + if( pRet->pPrior ) pRet->selFlags |= SF_Values;
133622 + pLeft->pPrior = 0;
133623 + pLeft->op = TK_SELECT;
133624 + assert( pLeft->pNext==0 );
133625 + assert( pRet->pNext==0 );
133626 + p = &pRet->pSrc->a[0];
133627 + p->pSelect = pLeft;
133628 + p->fg.viaCoroutine = 1;
133629 + p->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1;
133630 + p->regReturn = ++pParse->nMem;
133631 + p->iCursor = -1;
133632 + p->u1.nRow = 2;
133633 + sqlite3VdbeAddOp3(v,OP_InitCoroutine,p->regReturn,0,p->addrFillSub);
133634 + sqlite3SelectDestInit(&dest, SRT_Coroutine, p->regReturn);
133635 +
133636 + /* Allocate registers for the output of the co-routine. Do so so
133637 + ** that there are two unused registers immediately before those
133638 + ** used by the co-routine. This allows the code in sqlite3Insert()
133639 + ** to use these registers directly, instead of copying the output
133640 + ** of the co-routine to a separate array for processing. */
133641 + dest.iSdst = pParse->nMem + 3;
133642 + dest.nSdst = pLeft->pEList->nExpr;
133643 + pParse->nMem += 2 + dest.nSdst;
133644 +
133645 + pLeft->selFlags |= SF_MultiValue;
133646 + sqlite3Select(pParse, pLeft, &dest);
133647 + p->regResult = dest.iSdst;
133648 + assert( pParse->nErr || dest.iSdst>0 );
133649 + pLeft = pRet;
133650 + }
133651 + }else{
133652 + p = &pLeft->pSrc->a[0];
133653 + assert( !p->fg.isTabFunc && !p->fg.isIndexedBy );
133654 + p->u1.nRow++;
133655 + }
133656 +
133657 + if( pParse->nErr==0 ){
133658 + assert( p!=0 );
133659 + if( p->pSelect->pEList->nExpr!=pRow->nExpr ){
133660 + sqlite3SelectWrongNumTermsError(pParse, p->pSelect);
133661 + }else{
133662 + sqlite3ExprCodeExprList(pParse, pRow, p->regResult, 0, 0);
133663 + sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield, p->regReturn);
133664 + }
133665 + }
133666 + sqlite3ExprListDelete(pParse->db, pRow);
133667 + }
133668 +
133669 + return pLeft;
133670 +}
133671
133672 /* Forward declaration */
133673 static int xferOptimization(
@@ -133188,25 +134004,40 @@ SQLITE_PRIVATE void sqlite3Insert(
134004 if( pSelect ){
134005 /* Data is coming from a SELECT or from a multi-row VALUES clause.
134006 ** Generate a co-routine to run the SELECT. */
133191 - int regYield; /* Register holding co-routine entry-point */
133192 - int addrTop; /* Top of the co-routine */
134007 int rc; /* Result code */
134008
133195 - regYield = ++pParse->nMem;
133196 - addrTop = sqlite3VdbeCurrentAddr(v) + 1;
133197 - sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
133198 - sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
133199 - dest.iSdst = bIdListInOrder ? regData : 0;
133200 - dest.nSdst = pTab->nCol;
133201 - rc = sqlite3Select(pParse, pSelect, &dest);
133202 - regFromSelect = dest.iSdst;
133203 - assert( db->pParse==pParse );
133204 - if( rc || pParse->nErr ) goto insert_cleanup;
133205 - assert( db->mallocFailed==0 );
133206 - sqlite3VdbeEndCoroutine(v, regYield);
133207 - sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
133208 - assert( pSelect->pEList );
133209 - nColumn = pSelect->pEList->nExpr;
134009 + if( pSelect->pSrc->nSrc==1
134010 + && pSelect->pSrc->a[0].fg.viaCoroutine
134011 + && pSelect->pPrior==0
134012 + ){
134013 + SrcItem *pItem = &pSelect->pSrc->a[0];
134014 + dest.iSDParm = pItem->regReturn;
134015 + regFromSelect = pItem->regResult;
134016 + nColumn = pItem->pSelect->pEList->nExpr;
134017 + ExplainQueryPlan((pParse, 0, "SCAN %S", pItem));
134018 + if( bIdListInOrder && nColumn==pTab->nCol ){
134019 + regData = regFromSelect;
134020 + regRowid = regData - 1;
134021 + regIns = regRowid - (IsVirtual(pTab) ? 1 : 0);
134022 + }
134023 + }else{
134024 + int addrTop; /* Top of the co-routine */
134025 + int regYield = ++pParse->nMem;
134026 + addrTop = sqlite3VdbeCurrentAddr(v) + 1;
134027 + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
134028 + sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
134029 + dest.iSdst = bIdListInOrder ? regData : 0;
134030 + dest.nSdst = pTab->nCol;
134031 + rc = sqlite3Select(pParse, pSelect, &dest);
134032 + regFromSelect = dest.iSdst;
134033 + assert( db->pParse==pParse );
134034 + if( rc || pParse->nErr ) goto insert_cleanup;
134035 + assert( db->mallocFailed==0 );
134036 + sqlite3VdbeEndCoroutine(v, regYield);
134037 + sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
134038 + assert( pSelect->pEList );
134039 + nColumn = pSelect->pEList->nExpr;
134040 + }
134041
134042 /* Set useTempTable to TRUE if the result of the SELECT statement
134043 ** should be written into a temporary table (template 4). Set to
@@ -137931,6 +138762,34 @@ static const PragmaName aPragmaName[] = {
138762 /************** End of pragma.h **********************************************/
138763 /************** Continuing where we left off in pragma.c *********************/
138764
138765 +/*
138766 +** When the 0x10 bit of PRAGMA optimize is set, any ANALYZE commands
138767 +** will be run with an analysis_limit set to the lessor of the value of
138768 +** the following macro or to the actual analysis_limit if it is non-zero,
138769 +** in order to prevent PRAGMA optimize from running for too long.
138770 +**
138771 +** The value of 2000 is chosen emperically so that the worst-case run-time
138772 +** for PRAGMA optimize does not exceed 100 milliseconds against a variety
138773 +** of test databases on a RaspberryPI-4 compiled using -Os and without
138774 +** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
138775 +** this paragraph, "worst-case" means that ANALYZE ends up being
138776 +** run on every table in the database. The worst case typically only
138777 +** happens if PRAGMA optimize is run on a database file for which ANALYZE
138778 +** has not been previously run and the 0x10000 flag is included so that
138779 +** all tables are analyzed. The usual case for PRAGMA optimize is that
138780 +** no ANALYZE commands will be run at all, or if any ANALYZE happens it
138781 +** will be against a single table, so that expected timing for PRAGMA
138782 +** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
138783 +** flag or less than 100 microseconds without the 0x10000 flag.
138784 +**
138785 +** An analysis limit of 2000 is almost always sufficient for the query
138786 +** planner to fully characterize an index. The additional accuracy from
138787 +** a larger analysis is not usually helpful.
138788 +*/
138789 +#ifndef SQLITE_DEFAULT_OPTIMIZE_LIMIT
138790 +# define SQLITE_DEFAULT_OPTIMIZE_LIMIT 2000
138791 +#endif
138792 +
138793 /*
138794 ** Interpret the given string as a safety level. Return 0 for OFF,
138795 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
@@ -139576,7 +140435,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
140435 /* Set the maximum error count */
140436 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140437 if( zRight ){
139579 - if( sqlite3GetInt32(zRight, &mxErr) ){
140438 + if( sqlite3GetInt32(pValue->z, &mxErr) ){
140439 if( mxErr<=0 ){
140440 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140441 }
@@ -139593,7 +140452,6 @@ SQLITE_PRIVATE void sqlite3Pragma(
140452 Hash *pTbls; /* Set of all tables in the schema */
140453 int *aRoot; /* Array of root page numbers of all btrees */
140454 int cnt = 0; /* Number of entries in aRoot[] */
139596 - int mxIdx = 0; /* Maximum number of indexes for any table */
140455
140456 if( OMIT_TEMPDB && i==1 ) continue;
140457 if( iDb>=0 && i!=iDb ) continue;
@@ -139615,7 +140473,6 @@ SQLITE_PRIVATE void sqlite3Pragma(
140473 if( pObjTab && pObjTab!=pTab ) continue;
140474 if( HasRowid(pTab) ) cnt++;
140475 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
139618 - if( nIdx>mxIdx ) mxIdx = nIdx;
140476 }
140477 if( cnt==0 ) continue;
140478 if( pObjTab ) cnt++;
@@ -139635,11 +140492,11 @@ SQLITE_PRIVATE void sqlite3Pragma(
140492 aRoot[0] = cnt;
140493
140494 /* Make sure sufficient number of registers have been allocated */
139638 - sqlite3TouchRegister(pParse, 8+mxIdx);
140495 + sqlite3TouchRegister(pParse, 8+cnt);
140496 sqlite3ClearTempRegCache(pParse);
140497
140498 /* Do the b-tree integrity checks */
139642 - sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
140499 + sqlite3VdbeAddOp4(v, OP_IntegrityCk, 1, cnt, 8, (char*)aRoot,P4_INTARRAY);
140500 sqlite3VdbeChangeP5(v, (u8)i);
140501 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
140502 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
@@ -139649,6 +140506,36 @@ SQLITE_PRIVATE void sqlite3Pragma(
140506 integrityCheckResultRow(v);
140507 sqlite3VdbeJumpHere(v, addr);
140508
140509 + /* Check that the indexes all have the right number of rows */
140510 + cnt = pObjTab ? 1 : 0;
140511 + sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
140512 + for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
140513 + int iTab = 0;
140514 + Table *pTab = sqliteHashData(x);
140515 + Index *pIdx;
140516 + if( pObjTab && pObjTab!=pTab ) continue;
140517 + if( HasRowid(pTab) ){
140518 + iTab = cnt++;
140519 + }else{
140520 + iTab = cnt;
140521 + for(pIdx=pTab->pIndex; ALWAYS(pIdx); pIdx=pIdx->pNext){
140522 + if( IsPrimaryKeyIndex(pIdx) ) break;
140523 + iTab++;
140524 + }
140525 + }
140526 + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140527 + if( pIdx->pPartIdxWhere==0 ){
140528 + addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+cnt, 0, 8+iTab);
140529 + VdbeCoverageNeverNull(v);
140530 + sqlite3VdbeLoadString(v, 4, pIdx->zName);
140531 + sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
140532 + integrityCheckResultRow(v);
140533 + sqlite3VdbeJumpHere(v, addr);
140534 + }
140535 + cnt++;
140536 + }
140537 + }
140538 +
140539 /* Make sure all the indices are constructed correctly.
140540 */
140541 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
@@ -139972,21 +140859,9 @@ SQLITE_PRIVATE void sqlite3Pragma(
140859 }
140860 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
140861 sqlite3VdbeJumpHere(v, loopTop-1);
139975 - if( !isQuick ){
139976 - sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
139977 - for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
139978 - if( pPk==pIdx ) continue;
139979 - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
139980 - addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
139981 - sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
139982 - sqlite3VdbeLoadString(v, 4, pIdx->zName);
139983 - sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
139984 - integrityCheckResultRow(v);
139985 - sqlite3VdbeJumpHere(v, addr);
139986 - }
139987 - if( pPk ){
139988 - sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
139989 - }
140862 + if( pPk ){
140863 + assert( !isQuick );
140864 + sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
140865 }
140866 }
140867
@@ -140284,44 +141159,63 @@ SQLITE_PRIVATE void sqlite3Pragma(
141159 **
141160 ** The optional argument is a bitmask of optimizations to perform:
141161 **
140287 - ** 0x0001 Debugging mode. Do not actually perform any optimizations
140288 - ** but instead return one line of text for each optimization
140289 - ** that would have been done. Off by default.
141162 + ** 0x00001 Debugging mode. Do not actually perform any optimizations
141163 + ** but instead return one line of text for each optimization
141164 + ** that would have been done. Off by default.
141165 **
140291 - ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
140292 - ** See below for additional information.
141166 + ** 0x00002 Run ANALYZE on tables that might benefit. On by default.
141167 + ** See below for additional information.
141168 **
140294 - ** 0x0004 (Not yet implemented) Record usage and performance
140295 - ** information from the current session in the
140296 - ** database file so that it will be available to "optimize"
140297 - ** pragmas run by future database connections.
141169 + ** 0x00010 Run all ANALYZE operations using an analysis_limit that
141170 + ** is the lessor of the current analysis_limit and the
141171 + ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
141172 + ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is
141173 + ** currently (2024-02-19) set to 2000, which is such that
141174 + ** the worst case run-time for PRAGMA optimize on a 100MB
141175 + ** database will usually be less than 100 milliseconds on
141176 + ** a RaspberryPI-4 class machine. On by default.
141177 **
140299 - ** 0x0008 (Not yet implemented) Create indexes that might have
140300 - ** been helpful to recent queries
141178 + ** 0x10000 Look at tables to see if they need to be reanalyzed
141179 + ** due to growth or shrinkage even if they have not been
141180 + ** queried during the current connection. Off by default.
141181 **
140302 - ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
140303 - ** of the optimizations listed above except Debug Mode, including new
140304 - ** optimizations that have not yet been invented. If new optimizations are
140305 - ** ever added that should be off by default, those off-by-default
140306 - ** optimizations will have bitmasks of 0x10000 or larger.
141182 + ** The default MASK is and always shall be 0x0fffe. In the current
141183 + ** implementation, the default mask only covers the 0x00002 optimization,
141184 + ** though additional optimizations that are covered by 0x0fffe might be
141185 + ** added in the future. Optimizations that are off by default and must
141186 + ** be explicitly requested have masks of 0x10000 or greater.
141187 **
141188 ** DETERMINATION OF WHEN TO RUN ANALYZE
141189 **
141190 ** In the current implementation, a table is analyzed if only if all of
141191 ** the following are true:
141192 **
140313 - ** (1) MASK bit 0x02 is set.
141193 + ** (1) MASK bit 0x00002 is set.
141194 + **
141195 + ** (2) The table is an ordinary table, not a virtual table or view.
141196 + **
141197 + ** (3) The table name does not begin with "sqlite_".
141198 **
140315 - ** (2) The query planner used sqlite_stat1-style statistics for one or
140316 - ** more indexes of the table at some point during the lifetime of
140317 - ** the current connection.
141199 + ** (4) One or more of the following is true:
141200 + ** (4a) The 0x10000 MASK bit is set.
141201 + ** (4b) One or more indexes on the table lacks an entry
141202 + ** in the sqlite_stat1 table.
141203 + ** (4c) The query planner used sqlite_stat1-style statistics for one
141204 + ** or more indexes of the table at some point during the lifetime
141205 + ** of the current connection.
141206 **
140319 - ** (3) One or more indexes of the table are currently unanalyzed OR
140320 - ** the number of rows in the table has increased by 25 times or more
140321 - ** since the last time ANALYZE was run.
141207 + ** (5) One or more of the following is true:
141208 + ** (5a) One or more indexes on the table lacks an entry
141209 + ** in the sqlite_stat1 table. (Same as 4a)
141210 + ** (5b) The number of rows in the table has increased or decreased by
141211 + ** 10-fold. In other words, the current size of the table is
141212 + ** 10 times larger than the size in sqlite_stat1 or else the
141213 + ** current size is less than 1/10th the size in sqlite_stat1.
141214 **
141215 ** The rules for when tables are analyzed are likely to change in
140324 - ** future releases.
141216 + ** future releases. Future versions of SQLite might accept a string
141217 + ** literal argument to this pragma that contains a mnemonic description
141218 + ** of the options rather than a bitmap.
141219 */
141220 case PragTyp_OPTIMIZE: {
141221 int iDbLast; /* Loop termination point for the schema loop */
@@ -140333,6 +141227,10 @@ SQLITE_PRIVATE void sqlite3Pragma(
141227 LogEst szThreshold; /* Size threshold above which reanalysis needed */
141228 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
141229 u32 opMask; /* Mask of operations to perform */
141230 + int nLimit; /* Analysis limit to use */
141231 + int nCheck = 0; /* Number of tables to be optimized */
141232 + int nBtree = 0; /* Number of btrees to scan */
141233 + int nIndex; /* Number of indexes on the current table */
141234
141235 if( zRight ){
141236 opMask = (u32)sqlite3Atoi(zRight);
@@ -140340,6 +141238,14 @@ SQLITE_PRIVATE void sqlite3Pragma(
141238 }else{
141239 opMask = 0xfffe;
141240 }
141241 + if( (opMask & 0x10)==0 ){
141242 + nLimit = 0;
141243 + }else if( db->nAnalysisLimit>0
141244 + && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT ){
141245 + nLimit = 0;
141246 + }else{
141247 + nLimit = SQLITE_DEFAULT_OPTIMIZE_LIMIT;
141248 + }
141249 iTabCur = pParse->nTab++;
141250 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
141251 if( iDb==1 ) continue;
@@ -140348,23 +141254,61 @@ SQLITE_PRIVATE void sqlite3Pragma(
141254 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
141255 pTab = (Table*)sqliteHashData(k);
141256
140351 - /* If table pTab has not been used in a way that would benefit from
140352 - ** having analysis statistics during the current session, then skip it.
140353 - ** This also has the effect of skipping virtual tables and views */
140354 - if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
141257 + /* This only works for ordinary tables */
141258 + if( !IsOrdinaryTable(pTab) ) continue;
141259
140356 - /* Reanalyze if the table is 25 times larger than the last analysis */
140357 - szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
141260 + /* Do not scan system tables */
141261 + if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ) continue;
141262 +
141263 + /* Find the size of the table as last recorded in sqlite_stat1.
141264 + ** If any index is unanalyzed, then the threshold is -1 to
141265 + ** indicate a new, unanalyzed index
141266 + */
141267 + szThreshold = pTab->nRowLogEst;
141268 + nIndex = 0;
141269 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141270 + nIndex++;
141271 if( !pIdx->hasStat1 ){
140360 - szThreshold = 0; /* Always analyze if any index lacks statistics */
140361 - break;
141272 + szThreshold = -1; /* Always analyze if any index lacks statistics */
141273 }
141274 }
140364 - if( szThreshold ){
140365 - sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
140366 - sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
140367 - sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
141275 +
141276 + /* If table pTab has not been used in a way that would benefit from
141277 + ** having analysis statistics during the current session, then skip it,
141278 + ** unless the 0x10000 MASK bit is set. */
141279 + if( (pTab->tabFlags & TF_MaybeReanalyze)!=0 ){
141280 + /* Check for size change if stat1 has been used for a query */
141281 + }else if( opMask & 0x10000 ){
141282 + /* Check for size change if 0x10000 is set */
141283 + }else if( pTab->pIndex!=0 && szThreshold<0 ){
141284 + /* Do analysis if unanalyzed indexes exists */
141285 + }else{
141286 + /* Otherwise, we can skip this table */
141287 + continue;
141288 + }
141289 +
141290 + nCheck++;
141291 + if( nCheck==2 ){
141292 + /* If ANALYZE might be invoked two or more times, hold a write
141293 + ** transaction for efficiency */
141294 + sqlite3BeginWriteOperation(pParse, 0, iDb);
141295 + }
141296 + nBtree += nIndex+1;
141297 +
141298 + /* Reanalyze if the table is 10 times larger or smaller than
141299 + ** the last analysis. Unconditional reanalysis if there are
141300 + ** unanalyzed indexes. */
141301 + sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
141302 + if( szThreshold>=0 ){
141303 + const LogEst iRange = 33; /* 10x size change */
141304 + sqlite3VdbeAddOp4Int(v, OP_IfSizeBetween, iTabCur,
141305 + sqlite3VdbeCurrentAddr(v)+2+(opMask&1),
141306 + szThreshold>=iRange ? szThreshold-iRange : -1,
141307 + szThreshold+iRange);
141308 + VdbeCoverage(v);
141309 + }else{
141310 + sqlite3VdbeAddOp2(v, OP_Rewind, iTabCur,
141311 + sqlite3VdbeCurrentAddr(v)+2+(opMask&1));
141312 VdbeCoverage(v);
141313 }
141314 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
@@ -140374,11 +141318,27 @@ SQLITE_PRIVATE void sqlite3Pragma(
141318 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
141319 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
141320 }else{
140377 - sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
141321 + sqlite3VdbeAddOp4(v, OP_SqlExec, nLimit ? 0x02 : 00, nLimit, 0,
141322 + zSubSql, P4_DYNAMIC);
141323 }
141324 }
141325 }
141326 sqlite3VdbeAddOp0(v, OP_Expire);
141327 +
141328 + /* In a schema with a large number of tables and indexes, scale back
141329 + ** the analysis_limit to avoid excess run-time in the worst case.
141330 + */
141331 + if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
141332 + int iAddr, iEnd;
141333 + VdbeOp *aOp;
141334 + nLimit = 100*nLimit/nBtree;
141335 + if( nLimit<100 ) nLimit = 100;
141336 + aOp = sqlite3VdbeGetOp(v, 0);
141337 + iEnd = sqlite3VdbeCurrentAddr(v);
141338 + for(iAddr=0; iAddr<iEnd; iAddr++){
141339 + if( aOp[iAddr].opcode==OP_SqlExec ) aOp[iAddr].p2 = nLimit;
141340 + }
141341 + }
141342 break;
141343 }
141344
@@ -140642,9 +141602,9 @@ static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
141602 seen[0] = 0;
141603 seen[1] = 0;
141604 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
140645 - if( pConstraint->usable==0 ) continue;
140646 - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
141605 if( pConstraint->iColumn < pTab->iHidden ) continue;
141606 + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
141607 + if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT;
141608 j = pConstraint->iColumn - pTab->iHidden;
141609 assert( j < 2 );
141610 seen[j] = i+1;
@@ -140657,16 +141617,13 @@ static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
141617 j = seen[0]-1;
141618 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
141619 pIdxInfo->aConstraintUsage[j].omit = 1;
140660 - if( seen[1]==0 ){
140661 - pIdxInfo->estimatedCost = (double)1000;
140662 - pIdxInfo->estimatedRows = 1000;
140663 - return SQLITE_OK;
140664 - }
141620 pIdxInfo->estimatedCost = (double)20;
141621 pIdxInfo->estimatedRows = 20;
140667 - j = seen[1]-1;
140668 - pIdxInfo->aConstraintUsage[j].argvIndex = 2;
140669 - pIdxInfo->aConstraintUsage[j].omit = 1;
141622 + if( seen[1] ){
141623 + j = seen[1]-1;
141624 + pIdxInfo->aConstraintUsage[j].argvIndex = 2;
141625 + pIdxInfo->aConstraintUsage[j].omit = 1;
141626 + }
141627 return SQLITE_OK;
141628 }
141629
@@ -140686,6 +141643,7 @@ static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
141643 int i;
141644 sqlite3_finalize(pCsr->pPragma);
141645 pCsr->pPragma = 0;
141646 + pCsr->iRowid = 0;
141647 for(i=0; i<ArraySize(pCsr->azArg); i++){
141648 sqlite3_free(pCsr->azArg[i]);
141649 pCsr->azArg[i] = 0;
@@ -141486,7 +142444,13 @@ SQLITE_PRIVATE void *sqlite3ParserAddCleanup(
142444 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
142445 void *pPtr /* Pointer to object to be cleaned up */
142446 ){
141489 - ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
142447 + ParseCleanup *pCleanup;
142448 + if( sqlite3FaultSim(300) ){
142449 + pCleanup = 0;
142450 + sqlite3OomFault(pParse->db);
142451 + }else{
142452 + pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
142453 + }
142454 if( pCleanup ){
142455 pCleanup->pNext = pParse->pCleanup;
142456 pParse->pCleanup = pCleanup;
@@ -143608,9 +144572,16 @@ static void generateSortTail(
144572 int addrExplain; /* Address of OP_Explain instruction */
144573 #endif
144574
143611 - ExplainQueryPlan2(addrExplain, (pParse, 0,
143612 - "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat>0?"RIGHT PART OF ":"")
143613 - );
144575 + nKey = pOrderBy->nExpr - pSort->nOBSat;
144576 + if( pSort->nOBSat==0 || nKey==1 ){
144577 + ExplainQueryPlan2(addrExplain, (pParse, 0,
144578 + "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":""
144579 + ));
144580 + }else{
144581 + ExplainQueryPlan2(addrExplain, (pParse, 0,
144582 + "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey
144583 + ));
144584 + }
144585 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
144586 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
144587
@@ -143648,7 +144619,6 @@ static void generateSortTail(
144619 regRow = sqlite3GetTempRange(pParse, nColumn);
144620 }
144621 }
143651 - nKey = pOrderBy->nExpr - pSort->nOBSat;
144622 if( pSort->sortFlags & SORTFLAG_UseSorter ){
144623 int regSortOut = ++pParse->nMem;
144624 iSortTab = pParse->nTab++;
@@ -144253,8 +145223,7 @@ SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(
145223 NameContext sNC;
145224
145225 assert( pSelect!=0 );
144256 - testcase( (pSelect->selFlags & SF_Resolved)==0 );
144257 - assert( (pSelect->selFlags & SF_Resolved)!=0 || IN_RENAME_OBJECT );
145226 + assert( (pSelect->selFlags & SF_Resolved)!=0 );
145227 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
145228 assert( aff==SQLITE_AFF_NONE || aff==SQLITE_AFF_BLOB );
145229 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
@@ -144265,17 +145234,22 @@ SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(
145234 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
145235 const char *zType;
145236 i64 n;
145237 + int m = 0;
145238 + Select *pS2 = pSelect;
145239 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
145240 p = a[i].pExpr;
145241 /* pCol->szEst = ... // Column size est for SELECT tables never used */
145242 pCol->affinity = sqlite3ExprAffinity(p);
145243 + while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){
145244 + m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145245 + pS2 = pS2->pNext;
145246 + pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
145247 + }
145248 if( pCol->affinity<=SQLITE_AFF_NONE ){
145249 pCol->affinity = aff;
145250 }
144275 - if( pCol->affinity>=SQLITE_AFF_TEXT && pSelect->pNext ){
144276 - int m = 0;
144277 - Select *pS2;
144278 - for(m=0, pS2=pSelect->pNext; pS2; pS2=pS2->pNext){
145251 + if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){
145252 + for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
145253 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145254 }
145255 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
@@ -144305,12 +145279,12 @@ SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(
145279 }
145280 }
145281 if( zType ){
144308 - i64 m = sqlite3Strlen30(zType);
145282 + const i64 k = sqlite3Strlen30(zType);
145283 n = sqlite3Strlen30(pCol->zCnName);
144310 - pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
145284 + pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2);
145285 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
145286 if( pCol->zCnName ){
144313 - memcpy(&pCol->zCnName[n+1], zType, m+1);
145287 + memcpy(&pCol->zCnName[n+1], zType, k+1);
145288 pCol->colFlags |= COLFLAG_HASTYPE;
145289 }
145290 }
@@ -146707,7 +147681,7 @@ static void constInsert(
147681 ){
147682 int i;
147683 assert( pColumn->op==TK_COLUMN );
146710 - assert( sqlite3ExprIsConstant(pValue) );
147684 + assert( sqlite3ExprIsConstant(pConst->pParse, pValue) );
147685
147686 if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
147687 if( sqlite3ExprAffinity(pValue)!=0 ) return;
@@ -146765,10 +147739,10 @@ static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
147739 pLeft = pExpr->pLeft;
147740 assert( pRight!=0 );
147741 assert( pLeft!=0 );
146768 - if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
147742 + if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pLeft) ){
147743 constInsert(pConst,pRight,pLeft,pExpr);
147744 }
146771 - if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
147745 + if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pRight) ){
147746 constInsert(pConst,pLeft,pRight,pExpr);
147747 }
147748 }
@@ -146989,6 +147963,18 @@ static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
147963 ** The hope is that the terms added to the inner query will make it more
147964 ** efficient.
147965 **
147966 +** NAME AMBIGUITY
147967 +**
147968 +** This optimization is called the "WHERE-clause push-down optimization".
147969 +**
147970 +** Do not confuse this optimization with another unrelated optimization
147971 +** with a similar name: The "MySQL push-down optimization" causes WHERE
147972 +** clause terms that can be evaluated using only the index and without
147973 +** reference to the table are run first, so that if they are false,
147974 +** unnecessary table seeks are avoided.
147975 +**
147976 +** RULES
147977 +**
147978 ** Do not attempt this optimization if:
147979 **
147980 ** (1) (** This restriction was removed on 2017-09-29. We used to
@@ -147054,10 +148040,10 @@ static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
148040 ** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING
148041 ** clause and the subquery.
148042 **
147057 -** Without this restriction, the push-down optimization might move
147058 -** the ON/USING filter expression from the left side of a RIGHT JOIN
147059 -** over to the right side, which leads to incorrect answers. See
147060 -** also restriction (6) in sqlite3ExprIsSingleTableConstraint().
148043 +** Without this restriction, the WHERE-clause push-down optimization
148044 +** might move the ON/USING filter expression from the left side of a
148045 +** RIGHT JOIN over to the right side, which leads to incorrect answers.
148046 +** See also restriction (6) in sqlite3ExprIsSingleTableConstraint().
148047 **
148048 ** (10) The inner query is not the right-hand table of a RIGHT JOIN.
148049 **
@@ -147189,7 +148175,7 @@ static int pushDownWhereTerms(
148175 }
148176 #endif
148177
147192 - if( sqlite3ExprIsSingleTableConstraint(pWhere, pSrcList, iSrc) ){
148178 + if( sqlite3ExprIsSingleTableConstraint(pWhere, pSrcList, iSrc, 1) ){
148179 nChng++;
148180 pSubq->selFlags |= SF_PushDown;
148181 while( pSubq ){
@@ -148324,8 +149310,7 @@ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
149310 if( p->selFlags & SF_HasTypeInfo ) return;
149311 p->selFlags |= SF_HasTypeInfo;
149312 pParse = pWalker->pParse;
148327 - testcase( (p->selFlags & SF_Resolved)==0 );
148328 - assert( (p->selFlags & SF_Resolved) || IN_RENAME_OBJECT );
149313 + assert( (p->selFlags & SF_Resolved) );
149314 pTabList = p->pSrc;
149315 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
149316 Table *pTab = pFrom->pTab;
@@ -148395,6 +149380,8 @@ SQLITE_PRIVATE void sqlite3SelectPrep(
149380 */
149381 static void printAggInfo(AggInfo *pAggInfo){
149382 int ii;
149383 + sqlite3DebugPrintf("AggInfo %d/%p:\n",
149384 + pAggInfo->selId, pAggInfo);
149385 for(ii=0; ii<pAggInfo->nColumn; ii++){
149386 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
149387 sqlite3DebugPrintf(
@@ -149585,7 +150572,7 @@ SQLITE_PRIVATE int sqlite3Select(
150572 /* Generate code for all sub-queries in the FROM clause
150573 */
150574 pSub = pItem->pSelect;
149588 - if( pSub==0 ) continue;
150575 + if( pSub==0 || pItem->addrFillSub!=0 ) continue;
150576
150577 /* The code for a subquery should only be generated once. */
150578 assert( pItem->addrFillSub==0 );
@@ -149616,7 +150603,7 @@ SQLITE_PRIVATE int sqlite3Select(
150603 #endif
150604 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
150605 }else{
149619 - TREETRACE(0x4000,pParse,p,("Push-down not possible\n"));
150606 + TREETRACE(0x4000,pParse,p,("WHERE-lcause push-down not possible\n"));
150607 }
150608
150609 /* Convert unused result columns of the subquery into simple NULL
@@ -150497,6 +151484,12 @@ select_end:
151484 sqlite3ExprListDelete(db, pMinMaxOrderBy);
151485 #ifdef SQLITE_DEBUG
151486 if( pAggInfo && !db->mallocFailed ){
151487 +#if TREETRACE_ENABLED
151488 + if( sqlite3TreeTrace & 0x20 ){
151489 + TREETRACE(0x20,pParse,p,("Finished with AggInfo\n"));
151490 + printAggInfo(pAggInfo);
151491 + }
151492 +#endif
151493 for(i=0; i<pAggInfo->nColumn; i++){
151494 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
151495 if( pExpr==0 ) continue;
@@ -151678,6 +152671,72 @@ static ExprList *sqlite3ExpandReturning(
152671 return pNew;
152672 }
152673
152674 +/* If the Expr node is a subquery or an EXISTS operator or an IN operator that
152675 +** uses a subquery, and if the subquery is SF_Correlated, then mark the
152676 +** expression as EP_VarSelect.
152677 +*/
152678 +static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){
152679 + UNUSED_PARAMETER(NotUsed);
152680 + if( ExprUseXSelect(pExpr)
152681 + && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0
152682 + ){
152683 + testcase( ExprHasProperty(pExpr, EP_VarSelect) );
152684 + ExprSetProperty(pExpr, EP_VarSelect);
152685 + }
152686 + return WRC_Continue;
152687 +}
152688 +
152689 +
152690 +/*
152691 +** If the SELECT references the table pWalker->u.pTab, then do two things:
152692 +**
152693 +** (1) Mark the SELECT as as SF_Correlated.
152694 +** (2) Set pWalker->eCode to non-zero so that the caller will know
152695 +** that (1) has happened.
152696 +*/
152697 +static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){
152698 + int i;
152699 + SrcList *pSrc;
152700 + assert( pSelect!=0 );
152701 + pSrc = pSelect->pSrc;
152702 + assert( pSrc!=0 );
152703 + for(i=0; i<pSrc->nSrc; i++){
152704 + if( pSrc->a[i].pTab==pWalker->u.pTab ){
152705 + testcase( pSelect->selFlags & SF_Correlated );
152706 + pSelect->selFlags |= SF_Correlated;
152707 + pWalker->eCode = 1;
152708 + break;
152709 + }
152710 + }
152711 + return WRC_Continue;
152712 +}
152713 +
152714 +/*
152715 +** Scan the expression list that is the argument to RETURNING looking
152716 +** for subqueries that depend on the table which is being modified in the
152717 +** statement that is hosting the RETURNING clause (pTab). Mark all such
152718 +** subqueries as SF_Correlated. If the subqueries are part of an
152719 +** expression, mark the expression as EP_VarSelect.
152720 +**
152721 +** https://sqlite.org/forum/forumpost/2c83569ce8945d39
152722 +*/
152723 +static void sqlite3ProcessReturningSubqueries(
152724 + ExprList *pEList,
152725 + Table *pTab
152726 +){
152727 + Walker w;
152728 + memset(&w, 0, sizeof(w));
152729 + w.xExprCallback = sqlite3ExprWalkNoop;
152730 + w.xSelectCallback = sqlite3ReturningSubqueryCorrelated;
152731 + w.u.pTab = pTab;
152732 + sqlite3WalkExprList(&w, pEList);
152733 + if( w.eCode ){
152734 + w.xExprCallback = sqlite3ReturningSubqueryVarSelect;
152735 + w.xSelectCallback = sqlite3SelectWalkNoop;
152736 + sqlite3WalkExprList(&w, pEList);
152737 + }
152738 +}
152739 +
152740 /*
152741 ** Generate code for the RETURNING trigger. Unlike other triggers
152742 ** that invoke a subprogram in the bytecode, the code for RETURNING
@@ -151714,6 +152773,7 @@ static void codeReturningTrigger(
152773 sSelect.pSrc = &sFrom;
152774 sFrom.nSrc = 1;
152775 sFrom.a[0].pTab = pTab;
152776 + sFrom.a[0].zName = pTab->zName; /* tag-20240424-1 */
152777 sFrom.a[0].iCursor = -1;
152778 sqlite3SelectPrep(pParse, &sSelect, 0);
152779 if( pParse->nErr==0 ){
@@ -151740,6 +152800,7 @@ static void codeReturningTrigger(
152800 int i;
152801 int nCol = pNew->nExpr;
152802 int reg = pParse->nMem+1;
152803 + sqlite3ProcessReturningSubqueries(pNew, pTab);
152804 pParse->nMem += nCol+2;
152805 pReturning->iRetReg = reg;
152806 for(i=0; i<nCol; i++){
@@ -154950,6 +156011,8 @@ static int vtabCallConstructor(
156011 db->pVtabCtx = &sCtx;
156012 pTab->nTabRef++;
156013 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
156014 + assert( pTab!=0 );
156015 + assert( pTab->nTabRef>1 || rc!=SQLITE_OK );
156016 sqlite3DeleteTable(db, pTab);
156017 db->pVtabCtx = sCtx.pPrior;
156018 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
@@ -154972,7 +156035,7 @@ static int vtabCallConstructor(
156035 pVTable->nRef = 1;
156036 if( sCtx.bDeclared==0 ){
156037 const char *zFormat = "vtable constructor did not declare schema: %s";
154975 - *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
156038 + *pzErr = sqlite3MPrintf(db, zFormat, zModuleName);
156039 sqlite3VtabUnlock(pVTable);
156040 rc = SQLITE_ERROR;
156041 }else{
@@ -155150,12 +156213,30 @@ SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
156213 Table *pTab;
156214 Parse sParse;
156215 int initBusy;
156216 + int i;
156217 + const unsigned char *z;
156218 + static const u8 aKeyword[] = { TK_CREATE, TK_TABLE, 0 };
156219
156220 #ifdef SQLITE_ENABLE_API_ARMOR
156221 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
156222 return SQLITE_MISUSE_BKPT;
156223 }
156224 #endif
156225 +
156226 + /* Verify that the first two keywords in the CREATE TABLE statement
156227 + ** really are "CREATE" and "TABLE". If this is not the case, then
156228 + ** sqlite3_declare_vtab() is being misused.
156229 + */
156230 + z = (const unsigned char*)zCreateTable;
156231 + for(i=0; aKeyword[i]; i++){
156232 + int tokenType = 0;
156233 + do{ z += sqlite3GetToken(z, &tokenType); }while( tokenType==TK_SPACE );
156234 + if( tokenType!=aKeyword[i] ){
156235 + sqlite3ErrorWithMsg(db, SQLITE_ERROR, "syntax error");
156236 + return SQLITE_ERROR;
156237 + }
156238 + }
156239 +
156240 sqlite3_mutex_enter(db->mutex);
156241 pCtx = db->pVtabCtx;
156242 if( !pCtx || pCtx->bDeclared ){
@@ -155163,6 +156244,7 @@ SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
156244 sqlite3_mutex_leave(db->mutex);
156245 return SQLITE_MISUSE_BKPT;
156246 }
156247 +
156248 pTab = pCtx->pTab;
156249 assert( IsVirtual(pTab) );
156250
@@ -155176,11 +156258,10 @@ SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
156258 initBusy = db->init.busy;
156259 db->init.busy = 0;
156260 sParse.nQueryLoop = 1;
155179 - if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable)
155180 - && ALWAYS(sParse.pNewTable!=0)
155181 - && ALWAYS(!db->mallocFailed)
155182 - && IsOrdinaryTable(sParse.pNewTable)
155183 - ){
156261 + if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable) ){
156262 + assert( sParse.pNewTable!=0 );
156263 + assert( !db->mallocFailed );
156264 + assert( IsOrdinaryTable(sParse.pNewTable) );
156265 assert( sParse.zErrMsg==0 );
156266 if( !pTab->aCol ){
156267 Table *pNew = sParse.pNewTable;
@@ -157675,6 +158756,27 @@ static SQLITE_NOINLINE void filterPullDown(
158756 }
158757 }
158758
158759 +/*
158760 +** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...)
158761 +** operator. Return true if level pLoop is guaranteed to visit only one
158762 +** row for each key generated for the index.
158763 +*/
158764 +static int whereLoopIsOneRow(WhereLoop *pLoop){
158765 + if( pLoop->u.btree.pIndex->onError
158766 + && pLoop->nSkip==0
158767 + && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
158768 + ){
158769 + int ii;
158770 + for(ii=0; ii<pLoop->u.btree.nEq; ii++){
158771 + if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){
158772 + return 0;
158773 + }
158774 + }
158775 + return 1;
158776 + }
158777 + return 0;
158778 +}
158779 +
158780 /*
158781 ** Generate code for the start of the iLevel-th loop in the WHERE clause
158782 ** implementation described by pWInfo.
@@ -157753,7 +158855,7 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
158855 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
158856 pLevel->iLeftJoin = ++pParse->nMem;
158857 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
157756 - VdbeComment((v, "init LEFT JOIN no-match flag"));
158858 + VdbeComment((v, "init LEFT JOIN match flag"));
158859 }
158860
158861 /* Compute a safe address to jump to if we discover that the table for
@@ -158422,7 +159524,9 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
159524 }
159525
159526 /* Record the instruction used to terminate the loop. */
158425 - if( pLoop->wsFlags & WHERE_ONEROW ){
159527 + if( (pLoop->wsFlags & WHERE_ONEROW)
159528 + || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
159529 + ){
159530 pLevel->op = OP_Noop;
159531 }else if( bRev ){
159532 pLevel->op = OP_Prev;
@@ -158812,6 +159916,12 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
159916 ** iLoop==3: Code all remaining expressions.
159917 **
159918 ** An effort is made to skip unnecessary iterations of the loop.
159919 + **
159920 + ** This optimization of causing simple query restrictions to occur before
159921 + ** more complex one is call the "push-down" optimization in MySQL. Here
159922 + ** in SQLite, the name is "MySQL push-down", since there is also another
159923 + ** totally unrelated optimization called "WHERE-clause push-down".
159924 + ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware.
159925 */
159926 iLoop = (pIdx ? 1 : 2);
159927 do{
@@ -159062,7 +160172,16 @@ SQLITE_PRIVATE SQLITE_NOINLINE void sqlite3WhereRightJoinLoop(
160172 pRJ->regReturn);
160173 for(k=0; k<iLevel; k++){
160174 int iIdxCur;
160175 + SrcItem *pRight;
160176 + assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom );
160177 + pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom];
160178 mAll |= pWInfo->a[k].pWLoop->maskSelf;
160179 + if( pRight->fg.viaCoroutine ){
160180 + sqlite3VdbeAddOp3(
160181 + v, OP_Null, 0, pRight->regResult,
160182 + pRight->regResult + pRight->pSelect->pEList->nExpr-1
160183 + );
160184 + }
160185 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
160186 iIdxCur = pWInfo->a[k].iIdxCur;
160187 if( iIdxCur ){
@@ -160119,7 +161238,7 @@ static SQLITE_NOINLINE int exprMightBeIndexed2(
161238 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
161239 assert( pIdx->bHasExpr );
161240 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
160122 - && pExpr->op!=TK_STRING
161241 + && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr)
161242 ){
161243 aiCurCol[0] = iCur;
161244 aiCurCol[1] = XN_EXPR;
@@ -160768,6 +161887,7 @@ SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Selec
161887 continue;
161888 }
161889 if( pWC->a[ii].leftCursor!=iCsr ) return;
161890 + if( pWC->a[ii].prereqRight!=0 ) return;
161891 }
161892
161893 /* Check condition (5). Return early if it is not met. */
@@ -160782,12 +161902,14 @@ SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Selec
161902
161903 /* All conditions are met. Add the terms to the where-clause object. */
161904 assert( p->pLimit->op==TK_LIMIT );
160785 - whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
160786 - iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
160787 - if( p->iOffset>0 ){
161905 + if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){
161906 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
161907 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
161908 }
161909 + if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){
161910 + whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
161911 + iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
161912 + }
161913 }
161914 }
161915
@@ -161305,6 +162427,42 @@ static Expr *whereRightSubexprIsColumn(Expr *p){
162427 return 0;
162428 }
162429
162430 +/*
162431 +** Term pTerm is guaranteed to be a WO_IN term. It may be a component term
162432 +** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)".
162433 +** This function checks to see if the term is compatible with an index
162434 +** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so,
162435 +** it returns a pointer to the name of the collation sequence (e.g. "BINARY"
162436 +** or "NOCASE") used by the comparison in pTerm. If it is not compatible
162437 +** with affinity idxaff, NULL is returned.
162438 +*/
162439 +static SQLITE_NOINLINE const char *indexInAffinityOk(
162440 + Parse *pParse,
162441 + WhereTerm *pTerm,
162442 + u8 idxaff
162443 +){
162444 + Expr *pX = pTerm->pExpr;
162445 + Expr inexpr;
162446 +
162447 + assert( pTerm->eOperator & WO_IN );
162448 +
162449 + if( sqlite3ExprIsVector(pX->pLeft) ){
162450 + int iField = pTerm->u.x.iField - 1;
162451 + inexpr.flags = 0;
162452 + inexpr.op = TK_EQ;
162453 + inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
162454 + assert( ExprUseXSelect(pX) );
162455 + inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
162456 + pX = &inexpr;
162457 + }
162458 +
162459 + if( sqlite3IndexAffinityOk(pX, idxaff) ){
162460 + CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX);
162461 + return pRet ? pRet->zName : sqlite3StrBINARY;
162462 + }
162463 + return 0;
162464 +}
162465 +
162466 /*
162467 ** Advance to the next WhereTerm that matches according to the criteria
162468 ** established when the pScan object was initialized by whereScanInit().
@@ -161355,16 +162513,24 @@ static WhereTerm *whereScanNext(WhereScan *pScan){
162513 if( (pTerm->eOperator & pScan->opMask)!=0 ){
162514 /* Verify the affinity and collating sequence match */
162515 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
161358 - CollSeq *pColl;
162516 + const char *zCollName;
162517 Parse *pParse = pWC->pWInfo->pParse;
162518 pX = pTerm->pExpr;
161361 - if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
161362 - continue;
162519 +
162520 + if( (pTerm->eOperator & WO_IN) ){
162521 + zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
162522 + if( !zCollName ) continue;
162523 + }else{
162524 + CollSeq *pColl;
162525 + if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
162526 + continue;
162527 + }
162528 + assert(pX->pLeft);
162529 + pColl = sqlite3ExprCompareCollSeq(pParse, pX);
162530 + zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
162531 }
161364 - assert(pX->pLeft);
161365 - pColl = sqlite3ExprCompareCollSeq(pParse, pX);
161366 - if( pColl==0 ) pColl = pParse->db->pDfltColl;
161367 - if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
162532 +
162533 + if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
162534 continue;
162535 }
162536 }
@@ -161716,9 +162882,13 @@ static void translateColumnToCopy(
162882 ** are no-ops.
162883 */
162884 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
161719 -static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
162885 +static void whereTraceIndexInfoInputs(
162886 + sqlite3_index_info *p, /* The IndexInfo object */
162887 + Table *pTab /* The TABLE that is the virtual table */
162888 +){
162889 int i;
162890 if( (sqlite3WhereTrace & 0x10)==0 ) return;
162891 + sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName);
162892 for(i=0; i<p->nConstraint; i++){
162893 sqlite3DebugPrintf(
162894 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
@@ -161736,9 +162906,13 @@ static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
162906 p->aOrderBy[i].desc);
162907 }
162908 }
161739 -static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
162909 +static void whereTraceIndexInfoOutputs(
162910 + sqlite3_index_info *p, /* The IndexInfo object */
162911 + Table *pTab /* The TABLE that is the virtual table */
162912 +){
162913 int i;
162914 if( (sqlite3WhereTrace & 0x10)==0 ) return;
162915 + sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName);
162916 for(i=0; i<p->nConstraint; i++){
162917 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
162918 i,
@@ -161752,8 +162926,8 @@ static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
162926 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
162927 }
162928 #else
161755 -#define whereTraceIndexInfoInputs(A)
161756 -#define whereTraceIndexInfoOutputs(A)
162929 +#define whereTraceIndexInfoInputs(A,B)
162930 +#define whereTraceIndexInfoOutputs(A,B)
162931 #endif
162932
162933 /*
@@ -161937,7 +163111,7 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
163111 ** WHERE clause (or the ON clause of a LEFT join) that constrain which
163112 ** rows of the target table (pSrc) that can be used. */
163113 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
161940 - && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom)
163114 + && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom, 0)
163115 ){
163116 pPartial = sqlite3ExprAnd(pParse, pPartial,
163117 sqlite3ExprDup(pParse->db, pExpr, 0));
@@ -161979,7 +163153,7 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
163153 ** if they go out of sync.
163154 */
163155 if( IsView(pTable) ){
161982 - extraCols = ALLBITS;
163156 + extraCols = ALLBITS & ~idxCols;
163157 }else{
163158 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
163159 }
@@ -162206,7 +163380,7 @@ static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
163380 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
163381 Expr *pExpr = pTerm->pExpr;
163382 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
162209 - && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, iSrc)
163383 + && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, iSrc, 0)
163384 ){
163385 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
163386 }
@@ -162332,7 +163506,7 @@ static sqlite3_index_info *allocateIndexInfo(
163506 Expr *pE2;
163507
163508 /* Skip over constant terms in the ORDER BY clause */
162335 - if( sqlite3ExprIsConstant(pExpr) ){
163509 + if( sqlite3ExprIsConstant(0, pExpr) ){
163510 continue;
163511 }
163512
@@ -162367,7 +163541,7 @@ static sqlite3_index_info *allocateIndexInfo(
163541 }
163542 if( i==n ){
163543 nOrderBy = n;
162370 - if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
163544 + if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) && !pSrc->fg.rowidUsed ){
163545 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
163546 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
163547 eDistinct = 1;
@@ -162444,7 +163618,7 @@ static sqlite3_index_info *allocateIndexInfo(
163618 pIdxInfo->nConstraint = j;
163619 for(i=j=0; i<nOrderBy; i++){
163620 Expr *pExpr = pOrderBy->a[i].pExpr;
162447 - if( sqlite3ExprIsConstant(pExpr) ) continue;
163621 + if( sqlite3ExprIsConstant(0, pExpr) ) continue;
163622 assert( pExpr->op==TK_COLUMN
163623 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
163624 && pExpr->iColumn==pExpr->pLeft->iColumn) );
@@ -162496,11 +163670,11 @@ static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
163670 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
163671 int rc;
163672
162499 - whereTraceIndexInfoInputs(p);
163673 + whereTraceIndexInfoInputs(p, pTab);
163674 pParse->db->nSchemaLock++;
163675 rc = pVtab->pModule->xBestIndex(pVtab, p);
163676 pParse->db->nSchemaLock--;
162503 - whereTraceIndexInfoOutputs(p);
163677 + whereTraceIndexInfoOutputs(p, pTab);
163678
163679 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
163680 if( rc==SQLITE_NOMEM ){
@@ -163978,7 +165152,9 @@ static int whereLoopAddBtreeIndex(
165152 }
165153 if( pProbe->bUnordered || pProbe->bLowQual ){
165154 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
163981 - if( pProbe->bLowQual ) opMask &= ~(WO_EQ|WO_IN|WO_IS);
165155 + if( pProbe->bLowQual && pSrc->fg.isIndexedBy==0 ){
165156 + opMask &= ~(WO_EQ|WO_IN|WO_IS);
165157 + }
165158 }
165159
165160 assert( pNew->u.btree.nEq<pProbe->nColumn );
@@ -164245,10 +165421,13 @@ static int whereLoopAddBtreeIndex(
165421 }
165422 }
165423
164248 - /* Set rCostIdx to the cost of visiting selected rows in index. Add
164249 - ** it to pNew->rRun, which is currently set to the cost of the index
164250 - ** seek only. Then, if this is a non-covering index, add the cost of
164251 - ** visiting the rows in the main table. */
165424 + /* Set rCostIdx to the estimated cost of visiting selected rows in the
165425 + ** index. The estimate is the sum of two values:
165426 + ** 1. The cost of doing one search-by-key to find the first matching
165427 + ** entry
165428 + ** 2. Stepping forward in the index pNew->nOut times to find all
165429 + ** additional matching entries.
165430 + */
165431 assert( pSrc->pTab->szTabRow>0 );
165432 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
165433 /* The pProbe->szIdxRow is low for an IPK table since the interior
@@ -164259,7 +165438,15 @@ static int whereLoopAddBtreeIndex(
165438 }else{
165439 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
165440 }
164262 - pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
165441 + rCostIdx = sqlite3LogEstAdd(rLogSize, rCostIdx);
165442 +
165443 + /* Estimate the cost of running the loop. If all data is coming
165444 + ** from the index, then this is just the cost of doing the index
165445 + ** lookup and scan. But if some data is coming out of the main table,
165446 + ** we also have to add in the cost of doing pNew->nOut searches to
165447 + ** locate the row in the main table that corresponds to the index entry.
165448 + */
165449 + pNew->rRun = rCostIdx;
165450 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
165451 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
165452 }
@@ -164365,7 +165552,9 @@ static int indexMightHelpWithOrderBy(
165552 for(ii=0; ii<pOB->nExpr; ii++){
165553 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
165554 if( NEVER(pExpr==0) ) continue;
164368 - if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
165555 + if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN)
165556 + && pExpr->iTable==iCursor
165557 + ){
165558 if( pExpr->iColumn<0 ) return 1;
165559 for(jj=0; jj<pIndex->nKeyCol; jj++){
165560 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
@@ -164622,7 +165811,7 @@ static void wherePartIdxExpr(
165811 u8 aff;
165812
165813 if( pLeft->op!=TK_COLUMN ) return;
164625 - if( !sqlite3ExprIsConstant(pRight) ) return;
165814 + if( !sqlite3ExprIsConstant(0, pRight) ) return;
165815 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pParse, pPart)) ) return;
165816 if( pLeft->iColumn<0 ) return;
165817 aff = pIdx->pTable->aCol[pLeft->iColumn].affinity;
@@ -164895,7 +166084,9 @@ static int whereLoopAddBtree(
166084 " according to whereIsCoveringIndex()\n", pProbe->zName));
166085 }
166086 }
164898 - }else if( m==0 ){
166087 + }else if( m==0
166088 + && (HasRowid(pTab) || pWInfo->pSelect!=0 || sqlite3FaultSim(700))
166089 + ){
166090 WHERETRACE(0x200,
166091 ("-> %s a covering index according to bitmasks\n",
166092 pProbe->zName, m==0 ? "is" : "is not"));
@@ -164971,7 +166162,7 @@ static int whereLoopAddBtree(
166162 ** unique index is used (making the index functionally non-unique)
166163 ** then the sqlite_stat1 data becomes important for scoring the
166164 ** plan */
164974 - pTab->tabFlags |= TF_StatsUsed;
166165 + pTab->tabFlags |= TF_MaybeReanalyze;
166166 }
166167 #ifdef SQLITE_ENABLE_STAT4
166168 sqlite3Stat4ProbeFree(pBuilder->pRec);
@@ -164993,6 +166184,21 @@ static int isLimitTerm(WhereTerm *pTerm){
166184 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
166185 }
166186
166187 +/*
166188 +** Return true if the first nCons constraints in the pUsage array are
166189 +** marked as in-use (have argvIndex>0). False otherwise.
166190 +*/
166191 +static int allConstraintsUsed(
166192 + struct sqlite3_index_constraint_usage *aUsage,
166193 + int nCons
166194 +){
166195 + int ii;
166196 + for(ii=0; ii<nCons; ii++){
166197 + if( aUsage[ii].argvIndex<=0 ) return 0;
166198 + }
166199 + return 1;
166200 +}
166201 +
166202 /*
166203 ** Argument pIdxInfo is already populated with all constraints that may
166204 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
@@ -165133,13 +166339,20 @@ static int whereLoopAddVirtualOne(
166339 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
166340 }
166341
166342 + /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
166343 + ** terms. And if there are any, they should follow all other terms. */
166344 assert( pbRetryLimit || !isLimitTerm(pTerm) );
165137 - if( isLimitTerm(pTerm) && *pbIn ){
166345 + assert( !isLimitTerm(pTerm) || i>=nConstraint-2 );
166346 + assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) );
166347 +
166348 + if( isLimitTerm(pTerm) && (*pbIn || !allConstraintsUsed(pUsage, i)) ){
166349 /* If there is an IN(...) term handled as an == (separate call to
166350 ** xFilter for each value on the RHS of the IN) and a LIMIT or
165140 - ** OFFSET term handled as well, the plan is unusable. Set output
165141 - ** variable *pbRetryLimit to true to tell the caller to retry with
165142 - ** LIMIT and OFFSET disabled. */
166351 + ** OFFSET term handled as well, the plan is unusable. Similarly,
166352 + ** if there is a LIMIT/OFFSET and there are other unused terms,
166353 + ** the plan cannot be used. In these cases set variable *pbRetryLimit
166354 + ** to true to tell the caller to retry with LIMIT and OFFSET
166355 + ** disabled. */
166356 if( pIdxInfo->needToFreeIdxStr ){
166357 sqlite3_free(pIdxInfo->idxStr);
166358 pIdxInfo->idxStr = 0;
@@ -165996,7 +167209,7 @@ static i8 wherePathSatisfiesOrderBy(
167209 if( MASKBIT(i) & obSat ) continue;
167210 p = pOrderBy->a[i].pExpr;
167211 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
165999 - if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
167212 + if( mTerm==0 && !sqlite3ExprIsConstant(0,p) ) continue;
167213 if( (mTerm&~orderDistinctMask)==0 ){
167214 obSat |= MASKBIT(i);
167215 }
@@ -166465,10 +167678,9 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
167678 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
167679 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
167680 }
166468 - if( pWInfo->pSelect->pOrderBy
166469 - && pWInfo->nOBSat > pWInfo->pSelect->pOrderBy->nExpr ){
166470 - pWInfo->nOBSat = pWInfo->pSelect->pOrderBy->nExpr;
166471 - }
167681 + /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */
167682 + assert( pWInfo->pSelect->pOrderBy==0
167683 + || pWInfo->nOBSat <= pWInfo->pSelect->pOrderBy->nExpr );
167684 }else{
167685 pWInfo->revMask = pFrom->revLoop;
167686 if( pWInfo->nOBSat<=0 ){
@@ -166511,7 +167723,6 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
167723 }
167724 }
167725
166514 -
167726 pWInfo->nRowOut = pFrom->nRow;
167727
167728 /* Free temporary memory and return success */
@@ -166519,6 +167730,83 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
167730 return SQLITE_OK;
167731 }
167732
167733 +/*
167734 +** This routine implements a heuristic designed to improve query planning.
167735 +** This routine is called in between the first and second call to
167736 +** wherePathSolver(). Hence the name "Interstage" "Heuristic".
167737 +**
167738 +** The first call to wherePathSolver() (hereafter just "solver()") computes
167739 +** the best path without regard to the order of the outputs. The second call
167740 +** to the solver() builds upon the first call to try to find an alternative
167741 +** path that satisfies the ORDER BY clause.
167742 +**
167743 +** This routine looks at the results of the first solver() run, and for
167744 +** every FROM clause term in the resulting query plan that uses an equality
167745 +** constraint against an index, disable other WhereLoops for that same
167746 +** FROM clause term that would try to do a full-table scan. This prevents
167747 +** an index search from being converted into a full-table scan in order to
167748 +** satisfy an ORDER BY clause, since even though we might get slightly better
167749 +** performance using the full-scan without sorting if the output size
167750 +** estimates are very precise, we might also get severe performance
167751 +** degradation using the full-scan if the output size estimate is too large.
167752 +** It is better to err on the side of caution.
167753 +**
167754 +** Except, if the first solver() call generated a full-table scan in an outer
167755 +** loop then stop this analysis at the first full-scan, since the second
167756 +** solver() run might try to swap that full-scan for another in order to
167757 +** get the output into the correct order. In other words, we allow a
167758 +** rewrite like this:
167759 +**
167760 +** First Solver() Second Solver()
167761 +** |-- SCAN t1 |-- SCAN t2
167762 +** |-- SEARCH t2 `-- SEARCH t1
167763 +** `-- SORT USING B-TREE
167764 +**
167765 +** The purpose of this routine is to disallow rewrites such as:
167766 +**
167767 +** First Solver() Second Solver()
167768 +** |-- SEARCH t1 |-- SCAN t2 <--- bad!
167769 +** |-- SEARCH t2 `-- SEARCH t1
167770 +** `-- SORT USING B-TREE
167771 +**
167772 +** See test cases in test/whereN.test for the real-world query that
167773 +** originally provoked this heuristic.
167774 +*/
167775 +static SQLITE_NOINLINE void whereInterstageHeuristic(WhereInfo *pWInfo){
167776 + int i;
167777 +#ifdef WHERETRACE_ENABLED
167778 + int once = 0;
167779 +#endif
167780 + for(i=0; i<pWInfo->nLevel; i++){
167781 + WhereLoop *p = pWInfo->a[i].pWLoop;
167782 + if( p==0 ) break;
167783 + if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 ) continue;
167784 + if( (p->wsFlags & (WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 ){
167785 + u8 iTab = p->iTab;
167786 + WhereLoop *pLoop;
167787 + for(pLoop=pWInfo->pLoops; pLoop; pLoop=pLoop->pNextLoop){
167788 + if( pLoop->iTab!=iTab ) continue;
167789 + if( (pLoop->wsFlags & (WHERE_CONSTRAINT|WHERE_AUTO_INDEX))!=0 ){
167790 + /* Auto-index and index-constrained loops allowed to remain */
167791 + continue;
167792 + }
167793 +#ifdef WHERETRACE_ENABLED
167794 + if( sqlite3WhereTrace & 0x80 ){
167795 + if( once==0 ){
167796 + sqlite3DebugPrintf("Loops disabled by interstage heuristic:\n");
167797 + once = 1;
167798 + }
167799 + sqlite3WhereLoopPrint(pLoop, &pWInfo->sWC);
167800 + }
167801 +#endif /* WHERETRACE_ENABLED */
167802 + pLoop->prereq = ALLBITS; /* Prevent 2nd solver() from using this one */
167803 + }
167804 + }else{
167805 + break;
167806 + }
167807 + }
167808 +}
167809 +
167810 /*
167811 ** Most queries use only a single table (they are not joins) and have
167812 ** simple == constraints against indexed fields. This routine attempts
@@ -166687,6 +167975,10 @@ static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
167975 ** the right-most table of a subquery that was flattened into the
167976 ** main query and that subquery was the right-hand operand of an
167977 ** inner join that held an ON or USING clause.
167978 +** 6) The ORDER BY clause has 63 or fewer terms
167979 +** 7) The omit-noop-join optimization is enabled.
167980 +**
167981 +** Items (1), (6), and (7) are checked by the caller.
167982 **
167983 ** For example, given:
167984 **
@@ -166807,7 +168099,7 @@ static SQLITE_NOINLINE void whereCheckIfBloomFilterIsUseful(
168099 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
168100 Table *pTab = pItem->pTab;
168101 if( (pTab->tabFlags & TF_HasStat1)==0 ) break;
166810 - pTab->tabFlags |= TF_StatsUsed;
168102 + pTab->tabFlags |= TF_MaybeReanalyze;
168103 if( i>=1
168104 && (pLoop->wsFlags & reqFlags)==reqFlags
168105 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
@@ -166828,6 +168120,58 @@ static SQLITE_NOINLINE void whereCheckIfBloomFilterIsUseful(
168120 }
168121 }
168122
168123 +/*
168124 +** Expression Node callback for sqlite3ExprCanReturnSubtype().
168125 +**
168126 +** Only a function call is able to return a subtype. So if the node
168127 +** is not a function call, return WRC_Prune immediately.
168128 +**
168129 +** A function call is able to return a subtype if it has the
168130 +** SQLITE_RESULT_SUBTYPE property.
168131 +**
168132 +** Assume that every function is able to pass-through a subtype from
168133 +** one of its argument (using sqlite3_result_value()). Most functions
168134 +** are not this way, but we don't have a mechanism to distinguish those
168135 +** that are from those that are not, so assume they all work this way.
168136 +** That means that if one of its arguments is another function and that
168137 +** other function is able to return a subtype, then this function is
168138 +** able to return a subtype.
168139 +*/
168140 +static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){
168141 + int n;
168142 + FuncDef *pDef;
168143 + sqlite3 *db;
168144 + if( pExpr->op!=TK_FUNCTION ){
168145 + return WRC_Prune;
168146 + }
168147 + assert( ExprUseXList(pExpr) );
168148 + db = pWalker->pParse->db;
168149 + n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
168150 + pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
168151 + if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
168152 + pWalker->eCode = 1;
168153 + return WRC_Prune;
168154 + }
168155 + return WRC_Continue;
168156 +}
168157 +
168158 +/*
168159 +** Return TRUE if expression pExpr is able to return a subtype.
168160 +**
168161 +** A TRUE return does not guarantee that a subtype will be returned.
168162 +** It only indicates that a subtype return is possible. False positives
168163 +** are acceptable as they only disable an optimization. False negatives,
168164 +** on the other hand, can lead to incorrect answers.
168165 +*/
168166 +static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){
168167 + Walker w;
168168 + memset(&w, 0, sizeof(w));
168169 + w.pParse = pParse;
168170 + w.xExprCallback = exprNodeCanReturnSubtype;
168171 + sqlite3WalkExpr(&w, pExpr);
168172 + return w.eCode;
168173 +}
168174 +
168175 /*
168176 ** The index pIdx is used by a query and contains one or more expressions.
168177 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
@@ -166860,20 +168204,12 @@ static SQLITE_NOINLINE void whereAddIndexedExpr(
168204 }else{
168205 continue;
168206 }
166863 - if( sqlite3ExprIsConstant(pExpr) ) continue;
166864 - if( pExpr->op==TK_FUNCTION ){
168207 + if( sqlite3ExprIsConstant(0,pExpr) ) continue;
168208 + if( pExpr->op==TK_FUNCTION && sqlite3ExprCanReturnSubtype(pParse,pExpr) ){
168209 /* Functions that might set a subtype should not be replaced by the
168210 ** value taken from an expression index since the index omits the
168211 ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
166868 - int n;
166869 - FuncDef *pDef;
166870 - sqlite3 *db = pParse->db;
166871 - assert( ExprUseXList(pExpr) );
166872 - n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
166873 - pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
166874 - if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
166875 - continue;
166876 - }
168212 + continue;
168213 }
168214 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
168215 if( p==0 ) break;
@@ -167056,6 +168392,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
168392 if( pOrderBy && pOrderBy->nExpr>=BMS ){
168393 pOrderBy = 0;
168394 wctrlFlags &= ~WHERE_WANT_DISTINCT;
168395 + wctrlFlags |= WHERE_KEEP_ALL_JOINS; /* Disable omit-noop-join opt */
168396 }
168397
168398 /* The number of tables in the FROM clause is limited by the number of
@@ -167138,7 +168475,11 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
168475 ){
168476 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
168477 }
167141 - ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
168478 + if( ALWAYS(pWInfo->pSelect)
168479 + && (pWInfo->pSelect->selFlags & SF_MultiValue)==0
168480 + ){
168481 + ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
168482 + }
168483 }else{
168484 /* Assign a bit from the bitmask to every term in the FROM clause.
168485 **
@@ -167291,6 +168632,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
168632 wherePathSolver(pWInfo, 0);
168633 if( db->mallocFailed ) goto whereBeginError;
168634 if( pWInfo->pOrderBy ){
168635 + whereInterstageHeuristic(pWInfo);
168636 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
168637 if( db->mallocFailed ) goto whereBeginError;
168638 }
@@ -167351,10 +168693,10 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
168693 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
168694 */
168695 notReady = ~(Bitmask)0;
167354 - if( pWInfo->nLevel>=2
167355 - && pResultSet!=0 /* these two combine to guarantee */
167356 - && 0==(wctrlFlags & WHERE_AGG_DISTINCT) /* condition (1) above */
167357 - && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
168696 + if( pWInfo->nLevel>=2 /* Must be a join, or this opt8n is pointless */
168697 + && pResultSet!=0 /* Condition (1) */
168698 + && 0==(wctrlFlags & (WHERE_AGG_DISTINCT|WHERE_KEEP_ALL_JOINS)) /* (1),(6) */
168699 + && OptimizationEnabled(db, SQLITE_OmitNoopJoin) /* (7) */
168700 ){
168701 notReady = whereOmitNoopJoin(pWInfo, notReady);
168702 nTabList = pWInfo->nLevel;
@@ -167674,26 +169016,6 @@ whereBeginError:
169016 }
169017 #endif
169018
167677 -#ifdef SQLITE_DEBUG
167678 -/*
167679 -** Return true if cursor iCur is opened by instruction k of the
167680 -** bytecode. Used inside of assert() only.
167681 -*/
167682 -static int cursorIsOpen(Vdbe *v, int iCur, int k){
167683 - while( k>=0 ){
167684 - VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
167685 - if( pOp->p1!=iCur ) continue;
167686 - if( pOp->opcode==OP_Close ) return 0;
167687 - if( pOp->opcode==OP_OpenRead ) return 1;
167688 - if( pOp->opcode==OP_OpenWrite ) return 1;
167689 - if( pOp->opcode==OP_OpenDup ) return 1;
167690 - if( pOp->opcode==OP_OpenAutoindex ) return 1;
167691 - if( pOp->opcode==OP_OpenEphemeral ) return 1;
167692 - }
167693 - return 0;
167694 -}
167695 -#endif /* SQLITE_DEBUG */
167696 -
169019 /*
169020 ** Generate the end of the WHERE loop. See comments on
169021 ** sqlite3WhereBegin() for additional information.
@@ -167840,7 +169162,15 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
169162 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
169163 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
169164 if( (ws & WHERE_IDX_ONLY)==0 ){
167843 - assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
169165 + SrcItem *pSrc = &pTabList->a[pLevel->iFrom];
169166 + assert( pLevel->iTabCur==pSrc->iCursor );
169167 + if( pSrc->fg.viaCoroutine ){
169168 + int m, n;
169169 + n = pSrc->regResult;
169170 + assert( pSrc->pTab!=0 );
169171 + m = pSrc->pTab->nCol;
169172 + sqlite3VdbeAddOp3(v, OP_Null, 0, n, n+m-1);
169173 + }
169174 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
169175 }
169176 if( (ws & WHERE_INDEXED)
@@ -167890,6 +169220,7 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
169220 */
169221 if( pTabItem->fg.viaCoroutine ){
169222 testcase( pParse->db->mallocFailed );
169223 + assert( pTabItem->regResult>=0 );
169224 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
169225 pTabItem->regResult, 0);
169226 continue;
@@ -167984,16 +169315,10 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
169315 ** reference. Verify that this is harmless - that the
169316 ** table being referenced really is open.
169317 */
167987 -#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
167988 - assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
167989 - || cursorIsOpen(v,pOp->p1,k)
167990 - || pOp->opcode==OP_Offset
167991 - );
167992 -#else
167993 - assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
167994 - || cursorIsOpen(v,pOp->p1,k)
167995 - );
167996 -#endif
169318 + if( pLoop->wsFlags & WHERE_IDX_ONLY ){
169319 + sqlite3ErrorMsg(pParse, "internal query planner error");
169320 + pParse->rc = SQLITE_INTERNAL;
169321 + }
169322 }
169323 }else if( pOp->opcode==OP_Rowid ){
169324 pOp->p1 = pLevel->iIdxCur;
@@ -169194,7 +170519,7 @@ SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
170519 ** variable values in the expression tree.
170520 */
170521 static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
169197 - if( 0==sqlite3ExprIsConstant(pExpr) ){
170522 + if( 0==sqlite3ExprIsConstant(0,pExpr) ){
170523 if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
170524 sqlite3ExprDelete(pParse->db, pExpr);
170525 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
@@ -171266,9 +172591,9 @@ static void updateDeleteLimitError(
172591 break;
172592 }
172593 }
171269 - if( (p->selFlags & SF_MultiValue)==0 &&
171270 - (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
171271 - cnt>mxSelect
172594 + if( (p->selFlags & (SF_MultiValue|SF_Values))==0
172595 + && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0
172596 + && cnt>mxSelect
172597 ){
172598 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
172599 }
@@ -171288,6 +172613,14 @@ static void updateDeleteLimitError(
172613 return pSelect;
172614 }
172615
172616 + /* Memory allocator for parser stack resizing. This is a thin wrapper around
172617 + ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate
172618 + ** testing.
172619 + */
172620 + static void *parserStackRealloc(void *pOld, sqlite3_uint64 newSize){
172621 + return sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize);
172622 + }
172623 +
172624
172625 /* Construct a new Expr object from a single token */
172626 static Expr *tokenExpr(Parse *pParse, int op, Token t){
@@ -171537,8 +172870,8 @@ static void updateDeleteLimitError(
172870 #define TK_TRUEFALSE 170
172871 #define TK_ISNOT 171
172872 #define TK_FUNCTION 172
171540 -#define TK_UMINUS 173
171541 -#define TK_UPLUS 174
172873 +#define TK_UPLUS 173
172874 +#define TK_UMINUS 174
172875 #define TK_TRUTH 175
172876 #define TK_REGISTER 176
172877 #define TK_VECTOR 177
@@ -171547,8 +172880,9 @@ static void updateDeleteLimitError(
172880 #define TK_ASTERISK 180
172881 #define TK_SPAN 181
172882 #define TK_ERROR 182
171550 -#define TK_SPACE 183
171551 -#define TK_ILLEGAL 184
172883 +#define TK_QNUMBER 183
172884 +#define TK_SPACE 184
172885 +#define TK_ILLEGAL 185
172886 #endif
172887 /**************** End token definitions ***************************************/
172888
@@ -171589,6 +172923,9 @@ static void updateDeleteLimitError(
172923 ** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
172924 ** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
172925 ** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
172926 +** YYREALLOC Name of the realloc() function to use
172927 +** YYFREE Name of the free() function to use
172928 +** YYDYNSTACK True if stack space should be extended on heap
172929 ** YYERRORSYMBOL is the code number of the error symbol. If not
172930 ** defined, then do no error processing.
172931 ** YYNSTATE the combined number of states.
@@ -171602,37 +172939,39 @@ static void updateDeleteLimitError(
172939 ** YY_NO_ACTION The yy_action[] code for no-op
172940 ** YY_MIN_REDUCE Minimum value for reduce actions
172941 ** YY_MAX_REDUCE Maximum value for reduce actions
172942 +** YY_MIN_DSTRCTR Minimum symbol value that has a destructor
172943 +** YY_MAX_DSTRCTR Maximum symbol value that has a destructor
172944 */
172945 #ifndef INTERFACE
172946 # define INTERFACE 1
172947 #endif
172948 /************* Begin control #defines *****************************************/
172949 #define YYCODETYPE unsigned short int
171611 -#define YYNOCODE 319
172950 +#define YYNOCODE 322
172951 #define YYACTIONTYPE unsigned short int
172952 #define YYWILDCARD 101
172953 #define sqlite3ParserTOKENTYPE Token
172954 typedef union {
172955 int yyinit;
172956 sqlite3ParserTOKENTYPE yy0;
171618 - TriggerStep* yy33;
171619 - Window* yy41;
171620 - Select* yy47;
171621 - SrcList* yy131;
171622 - struct TrigEvent yy180;
171623 - struct {int value; int mask;} yy231;
171624 - IdList* yy254;
171625 - u32 yy285;
171626 - ExprList* yy322;
171627 - Cte* yy385;
171628 - int yy394;
171629 - Upsert* yy444;
171630 - u8 yy516;
171631 - With* yy521;
171632 - const char* yy522;
171633 - Expr* yy528;
171634 - OnOrUsing yy561;
171635 - struct FrameBound yy595;
172957 + ExprList* yy14;
172958 + With* yy59;
172959 + Cte* yy67;
172960 + Upsert* yy122;
172961 + IdList* yy132;
172962 + int yy144;
172963 + const char* yy168;
172964 + SrcList* yy203;
172965 + Window* yy211;
172966 + OnOrUsing yy269;
172967 + struct TrigEvent yy286;

This file is too large to show in full.

src/database/sqlite/sqlite3.h
+74 -23
@@ -146,9 +146,9 @@ extern "C" {
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 -#define SQLITE_VERSION "3.45.3"
150 -#define SQLITE_VERSION_NUMBER 3045003
151 -#define SQLITE_SOURCE_ID "2024-04-15 13:34:05 8653b758870e6ef0c98d46b3ace27849054af85da891eb121e9aaa537f1e8355"
149 +#define SQLITE_VERSION "3.46.1"
150 +#define SQLITE_VERSION_NUMBER 3046001
151 +#define SQLITE_SOURCE_ID "2024-08-13 09:16:08 c9c2ab54ba1f5f46360f1b4f35d849cd3f080e6fc2b6c60e91b16c63f69a1e33"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
@@ -764,11 +764,11 @@ struct sqlite3_file {
764 ** </ul>
765 ** xLock() upgrades the database file lock. In other words, xLock() moves the
766 ** database file lock in the direction NONE toward EXCLUSIVE. The argument to
767 -** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
767 +** xLock() is always one of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
768 ** SQLITE_LOCK_NONE. If the database file lock is already at or above the
769 ** requested lock, then the call to xLock() is a no-op.
770 ** xUnlock() downgrades the database file lock to either SHARED or NONE.
771 -* If the lock is already at or below the requested lock state, then the call
771 +** If the lock is already at or below the requested lock state, then the call
772 ** to xUnlock() is a no-op.
773 ** The xCheckReservedLock() method checks whether any database connection,
774 ** either in this process or in some other process, is holding a RESERVED,
@@ -3305,8 +3305,8 @@ SQLITE_API int sqlite3_set_authorizer(
3305 #define SQLITE_RECURSIVE 33 /* NULL NULL */
3306
3307 /*
3308 -** CAPI3REF: Tracing And Profiling Functions
3309 -** METHOD: sqlite3
3308 +** CAPI3REF: Deprecated Tracing And Profiling Functions
3309 +** DEPRECATED
3310 **
3311 ** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3312 ** instead of the routines described here.
@@ -6887,6 +6887,12 @@ SQLITE_API int sqlite3_autovacuum_pages(
6887 ** The exceptions defined in this paragraph might change in a future
6888 ** release of SQLite.
6889 **
6890 +** Whether the update hook is invoked before or after the
6891 +** corresponding change is currently unspecified and may differ
6892 +** depending on the type of change. Do not rely on the order of the
6893 +** hook call with regards to the final result of the operation which
6894 +** triggers the hook.
6895 +**
6896 ** The update hook implementation must not do anything that will modify
6897 ** the database connection that invoked the update hook. Any actions
6898 ** to modify the database connection must be deferred until after the
@@ -8357,7 +8363,7 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8363 ** The sqlite3_keyword_count() interface returns the number of distinct
8364 ** keywords understood by SQLite.
8365 **
8360 -** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8366 +** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8367 ** makes *Z point to that keyword expressed as UTF8 and writes the number
8368 ** of bytes in the keyword into *L. The string that *Z points to is not
8369 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
@@ -9936,24 +9942,45 @@ SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9942 ** <li value="2"><p>
9943 ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
9944 ** that the query planner does not need the rows returned in any particular
9939 -** order, as long as rows with the same values in all "aOrderBy" columns
9940 -** are adjacent.)^ ^(Furthermore, only a single row for each particular
9941 -** combination of values in the columns identified by the "aOrderBy" field
9942 -** needs to be returned.)^ ^It is always ok for two or more rows with the same
9943 -** values in all "aOrderBy" columns to be returned, as long as all such rows
9944 -** are adjacent. ^The virtual table may, if it chooses, omit extra rows
9945 -** that have the same value for all columns identified by "aOrderBy".
9946 -** ^However omitting the extra rows is optional.
9945 +** order, as long as rows with the same values in all columns identified
9946 +** by "aOrderBy" are adjacent.)^ ^(Furthermore, when two or more rows
9947 +** contain the same values for all columns identified by "colUsed", all but
9948 +** one such row may optionally be omitted from the result.)^
9949 +** The virtual table is not required to omit rows that are duplicates
9950 +** over the "colUsed" columns, but if the virtual table can do that without
9951 +** too much extra effort, it could potentially help the query to run faster.
9952 ** This mode is used for a DISTINCT query.
9953 ** <li value="3"><p>
9949 -** ^(If the sqlite3_vtab_distinct() interface returns 3, that means
9950 -** that the query planner needs only distinct rows but it does need the
9951 -** rows to be sorted.)^ ^The virtual table implementation is free to omit
9952 -** rows that are identical in all aOrderBy columns, if it wants to, but
9953 -** it is not required to omit any rows. This mode is used for queries
9954 +** ^(If the sqlite3_vtab_distinct() interface returns 3, that means the
9955 +** virtual table must return rows in the order defined by "aOrderBy" as
9956 +** if the sqlite3_vtab_distinct() interface had returned 0. However if
9957 +** two or more rows in the result have the same values for all columns
9958 +** identified by "colUsed", then all but one such row may optionally be
9959 +** omitted.)^ Like when the return value is 2, the virtual table
9960 +** is not required to omit rows that are duplicates over the "colUsed"
9961 +** columns, but if the virtual table can do that without
9962 +** too much extra effort, it could potentially help the query to run faster.
9963 +** This mode is used for queries
9964 ** that have both DISTINCT and ORDER BY clauses.
9965 ** </ol>
9966 **
9967 +** <p>The following table summarizes the conditions under which the
9968 +** virtual table is allowed to set the "orderByConsumed" flag based on
9969 +** the value returned by sqlite3_vtab_distinct(). This table is a
9970 +** restatement of the previous four paragraphs:
9971 +**
9972 +** <table border=1 cellspacing=0 cellpadding=10 width="90%">
9973 +** <tr>
9974 +** <td valign="top">sqlite3_vtab_distinct() return value
9975 +** <td valign="top">Rows are returned in aOrderBy order
9976 +** <td valign="top">Rows with the same value in all aOrderBy columns are adjacent
9977 +** <td valign="top">Duplicates over all colUsed columns may be omitted
9978 +** <tr><td>0<td>yes<td>yes<td>no
9979 +** <tr><td>1<td>no<td>yes<td>no
9980 +** <tr><td>2<td>no<td>yes<td>yes
9981 +** <tr><td>3<td>yes<td>yes<td>yes
9982 +** </table>
9983 +**
9984 ** ^For the purposes of comparing virtual table output values to see if the
9985 ** values are same value for sorting purposes, two NULL values are considered
9986 ** to be the same. In other words, the comparison operator is "IS"
@@ -11998,6 +12025,30 @@ SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const c
12025 */
12026 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12027
12028 +/*
12029 +** CAPI3REF: Add A Single Change To A Changegroup
12030 +** METHOD: sqlite3_changegroup
12031 +**
12032 +** This function adds the single change currently indicated by the iterator
12033 +** passed as the second argument to the changegroup object. The rules for
12034 +** adding the change are just as described for [sqlite3changegroup_add()].
12035 +**
12036 +** If the change is successfully added to the changegroup, SQLITE_OK is
12037 +** returned. Otherwise, an SQLite error code is returned.
12038 +**
12039 +** The iterator must point to a valid entry when this function is called.
12040 +** If it does not, SQLITE_ERROR is returned and no change is added to the
12041 +** changegroup. Additionally, the iterator must not have been opened with
12042 +** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12043 +** returned.
12044 +*/
12045 +SQLITE_API int sqlite3changegroup_add_change(
12046 + sqlite3_changegroup*,
12047 + sqlite3_changeset_iter*
12048 +);
12049 +
12050 +
12051 +
12052 /*
12053 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12054 ** METHOD: sqlite3_changegroup
@@ -12802,8 +12853,8 @@ struct Fts5PhraseIter {
12853 ** EXTENSION API FUNCTIONS
12854 **
12855 ** xUserData(pFts):
12805 -** Return a copy of the context pointer the extension function was
12806 -** registered with.
12856 +** Return a copy of the pUserData pointer passed to the xCreateFunction()
12857 +** API when the extension function was registered.
12858 **
12859 ** xColumnTotalSize(pFts, iCol, pnToken):
12860 ** If parameter iCol is less than zero, set output variable *pnToken
src/database/sqlite/sqlite_functions.c
+4 -1
@@ -140,6 +140,10 @@ int configure_sqlite_database(sqlite3 *database, int target_version, const char
140 if (init_database_batch(database, list, description))
141 return 1;
142
143 + snprintfz(buf, sizeof(buf) - 1, "PRAGMA optimize=0x10002");
144 + if (init_database_batch(database, list, description))
145 + return 1;
146 +
147 return 0;
148 }
149
@@ -338,7 +342,6 @@ void sql_close_database(sqlite3 *database, const char *database_name)
342 if (unlikely(!database))
343 return;
344
341 - (void) db_execute(database, "PRAGMA analysis_limit=10000");
345 (void) db_execute(database, "PRAGMA optimize");
346
347 netdata_log_info("%s: Closing sqlite database", database_name);