| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.41.1. By combining all the individual C code files into this |
| 3 | +** version 3.42.0. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -450,13 +450,13 @@ |
| 450 | 450 | ** |
| 451 | 451 | ** See also: [sqlite3_libversion()], |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | | -#define SQLITE_VERSION "3.41.1" |
| 456 | | -#define SQLITE_VERSION_NUMBER 3041001 |
| 457 | | -#define SQLITE_SOURCE_ID "2023-03-09 16:04:34 cc8a0ee40cfc83ab42a0ff452de0a53fe17784aefb944ea7ac2cb078a8310730" |
| 455 | +#define SQLITE_VERSION "3.42.0" |
| 456 | +#define SQLITE_VERSION_NUMBER 3042000 |
| 457 | +#define SQLITE_SOURCE_ID "2023-04-10 18:44:00 4c5a3c5fb351cc1c2ce16c33314ce19c53531f09263f87456283d9d756002f9d" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -1959,23 +1959,26 @@ |
| 1959 | 1959 | ** |
| 1960 | 1960 | ** <b>The sqlite3_config() interface is not threadsafe. The application |
| 1961 | 1961 | ** must ensure that no other SQLite interfaces are invoked by other |
| 1962 | 1962 | ** threads while sqlite3_config() is running.</b> |
| 1963 | 1963 | ** |
| 1964 | | -** The sqlite3_config() interface |
| 1965 | | -** may only be invoked prior to library initialization using |
| 1966 | | -** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. |
| 1967 | | -** ^If sqlite3_config() is called after [sqlite3_initialize()] and before |
| 1968 | | -** [sqlite3_shutdown()] then it will return SQLITE_MISUSE. |
| 1969 | | -** Note, however, that ^sqlite3_config() can be called as part of the |
| 1970 | | -** implementation of an application-defined [sqlite3_os_init()]. |
| 1971 | | -** |
| 1972 | 1964 | ** The first argument to sqlite3_config() is an integer |
| 1973 | 1965 | ** [configuration option] that determines |
| 1974 | 1966 | ** what property of SQLite is to be configured. Subsequent arguments |
| 1975 | 1967 | ** vary depending on the [configuration option] |
| 1976 | 1968 | ** in the first argument. |
| 1969 | +** |
| 1970 | +** For most configuration options, the sqlite3_config() interface |
| 1971 | +** may only be invoked prior to library initialization using |
| 1972 | +** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. |
| 1973 | +** The exceptional configuration options that may be invoked at any time |
| 1974 | +** are called "anytime configuration options". |
| 1975 | +** ^If sqlite3_config() is called after [sqlite3_initialize()] and before |
| 1976 | +** [sqlite3_shutdown()] with a first argument that is not an anytime |
| 1977 | +** configuration option, then the sqlite3_config() call will return SQLITE_MISUSE. |
| 1978 | +** Note, however, that ^sqlite3_config() can be called as part of the |
| 1979 | +** implementation of an application-defined [sqlite3_os_init()]. |
| 1977 | 1980 | ** |
| 1978 | 1981 | ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. |
| 1979 | 1982 | ** ^If the option is unknown or SQLite is unable to set the option |
| 1980 | 1983 | ** then this routine returns a non-zero [error code]. |
| 1981 | 1984 | */ |
| | @@ -2079,10 +2082,27 @@ |
| 2079 | 2082 | ** CAPI3REF: Configuration Options |
| 2080 | 2083 | ** KEYWORDS: {configuration option} |
| 2081 | 2084 | ** |
| 2082 | 2085 | ** These constants are the available integer configuration options that |
| 2083 | 2086 | ** can be passed as the first argument to the [sqlite3_config()] interface. |
| 2087 | +** |
| 2088 | +** Most of the configuration options for sqlite3_config() |
| 2089 | +** will only work if invoked prior to [sqlite3_initialize()] or after |
| 2090 | +** [sqlite3_shutdown()]. The few exceptions to this rule are called |
| 2091 | +** "anytime configuration options". |
| 2092 | +** ^Calling [sqlite3_config()] with a first argument that is not an |
| 2093 | +** anytime configuration option in between calls to [sqlite3_initialize()] and |
| 2094 | +** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE. |
| 2095 | +** |
| 2096 | +** The set of anytime configuration options can change (by insertions |
| 2097 | +** and/or deletions) from one release of SQLite to the next. |
| 2098 | +** As of SQLite version 3.42.0, the complete set of anytime configuration |
| 2099 | +** options is: |
| 2100 | +** <ul> |
| 2101 | +** <li> SQLITE_CONFIG_LOG |
| 2102 | +** <li> SQLITE_CONFIG_PCACHE_HDRSZ |
| 2103 | +** </ul> |
| 2084 | 2104 | ** |
| 2085 | 2105 | ** New configuration options may be added in future releases of SQLite. |
| 2086 | 2106 | ** Existing configuration options might be discontinued. Applications |
| 2087 | 2107 | ** should check the return code from [sqlite3_config()] to make sure that |
| 2088 | 2108 | ** the call worked. The [sqlite3_config()] interface will return a |
| | @@ -2740,10 +2760,30 @@ |
| 2740 | 2760 | ** the [VACUUM] command will fail with an obscure error when attempting to |
| 2741 | 2761 | ** process a table with generated columns and a descending index. This is |
| 2742 | 2762 | ** not considered a bug since SQLite versions 3.3.0 and earlier do not support |
| 2743 | 2763 | ** either generated columns or decending indexes. |
| 2744 | 2764 | ** </dd> |
| 2765 | +** |
| 2766 | +** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]] |
| 2767 | +** <dt>SQLITE_DBCONFIG_STMT_SCANSTATUS</td> |
| 2768 | +** <dd>The SQLITE_DBCONFIG_STMT_SCANSTATUS option is only useful in |
| 2769 | +** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears |
| 2770 | +** a flag that enables collection of the sqlite3_stmt_scanstatus_v2() |
| 2771 | +** statistics. For statistics to be collected, the flag must be set on |
| 2772 | +** the database handle both when the SQL statement is prepared and when it |
| 2773 | +** is stepped. The flag is set (collection of statistics is enabled) |
| 2774 | +** by default.</dd> |
| 2775 | +** |
| 2776 | +** [[SQLITE_DBCONFIG_REVERSE_SCANORDER]] |
| 2777 | +** <dt>SQLITE_DBCONFIG_REVERSE_SCANORDER</td> |
| 2778 | +** <dd>The SQLITE_DBCONFIG_REVERSE_SCANORDER option change the default order |
| 2779 | +** in which tables and indexes are scanned so that the scans start at the end |
| 2780 | +** and work toward the beginning rather than starting at the beginning and |
| 2781 | +** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the |
| 2782 | +** same as setting [PRAGMA reverse_unordered_selects]. This configuration option |
| 2783 | +** is useful for application testing.</dd> |
| 2784 | +** |
| 2745 | 2785 | ** </dl> |
| 2746 | 2786 | */ |
| 2747 | 2787 | #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ |
| 2748 | 2788 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 2749 | 2789 | #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ |
| | @@ -2760,11 +2800,13 @@ |
| 2760 | 2800 | #define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */ |
| 2761 | 2801 | #define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */ |
| 2762 | 2802 | #define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */ |
| 2763 | 2803 | #define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */ |
| 2764 | 2804 | #define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */ |
| 2765 | | -#define SQLITE_DBCONFIG_MAX 1017 /* Largest DBCONFIG */ |
| 2805 | +#define SQLITE_DBCONFIG_STMT_SCANSTATUS 1018 /* int int* */ |
| 2806 | +#define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */ |
| 2807 | +#define SQLITE_DBCONFIG_MAX 1019 /* Largest DBCONFIG */ |
| 2766 | 2808 | |
| 2767 | 2809 | /* |
| 2768 | 2810 | ** CAPI3REF: Enable Or Disable Extended Result Codes |
| 2769 | 2811 | ** METHOD: sqlite3 |
| 2770 | 2812 | ** |
| | @@ -9868,22 +9910,32 @@ |
| 9868 | 9910 | ** </dd> |
| 9869 | 9911 | ** |
| 9870 | 9912 | ** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt> |
| 9871 | 9913 | ** <dd>Calls of the form |
| 9872 | 9914 | ** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the |
| 9873 | | -** the [xConnect] or [xCreate] methods of a [virtual table] implmentation |
| 9915 | +** the [xConnect] or [xCreate] methods of a [virtual table] implementation |
| 9874 | 9916 | ** identify that virtual table as being safe to use from within triggers |
| 9875 | 9917 | ** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the |
| 9876 | 9918 | ** virtual table can do no serious harm even if it is controlled by a |
| 9877 | 9919 | ** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS |
| 9878 | 9920 | ** flag unless absolutely necessary. |
| 9879 | 9921 | ** </dd> |
| 9922 | +** |
| 9923 | +** [[SQLITE_VTAB_USES_ALL_SCHEMAS]]<dt>SQLITE_VTAB_USES_ALL_SCHEMAS</dt> |
| 9924 | +** <dd>Calls of the form |
| 9925 | +** [sqlite3_vtab_config](db,SQLITE_VTAB_USES_ALL_SCHEMA) from within the |
| 9926 | +** the [xConnect] or [xCreate] methods of a [virtual table] implementation |
| 9927 | +** instruct the query planner to begin at least a read transaction on |
| 9928 | +** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the |
| 9929 | +** virtual table is used. |
| 9930 | +** </dd> |
| 9880 | 9931 | ** </dl> |
| 9881 | 9932 | */ |
| 9882 | 9933 | #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1 |
| 9883 | 9934 | #define SQLITE_VTAB_INNOCUOUS 2 |
| 9884 | 9935 | #define SQLITE_VTAB_DIRECTONLY 3 |
| 9936 | +#define SQLITE_VTAB_USES_ALL_SCHEMAS 4 |
| 9885 | 9937 | |
| 9886 | 9938 | /* |
| 9887 | 9939 | ** CAPI3REF: Determine The Virtual Table Conflict Policy |
| 9888 | 9940 | ** |
| 9889 | 9941 | ** This function may only be called from within a call to the [xUpdate] method |
| | @@ -12217,13 +12269,27 @@ |
| 12217 | 12269 | ** |
| 12218 | 12270 | ** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd> |
| 12219 | 12271 | ** Invert the changeset before applying it. This is equivalent to inverting |
| 12220 | 12272 | ** a changeset using sqlite3changeset_invert() before applying it. It is |
| 12221 | 12273 | ** an error to specify this flag with a patchset. |
| 12274 | +** |
| 12275 | +** <dt>SQLITE_CHANGESETAPPLY_IGNORENOOP <dd> |
| 12276 | +** Do not invoke the conflict handler callback for any changes that |
| 12277 | +** would not actually modify the database even if they were applied. |
| 12278 | +** Specifically, this means that the conflict handler is not invoked |
| 12279 | +** for: |
| 12280 | +** <ul> |
| 12281 | +** <li>a delete change if the row being deleted cannot be found, |
| 12282 | +** <li>an update change if the modified fields are already set to |
| 12283 | +** their new values in the conflicting row, or |
| 12284 | +** <li>an insert change if all fields of the conflicting row match |
| 12285 | +** the row being inserted. |
| 12286 | +** </ul> |
| 12222 | 12287 | */ |
| 12223 | 12288 | #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001 |
| 12224 | 12289 | #define SQLITE_CHANGESETAPPLY_INVERT 0x0002 |
| 12290 | +#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004 |
| 12225 | 12291 | |
| 12226 | 12292 | /* |
| 12227 | 12293 | ** CAPI3REF: Constants Passed To The Conflict Handler |
| 12228 | 12294 | ** |
| 12229 | 12295 | ** Values that may be passed as the second argument to a conflict-handler. |
| | @@ -13516,12 +13582,12 @@ |
| 13516 | 13582 | #pragma warn -csu /* Comparing signed and unsigned */ |
| 13517 | 13583 | #pragma warn -spa /* Suspicious pointer arithmetic */ |
| 13518 | 13584 | #endif |
| 13519 | 13585 | |
| 13520 | 13586 | /* |
| 13521 | | -** WAL mode depends on atomic aligned 32-bit loads and stores in a few |
| 13522 | | -** places. The following macros try to make this explicit. |
| 13587 | +** A few places in the code require atomic load/store of aligned |
| 13588 | +** integer values. |
| 13523 | 13589 | */ |
| 13524 | 13590 | #ifndef __has_extension |
| 13525 | 13591 | # define __has_extension(x) 0 /* compatibility with non-clang compilers */ |
| 13526 | 13592 | #endif |
| 13527 | 13593 | #if GCC_VERSION>=4007000 || __has_extension(c_atomic) |
| | @@ -13573,19 +13639,26 @@ |
| 13573 | 13639 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 13574 | 13640 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 13575 | 13641 | #endif |
| 13576 | 13642 | |
| 13577 | 13643 | /* |
| 13578 | | -** A macro to hint to the compiler that a function should not be |
| 13644 | +** Macros to hint to the compiler that a function should or should not be |
| 13579 | 13645 | ** inlined. |
| 13580 | 13646 | */ |
| 13581 | 13647 | #if defined(__GNUC__) |
| 13582 | 13648 | # define SQLITE_NOINLINE __attribute__((noinline)) |
| 13649 | +# define SQLITE_INLINE __attribute__((always_inline)) inline |
| 13583 | 13650 | #elif defined(_MSC_VER) && _MSC_VER>=1310 |
| 13584 | 13651 | # define SQLITE_NOINLINE __declspec(noinline) |
| 13652 | +# define SQLITE_INLINE __forceinline |
| 13585 | 13653 | #else |
| 13586 | 13654 | # define SQLITE_NOINLINE |
| 13655 | +# define SQLITE_INLINE |
| 13656 | +#endif |
| 13657 | +#if defined(SQLITE_COVERAGE_TEST) |
| 13658 | +# undef SQLITE_INLINE |
| 13659 | +# define SQLITE_INLINE |
| 13587 | 13660 | #endif |
| 13588 | 13661 | |
| 13589 | 13662 | /* |
| 13590 | 13663 | ** Make sure that the compiler intrinsics we desire are enabled when |
| 13591 | 13664 | ** compiling with an appropriate version of MSVC unless prevented by |
| | @@ -16541,10 +16614,14 @@ |
| 16541 | 16614 | #endif |
| 16542 | 16615 | |
| 16543 | 16616 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 16544 | 16617 | SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*); |
| 16545 | 16618 | #endif |
| 16619 | + |
| 16620 | +#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG) |
| 16621 | +SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr); |
| 16622 | +#endif |
| 16546 | 16623 | |
| 16547 | 16624 | #endif /* SQLITE_VDBE_H */ |
| 16548 | 16625 | |
| 16549 | 16626 | /************** End of vdbe.h ************************************************/ |
| 16550 | 16627 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| | @@ -16590,11 +16667,11 @@ |
| 16590 | 16667 | /********************************************************************** |
| 16591 | 16668 | ** Elements above, except pCache, are public. All that follow are |
| 16592 | 16669 | ** private to pcache.c and should not be accessed by other modules. |
| 16593 | 16670 | ** pCache is grouped with the public elements for efficiency. |
| 16594 | 16671 | */ |
| 16595 | | - i16 nRef; /* Number of users of this page */ |
| 16672 | + i64 nRef; /* Number of users of this page */ |
| 16596 | 16673 | PgHdr *pDirtyNext; /* Next element in list of dirty pages */ |
| 16597 | 16674 | PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ |
| 16598 | 16675 | /* NB: pDirtyNext and pDirtyPrev are undefined if the |
| 16599 | 16676 | ** PgHdr object is not dirty */ |
| 16600 | 16677 | }; |
| | @@ -16671,16 +16748,16 @@ |
| 16671 | 16748 | |
| 16672 | 16749 | /* Discard the contents of the cache */ |
| 16673 | 16750 | SQLITE_PRIVATE void sqlite3PcacheClear(PCache*); |
| 16674 | 16751 | |
| 16675 | 16752 | /* Return the total number of outstanding page references */ |
| 16676 | | -SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*); |
| 16753 | +SQLITE_PRIVATE i64 sqlite3PcacheRefCount(PCache*); |
| 16677 | 16754 | |
| 16678 | 16755 | /* Increment the reference count of an existing page */ |
| 16679 | 16756 | SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*); |
| 16680 | 16757 | |
| 16681 | | -SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*); |
| 16758 | +SQLITE_PRIVATE i64 sqlite3PcachePageRefcount(PgHdr*); |
| 16682 | 16759 | |
| 16683 | 16760 | /* Return the total number of pages stored in the cache */ |
| 16684 | 16761 | SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*); |
| 16685 | 16762 | |
| 16686 | 16763 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| | @@ -17251,11 +17328,11 @@ |
| 17251 | 17328 | #define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and |
| 17252 | 17329 | ** vtabs in the schema definition */ |
| 17253 | 17330 | #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */ |
| 17254 | 17331 | /* result set is empty */ |
| 17255 | 17332 | #define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */ |
| 17256 | | -#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */ |
| 17333 | +#define SQLITE_StmtScanStatus 0x00000400 /* Enable stmt_scanstats() counters */ |
| 17257 | 17334 | #define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */ |
| 17258 | 17335 | #define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */ |
| 17259 | 17336 | #define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */ |
| 17260 | 17337 | #define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */ |
| 17261 | 17338 | #define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */ |
| | @@ -17277,10 +17354,11 @@ |
| 17277 | 17354 | #define SQLITE_EnableView 0x80000000 /* Enable the use of views */ |
| 17278 | 17355 | #define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */ |
| 17279 | 17356 | /* DELETE, or UPDATE and return */ |
| 17280 | 17357 | /* the count using a callback. */ |
| 17281 | 17358 | #define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */ |
| 17359 | +#define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */ |
| 17282 | 17360 | |
| 17283 | 17361 | /* Flags used only if debugging */ |
| 17284 | 17362 | #ifdef SQLITE_DEBUG |
| 17285 | 17363 | #define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */ |
| 17286 | 17364 | #define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */ |
| | @@ -17333,10 +17411,11 @@ |
| 17333 | 17411 | #define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */ |
| 17334 | 17412 | #define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */ |
| 17335 | 17413 | /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */ |
| 17336 | 17414 | #define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */ |
| 17337 | 17415 | #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */ |
| 17416 | +#define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */ |
| 17338 | 17417 | #define SQLITE_AllOpts 0xffffffff /* All optimizations */ |
| 17339 | 17418 | |
| 17340 | 17419 | /* |
| 17341 | 17420 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 17342 | 17421 | */ |
| | @@ -17804,10 +17883,11 @@ |
| 17804 | 17883 | sqlite3 *db; /* Database connection associated with this table */ |
| 17805 | 17884 | Module *pMod; /* Pointer to module implementation */ |
| 17806 | 17885 | sqlite3_vtab *pVtab; /* Pointer to vtab instance */ |
| 17807 | 17886 | int nRef; /* Number of pointers to this structure */ |
| 17808 | 17887 | u8 bConstraint; /* True if constraints are supported */ |
| 17888 | + u8 bAllSchemas; /* True if might use any attached schema */ |
| 17809 | 17889 | u8 eVtabRisk; /* Riskiness of allowing hacker access */ |
| 17810 | 17890 | int iSavepoint; /* Depth of the SAVEPOINT stack */ |
| 17811 | 17891 | VTable *pNext; /* Next in linked list (see above) */ |
| 17812 | 17892 | }; |
| 17813 | 17893 | |
| | @@ -18835,11 +18915,11 @@ |
| 18835 | 18915 | #define NC_IsCheck 0x000004 /* True if resolving a CHECK constraint */ |
| 18836 | 18916 | #define NC_GenCol 0x000008 /* True for a GENERATED ALWAYS AS clause */ |
| 18837 | 18917 | #define NC_HasAgg 0x000010 /* One or more aggregate functions seen */ |
| 18838 | 18918 | #define NC_IdxExpr 0x000020 /* True if resolving columns of CREATE INDEX */ |
| 18839 | 18919 | #define NC_SelfRef 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */ |
| 18840 | | -#define NC_VarSelect 0x000040 /* A correlated subquery has been seen */ |
| 18920 | +#define NC_Subquery 0x000040 /* A subquery has been seen */ |
| 18841 | 18921 | #define NC_UEList 0x000080 /* True if uNC.pEList is used */ |
| 18842 | 18922 | #define NC_UAggInfo 0x000100 /* True if uNC.pAggInfo is used */ |
| 18843 | 18923 | #define NC_UUpsert 0x000200 /* True if uNC.pUpsert is used */ |
| 18844 | 18924 | #define NC_UBaseReg 0x000400 /* True if uNC.iBaseReg is used */ |
| 18845 | 18925 | #define NC_MinMaxAgg 0x001000 /* min/max aggregates seen. See note above */ |
| | @@ -19207,10 +19287,13 @@ |
| 19207 | 19287 | u8 prepFlags; /* SQLITE_PREPARE_* flags */ |
| 19208 | 19288 | u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */ |
| 19209 | 19289 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 19210 | 19290 | u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */ |
| 19211 | 19291 | #endif |
| 19292 | +#ifdef SQLITE_DEBUG |
| 19293 | + u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */ |
| 19294 | +#endif |
| 19212 | 19295 | int nRangeReg; /* Size of the temporary register block */ |
| 19213 | 19296 | int iRangeReg; /* First register in temporary register block */ |
| 19214 | 19297 | int nErr; /* Number of errors seen */ |
| 19215 | 19298 | int nTab; /* Number of previously allocated VDBE cursors */ |
| 19216 | 19299 | int nMem; /* Number of memory cells used so far */ |
| | @@ -19667,10 +19750,11 @@ |
| 19667 | 19750 | struct RenameCtx *pRename; /* RENAME COLUMN context */ |
| 19668 | 19751 | struct Table *pTab; /* Table of generated column */ |
| 19669 | 19752 | struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */ |
| 19670 | 19753 | SrcItem *pSrcItem; /* A single FROM clause item */ |
| 19671 | 19754 | DbFixer *pFix; /* See sqlite3FixSelect() */ |
| 19755 | + Mem *aMem; /* See sqlite3BtreeCursorHint() */ |
| 19672 | 19756 | } u; |
| 19673 | 19757 | }; |
| 19674 | 19758 | |
| 19675 | 19759 | /* |
| 19676 | 19760 | ** The following structure contains information used by the sqliteFix... |
| | @@ -20138,10 +20222,14 @@ |
| 20138 | 20222 | SQLITE_PRIVATE int sqlite3GetTempReg(Parse*); |
| 20139 | 20223 | SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int); |
| 20140 | 20224 | SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int); |
| 20141 | 20225 | SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int); |
| 20142 | 20226 | SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*); |
| 20227 | +SQLITE_PRIVATE void sqlite3TouchRegister(Parse*,int); |
| 20228 | +#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG) |
| 20229 | +SQLITE_PRIVATE int sqlite3FirstAvailableRegister(Parse*,int); |
| 20230 | +#endif |
| 20143 | 20231 | #ifdef SQLITE_DEBUG |
| 20144 | 20232 | SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int); |
| 20145 | 20233 | #endif |
| 20146 | 20234 | SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int); |
| 20147 | 20235 | SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*); |
| | @@ -20288,11 +20376,11 @@ |
| 20288 | 20376 | SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); |
| 20289 | 20377 | SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, |
| 20290 | 20378 | Expr*,ExprList*,u32,Expr*); |
| 20291 | 20379 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*); |
| 20292 | 20380 | SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*); |
| 20293 | | -SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int); |
| 20381 | +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, Trigger*); |
| 20294 | 20382 | SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); |
| 20295 | 20383 | #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
| 20296 | 20384 | SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*); |
| 20297 | 20385 | #endif |
| 20298 | 20386 | SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*); |
| | @@ -20825,14 +20913,11 @@ |
| 20825 | 20913 | SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*); |
| 20826 | 20914 | SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *); |
| 20827 | 20915 | SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); |
| 20828 | 20916 | |
| 20829 | 20917 | SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); |
| 20830 | | -#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \ |
| 20831 | | - && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 20832 | | -SQLITE_PRIVATE void sqlite3VtabUsesAllSchemas(sqlite3_index_info*); |
| 20833 | | -#endif |
| 20918 | +SQLITE_PRIVATE void sqlite3VtabUsesAllSchemas(Parse*); |
| 20834 | 20919 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); |
| 20835 | 20920 | SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); |
| 20836 | 20921 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 20837 | 20922 | SQLITE_PRIVATE void sqlite3ParseObjectInit(Parse*,sqlite3*); |
| 20838 | 20923 | SQLITE_PRIVATE void sqlite3ParseObjectReset(Parse*); |
| | @@ -21074,10 +21159,16 @@ |
| 21074 | 21159 | #if defined(VDBE_PROFILE) \ |
| 21075 | 21160 | || defined(SQLITE_PERFORMANCE_TRACE) \ |
| 21076 | 21161 | || defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 21077 | 21162 | SQLITE_PRIVATE sqlite3_uint64 sqlite3Hwtime(void); |
| 21078 | 21163 | #endif |
| 21164 | + |
| 21165 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 21166 | +# define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus) |
| 21167 | +#else |
| 21168 | +# define IS_STMT_SCANSTATUS(db) 0 |
| 21169 | +#endif |
| 21079 | 21170 | |
| 21080 | 21171 | #endif /* SQLITEINT_H */ |
| 21081 | 21172 | |
| 21082 | 21173 | /************** End of sqliteInt.h *******************************************/ |
| 21083 | 21174 | /************** Begin file os_common.h ***************************************/ |
| | @@ -22264,11 +22355,11 @@ |
| 22264 | 22355 | 0, /* xAltLocaltime */ |
| 22265 | 22356 | 0x7ffffffe, /* iOnceResetThreshold */ |
| 22266 | 22357 | SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */ |
| 22267 | 22358 | 0, /* iPrngSeed */ |
| 22268 | 22359 | #ifdef SQLITE_DEBUG |
| 22269 | | - {0,0,0,0,0,0} /* aTune */ |
| 22360 | + {0,0,0,0,0,0}, /* aTune */ |
| 22270 | 22361 | #endif |
| 22271 | 22362 | }; |
| 22272 | 22363 | |
| 22273 | 22364 | /* |
| 22274 | 22365 | ** Hash table for global functions - functions common to all |
| | @@ -30069,10 +30160,24 @@ |
| 30069 | 30160 | *val = (*val - d)*10.0; |
| 30070 | 30161 | return (char)digit; |
| 30071 | 30162 | } |
| 30072 | 30163 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 30073 | 30164 | |
| 30165 | +#ifndef SQLITE_OMIT_FLOATING_POINT |
| 30166 | +/* |
| 30167 | +** "*val" is a u64. *msd is a divisor used to extract the |
| 30168 | +** most significant digit of *val. Extract that most significant |
| 30169 | +** digit and return it. |
| 30170 | +*/ |
| 30171 | +static char et_getdigit_int(u64 *val, u64 *msd){ |
| 30172 | + u64 x = (*val)/(*msd); |
| 30173 | + *val -= x*(*msd); |
| 30174 | + if( *msd>=10 ) *msd /= 10; |
| 30175 | + return '0' + (char)(x & 15); |
| 30176 | +} |
| 30177 | +#endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 30178 | + |
| 30074 | 30179 | /* |
| 30075 | 30180 | ** Set the StrAccum object to an error mode. |
| 30076 | 30181 | */ |
| 30077 | 30182 | SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){ |
| 30078 | 30183 | assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); |
| | @@ -30161,10 +30266,12 @@ |
| 30161 | 30266 | etByte xtype = etINVALID; /* Conversion paradigm */ |
| 30162 | 30267 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
| 30163 | 30268 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 30164 | 30269 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 30165 | 30270 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 30271 | + sqlite_uint64 msd; /* Divisor to get most-significant-digit |
| 30272 | + ** of longvalue */ |
| 30166 | 30273 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 30167 | 30274 | char *zOut; /* Rendering buffer */ |
| 30168 | 30275 | int nOut; /* Size of the rendering buffer */ |
| 30169 | 30276 | char *zExtra = 0; /* Malloced memory used by some conversion */ |
| 30170 | 30277 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| | @@ -30467,56 +30574,82 @@ |
| 30467 | 30574 | realvalue = -realvalue; |
| 30468 | 30575 | prefix = '-'; |
| 30469 | 30576 | }else{ |
| 30470 | 30577 | prefix = flag_prefix; |
| 30471 | 30578 | } |
| 30579 | + exp = 0; |
| 30472 | 30580 | if( xtype==etGENERIC && precision>0 ) precision--; |
| 30473 | 30581 | testcase( precision>0xfff ); |
| 30474 | | - idx = precision & 0xfff; |
| 30475 | | - rounder = arRound[idx%10]; |
| 30476 | | - while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; } |
| 30477 | | - if( xtype==etFLOAT ){ |
| 30478 | | - double rx = (double)realvalue; |
| 30479 | | - sqlite3_uint64 u; |
| 30480 | | - int ex; |
| 30481 | | - memcpy(&u, &rx, sizeof(u)); |
| 30482 | | - ex = -1023 + (int)((u>>52)&0x7ff); |
| 30483 | | - if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16; |
| 30484 | | - realvalue += rounder; |
| 30485 | | - } |
| 30486 | | - /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 30487 | | - exp = 0; |
| 30488 | | - if( sqlite3IsNaN((double)realvalue) ){ |
| 30489 | | - bufpt = "NaN"; |
| 30490 | | - length = 3; |
| 30491 | | - break; |
| 30492 | | - } |
| 30493 | | - if( realvalue>0.0 ){ |
| 30494 | | - LONGDOUBLE_TYPE scale = 1.0; |
| 30495 | | - while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} |
| 30496 | | - while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } |
| 30497 | | - while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } |
| 30498 | | - realvalue /= scale; |
| 30499 | | - while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } |
| 30500 | | - while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } |
| 30501 | | - if( exp>350 ){ |
| 30502 | | - bufpt = buf; |
| 30503 | | - buf[0] = prefix; |
| 30504 | | - memcpy(buf+(prefix!=0),"Inf",4); |
| 30505 | | - length = 3+(prefix!=0); |
| 30506 | | - break; |
| 30507 | | - } |
| 30508 | | - } |
| 30509 | | - bufpt = buf; |
| 30582 | + if( realvalue<1.0e+16 |
| 30583 | + && realvalue==(LONGDOUBLE_TYPE)(longvalue = (u64)realvalue) |
| 30584 | + ){ |
| 30585 | + /* Number is a pure integer that can be represented as u64 */ |
| 30586 | + for(msd=1; msd*10<=longvalue; msd *= 10, exp++){} |
| 30587 | + if( exp>precision && xtype!=etFLOAT ){ |
| 30588 | + u64 rnd = msd/2; |
| 30589 | + int kk = precision; |
| 30590 | + while( kk-- > 0 ){ rnd /= 10; } |
| 30591 | + longvalue += rnd; |
| 30592 | + } |
| 30593 | + }else{ |
| 30594 | + msd = 0; |
| 30595 | + longvalue = 0; /* To prevent a compiler warning */ |
| 30596 | + idx = precision & 0xfff; |
| 30597 | + rounder = arRound[idx%10]; |
| 30598 | + while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; } |
| 30599 | + if( xtype==etFLOAT ){ |
| 30600 | + double rx = (double)realvalue; |
| 30601 | + sqlite3_uint64 u; |
| 30602 | + int ex; |
| 30603 | + memcpy(&u, &rx, sizeof(u)); |
| 30604 | + ex = -1023 + (int)((u>>52)&0x7ff); |
| 30605 | + if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16; |
| 30606 | + realvalue += rounder; |
| 30607 | + } |
| 30608 | + if( sqlite3IsNaN((double)realvalue) ){ |
| 30609 | + if( flag_zeropad ){ |
| 30610 | + bufpt = "null"; |
| 30611 | + length = 4; |
| 30612 | + }else{ |
| 30613 | + bufpt = "NaN"; |
| 30614 | + length = 3; |
| 30615 | + } |
| 30616 | + break; |
| 30617 | + } |
| 30618 | + |
| 30619 | + /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 30620 | + if( ALWAYS(realvalue>0.0) ){ |
| 30621 | + LONGDOUBLE_TYPE scale = 1.0; |
| 30622 | + while( realvalue>=1e100*scale && exp<=350){ scale*=1e100;exp+=100;} |
| 30623 | + while( realvalue>=1e10*scale && exp<=350 ){ scale*=1e10; exp+=10; } |
| 30624 | + while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } |
| 30625 | + realvalue /= scale; |
| 30626 | + while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } |
| 30627 | + while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } |
| 30628 | + if( exp>350 ){ |
| 30629 | + if( flag_zeropad ){ |
| 30630 | + realvalue = 9.0; |
| 30631 | + exp = 999; |
| 30632 | + }else{ |
| 30633 | + bufpt = buf; |
| 30634 | + buf[0] = prefix; |
| 30635 | + memcpy(buf+(prefix!=0),"Inf",4); |
| 30636 | + length = 3+(prefix!=0); |
| 30637 | + break; |
| 30638 | + } |
| 30639 | + } |
| 30640 | + if( xtype!=etFLOAT ){ |
| 30641 | + realvalue += rounder; |
| 30642 | + if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 30643 | + } |
| 30644 | + } |
| 30645 | + } |
| 30646 | + |
| 30510 | 30647 | /* |
| 30511 | 30648 | ** If the field type is etGENERIC, then convert to either etEXP |
| 30512 | 30649 | ** or etFLOAT, as appropriate. |
| 30513 | 30650 | */ |
| 30514 | | - if( xtype!=etFLOAT ){ |
| 30515 | | - realvalue += rounder; |
| 30516 | | - if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 30517 | | - } |
| 30518 | 30651 | if( xtype==etGENERIC ){ |
| 30519 | 30652 | flag_rtz = !flag_alternateform; |
| 30520 | 30653 | if( exp<-4 || exp>precision ){ |
| 30521 | 30654 | xtype = etEXP; |
| 30522 | 30655 | }else{ |
| | @@ -30529,28 +30662,33 @@ |
| 30529 | 30662 | if( xtype==etEXP ){ |
| 30530 | 30663 | e2 = 0; |
| 30531 | 30664 | }else{ |
| 30532 | 30665 | e2 = exp; |
| 30533 | 30666 | } |
| 30667 | + nsd = 16 + flag_altform2*10; |
| 30668 | + bufpt = buf; |
| 30534 | 30669 | { |
| 30535 | 30670 | i64 szBufNeeded; /* Size of a temporary buffer needed */ |
| 30536 | 30671 | szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; |
| 30537 | 30672 | if( szBufNeeded > etBUFSIZE ){ |
| 30538 | 30673 | bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); |
| 30539 | 30674 | if( bufpt==0 ) return; |
| 30540 | 30675 | } |
| 30541 | 30676 | } |
| 30542 | 30677 | zOut = bufpt; |
| 30543 | | - nsd = 16 + flag_altform2*10; |
| 30544 | 30678 | flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
| 30545 | 30679 | /* The sign in front of the number */ |
| 30546 | 30680 | if( prefix ){ |
| 30547 | 30681 | *(bufpt++) = prefix; |
| 30548 | 30682 | } |
| 30549 | 30683 | /* Digits prior to the decimal point */ |
| 30550 | 30684 | if( e2<0 ){ |
| 30551 | 30685 | *(bufpt++) = '0'; |
| 30686 | + }else if( msd>0 ){ |
| 30687 | + for(; e2>=0; e2--){ |
| 30688 | + *(bufpt++) = et_getdigit_int(&longvalue,&msd); |
| 30689 | + } |
| 30552 | 30690 | }else{ |
| 30553 | 30691 | for(; e2>=0; e2--){ |
| 30554 | 30692 | *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 30555 | 30693 | } |
| 30556 | 30694 | } |
| | @@ -30563,12 +30701,18 @@ |
| 30563 | 30701 | for(e2++; e2<0; precision--, e2++){ |
| 30564 | 30702 | assert( precision>0 ); |
| 30565 | 30703 | *(bufpt++) = '0'; |
| 30566 | 30704 | } |
| 30567 | 30705 | /* Significant digits after the decimal point */ |
| 30568 | | - while( (precision--)>0 ){ |
| 30569 | | - *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 30706 | + if( msd>0 ){ |
| 30707 | + while( (precision--)>0 ){ |
| 30708 | + *(bufpt++) = et_getdigit_int(&longvalue,&msd); |
| 30709 | + } |
| 30710 | + }else{ |
| 30711 | + while( (precision--)>0 ){ |
| 30712 | + *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 30713 | + } |
| 30570 | 30714 | } |
| 30571 | 30715 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 30572 | 30716 | if( flag_rtz && flag_dp ){ |
| 30573 | 30717 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
| 30574 | 30718 | assert( bufpt>zOut ); |
| | @@ -31245,16 +31389,26 @@ |
| 31245 | 31389 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 31246 | 31390 | zBuf[acc.nChar] = 0; |
| 31247 | 31391 | return zBuf; |
| 31248 | 31392 | } |
| 31249 | 31393 | SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
| 31250 | | - char *z; |
| 31394 | + StrAccum acc; |
| 31251 | 31395 | va_list ap; |
| 31396 | + if( n<=0 ) return zBuf; |
| 31397 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 31398 | + if( zBuf==0 || zFormat==0 ) { |
| 31399 | + (void)SQLITE_MISUSE_BKPT; |
| 31400 | + if( zBuf ) zBuf[0] = 0; |
| 31401 | + return zBuf; |
| 31402 | + } |
| 31403 | +#endif |
| 31404 | + sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
| 31252 | 31405 | va_start(ap,zFormat); |
| 31253 | | - z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); |
| 31406 | + sqlite3_str_vappendf(&acc, zFormat, ap); |
| 31254 | 31407 | va_end(ap); |
| 31255 | | - return z; |
| 31408 | + zBuf[acc.nChar] = 0; |
| 31409 | + return zBuf; |
| 31256 | 31410 | } |
| 31257 | 31411 | |
| 31258 | 31412 | /* |
| 31259 | 31413 | ** This is the routine that actually formats the sqlite3_log() message. |
| 31260 | 31414 | ** We house it in a separate routine from sqlite3_log() to avoid using |
| | @@ -52652,11 +52806,11 @@ |
| 52652 | 52806 | ** pointers). |
| 52653 | 52807 | */ |
| 52654 | 52808 | struct PCache { |
| 52655 | 52809 | PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ |
| 52656 | 52810 | PgHdr *pSynced; /* Last synced page in dirty page list */ |
| 52657 | | - int nRefSum; /* Sum of ref counts over all pages */ |
| 52811 | + i64 nRefSum; /* Sum of ref counts over all pages */ |
| 52658 | 52812 | int szCache; /* Configured cache size */ |
| 52659 | 52813 | int szSpill; /* Size before spilling occurs */ |
| 52660 | 52814 | int szPage; /* Size of every page in this cache */ |
| 52661 | 52815 | int szExtra; /* Size of extra space for each page */ |
| 52662 | 52816 | u8 bPurgeable; /* True if pages are on backing store */ |
| | @@ -52682,11 +52836,11 @@ |
| 52682 | 52836 | static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){ |
| 52683 | 52837 | PgHdr *pPg; |
| 52684 | 52838 | unsigned char *a; |
| 52685 | 52839 | int j; |
| 52686 | 52840 | pPg = (PgHdr*)pLower->pExtra; |
| 52687 | | - printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); |
| 52841 | + printf("%3lld: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); |
| 52688 | 52842 | a = (unsigned char *)pLower->pBuf; |
| 52689 | 52843 | for(j=0; j<12; j++) printf("%02x", a[j]); |
| 52690 | 52844 | printf(" ptr %p\n", pPg); |
| 52691 | 52845 | } |
| 52692 | 52846 | static void pcacheDump(PCache *pCache){ |
| | @@ -53426,18 +53580,18 @@ |
| 53426 | 53580 | ** Return the total number of references to all pages held by the cache. |
| 53427 | 53581 | ** |
| 53428 | 53582 | ** This is not the total number of pages referenced, but the sum of the |
| 53429 | 53583 | ** reference count for all pages. |
| 53430 | 53584 | */ |
| 53431 | | -SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){ |
| 53585 | +SQLITE_PRIVATE i64 sqlite3PcacheRefCount(PCache *pCache){ |
| 53432 | 53586 | return pCache->nRefSum; |
| 53433 | 53587 | } |
| 53434 | 53588 | |
| 53435 | 53589 | /* |
| 53436 | 53590 | ** Return the number of references to the page supplied as an argument. |
| 53437 | 53591 | */ |
| 53438 | | -SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){ |
| 53592 | +SQLITE_PRIVATE i64 sqlite3PcachePageRefcount(PgHdr *p){ |
| 53439 | 53593 | return p->nRef; |
| 53440 | 53594 | } |
| 53441 | 53595 | |
| 53442 | 53596 | /* |
| 53443 | 53597 | ** Return the total number of pages in the cache. |
| | @@ -68091,12 +68245,13 @@ |
| 68091 | 68245 | int mxErr; /* Stop accumulating errors when this reaches zero */ |
| 68092 | 68246 | int nErr; /* Number of messages written to zErrMsg so far */ |
| 68093 | 68247 | int rc; /* SQLITE_OK, SQLITE_NOMEM, or SQLITE_INTERRUPT */ |
| 68094 | 68248 | u32 nStep; /* Number of steps into the integrity_check process */ |
| 68095 | 68249 | const char *zPfx; /* Error message prefix */ |
| 68096 | | - Pgno v1; /* Value for first %u substitution in zPfx */ |
| 68097 | | - int v2; /* Value for second %d substitution in zPfx */ |
| 68250 | + Pgno v0; /* Value for first %u substitution in zPfx (root page) */ |
| 68251 | + Pgno v1; /* Value for second %u substitution in zPfx (current pg) */ |
| 68252 | + int v2; /* Value for third %d substitution in zPfx */ |
| 68098 | 68253 | StrAccum errMsg; /* Accumulate the error message text here */ |
| 68099 | 68254 | u32 *heap; /* Min-heap used for analyzing cell coverage */ |
| 68100 | 68255 | sqlite3 *db; /* Database connection running the check */ |
| 68101 | 68256 | }; |
| 68102 | 68257 | |
| | @@ -68555,12 +68710,12 @@ |
| 68555 | 68710 | */ |
| 68556 | 68711 | #ifdef SQLITE_DEBUG |
| 68557 | 68712 | int corruptPageError(int lineno, MemPage *p){ |
| 68558 | 68713 | char *zMsg; |
| 68559 | 68714 | sqlite3BeginBenignMalloc(); |
| 68560 | | - zMsg = sqlite3_mprintf("database corruption page %d of %s", |
| 68561 | | - (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0) |
| 68715 | + zMsg = sqlite3_mprintf("database corruption page %u of %s", |
| 68716 | + p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0) |
| 68562 | 68717 | ); |
| 68563 | 68718 | sqlite3EndBenignMalloc(); |
| 68564 | 68719 | if( zMsg ){ |
| 68565 | 68720 | sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); |
| 68566 | 68721 | } |
| | @@ -69365,12 +69520,29 @@ |
| 69365 | 69520 | ** and number of the varargs parameters) is determined by the eHintType |
| 69366 | 69521 | ** parameter. See the definitions of the BTREE_HINT_* macros for details. |
| 69367 | 69522 | */ |
| 69368 | 69523 | SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ |
| 69369 | 69524 | /* Used only by system that substitute their own storage engine */ |
| 69525 | +#ifdef SQLITE_DEBUG |
| 69526 | + if( ALWAYS(eHintType==BTREE_HINT_RANGE) ){ |
| 69527 | + va_list ap; |
| 69528 | + Expr *pExpr; |
| 69529 | + Walker w; |
| 69530 | + memset(&w, 0, sizeof(w)); |
| 69531 | + w.xExprCallback = sqlite3CursorRangeHintExprCheck; |
| 69532 | + va_start(ap, eHintType); |
| 69533 | + pExpr = va_arg(ap, Expr*); |
| 69534 | + w.u.aMem = va_arg(ap, Mem*); |
| 69535 | + va_end(ap); |
| 69536 | + assert( pExpr!=0 ); |
| 69537 | + assert( w.u.aMem!=0 ); |
| 69538 | + sqlite3WalkExpr(&w, pExpr); |
| 69539 | + } |
| 69540 | +#endif /* SQLITE_DEBUG */ |
| 69370 | 69541 | } |
| 69371 | | -#endif |
| 69542 | +#endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
| 69543 | + |
| 69372 | 69544 | |
| 69373 | 69545 | /* |
| 69374 | 69546 | ** Provide flag hints to the cursor. |
| 69375 | 69547 | */ |
| 69376 | 69548 | SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ |
| | @@ -69451,11 +69623,11 @@ |
| 69451 | 69623 | } |
| 69452 | 69624 | assert( offset <= (int)pBt->usableSize-5 ); |
| 69453 | 69625 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
| 69454 | 69626 | |
| 69455 | 69627 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 69456 | | - TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
| 69628 | + TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent)); |
| 69457 | 69629 | *pRC= rc = sqlite3PagerWrite(pDbPage); |
| 69458 | 69630 | if( rc==SQLITE_OK ){ |
| 69459 | 69631 | pPtrmap[offset] = eType; |
| 69460 | 69632 | put4byte(&pPtrmap[offset+1], parent); |
| 69461 | 69633 | } |
| | @@ -69650,31 +69822,35 @@ |
| 69650 | 69822 | ** This routine is a high-runner. |
| 69651 | 69823 | */ |
| 69652 | 69824 | iKey = *pIter; |
| 69653 | 69825 | if( iKey>=0x80 ){ |
| 69654 | 69826 | u8 x; |
| 69655 | | - iKey = ((iKey&0x7f)<<7) | ((x = *++pIter) & 0x7f); |
| 69656 | | - if( x>=0x80 ){ |
| 69657 | | - iKey = (iKey<<7) | ((x =*++pIter) & 0x7f); |
| 69658 | | - if( x>=0x80 ){ |
| 69659 | | - iKey = (iKey<<7) | ((x = *++pIter) & 0x7f); |
| 69660 | | - if( x>=0x80 ){ |
| 69661 | | - iKey = (iKey<<7) | ((x = *++pIter) & 0x7f); |
| 69662 | | - if( x>=0x80 ){ |
| 69663 | | - iKey = (iKey<<7) | ((x = *++pIter) & 0x7f); |
| 69664 | | - if( x>=0x80 ){ |
| 69665 | | - iKey = (iKey<<7) | ((x = *++pIter) & 0x7f); |
| 69666 | | - if( x>=0x80 ){ |
| 69667 | | - iKey = (iKey<<7) | ((x = *++pIter) & 0x7f); |
| 69668 | | - if( x>=0x80 ){ |
| 69669 | | - iKey = (iKey<<8) | (*++pIter); |
| 69670 | | - } |
| 69671 | | - } |
| 69672 | | - } |
| 69673 | | - } |
| 69674 | | - } |
| 69675 | | - } |
| 69827 | + iKey = (iKey<<7) ^ (x = *++pIter); |
| 69828 | + if( x>=0x80 ){ |
| 69829 | + iKey = (iKey<<7) ^ (x = *++pIter); |
| 69830 | + if( x>=0x80 ){ |
| 69831 | + iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter); |
| 69832 | + if( x>=0x80 ){ |
| 69833 | + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); |
| 69834 | + if( x>=0x80 ){ |
| 69835 | + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); |
| 69836 | + if( x>=0x80 ){ |
| 69837 | + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); |
| 69838 | + if( x>=0x80 ){ |
| 69839 | + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); |
| 69840 | + if( x>=0x80 ){ |
| 69841 | + iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter); |
| 69842 | + } |
| 69843 | + } |
| 69844 | + } |
| 69845 | + } |
| 69846 | + } |
| 69847 | + }else{ |
| 69848 | + iKey ^= 0x204000; |
| 69849 | + } |
| 69850 | + }else{ |
| 69851 | + iKey ^= 0x4000; |
| 69676 | 69852 | } |
| 69677 | 69853 | } |
| 69678 | 69854 | pIter++; |
| 69679 | 69855 | |
| 69680 | 69856 | pInfo->nKey = *(i64*)&iKey; |
| | @@ -69747,14 +69923,57 @@ |
| 69747 | 69923 | ** data header and the local payload, but not any overflow page or |
| 69748 | 69924 | ** the space used by the cell pointer. |
| 69749 | 69925 | ** |
| 69750 | 69926 | ** cellSizePtrNoPayload() => table internal nodes |
| 69751 | 69927 | ** cellSizePtrTableLeaf() => table leaf nodes |
| 69752 | | -** cellSizePtr() => all index nodes & table leaf nodes |
| 69928 | +** cellSizePtr() => index internal nodes |
| 69929 | +** cellSizeIdxLeaf() => index leaf nodes |
| 69753 | 69930 | */ |
| 69754 | 69931 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 69755 | | - u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
| 69932 | + u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ |
| 69933 | + u8 *pEnd; /* End mark for a varint */ |
| 69934 | + u32 nSize; /* Size value to return */ |
| 69935 | + |
| 69936 | +#ifdef SQLITE_DEBUG |
| 69937 | + /* The value returned by this function should always be the same as |
| 69938 | + ** the (CellInfo.nSize) value found by doing a full parse of the |
| 69939 | + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 69940 | + ** this function verifies that this invariant is not violated. */ |
| 69941 | + CellInfo debuginfo; |
| 69942 | + pPage->xParseCell(pPage, pCell, &debuginfo); |
| 69943 | +#endif |
| 69944 | + |
| 69945 | + assert( pPage->childPtrSize==4 ); |
| 69946 | + nSize = *pIter; |
| 69947 | + if( nSize>=0x80 ){ |
| 69948 | + pEnd = &pIter[8]; |
| 69949 | + nSize &= 0x7f; |
| 69950 | + do{ |
| 69951 | + nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 69952 | + }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 69953 | + } |
| 69954 | + pIter++; |
| 69955 | + testcase( nSize==pPage->maxLocal ); |
| 69956 | + testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 69957 | + if( nSize<=pPage->maxLocal ){ |
| 69958 | + nSize += (u32)(pIter - pCell); |
| 69959 | + assert( nSize>4 ); |
| 69960 | + }else{ |
| 69961 | + int minLocal = pPage->minLocal; |
| 69962 | + nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
| 69963 | + testcase( nSize==pPage->maxLocal ); |
| 69964 | + testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 69965 | + if( nSize>pPage->maxLocal ){ |
| 69966 | + nSize = minLocal; |
| 69967 | + } |
| 69968 | + nSize += 4 + (u16)(pIter - pCell); |
| 69969 | + } |
| 69970 | + assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
| 69971 | + return (u16)nSize; |
| 69972 | +} |
| 69973 | +static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){ |
| 69974 | + u8 *pIter = pCell; /* For looping over bytes of pCell */ |
| 69756 | 69975 | u8 *pEnd; /* End mark for a varint */ |
| 69757 | 69976 | u32 nSize; /* Size value to return */ |
| 69758 | 69977 | |
| 69759 | 69978 | #ifdef SQLITE_DEBUG |
| 69760 | 69979 | /* The value returned by this function should always be the same as |
| | @@ -69763,10 +69982,11 @@ |
| 69763 | 69982 | ** this function verifies that this invariant is not violated. */ |
| 69764 | 69983 | CellInfo debuginfo; |
| 69765 | 69984 | pPage->xParseCell(pPage, pCell, &debuginfo); |
| 69766 | 69985 | #endif |
| 69767 | 69986 | |
| 69987 | + assert( pPage->childPtrSize==0 ); |
| 69768 | 69988 | nSize = *pIter; |
| 69769 | 69989 | if( nSize>=0x80 ){ |
| 69770 | 69990 | pEnd = &pIter[8]; |
| 69771 | 69991 | nSize &= 0x7f; |
| 69772 | 69992 | do{ |
| | @@ -69999,14 +70219,14 @@ |
| 69999 | 70219 | testcase( pc==iCellFirst ); |
| 70000 | 70220 | testcase( pc==iCellLast ); |
| 70001 | 70221 | /* These conditions have already been verified in btreeInitPage() |
| 70002 | 70222 | ** if PRAGMA cell_size_check=ON. |
| 70003 | 70223 | */ |
| 70004 | | - if( pc<iCellStart || pc>iCellLast ){ |
| 70224 | + if( pc>iCellLast ){ |
| 70005 | 70225 | return SQLITE_CORRUPT_PAGE(pPage); |
| 70006 | 70226 | } |
| 70007 | | - assert( pc>=iCellStart && pc<=iCellLast ); |
| 70227 | + assert( pc>=0 && pc<=iCellLast ); |
| 70008 | 70228 | size = pPage->xCellSize(pPage, &src[pc]); |
| 70009 | 70229 | cbrk -= size; |
| 70010 | 70230 | if( cbrk<iCellStart || pc+size>usableSize ){ |
| 70011 | 70231 | return SQLITE_CORRUPT_PAGE(pPage); |
| 70012 | 70232 | } |
| | @@ -70117,11 +70337,11 @@ |
| 70117 | 70337 | ** all the space together, however. This routine will avoid using |
| 70118 | 70338 | ** the first two bytes past the cell pointer area since presumably this |
| 70119 | 70339 | ** allocation is being made in order to insert a new cell, so we will |
| 70120 | 70340 | ** also end up needing a new cell pointer. |
| 70121 | 70341 | */ |
| 70122 | | -static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 70342 | +static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 70123 | 70343 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 70124 | 70344 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 70125 | 70345 | int top; /* First byte of cell content area */ |
| 70126 | 70346 | int rc = SQLITE_OK; /* Integer return code */ |
| 70127 | 70347 | u8 *pTmp; /* Temp ptr into data[] */ |
| | @@ -70143,17 +70363,18 @@ |
| 70143 | 70363 | ** then the cell content offset of an empty page wants to be 65536. |
| 70144 | 70364 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 70145 | 70365 | ** integer, so a value of 0 is used in its place. */ |
| 70146 | 70366 | pTmp = &data[hdr+5]; |
| 70147 | 70367 | top = get2byte(pTmp); |
| 70148 | | - assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */ |
| 70149 | 70368 | if( gap>top ){ |
| 70150 | 70369 | if( top==0 && pPage->pBt->usableSize==65536 ){ |
| 70151 | 70370 | top = 65536; |
| 70152 | 70371 | }else{ |
| 70153 | 70372 | return SQLITE_CORRUPT_PAGE(pPage); |
| 70154 | 70373 | } |
| 70374 | + }else if( top>(int)pPage->pBt->usableSize ){ |
| 70375 | + return SQLITE_CORRUPT_PAGE(pPage); |
| 70155 | 70376 | } |
| 70156 | 70377 | |
| 70157 | 70378 | /* If there is enough space between gap and top for one more cell pointer, |
| 70158 | 70379 | ** and if the freelist is not empty, then search the |
| 70159 | 70380 | ** freelist looking for a slot big enough to satisfy the request. |
| | @@ -70232,11 +70453,11 @@ |
| 70232 | 70453 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 70233 | 70454 | assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 70234 | 70455 | assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
| 70235 | 70456 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 70236 | 70457 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 70237 | | - assert( iStart<=pPage->pBt->usableSize-4 ); |
| 70458 | + assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 ); |
| 70238 | 70459 | |
| 70239 | 70460 | /* The list of freeblocks must be in ascending order. Find the |
| 70240 | 70461 | ** spot on the list where iStart should be inserted. |
| 70241 | 70462 | */ |
| 70242 | 70463 | hdr = pPage->hdrOffset; |
| | @@ -70289,10 +70510,15 @@ |
| 70289 | 70510 | if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage); |
| 70290 | 70511 | data[hdr+7] -= nFrag; |
| 70291 | 70512 | } |
| 70292 | 70513 | pTmp = &data[hdr+5]; |
| 70293 | 70514 | x = get2byte(pTmp); |
| 70515 | + if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){ |
| 70516 | + /* Overwrite deleted information with zeros when the secure_delete |
| 70517 | + ** option is enabled */ |
| 70518 | + memset(&data[iStart], 0, iSize); |
| 70519 | + } |
| 70294 | 70520 | if( iStart<=x ){ |
| 70295 | 70521 | /* The new freeblock is at the beginning of the cell content area, |
| 70296 | 70522 | ** so just extend the cell content area rather than create another |
| 70297 | 70523 | ** freelist entry */ |
| 70298 | 70524 | if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage); |
| | @@ -70300,18 +70526,13 @@ |
| 70300 | 70526 | put2byte(&data[hdr+1], iFreeBlk); |
| 70301 | 70527 | put2byte(&data[hdr+5], iEnd); |
| 70302 | 70528 | }else{ |
| 70303 | 70529 | /* Insert the new freeblock into the freelist */ |
| 70304 | 70530 | put2byte(&data[iPtr], iStart); |
| 70305 | | - } |
| 70306 | | - if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){ |
| 70307 | | - /* Overwrite deleted information with zeros when the secure_delete |
| 70308 | | - ** option is enabled */ |
| 70309 | | - memset(&data[iStart], 0, iSize); |
| 70310 | | - } |
| 70311 | | - put2byte(&data[iStart], iFreeBlk); |
| 70312 | | - put2byte(&data[iStart+2], iSize); |
| 70531 | + put2byte(&data[iStart], iFreeBlk); |
| 70532 | + put2byte(&data[iStart+2], iSize); |
| 70533 | + } |
| 70313 | 70534 | pPage->nFree += iOrigSize; |
| 70314 | 70535 | return SQLITE_OK; |
| 70315 | 70536 | } |
| 70316 | 70537 | |
| 70317 | 70538 | /* |
| | @@ -70344,18 +70565,18 @@ |
| 70344 | 70565 | pPage->maxLocal = pBt->maxLeaf; |
| 70345 | 70566 | pPage->minLocal = pBt->minLeaf; |
| 70346 | 70567 | }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){ |
| 70347 | 70568 | pPage->intKey = 0; |
| 70348 | 70569 | pPage->intKeyLeaf = 0; |
| 70349 | | - pPage->xCellSize = cellSizePtr; |
| 70570 | + pPage->xCellSize = cellSizePtrIdxLeaf; |
| 70350 | 70571 | pPage->xParseCell = btreeParseCellPtrIndex; |
| 70351 | 70572 | pPage->maxLocal = pBt->maxLocal; |
| 70352 | 70573 | pPage->minLocal = pBt->minLocal; |
| 70353 | 70574 | }else{ |
| 70354 | 70575 | pPage->intKey = 0; |
| 70355 | 70576 | pPage->intKeyLeaf = 0; |
| 70356 | | - pPage->xCellSize = cellSizePtr; |
| 70577 | + pPage->xCellSize = cellSizePtrIdxLeaf; |
| 70357 | 70578 | pPage->xParseCell = btreeParseCellPtrIndex; |
| 70358 | 70579 | return SQLITE_CORRUPT_PAGE(pPage); |
| 70359 | 70580 | } |
| 70360 | 70581 | }else{ |
| 70361 | 70582 | pPage->childPtrSize = 4; |
| | @@ -72217,11 +72438,11 @@ |
| 72217 | 72438 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 72218 | 72439 | assert( pDbPage->pBt==pBt ); |
| 72219 | 72440 | if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT; |
| 72220 | 72441 | |
| 72221 | 72442 | /* Move page iDbPage from its current location to page number iFreePage */ |
| 72222 | | - TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 72443 | + TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n", |
| 72223 | 72444 | iDbPage, iFreePage, iPtrPage, eType)); |
| 72224 | 72445 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
| 72225 | 72446 | if( rc!=SQLITE_OK ){ |
| 72226 | 72447 | return rc; |
| 72227 | 72448 | } |
| | @@ -74503,11 +74724,12 @@ |
| 74503 | 74724 | } |
| 74504 | 74725 | } |
| 74505 | 74726 | |
| 74506 | 74727 | pPage = pCur->pPage; |
| 74507 | 74728 | idx = ++pCur->ix; |
| 74508 | | - if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){ |
| 74729 | + if( sqlite3FaultSim(412) ) pPage->isInit = 0; |
| 74730 | + if( !pPage->isInit ){ |
| 74509 | 74731 | return SQLITE_CORRUPT_BKPT; |
| 74510 | 74732 | } |
| 74511 | 74733 | |
| 74512 | 74734 | if( idx>=pPage->nCell ){ |
| 74513 | 74735 | if( !pPage->leaf ){ |
| | @@ -74766,11 +74988,11 @@ |
| 74766 | 74988 | } |
| 74767 | 74989 | *pPgno = iTrunk; |
| 74768 | 74990 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 74769 | 74991 | *ppPage = pTrunk; |
| 74770 | 74992 | pTrunk = 0; |
| 74771 | | - TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 74993 | + TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1)); |
| 74772 | 74994 | }else if( k>(u32)(pBt->usableSize/4 - 2) ){ |
| 74773 | 74995 | /* Value of k is out of range. Database corruption */ |
| 74774 | 74996 | rc = SQLITE_CORRUPT_PGNO(iTrunk); |
| 74775 | 74997 | goto end_allocate_page; |
| 74776 | 74998 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| | @@ -74832,11 +75054,11 @@ |
| 74832 | 75054 | } |
| 74833 | 75055 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 74834 | 75056 | } |
| 74835 | 75057 | } |
| 74836 | 75058 | pTrunk = 0; |
| 74837 | | - TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 75059 | + TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1)); |
| 74838 | 75060 | #endif |
| 74839 | 75061 | }else if( k>0 ){ |
| 74840 | 75062 | /* Extract a leaf from the trunk */ |
| 74841 | 75063 | u32 closest; |
| 74842 | 75064 | Pgno iPage; |
| | @@ -74877,12 +75099,12 @@ |
| 74877 | 75099 | if( !searchList |
| 74878 | 75100 | || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) |
| 74879 | 75101 | ){ |
| 74880 | 75102 | int noContent; |
| 74881 | 75103 | *pPgno = iPage; |
| 74882 | | - TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 74883 | | - ": %d more free pages\n", |
| 75104 | + TRACE(("ALLOCATE: %u was leaf %u of %u on trunk %u" |
| 75105 | + ": %u more free pages\n", |
| 74884 | 75106 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 74885 | 75107 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 74886 | 75108 | if( rc ) goto end_allocate_page; |
| 74887 | 75109 | if( closest<k-1 ){ |
| 74888 | 75110 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| | @@ -74934,11 +75156,11 @@ |
| 74934 | 75156 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 74935 | 75157 | ** at the end of the file instead of one. The first allocated page |
| 74936 | 75158 | ** becomes a new pointer-map page, the second is used by the caller. |
| 74937 | 75159 | */ |
| 74938 | 75160 | MemPage *pPg = 0; |
| 74939 | | - TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 75161 | + TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage)); |
| 74940 | 75162 | assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
| 74941 | 75163 | rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); |
| 74942 | 75164 | if( rc==SQLITE_OK ){ |
| 74943 | 75165 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 74944 | 75166 | releasePage(pPg); |
| | @@ -74957,11 +75179,11 @@ |
| 74957 | 75179 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 74958 | 75180 | if( rc!=SQLITE_OK ){ |
| 74959 | 75181 | releasePage(*ppPage); |
| 74960 | 75182 | *ppPage = 0; |
| 74961 | 75183 | } |
| 74962 | | - TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
| 75184 | + TRACE(("ALLOCATE: %u from end of file\n", *pPgno)); |
| 74963 | 75185 | } |
| 74964 | 75186 | |
| 74965 | 75187 | assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 74966 | 75188 | |
| 74967 | 75189 | end_allocate_page: |
| | @@ -75085,11 +75307,11 @@ |
| 75085 | 75307 | if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){ |
| 75086 | 75308 | sqlite3PagerDontWrite(pPage->pDbPage); |
| 75087 | 75309 | } |
| 75088 | 75310 | rc = btreeSetHasContent(pBt, iPage); |
| 75089 | 75311 | } |
| 75090 | | - TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); |
| 75312 | + TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno)); |
| 75091 | 75313 | goto freepage_out; |
| 75092 | 75314 | } |
| 75093 | 75315 | } |
| 75094 | 75316 | |
| 75095 | 75317 | /* If control flows to this point, then it was not possible to add the |
| | @@ -75106,11 +75328,11 @@ |
| 75106 | 75328 | goto freepage_out; |
| 75107 | 75329 | } |
| 75108 | 75330 | put4byte(pPage->aData, iTrunk); |
| 75109 | 75331 | put4byte(&pPage->aData[4], 0); |
| 75110 | 75332 | put4byte(&pPage1->aData[32], iPage); |
| 75111 | | - TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); |
| 75333 | + TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk)); |
| 75112 | 75334 | |
| 75113 | 75335 | freepage_out: |
| 75114 | 75336 | if( pPage ){ |
| 75115 | 75337 | pPage->isInit = 0; |
| 75116 | 75338 | } |
| | @@ -75465,10 +75687,18 @@ |
| 75465 | 75687 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 75466 | 75688 | ** in pPage->apOvfl[] and make it point to the cell content (either |
| 75467 | 75689 | ** in pTemp or the original pCell) and also record its index. |
| 75468 | 75690 | ** Allocating a new entry in pPage->aCell[] implies that |
| 75469 | 75691 | ** pPage->nOverflow is incremented. |
| 75692 | +** |
| 75693 | +** The insertCellFast() routine below works exactly the same as |
| 75694 | +** insertCell() except that it lacks the pTemp and iChild parameters |
| 75695 | +** which are assumed zero. Other than that, the two routines are the |
| 75696 | +** same. |
| 75697 | +** |
| 75698 | +** Fixes or enhancements to this routine should be reflected in |
| 75699 | +** insertCellFast()! |
| 75470 | 75700 | */ |
| 75471 | 75701 | static int insertCell( |
| 75472 | 75702 | MemPage *pPage, /* Page into which we are copying */ |
| 75473 | 75703 | int i, /* New cell becomes the i-th cell of the page */ |
| 75474 | 75704 | u8 *pCell, /* Content of the new cell */ |
| | @@ -75487,18 +75717,107 @@ |
| 75487 | 75717 | assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); |
| 75488 | 75718 | assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); |
| 75489 | 75719 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 75490 | 75720 | assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB ); |
| 75491 | 75721 | assert( pPage->nFree>=0 ); |
| 75722 | + assert( iChild>0 ); |
| 75492 | 75723 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
| 75493 | 75724 | if( pTemp ){ |
| 75494 | 75725 | memcpy(pTemp, pCell, sz); |
| 75495 | 75726 | pCell = pTemp; |
| 75496 | 75727 | } |
| 75497 | | - if( iChild ){ |
| 75498 | | - put4byte(pCell, iChild); |
| 75728 | + put4byte(pCell, iChild); |
| 75729 | + j = pPage->nOverflow++; |
| 75730 | + /* Comparison against ArraySize-1 since we hold back one extra slot |
| 75731 | + ** as a contingency. In other words, never need more than 3 overflow |
| 75732 | + ** slots but 4 are allocated, just to be safe. */ |
| 75733 | + assert( j < ArraySize(pPage->apOvfl)-1 ); |
| 75734 | + pPage->apOvfl[j] = pCell; |
| 75735 | + pPage->aiOvfl[j] = (u16)i; |
| 75736 | + |
| 75737 | + /* When multiple overflows occur, they are always sequential and in |
| 75738 | + ** sorted order. This invariants arise because multiple overflows can |
| 75739 | + ** only occur when inserting divider cells into the parent page during |
| 75740 | + ** balancing, and the dividers are adjacent and sorted. |
| 75741 | + */ |
| 75742 | + assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ |
| 75743 | + assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ |
| 75744 | + }else{ |
| 75745 | + int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 75746 | + if( NEVER(rc!=SQLITE_OK) ){ |
| 75747 | + return rc; |
| 75499 | 75748 | } |
| 75749 | + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 75750 | + data = pPage->aData; |
| 75751 | + assert( &data[pPage->cellOffset]==pPage->aCellIdx ); |
| 75752 | + rc = allocateSpace(pPage, sz, &idx); |
| 75753 | + if( rc ){ return rc; } |
| 75754 | + /* The allocateSpace() routine guarantees the following properties |
| 75755 | + ** if it returns successfully */ |
| 75756 | + assert( idx >= 0 ); |
| 75757 | + assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); |
| 75758 | + assert( idx+sz <= (int)pPage->pBt->usableSize ); |
| 75759 | + pPage->nFree -= (u16)(2 + sz); |
| 75760 | + /* In a corrupt database where an entry in the cell index section of |
| 75761 | + ** a btree page has a value of 3 or less, the pCell value might point |
| 75762 | + ** as many as 4 bytes in front of the start of the aData buffer for |
| 75763 | + ** the source page. Make sure this does not cause problems by not |
| 75764 | + ** reading the first 4 bytes */ |
| 75765 | + memcpy(&data[idx+4], pCell+4, sz-4); |
| 75766 | + put4byte(&data[idx], iChild); |
| 75767 | + pIns = pPage->aCellIdx + i*2; |
| 75768 | + memmove(pIns+2, pIns, 2*(pPage->nCell - i)); |
| 75769 | + put2byte(pIns, idx); |
| 75770 | + pPage->nCell++; |
| 75771 | + /* increment the cell count */ |
| 75772 | + if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; |
| 75773 | + assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB ); |
| 75774 | +#ifndef SQLITE_OMIT_AUTOVACUUM |
| 75775 | + if( pPage->pBt->autoVacuum ){ |
| 75776 | + int rc2 = SQLITE_OK; |
| 75777 | + /* The cell may contain a pointer to an overflow page. If so, write |
| 75778 | + ** the entry for the overflow page into the pointer map. |
| 75779 | + */ |
| 75780 | + ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2); |
| 75781 | + if( rc2 ) return rc2; |
| 75782 | + } |
| 75783 | +#endif |
| 75784 | + } |
| 75785 | + return SQLITE_OK; |
| 75786 | +} |
| 75787 | + |
| 75788 | +/* |
| 75789 | +** This variant of insertCell() assumes that the pTemp and iChild |
| 75790 | +** parameters are both zero. Use this variant in sqlite3BtreeInsert() |
| 75791 | +** for performance improvement, and also so that this variant is only |
| 75792 | +** called from that one place, and is thus inlined, and thus runs must |
| 75793 | +** faster. |
| 75794 | +** |
| 75795 | +** Fixes or enhancements to this routine should be reflected into |
| 75796 | +** the insertCell() routine. |
| 75797 | +*/ |
| 75798 | +static int insertCellFast( |
| 75799 | + MemPage *pPage, /* Page into which we are copying */ |
| 75800 | + int i, /* New cell becomes the i-th cell of the page */ |
| 75801 | + u8 *pCell, /* Content of the new cell */ |
| 75802 | + int sz /* Bytes of content in pCell */ |
| 75803 | +){ |
| 75804 | + int idx = 0; /* Where to write new cell content in data[] */ |
| 75805 | + int j; /* Loop counter */ |
| 75806 | + u8 *data; /* The content of the whole page */ |
| 75807 | + u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ |
| 75808 | + |
| 75809 | + assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 75810 | + assert( MX_CELL(pPage->pBt)<=10921 ); |
| 75811 | + assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); |
| 75812 | + assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); |
| 75813 | + assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); |
| 75814 | + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 75815 | + assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB ); |
| 75816 | + assert( pPage->nFree>=0 ); |
| 75817 | + assert( pPage->nOverflow==0 ); |
| 75818 | + if( sz+2>pPage->nFree ){ |
| 75500 | 75819 | j = pPage->nOverflow++; |
| 75501 | 75820 | /* Comparison against ArraySize-1 since we hold back one extra slot |
| 75502 | 75821 | ** as a contingency. In other words, never need more than 3 overflow |
| 75503 | 75822 | ** slots but 4 are allocated, just to be safe. */ |
| 75504 | 75823 | assert( j < ArraySize(pPage->apOvfl)-1 ); |
| | @@ -75526,21 +75845,11 @@ |
| 75526 | 75845 | ** if it returns successfully */ |
| 75527 | 75846 | assert( idx >= 0 ); |
| 75528 | 75847 | assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); |
| 75529 | 75848 | assert( idx+sz <= (int)pPage->pBt->usableSize ); |
| 75530 | 75849 | pPage->nFree -= (u16)(2 + sz); |
| 75531 | | - if( iChild ){ |
| 75532 | | - /* In a corrupt database where an entry in the cell index section of |
| 75533 | | - ** a btree page has a value of 3 or less, the pCell value might point |
| 75534 | | - ** as many as 4 bytes in front of the start of the aData buffer for |
| 75535 | | - ** the source page. Make sure this does not cause problems by not |
| 75536 | | - ** reading the first 4 bytes */ |
| 75537 | | - memcpy(&data[idx+4], pCell+4, sz-4); |
| 75538 | | - put4byte(&data[idx], iChild); |
| 75539 | | - }else{ |
| 75540 | | - memcpy(&data[idx], pCell, sz); |
| 75541 | | - } |
| 75850 | + memcpy(&data[idx], pCell, sz); |
| 75542 | 75851 | pIns = pPage->aCellIdx + i*2; |
| 75543 | 75852 | memmove(pIns+2, pIns, 2*(pPage->nCell - i)); |
| 75544 | 75853 | put2byte(pIns, idx); |
| 75545 | 75854 | pPage->nCell++; |
| 75546 | 75855 | /* increment the cell count */ |
| | @@ -75721,11 +76030,11 @@ |
| 75721 | 76030 | int k; /* Current slot in pCArray->apEnd[] */ |
| 75722 | 76031 | u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */ |
| 75723 | 76032 | |
| 75724 | 76033 | assert( i<iEnd ); |
| 75725 | 76034 | j = get2byte(&aData[hdr+5]); |
| 75726 | | - if( j>(u32)usableSize ){ j = 0; } |
| 76035 | + if( NEVER(j>(u32)usableSize) ){ j = 0; } |
| 75727 | 76036 | memcpy(&pTmp[j], &aData[j], usableSize - j); |
| 75728 | 76037 | |
| 75729 | 76038 | for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){} |
| 75730 | 76039 | pSrcEnd = pCArray->apEnd[k]; |
| 75731 | 76040 | |
| | @@ -75865,46 +76174,54 @@ |
| 75865 | 76174 | ){ |
| 75866 | 76175 | u8 * const aData = pPg->aData; |
| 75867 | 76176 | u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
| 75868 | 76177 | u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
| 75869 | 76178 | int nRet = 0; |
| 75870 | | - int i; |
| 76179 | + int i, j; |
| 75871 | 76180 | int iEnd = iFirst + nCell; |
| 75872 | | - u8 *pFree = 0; /* \__ Parameters for pending call to */ |
| 75873 | | - int szFree = 0; /* / freeSpace() */ |
| 76181 | + int nFree = 0; |
| 76182 | + int aOfst[10]; |
| 76183 | + int aAfter[10]; |
| 75874 | 76184 | |
| 75875 | 76185 | for(i=iFirst; i<iEnd; i++){ |
| 75876 | 76186 | u8 *pCell = pCArray->apCell[i]; |
| 75877 | 76187 | if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ |
| 75878 | 76188 | int sz; |
| 76189 | + int iAfter; |
| 76190 | + int iOfst; |
| 75879 | 76191 | /* No need to use cachedCellSize() here. The sizes of all cells that |
| 75880 | 76192 | ** are to be freed have already been computing while deciding which |
| 75881 | 76193 | ** cells need freeing */ |
| 75882 | 76194 | sz = pCArray->szCell[i]; assert( sz>0 ); |
| 75883 | | - if( pFree!=(pCell + sz) ){ |
| 75884 | | - if( pFree ){ |
| 75885 | | - assert( pFree>aData && (pFree - aData)<65536 ); |
| 75886 | | - freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 75887 | | - } |
| 75888 | | - pFree = pCell; |
| 75889 | | - szFree = sz; |
| 75890 | | - if( pFree+sz>pEnd ){ |
| 75891 | | - return 0; |
| 75892 | | - } |
| 75893 | | - }else{ |
| 75894 | | - /* The current cell is adjacent to and before the pFree cell. |
| 75895 | | - ** Combine the two regions into one to reduce the number of calls |
| 75896 | | - ** to freeSpace(). */ |
| 75897 | | - pFree = pCell; |
| 75898 | | - szFree += sz; |
| 76195 | + iOfst = (u16)(pCell - aData); |
| 76196 | + iAfter = iOfst+sz; |
| 76197 | + for(j=0; j<nFree; j++){ |
| 76198 | + if( aOfst[j]==iAfter ){ |
| 76199 | + aOfst[j] = iOfst; |
| 76200 | + break; |
| 76201 | + }else if( aAfter[j]==iOfst ){ |
| 76202 | + aAfter[j] = iAfter; |
| 76203 | + break; |
| 76204 | + } |
| 76205 | + } |
| 76206 | + if( j>=nFree ){ |
| 76207 | + if( nFree>=sizeof(aOfst)/sizeof(aOfst[0]) ){ |
| 76208 | + for(j=0; j<nFree; j++){ |
| 76209 | + freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]); |
| 76210 | + } |
| 76211 | + nFree = 0; |
| 76212 | + } |
| 76213 | + aOfst[nFree] = iOfst; |
| 76214 | + aAfter[nFree] = iAfter; |
| 76215 | + if( &aData[iAfter]>pEnd ) return 0; |
| 76216 | + nFree++; |
| 75899 | 76217 | } |
| 75900 | 76218 | nRet++; |
| 75901 | 76219 | } |
| 75902 | 76220 | } |
| 75903 | | - if( pFree ){ |
| 75904 | | - assert( pFree>aData && (pFree - aData)<65536 ); |
| 75905 | | - freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 76221 | + for(j=0; j<nFree; j++){ |
| 76222 | + freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]); |
| 75906 | 76223 | } |
| 75907 | 76224 | return nRet; |
| 75908 | 76225 | } |
| 75909 | 76226 | |
| 75910 | 76227 | /* |
| | @@ -75955,11 +76272,11 @@ |
| 75955 | 76272 | nCell -= nTail; |
| 75956 | 76273 | } |
| 75957 | 76274 | |
| 75958 | 76275 | pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
| 75959 | 76276 | if( pData<pBegin ) goto editpage_fail; |
| 75960 | | - if( pData>pPg->aDataEnd ) goto editpage_fail; |
| 76277 | + if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail; |
| 75961 | 76278 | |
| 75962 | 76279 | /* Add cells to the start of the page */ |
| 75963 | 76280 | if( iNew<iOld ){ |
| 75964 | 76281 | int nAdd = MIN(nNew,iOld-iNew); |
| 75965 | 76282 | assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
| | @@ -76692,11 +77009,11 @@ |
| 76692 | 77009 | ** (2) pPage is a virtual root page. A virtual root page is when |
| 76693 | 77010 | ** the real root page is page 1 and we are the only child of |
| 76694 | 77011 | ** that page. |
| 76695 | 77012 | */ |
| 76696 | 77013 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); |
| 76697 | | - TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", |
| 77014 | + TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n", |
| 76698 | 77015 | apOld[0]->pgno, apOld[0]->nCell, |
| 76699 | 77016 | nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, |
| 76700 | 77017 | nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 |
| 76701 | 77018 | )); |
| 76702 | 77019 | |
| | @@ -76776,12 +77093,12 @@ |
| 76776 | 77093 | apNew[i]->pgno = pgnoB; |
| 76777 | 77094 | apNew[iB]->pgno = pgnoA; |
| 76778 | 77095 | } |
| 76779 | 77096 | } |
| 76780 | 77097 | |
| 76781 | | - TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
| 76782 | | - "%d(%d nc=%d) %d(%d nc=%d)\n", |
| 77098 | + TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) " |
| 77099 | + "%u(%u nc=%u) %u(%u nc=%u)\n", |
| 76783 | 77100 | apNew[0]->pgno, szNew[0], cntNew[0], |
| 76784 | 77101 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 76785 | 77102 | nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, |
| 76786 | 77103 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 76787 | 77104 | nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, |
| | @@ -77022,11 +77339,11 @@ |
| 77022 | 77339 | ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
| 77023 | 77340 | } |
| 77024 | 77341 | } |
| 77025 | 77342 | |
| 77026 | 77343 | assert( pParent->isInit ); |
| 77027 | | - TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 77344 | + TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n", |
| 77028 | 77345 | nOld, nNew, b.nCell)); |
| 77029 | 77346 | |
| 77030 | 77347 | /* Free any old pages that were not reused as new pages. |
| 77031 | 77348 | */ |
| 77032 | 77349 | for(i=nNew; i<nOld; i++){ |
| | @@ -77107,11 +77424,11 @@ |
| 77107 | 77424 | } |
| 77108 | 77425 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
| 77109 | 77426 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 77110 | 77427 | assert( pChild->nCell==pRoot->nCell || CORRUPT_DB ); |
| 77111 | 77428 | |
| 77112 | | - TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); |
| 77429 | + TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno)); |
| 77113 | 77430 | |
| 77114 | 77431 | /* Copy the overflow cells from pRoot to pChild */ |
| 77115 | 77432 | memcpy(pChild->aiOvfl, pRoot->aiOvfl, |
| 77116 | 77433 | pRoot->nOverflow*sizeof(pRoot->aiOvfl[0])); |
| 77117 | 77434 | memcpy(pChild->apOvfl, pRoot->apOvfl, |
| | @@ -77605,11 +77922,11 @@ |
| 77605 | 77922 | rc = btreeComputeFreeSpace(pPage); |
| 77606 | 77923 | } |
| 77607 | 77924 | if( rc ) return rc; |
| 77608 | 77925 | } |
| 77609 | 77926 | |
| 77610 | | - TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 77927 | + TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n", |
| 77611 | 77928 | pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno, |
| 77612 | 77929 | loc==0 ? "overwrite" : "new entry")); |
| 77613 | 77930 | assert( pPage->isInit || CORRUPT_DB ); |
| 77614 | 77931 | newCell = p->pBt->pTmpSpace; |
| 77615 | 77932 | assert( newCell!=0 ); |
| | @@ -77632,10 +77949,11 @@ |
| 77632 | 77949 | if( rc ) goto end_insert; |
| 77633 | 77950 | } |
| 77634 | 77951 | assert( szNew==pPage->xCellSize(pPage, newCell) ); |
| 77635 | 77952 | assert( szNew <= MX_CELL_SIZE(p->pBt) ); |
| 77636 | 77953 | idx = pCur->ix; |
| 77954 | + pCur->info.nSize = 0; |
| 77637 | 77955 | if( loc==0 ){ |
| 77638 | 77956 | CellInfo info; |
| 77639 | 77957 | assert( idx>=0 ); |
| 77640 | 77958 | if( idx>=pPage->nCell ){ |
| 77641 | 77959 | return SQLITE_CORRUPT_BKPT; |
| | @@ -77680,11 +77998,11 @@ |
| 77680 | 77998 | idx = ++pCur->ix; |
| 77681 | 77999 | pCur->curFlags &= ~BTCF_ValidNKey; |
| 77682 | 78000 | }else{ |
| 77683 | 78001 | assert( pPage->leaf ); |
| 77684 | 78002 | } |
| 77685 | | - rc = insertCell(pPage, idx, newCell, szNew, 0, 0); |
| 78003 | + rc = insertCellFast(pPage, idx, newCell, szNew); |
| 77686 | 78004 | assert( pPage->nOverflow==0 || rc==SQLITE_OK ); |
| 77687 | 78005 | assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); |
| 77688 | 78006 | |
| 77689 | 78007 | /* If no error has occurred and pPage has an overflow cell, call balance() |
| 77690 | 78008 | ** to redistribute the cells within the tree. Since balance() may move |
| | @@ -77704,11 +78022,10 @@ |
| 77704 | 78022 | ** the b-tree if possible. If the cursor is left pointing to the last |
| 77705 | 78023 | ** entry in the table, and the next row inserted has an integer key |
| 77706 | 78024 | ** larger than the largest existing key, it is possible to insert the |
| 77707 | 78025 | ** row without seeking the cursor. This can be a big performance boost. |
| 77708 | 78026 | */ |
| 77709 | | - pCur->info.nSize = 0; |
| 77710 | 78027 | if( pPage->nOverflow ){ |
| 77711 | 78028 | assert( rc==SQLITE_OK ); |
| 77712 | 78029 | pCur->curFlags &= ~(BTCF_ValidNKey); |
| 77713 | 78030 | rc = balance(pCur); |
| 77714 | 78031 | |
| | @@ -77904,10 +78221,13 @@ |
| 77904 | 78221 | return SQLITE_CORRUPT_BKPT; |
| 77905 | 78222 | } |
| 77906 | 78223 | pCell = findCell(pPage, iCellIdx); |
| 77907 | 78224 | if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){ |
| 77908 | 78225 | return SQLITE_CORRUPT_BKPT; |
| 78226 | + } |
| 78227 | + if( pCell<&pPage->aCellIdx[pPage->nCell] ){ |
| 78228 | + return SQLITE_CORRUPT_BKPT; |
| 77909 | 78229 | } |
| 77910 | 78230 | |
| 77911 | 78231 | /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must |
| 77912 | 78232 | ** be preserved following this delete operation. If the current delete |
| 77913 | 78233 | ** will cause a b-tree rebalance, then this is done by saving the cursor |
| | @@ -78653,11 +78973,12 @@ |
| 78653 | 78973 | va_start(ap, zFormat); |
| 78654 | 78974 | if( pCheck->errMsg.nChar ){ |
| 78655 | 78975 | sqlite3_str_append(&pCheck->errMsg, "\n", 1); |
| 78656 | 78976 | } |
| 78657 | 78977 | if( pCheck->zPfx ){ |
| 78658 | | - sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2); |
| 78978 | + sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, |
| 78979 | + pCheck->v0, pCheck->v1, pCheck->v2); |
| 78659 | 78980 | } |
| 78660 | 78981 | sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap); |
| 78661 | 78982 | va_end(ap); |
| 78662 | 78983 | if( pCheck->errMsg.accError==SQLITE_NOMEM ){ |
| 78663 | 78984 | checkOom(pCheck); |
| | @@ -78693,15 +79014,15 @@ |
| 78693 | 79014 | ** |
| 78694 | 79015 | ** Also check that the page number is in bounds. |
| 78695 | 79016 | */ |
| 78696 | 79017 | static int checkRef(IntegrityCk *pCheck, Pgno iPage){ |
| 78697 | 79018 | if( iPage>pCheck->nPage || iPage==0 ){ |
| 78698 | | - checkAppendMsg(pCheck, "invalid page number %d", iPage); |
| 79019 | + checkAppendMsg(pCheck, "invalid page number %u", iPage); |
| 78699 | 79020 | return 1; |
| 78700 | 79021 | } |
| 78701 | 79022 | if( getPageReferenced(pCheck, iPage) ){ |
| 78702 | | - checkAppendMsg(pCheck, "2nd reference to page %d", iPage); |
| 79023 | + checkAppendMsg(pCheck, "2nd reference to page %u", iPage); |
| 78703 | 79024 | return 1; |
| 78704 | 79025 | } |
| 78705 | 79026 | setPageReferenced(pCheck, iPage); |
| 78706 | 79027 | return 0; |
| 78707 | 79028 | } |
| | @@ -78723,17 +79044,17 @@ |
| 78723 | 79044 | Pgno iPtrmapParent; |
| 78724 | 79045 | |
| 78725 | 79046 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 78726 | 79047 | if( rc!=SQLITE_OK ){ |
| 78727 | 79048 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) checkOom(pCheck); |
| 78728 | | - checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); |
| 79049 | + checkAppendMsg(pCheck, "Failed to read ptrmap key=%u", iChild); |
| 78729 | 79050 | return; |
| 78730 | 79051 | } |
| 78731 | 79052 | |
| 78732 | 79053 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 78733 | 79054 | checkAppendMsg(pCheck, |
| 78734 | | - "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 79055 | + "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)", |
| 78735 | 79056 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 78736 | 79057 | } |
| 78737 | 79058 | } |
| 78738 | 79059 | #endif |
| 78739 | 79060 | |
| | @@ -78754,11 +79075,11 @@ |
| 78754 | 79075 | DbPage *pOvflPage; |
| 78755 | 79076 | unsigned char *pOvflData; |
| 78756 | 79077 | if( checkRef(pCheck, iPage) ) break; |
| 78757 | 79078 | N--; |
| 78758 | 79079 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){ |
| 78759 | | - checkAppendMsg(pCheck, "failed to get page %d", iPage); |
| 79080 | + checkAppendMsg(pCheck, "failed to get page %u", iPage); |
| 78760 | 79081 | break; |
| 78761 | 79082 | } |
| 78762 | 79083 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
| 78763 | 79084 | if( isFreeList ){ |
| 78764 | 79085 | u32 n = (u32)get4byte(&pOvflData[4]); |
| | @@ -78767,11 +79088,11 @@ |
| 78767 | 79088 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); |
| 78768 | 79089 | } |
| 78769 | 79090 | #endif |
| 78770 | 79091 | if( n>pCheck->pBt->usableSize/4-2 ){ |
| 78771 | 79092 | checkAppendMsg(pCheck, |
| 78772 | | - "freelist leaf count too big on page %d", iPage); |
| 79093 | + "freelist leaf count too big on page %u", iPage); |
| 78773 | 79094 | N--; |
| 78774 | 79095 | }else{ |
| 78775 | 79096 | for(i=0; i<(int)n; i++){ |
| 78776 | 79097 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
| 78777 | 79098 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| | @@ -78799,11 +79120,11 @@ |
| 78799 | 79120 | iPage = get4byte(pOvflData); |
| 78800 | 79121 | sqlite3PagerUnref(pOvflPage); |
| 78801 | 79122 | } |
| 78802 | 79123 | if( N && nErrAtStart==pCheck->nErr ){ |
| 78803 | 79124 | checkAppendMsg(pCheck, |
| 78804 | | - "%s is %d but should be %d", |
| 79125 | + "%s is %u but should be %u", |
| 78805 | 79126 | isFreeList ? "size" : "overflow list length", |
| 78806 | 79127 | expected-N, expected); |
| 78807 | 79128 | } |
| 78808 | 79129 | } |
| 78809 | 79130 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| | @@ -78914,12 +79235,12 @@ |
| 78914 | 79235 | if( pCheck->mxErr==0 ) goto end_of_check; |
| 78915 | 79236 | pBt = pCheck->pBt; |
| 78916 | 79237 | usableSize = pBt->usableSize; |
| 78917 | 79238 | if( iPage==0 ) return 0; |
| 78918 | 79239 | if( checkRef(pCheck, iPage) ) return 0; |
| 78919 | | - pCheck->zPfx = "Page %u: "; |
| 78920 | | - pCheck->v1 = iPage; |
| 79240 | + pCheck->zPfx = "Tree %u page %u: "; |
| 79241 | + pCheck->v0 = pCheck->v1 = iPage; |
| 78921 | 79242 | if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){ |
| 78922 | 79243 | checkAppendMsg(pCheck, |
| 78923 | 79244 | "unable to get the page. error code=%d", rc); |
| 78924 | 79245 | goto end_of_check; |
| 78925 | 79246 | } |
| | @@ -78941,11 +79262,11 @@ |
| 78941 | 79262 | } |
| 78942 | 79263 | data = pPage->aData; |
| 78943 | 79264 | hdr = pPage->hdrOffset; |
| 78944 | 79265 | |
| 78945 | 79266 | /* Set up for cell analysis */ |
| 78946 | | - pCheck->zPfx = "On tree page %u cell %d: "; |
| 79267 | + pCheck->zPfx = "Tree %u page %u cell %u: "; |
| 78947 | 79268 | contentOffset = get2byteNotZero(&data[hdr+5]); |
| 78948 | 79269 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 78949 | 79270 | |
| 78950 | 79271 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 78951 | 79272 | ** number of cells on the page. */ |
| | @@ -78961,11 +79282,11 @@ |
| 78961 | 79282 | if( !pPage->leaf ){ |
| 78962 | 79283 | /* Analyze the right-child page of internal pages */ |
| 78963 | 79284 | pgno = get4byte(&data[hdr+8]); |
| 78964 | 79285 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 78965 | 79286 | if( pBt->autoVacuum ){ |
| 78966 | | - pCheck->zPfx = "On page %u at right child: "; |
| 79287 | + pCheck->zPfx = "Tree %u page %u right child: "; |
| 78967 | 79288 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 78968 | 79289 | } |
| 78969 | 79290 | #endif |
| 78970 | 79291 | depth = checkTreePage(pCheck, pgno, &maxKey, maxKey); |
| 78971 | 79292 | keyCanBeEqual = 0; |
| | @@ -78985,11 +79306,11 @@ |
| 78985 | 79306 | pCheck->v2 = i; |
| 78986 | 79307 | assert( pCellIdx==&data[cellStart + i*2] ); |
| 78987 | 79308 | pc = get2byteAligned(pCellIdx); |
| 78988 | 79309 | pCellIdx -= 2; |
| 78989 | 79310 | if( pc<contentOffset || pc>usableSize-4 ){ |
| 78990 | | - checkAppendMsg(pCheck, "Offset %d out of range %d..%d", |
| 79311 | + checkAppendMsg(pCheck, "Offset %u out of range %u..%u", |
| 78991 | 79312 | pc, contentOffset, usableSize-4); |
| 78992 | 79313 | doCoverageCheck = 0; |
| 78993 | 79314 | continue; |
| 78994 | 79315 | } |
| 78995 | 79316 | pCell = &data[pc]; |
| | @@ -79117,11 +79438,11 @@ |
| 79117 | 79438 | ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
| 79118 | 79439 | ** number of fragmented free bytes within the cell content area. |
| 79119 | 79440 | */ |
| 79120 | 79441 | if( heap[0]==0 && nFrag!=data[hdr+7] ){ |
| 79121 | 79442 | checkAppendMsg(pCheck, |
| 79122 | | - "Fragmentation of %d bytes reported as %d on page %u", |
| 79443 | + "Fragmentation of %u bytes reported as %u on page %u", |
| 79123 | 79444 | nFrag, data[hdr+7], iPage); |
| 79124 | 79445 | } |
| 79125 | 79446 | } |
| 79126 | 79447 | |
| 79127 | 79448 | end_of_check: |
| | @@ -79214,11 +79535,11 @@ |
| 79214 | 79535 | if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); |
| 79215 | 79536 | |
| 79216 | 79537 | /* Check the integrity of the freelist |
| 79217 | 79538 | */ |
| 79218 | 79539 | if( bCkFreelist ){ |
| 79219 | | - sCheck.zPfx = "Main freelist: "; |
| 79540 | + sCheck.zPfx = "Freelist: "; |
| 79220 | 79541 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 79221 | 79542 | get4byte(&pBt->pPage1->aData[36])); |
| 79222 | 79543 | sCheck.zPfx = 0; |
| 79223 | 79544 | } |
| 79224 | 79545 | |
| | @@ -79231,11 +79552,11 @@ |
| 79231 | 79552 | Pgno mxInHdr; |
| 79232 | 79553 | for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i]; |
| 79233 | 79554 | mxInHdr = get4byte(&pBt->pPage1->aData[52]); |
| 79234 | 79555 | if( mx!=mxInHdr ){ |
| 79235 | 79556 | checkAppendMsg(&sCheck, |
| 79236 | | - "max rootpage (%d) disagrees with header (%d)", |
| 79557 | + "max rootpage (%u) disagrees with header (%u)", |
| 79237 | 79558 | mx, mxInHdr |
| 79238 | 79559 | ); |
| 79239 | 79560 | } |
| 79240 | 79561 | }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){ |
| 79241 | 79562 | checkAppendMsg(&sCheck, |
| | @@ -79262,23 +79583,23 @@ |
| 79262 | 79583 | */ |
| 79263 | 79584 | if( !bPartial ){ |
| 79264 | 79585 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
| 79265 | 79586 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 79266 | 79587 | if( getPageReferenced(&sCheck, i)==0 ){ |
| 79267 | | - checkAppendMsg(&sCheck, "Page %d is never used", i); |
| 79588 | + checkAppendMsg(&sCheck, "Page %u: never used", i); |
| 79268 | 79589 | } |
| 79269 | 79590 | #else |
| 79270 | 79591 | /* If the database supports auto-vacuum, make sure no tables contain |
| 79271 | 79592 | ** references to pointer-map pages. |
| 79272 | 79593 | */ |
| 79273 | 79594 | if( getPageReferenced(&sCheck, i)==0 && |
| 79274 | 79595 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
| 79275 | | - checkAppendMsg(&sCheck, "Page %d is never used", i); |
| 79596 | + checkAppendMsg(&sCheck, "Page %u: never used", i); |
| 79276 | 79597 | } |
| 79277 | 79598 | if( getPageReferenced(&sCheck, i)!=0 && |
| 79278 | 79599 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
| 79279 | | - checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); |
| 79600 | + checkAppendMsg(&sCheck, "Page %u: pointer map referenced", i); |
| 79280 | 79601 | } |
| 79281 | 79602 | #endif |
| 79282 | 79603 | } |
| 79283 | 79604 | } |
| 79284 | 79605 | |
| | @@ -80806,11 +81127,11 @@ |
| 80806 | 81127 | return SQLITE_NOMEM_BKPT; |
| 80807 | 81128 | } |
| 80808 | 81129 | |
| 80809 | 81130 | vdbeMemRenderNum(nByte, pMem->z, pMem); |
| 80810 | 81131 | assert( pMem->z!=0 ); |
| 80811 | | - assert( pMem->n==sqlite3Strlen30NN(pMem->z) ); |
| 81132 | + assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) ); |
| 80812 | 81133 | pMem->enc = SQLITE_UTF8; |
| 80813 | 81134 | pMem->flags |= MEM_Str|MEM_Term; |
| 80814 | 81135 | if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); |
| 80815 | 81136 | sqlite3VdbeChangeEncoding(pMem, enc); |
| 80816 | 81137 | return SQLITE_OK; |
| | @@ -81850,10 +82171,13 @@ |
| 81850 | 82171 | assert( ExprUseXList(p) ); |
| 81851 | 82172 | pList = p->x.pList; |
| 81852 | 82173 | if( pList ) nVal = pList->nExpr; |
| 81853 | 82174 | assert( !ExprHasProperty(p, EP_IntValue) ); |
| 81854 | 82175 | pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0); |
| 82176 | +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 82177 | + if( pFunc==0 ) return SQLITE_OK; |
| 82178 | +#endif |
| 81855 | 82179 | assert( pFunc ); |
| 81856 | 82180 | if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 |
| 81857 | 82181 | || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) |
| 81858 | 82182 | ){ |
| 81859 | 82183 | return SQLITE_OK; |
| | @@ -81886,20 +82210,15 @@ |
| 81886 | 82210 | rc = ctx.isError; |
| 81887 | 82211 | sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal)); |
| 81888 | 82212 | }else{ |
| 81889 | 82213 | sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8); |
| 81890 | 82214 | assert( rc==SQLITE_OK ); |
| 81891 | | - assert( enc==pVal->enc |
| 81892 | | - || (pVal->flags & MEM_Str)==0 |
| 81893 | | - || db->mallocFailed ); |
| 81894 | | -#if 0 /* Not reachable except after a prior failure */ |
| 81895 | 82215 | rc = sqlite3VdbeChangeEncoding(pVal, enc); |
| 81896 | | - if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){ |
| 82216 | + if( NEVER(rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal)) ){ |
| 81897 | 82217 | rc = SQLITE_TOOBIG; |
| 81898 | 82218 | pCtx->pParse->nErr++; |
| 81899 | 82219 | } |
| 81900 | | -#endif |
| 81901 | 82220 | } |
| 81902 | 82221 | |
| 81903 | 82222 | value_from_function_out: |
| 81904 | 82223 | if( rc!=SQLITE_OK ){ |
| 81905 | 82224 | pVal = 0; |
| | @@ -81959,10 +82278,17 @@ |
| 81959 | 82278 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 81960 | 82279 | aff = sqlite3AffinityType(pExpr->u.zToken,0); |
| 81961 | 82280 | rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); |
| 81962 | 82281 | testcase( rc!=SQLITE_OK ); |
| 81963 | 82282 | if( *ppVal ){ |
| 82283 | +#ifdef SQLITE_ENABLE_STAT4 |
| 82284 | + rc = ExpandBlob(*ppVal); |
| 82285 | +#else |
| 82286 | + /* zero-blobs only come from functions, not literal values. And |
| 82287 | + ** functions are only processed under STAT4 */ |
| 82288 | + assert( (ppVal[0][0].flags & MEM_Zero)==0 ); |
| 82289 | +#endif |
| 81964 | 82290 | sqlite3VdbeMemCast(*ppVal, aff, enc); |
| 81965 | 82291 | sqlite3ValueApplyAffinity(*ppVal, affinity, enc); |
| 81966 | 82292 | } |
| 81967 | 82293 | return rc; |
| 81968 | 82294 | } |
| | @@ -82805,14 +83131,14 @@ |
| 82805 | 83131 | ** If the bPush flag is true, then make this opcode the parent for |
| 82806 | 83132 | ** subsequent Explains until sqlite3VdbeExplainPop() is called. |
| 82807 | 83133 | */ |
| 82808 | 83134 | SQLITE_PRIVATE int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){ |
| 82809 | 83135 | int addr = 0; |
| 82810 | | -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 83136 | +#if !defined(SQLITE_DEBUG) |
| 82811 | 83137 | /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined. |
| 82812 | 83138 | ** But omit them (for performance) during production builds */ |
| 82813 | | - if( pParse->explain==2 ) |
| 83139 | + if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) |
| 82814 | 83140 | #endif |
| 82815 | 83141 | { |
| 82816 | 83142 | char *zMsg; |
| 82817 | 83143 | Vdbe *v; |
| 82818 | 83144 | va_list ap; |
| | @@ -83484,22 +83810,24 @@ |
| 83484 | 83810 | int addrLoop, /* Address of loop counter */ |
| 83485 | 83811 | int addrVisit, /* Address of rows visited counter */ |
| 83486 | 83812 | LogEst nEst, /* Estimated number of output rows */ |
| 83487 | 83813 | const char *zName /* Name of table or index being scanned */ |
| 83488 | 83814 | ){ |
| 83489 | | - sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus); |
| 83490 | | - ScanStatus *aNew; |
| 83491 | | - aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); |
| 83492 | | - if( aNew ){ |
| 83493 | | - ScanStatus *pNew = &aNew[p->nScan++]; |
| 83494 | | - memset(pNew, 0, sizeof(ScanStatus)); |
| 83495 | | - pNew->addrExplain = addrExplain; |
| 83496 | | - pNew->addrLoop = addrLoop; |
| 83497 | | - pNew->addrVisit = addrVisit; |
| 83498 | | - pNew->nEst = nEst; |
| 83499 | | - pNew->zName = sqlite3DbStrDup(p->db, zName); |
| 83500 | | - p->aScan = aNew; |
| 83815 | + if( IS_STMT_SCANSTATUS(p->db) ){ |
| 83816 | + sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus); |
| 83817 | + ScanStatus *aNew; |
| 83818 | + aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); |
| 83819 | + if( aNew ){ |
| 83820 | + ScanStatus *pNew = &aNew[p->nScan++]; |
| 83821 | + memset(pNew, 0, sizeof(ScanStatus)); |
| 83822 | + pNew->addrExplain = addrExplain; |
| 83823 | + pNew->addrLoop = addrLoop; |
| 83824 | + pNew->addrVisit = addrVisit; |
| 83825 | + pNew->nEst = nEst; |
| 83826 | + pNew->zName = sqlite3DbStrDup(p->db, zName); |
| 83827 | + p->aScan = aNew; |
| 83828 | + } |
| 83501 | 83829 | } |
| 83502 | 83830 | } |
| 83503 | 83831 | |
| 83504 | 83832 | /* |
| 83505 | 83833 | ** Add the range of instructions from addrStart to addrEnd (inclusive) to |
| | @@ -83512,24 +83840,26 @@ |
| 83512 | 83840 | Vdbe *p, |
| 83513 | 83841 | int addrExplain, |
| 83514 | 83842 | int addrStart, |
| 83515 | 83843 | int addrEnd |
| 83516 | 83844 | ){ |
| 83517 | | - ScanStatus *pScan = 0; |
| 83518 | | - int ii; |
| 83519 | | - for(ii=p->nScan-1; ii>=0; ii--){ |
| 83520 | | - pScan = &p->aScan[ii]; |
| 83521 | | - if( pScan->addrExplain==addrExplain ) break; |
| 83522 | | - pScan = 0; |
| 83523 | | - } |
| 83524 | | - if( pScan ){ |
| 83525 | | - if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1; |
| 83526 | | - for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ |
| 83527 | | - if( pScan->aAddrRange[ii]==0 ){ |
| 83528 | | - pScan->aAddrRange[ii] = addrStart; |
| 83529 | | - pScan->aAddrRange[ii+1] = addrEnd; |
| 83530 | | - break; |
| 83845 | + if( IS_STMT_SCANSTATUS(p->db) ){ |
| 83846 | + ScanStatus *pScan = 0; |
| 83847 | + int ii; |
| 83848 | + for(ii=p->nScan-1; ii>=0; ii--){ |
| 83849 | + pScan = &p->aScan[ii]; |
| 83850 | + if( pScan->addrExplain==addrExplain ) break; |
| 83851 | + pScan = 0; |
| 83852 | + } |
| 83853 | + if( pScan ){ |
| 83854 | + if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1; |
| 83855 | + for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ |
| 83856 | + if( pScan->aAddrRange[ii]==0 ){ |
| 83857 | + pScan->aAddrRange[ii] = addrStart; |
| 83858 | + pScan->aAddrRange[ii+1] = addrEnd; |
| 83859 | + break; |
| 83860 | + } |
| 83531 | 83861 | } |
| 83532 | 83862 | } |
| 83533 | 83863 | } |
| 83534 | 83864 | } |
| 83535 | 83865 | |
| | @@ -83542,23 +83872,25 @@ |
| 83542 | 83872 | Vdbe *p, |
| 83543 | 83873 | int addrExplain, |
| 83544 | 83874 | int addrLoop, |
| 83545 | 83875 | int addrVisit |
| 83546 | 83876 | ){ |
| 83547 | | - ScanStatus *pScan = 0; |
| 83548 | | - int ii; |
| 83549 | | - for(ii=p->nScan-1; ii>=0; ii--){ |
| 83550 | | - pScan = &p->aScan[ii]; |
| 83551 | | - if( pScan->addrExplain==addrExplain ) break; |
| 83552 | | - pScan = 0; |
| 83553 | | - } |
| 83554 | | - if( pScan ){ |
| 83555 | | - pScan->addrLoop = addrLoop; |
| 83556 | | - pScan->addrVisit = addrVisit; |
| 83877 | + if( IS_STMT_SCANSTATUS(p->db) ){ |
| 83878 | + ScanStatus *pScan = 0; |
| 83879 | + int ii; |
| 83880 | + for(ii=p->nScan-1; ii>=0; ii--){ |
| 83881 | + pScan = &p->aScan[ii]; |
| 83882 | + if( pScan->addrExplain==addrExplain ) break; |
| 83883 | + pScan = 0; |
| 83884 | + } |
| 83885 | + if( pScan ){ |
| 83886 | + pScan->addrLoop = addrLoop; |
| 83887 | + pScan->addrVisit = addrVisit; |
| 83888 | + } |
| 83557 | 83889 | } |
| 83558 | 83890 | } |
| 83559 | | -#endif |
| 83891 | +#endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */ |
| 83560 | 83892 | |
| 83561 | 83893 | |
| 83562 | 83894 | /* |
| 83563 | 83895 | ** Change the value of the opcode, or P1, P2, P3, or P5 operands |
| 83564 | 83896 | ** for a specific instruction. |
| | @@ -85682,10 +86014,12 @@ |
| 85682 | 86014 | db->nDeferredCons = 0; |
| 85683 | 86015 | db->nDeferredImmCons = 0; |
| 85684 | 86016 | db->flags &= ~(u64)SQLITE_DeferFKs; |
| 85685 | 86017 | sqlite3CommitInternalChanges(db); |
| 85686 | 86018 | } |
| 86019 | + }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){ |
| 86020 | + p->nChange = 0; |
| 85687 | 86021 | }else{ |
| 85688 | 86022 | sqlite3RollbackAll(db, SQLITE_OK); |
| 85689 | 86023 | p->nChange = 0; |
| 85690 | 86024 | } |
| 85691 | 86025 | db->nStatement = 0; |
| | @@ -86000,13 +86334,13 @@ |
| 86000 | 86334 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 86001 | 86335 | if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql); |
| 86002 | 86336 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 86003 | 86337 | sqlite3DbFree(db, p->zNormSql); |
| 86004 | 86338 | { |
| 86005 | | - DblquoteStr *pThis, *pNext; |
| 86006 | | - for(pThis=p->pDblStr; pThis; pThis=pNext){ |
| 86007 | | - pNext = pThis->pNextStr; |
| 86339 | + DblquoteStr *pThis, *pNxt; |
| 86340 | + for(pThis=p->pDblStr; pThis; pThis=pNxt){ |
| 86341 | + pNxt = pThis->pNextStr; |
| 86008 | 86342 | sqlite3DbFree(db, pThis); |
| 86009 | 86343 | } |
| 86010 | 86344 | } |
| 86011 | 86345 | #endif |
| 86012 | 86346 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| | @@ -87629,10 +87963,24 @@ |
| 87629 | 87963 | return 0; |
| 87630 | 87964 | } |
| 87631 | 87965 | return 1; |
| 87632 | 87966 | } |
| 87633 | 87967 | |
| 87968 | +#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG) |
| 87969 | +/* |
| 87970 | +** This Walker callback is used to help verify that calls to |
| 87971 | +** sqlite3BtreeCursorHint() with opcode BTREE_HINT_RANGE have |
| 87972 | +** byte-code register values correctly initialized. |
| 87973 | +*/ |
| 87974 | +SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){ |
| 87975 | + if( pExpr->op==TK_REGISTER ){ |
| 87976 | + assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 ); |
| 87977 | + } |
| 87978 | + return WRC_Continue; |
| 87979 | +} |
| 87980 | +#endif /* SQLITE_ENABLE_CURSOR_HINTS && SQLITE_DEBUG */ |
| 87981 | + |
| 87634 | 87982 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 87635 | 87983 | /* |
| 87636 | 87984 | ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored |
| 87637 | 87985 | ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored |
| 87638 | 87986 | ** in memory obtained from sqlite3DbMalloc). |
| | @@ -88015,11 +88363,11 @@ |
| 88015 | 88363 | SQLITE_NULL, /* 0x1d (not possible) */ |
| 88016 | 88364 | SQLITE_INTEGER, /* 0x1e (not possible) */ |
| 88017 | 88365 | SQLITE_NULL, /* 0x1f (not possible) */ |
| 88018 | 88366 | SQLITE_FLOAT, /* 0x20 INTREAL */ |
| 88019 | 88367 | SQLITE_NULL, /* 0x21 (not possible) */ |
| 88020 | | - SQLITE_TEXT, /* 0x22 INTREAL + TEXT */ |
| 88368 | + SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */ |
| 88021 | 88369 | SQLITE_NULL, /* 0x23 (not possible) */ |
| 88022 | 88370 | SQLITE_FLOAT, /* 0x24 (not possible) */ |
| 88023 | 88371 | SQLITE_NULL, /* 0x25 (not possible) */ |
| 88024 | 88372 | SQLITE_FLOAT, /* 0x26 (not possible) */ |
| 88025 | 88373 | SQLITE_NULL, /* 0x27 (not possible) */ |
| | @@ -89882,19 +90230,28 @@ |
| 89882 | 90230 | int iScanStatusOp, /* Which metric to return */ |
| 89883 | 90231 | int flags, |
| 89884 | 90232 | void *pOut /* OUT: Write the answer here */ |
| 89885 | 90233 | ){ |
| 89886 | 90234 | Vdbe *p = (Vdbe*)pStmt; |
| 89887 | | - ScanStatus *pScan; |
| 90235 | + VdbeOp *aOp = p->aOp; |
| 90236 | + int nOp = p->nOp; |
| 90237 | + ScanStatus *pScan = 0; |
| 89888 | 90238 | int idx; |
| 90239 | + |
| 90240 | + if( p->pFrame ){ |
| 90241 | + VdbeFrame *pFrame; |
| 90242 | + for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 90243 | + aOp = pFrame->aOp; |
| 90244 | + nOp = pFrame->nOp; |
| 90245 | + } |
| 89889 | 90246 | |
| 89890 | 90247 | if( iScan<0 ){ |
| 89891 | 90248 | int ii; |
| 89892 | 90249 | if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ |
| 89893 | 90250 | i64 res = 0; |
| 89894 | | - for(ii=0; ii<p->nOp; ii++){ |
| 89895 | | - res += p->aOp[ii].nCycle; |
| 90251 | + for(ii=0; ii<nOp; ii++){ |
| 90252 | + res += aOp[ii].nCycle; |
| 89896 | 90253 | } |
| 89897 | 90254 | *(i64*)pOut = res; |
| 89898 | 90255 | return 0; |
| 89899 | 90256 | } |
| 89900 | 90257 | return 1; |
| | @@ -89916,19 +90273,19 @@ |
| 89916 | 90273 | if( idx>=p->nScan ) return 1; |
| 89917 | 90274 | |
| 89918 | 90275 | switch( iScanStatusOp ){ |
| 89919 | 90276 | case SQLITE_SCANSTAT_NLOOP: { |
| 89920 | 90277 | if( pScan->addrLoop>0 ){ |
| 89921 | | - *(sqlite3_int64*)pOut = p->aOp[pScan->addrLoop].nExec; |
| 90278 | + *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; |
| 89922 | 90279 | }else{ |
| 89923 | 90280 | *(sqlite3_int64*)pOut = -1; |
| 89924 | 90281 | } |
| 89925 | 90282 | break; |
| 89926 | 90283 | } |
| 89927 | 90284 | case SQLITE_SCANSTAT_NVISIT: { |
| 89928 | 90285 | if( pScan->addrVisit>0 ){ |
| 89929 | | - *(sqlite3_int64*)pOut = p->aOp[pScan->addrVisit].nExec; |
| 90286 | + *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec; |
| 89930 | 90287 | }else{ |
| 89931 | 90288 | *(sqlite3_int64*)pOut = -1; |
| 89932 | 90289 | } |
| 89933 | 90290 | break; |
| 89934 | 90291 | } |
| | @@ -89946,27 +90303,27 @@ |
| 89946 | 90303 | *(const char**)pOut = pScan->zName; |
| 89947 | 90304 | break; |
| 89948 | 90305 | } |
| 89949 | 90306 | case SQLITE_SCANSTAT_EXPLAIN: { |
| 89950 | 90307 | if( pScan->addrExplain ){ |
| 89951 | | - *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; |
| 90308 | + *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z; |
| 89952 | 90309 | }else{ |
| 89953 | 90310 | *(const char**)pOut = 0; |
| 89954 | 90311 | } |
| 89955 | 90312 | break; |
| 89956 | 90313 | } |
| 89957 | 90314 | case SQLITE_SCANSTAT_SELECTID: { |
| 89958 | 90315 | if( pScan->addrExplain ){ |
| 89959 | | - *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; |
| 90316 | + *(int*)pOut = aOp[ pScan->addrExplain ].p1; |
| 89960 | 90317 | }else{ |
| 89961 | 90318 | *(int*)pOut = -1; |
| 89962 | 90319 | } |
| 89963 | 90320 | break; |
| 89964 | 90321 | } |
| 89965 | 90322 | case SQLITE_SCANSTAT_PARENTID: { |
| 89966 | 90323 | if( pScan->addrExplain ){ |
| 89967 | | - *(int*)pOut = p->aOp[ pScan->addrExplain ].p2; |
| 90324 | + *(int*)pOut = aOp[ pScan->addrExplain ].p2; |
| 89968 | 90325 | }else{ |
| 89969 | 90326 | *(int*)pOut = -1; |
| 89970 | 90327 | } |
| 89971 | 90328 | break; |
| 89972 | 90329 | } |
| | @@ -89980,22 +90337,22 @@ |
| 89980 | 90337 | int iIns = pScan->aAddrRange[ii]; |
| 89981 | 90338 | int iEnd = pScan->aAddrRange[ii+1]; |
| 89982 | 90339 | if( iIns==0 ) break; |
| 89983 | 90340 | if( iIns>0 ){ |
| 89984 | 90341 | while( iIns<=iEnd ){ |
| 89985 | | - res += p->aOp[iIns].nCycle; |
| 90342 | + res += aOp[iIns].nCycle; |
| 89986 | 90343 | iIns++; |
| 89987 | 90344 | } |
| 89988 | 90345 | }else{ |
| 89989 | 90346 | int iOp; |
| 89990 | | - for(iOp=0; iOp<p->nOp; iOp++){ |
| 89991 | | - Op *pOp = &p->aOp[iOp]; |
| 90347 | + for(iOp=0; iOp<nOp; iOp++){ |
| 90348 | + Op *pOp = &aOp[iOp]; |
| 89992 | 90349 | if( pOp->p1!=iEnd ) continue; |
| 89993 | 90350 | if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ |
| 89994 | 90351 | continue; |
| 89995 | 90352 | } |
| 89996 | | - res += p->aOp[iOp].nCycle; |
| 90353 | + res += aOp[iOp].nCycle; |
| 89997 | 90354 | } |
| 89998 | 90355 | } |
| 89999 | 90356 | } |
| 90000 | 90357 | } |
| 90001 | 90358 | *(i64*)pOut = res; |
| | @@ -90914,11 +91271,14 @@ |
| 90914 | 91271 | if( p->flags & (MEM_Int|MEM_IntReal) ){ |
| 90915 | 91272 | h += p->u.i; |
| 90916 | 91273 | }else if( p->flags & MEM_Real ){ |
| 90917 | 91274 | h += sqlite3VdbeIntValue(p); |
| 90918 | 91275 | }else if( p->flags & (MEM_Str|MEM_Blob) ){ |
| 90919 | | - /* no-op */ |
| 91276 | + /* All strings have the same hash and all blobs have the same hash, |
| 91277 | + ** though, at least, those hashes are different from each other and |
| 91278 | + ** from NULL. */ |
| 91279 | + h += 4093 + (p->flags & (MEM_Str|MEM_Blob)); |
| 90920 | 91280 | } |
| 90921 | 91281 | } |
| 90922 | 91282 | return h; |
| 90923 | 91283 | } |
| 90924 | 91284 | |
| | @@ -90964,10 +91324,11 @@ |
| 90964 | 91324 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 90965 | 91325 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 90966 | 91326 | Mem *pOut = 0; /* Output operand */ |
| 90967 | 91327 | #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) |
| 90968 | 91328 | u64 *pnCycle = 0; |
| 91329 | + int bStmtScanStatus = IS_STMT_SCANSTATUS(db)!=0; |
| 90969 | 91330 | #endif |
| 90970 | 91331 | /*** INSERT STACK UNION HERE ***/ |
| 90971 | 91332 | |
| 90972 | 91333 | assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ |
| 90973 | 91334 | if( DbMaskNonZero(p->lockMask) ){ |
| | @@ -91028,17 +91389,21 @@ |
| 91028 | 91389 | ** jumps to abort_due_to_error. */ |
| 91029 | 91390 | assert( rc==SQLITE_OK ); |
| 91030 | 91391 | |
| 91031 | 91392 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
| 91032 | 91393 | nVmStep++; |
| 91033 | | -#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) |
| 91394 | + |
| 91395 | +#if defined(VDBE_PROFILE) |
| 91034 | 91396 | pOp->nExec++; |
| 91035 | 91397 | pnCycle = &pOp->nCycle; |
| 91036 | | -# ifdef VDBE_PROFILE |
| 91037 | | - if( sqlite3NProfileCnt==0 ) |
| 91038 | | -# endif |
| 91398 | + if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime(); |
| 91399 | +#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 91400 | + if( bStmtScanStatus ){ |
| 91401 | + pOp->nExec++; |
| 91402 | + pnCycle = &pOp->nCycle; |
| 91039 | 91403 | *pnCycle -= sqlite3Hwtime(); |
| 91404 | + } |
| 91040 | 91405 | #endif |
| 91041 | 91406 | |
| 91042 | 91407 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 91043 | 91408 | */ |
| 91044 | 91409 | #ifdef SQLITE_DEBUG |
| | @@ -92848,10 +93213,16 @@ |
| 92848 | 93213 | ** from the value in that register. |
| 92849 | 93214 | ** |
| 92850 | 93215 | ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant |
| 92851 | 93216 | ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. |
| 92852 | 93217 | ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. |
| 93218 | +** |
| 93219 | +** WARNING: This opcode does not reliably distinguish between NULL and REAL |
| 93220 | +** when P1>=0. If the database contains a NaN value, this opcode will think |
| 93221 | +** that the datatype is REAL when it should be NULL. When P1<0 and the value |
| 93222 | +** is already stored in register P3, then this opcode does reliably |
| 93223 | +** distinguish between NULL and REAL. The problem only arises then P1>=0. |
| 92853 | 93224 | ** |
| 92854 | 93225 | ** Take the jump to address P2 if and only if the datatype of the |
| 92855 | 93226 | ** value determined by P1 and P3 corresponds to one of the bits in the |
| 92856 | 93227 | ** P5 bitmask. |
| 92857 | 93228 | ** |
| | @@ -95196,10 +95567,11 @@ |
| 95196 | 95567 | #endif |
| 95197 | 95568 | VdbeBranchTaken(0,3); |
| 95198 | 95569 | break; |
| 95199 | 95570 | } |
| 95200 | 95571 | nStep--; |
| 95572 | + pC->cacheStatus = CACHE_STALE; |
| 95201 | 95573 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
| 95202 | 95574 | if( rc ){ |
| 95203 | 95575 | if( rc==SQLITE_DONE ){ |
| 95204 | 95576 | rc = SQLITE_OK; |
| 95205 | 95577 | goto seekscan_search_fail; |
| | @@ -97848,10 +98220,11 @@ |
| 97848 | 98220 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
| 97849 | 98221 | goto abort_due_to_error; |
| 97850 | 98222 | } |
| 97851 | 98223 | sqlite3VdbeChangeEncoding(pMem, encoding); |
| 97852 | 98224 | UPDATE_MAX_BLOBSIZE(pMem); |
| 98225 | + REGISTER_TRACE((int)(pMem-aMem), pMem); |
| 97853 | 98226 | break; |
| 97854 | 98227 | } |
| 97855 | 98228 | |
| 97856 | 98229 | #ifndef SQLITE_OMIT_WAL |
| 97857 | 98230 | /* Opcode: Checkpoint P1 P2 P3 * * |
| | @@ -98986,12 +99359,14 @@ |
| 98986 | 99359 | |
| 98987 | 99360 | #if defined(VDBE_PROFILE) |
| 98988 | 99361 | *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
| 98989 | 99362 | pnCycle = 0; |
| 98990 | 99363 | #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 98991 | | - *pnCycle += sqlite3Hwtime(); |
| 98992 | | - pnCycle = 0; |
| 99364 | + if( pnCycle ){ |
| 99365 | + *pnCycle += sqlite3Hwtime(); |
| 99366 | + pnCycle = 0; |
| 99367 | + } |
| 98993 | 99368 | #endif |
| 98994 | 99369 | |
| 98995 | 99370 | /* The following code adds nothing to the actual functionality |
| 98996 | 99371 | ** of the program. It is only here for testing and debugging. |
| 98997 | 99372 | ** On the other hand, it does burn CPU cycles every time through |
| | @@ -104013,11 +104388,12 @@ |
| 104013 | 104388 | if( pParse->pTriggerTab!=0 ){ |
| 104014 | 104389 | int op = pParse->eTriggerOp; |
| 104015 | 104390 | assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); |
| 104016 | 104391 | if( pParse->bReturning ){ |
| 104017 | 104392 | if( (pNC->ncFlags & NC_UBaseReg)!=0 |
| 104018 | | - && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0) |
| 104393 | + && ALWAYS(zTab==0 |
| 104394 | + || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0) |
| 104019 | 104395 | ){ |
| 104020 | 104396 | pExpr->iTable = op!=TK_DELETE; |
| 104021 | 104397 | pTab = pParse->pTriggerTab; |
| 104022 | 104398 | } |
| 104023 | 104399 | }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){ |
| | @@ -104798,12 +105174,12 @@ |
| 104798 | 105174 | sqlite3WalkSelect(pWalker, pExpr->x.pSelect); |
| 104799 | 105175 | } |
| 104800 | 105176 | assert( pNC->nRef>=nRef ); |
| 104801 | 105177 | if( nRef!=pNC->nRef ){ |
| 104802 | 105178 | ExprSetProperty(pExpr, EP_VarSelect); |
| 104803 | | - pNC->ncFlags |= NC_VarSelect; |
| 104804 | 105179 | } |
| 105180 | + pNC->ncFlags |= NC_Subquery; |
| 104805 | 105181 | } |
| 104806 | 105182 | break; |
| 104807 | 105183 | } |
| 104808 | 105184 | case TK_VARIABLE: { |
| 104809 | 105185 | testcase( pNC->ncFlags & NC_IsCheck ); |
| | @@ -105987,15 +106363,14 @@ |
| 105987 | 106363 | if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ |
| 105988 | 106364 | p = p->pLeft; |
| 105989 | 106365 | }else{ |
| 105990 | 106366 | Expr *pNext = p->pRight; |
| 105991 | 106367 | /* The Expr.x union is never used at the same time as Expr.pRight */ |
| 105992 | | - assert( ExprUseXList(p) ); |
| 105993 | | - assert( p->x.pList==0 || p->pRight==0 ); |
| 105994 | | - if( p->x.pList!=0 && !db->mallocFailed ){ |
| 106368 | + assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 ); |
| 106369 | + if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){ |
| 105995 | 106370 | int i; |
| 105996 | | - for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){ |
| 106371 | + for(i=0; i<p->x.pList->nExpr; i++){ |
| 105997 | 106372 | if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ |
| 105998 | 106373 | pNext = p->x.pList->a[i].pExpr; |
| 105999 | 106374 | break; |
| 106000 | 106375 | } |
| 106001 | 106376 | } |
| | @@ -108359,11 +108734,11 @@ |
| 108359 | 108734 | |
| 108360 | 108735 | /* |
| 108361 | 108736 | ** pX is the RHS of an IN operator. If pX is a SELECT statement |
| 108362 | 108737 | ** that can be simplified to a direct table access, then return |
| 108363 | 108738 | ** a pointer to the SELECT statement. If pX is not a SELECT statement, |
| 108364 | | -** or if the SELECT statement needs to be manifested into a transient |
| 108739 | +** or if the SELECT statement needs to be materialized into a transient |
| 108365 | 108740 | ** table, then return NULL. |
| 108366 | 108741 | */ |
| 108367 | 108742 | #ifndef SQLITE_OMIT_SUBQUERY |
| 108368 | 108743 | static Select *isCandidateForInOpt(const Expr *pX){ |
| 108369 | 108744 | Select *p; |
| | @@ -108645,11 +109020,10 @@ |
| 108645 | 109020 | Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
| 108646 | 109021 | Expr *pRhs = pEList->a[i].pExpr; |
| 108647 | 109022 | CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
| 108648 | 109023 | int j; |
| 108649 | 109024 | |
| 108650 | | - assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr ); |
| 108651 | 109025 | for(j=0; j<nExpr; j++){ |
| 108652 | 109026 | if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; |
| 108653 | 109027 | assert( pIdx->azColl[j] ); |
| 108654 | 109028 | if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ |
| 108655 | 109029 | continue; |
| | @@ -109548,10 +109922,11 @@ |
| 109548 | 109922 | Column *pCol, /* The generated column */ |
| 109549 | 109923 | int regOut /* Put the result in this register */ |
| 109550 | 109924 | ){ |
| 109551 | 109925 | int iAddr; |
| 109552 | 109926 | Vdbe *v = pParse->pVdbe; |
| 109927 | + int nErr = pParse->nErr; |
| 109553 | 109928 | assert( v!=0 ); |
| 109554 | 109929 | assert( pParse->iSelfTab!=0 ); |
| 109555 | 109930 | if( pParse->iSelfTab>0 ){ |
| 109556 | 109931 | iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut); |
| 109557 | 109932 | }else{ |
| | @@ -109560,10 +109935,11 @@ |
| 109560 | 109935 | sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut); |
| 109561 | 109936 | if( pCol->affinity>=SQLITE_AFF_TEXT ){ |
| 109562 | 109937 | sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1); |
| 109563 | 109938 | } |
| 109564 | 109939 | if( iAddr ) sqlite3VdbeJumpHere(v, iAddr); |
| 109940 | + if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1; |
| 109565 | 109941 | } |
| 109566 | 109942 | #endif /* SQLITE_OMIT_GENERATED_COLUMNS */ |
| 109567 | 109943 | |
| 109568 | 109944 | /* |
| 109569 | 109945 | ** Generate code to extract the value of the iCol-th column of a table. |
| | @@ -109576,10 +109952,11 @@ |
| 109576 | 109952 | int regOut /* Extract the value into this register */ |
| 109577 | 109953 | ){ |
| 109578 | 109954 | Column *pCol; |
| 109579 | 109955 | assert( v!=0 ); |
| 109580 | 109956 | assert( pTab!=0 ); |
| 109957 | + assert( iCol!=XN_EXPR ); |
| 109581 | 109958 | if( iCol<0 || iCol==pTab->iPKey ){ |
| 109582 | 109959 | sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); |
| 109583 | 109960 | VdbeComment((v, "%s.rowid", pTab->zName)); |
| 109584 | 109961 | }else{ |
| 109585 | 109962 | int op; |
| | @@ -109928,11 +110305,23 @@ |
| 109928 | 110305 | switch( op ){ |
| 109929 | 110306 | case TK_AGG_COLUMN: { |
| 109930 | 110307 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 109931 | 110308 | struct AggInfo_col *pCol; |
| 109932 | 110309 | assert( pAggInfo!=0 ); |
| 109933 | | - assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 110310 | + assert( pExpr->iAgg>=0 ); |
| 110311 | + if( pExpr->iAgg>=pAggInfo->nColumn ){ |
| 110312 | + /* Happens when the left table of a RIGHT JOIN is null and |
| 110313 | + ** is using an expression index */ |
| 110314 | + sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
| 110315 | +#ifdef SQLITE_VDBE_COVERAGE |
| 110316 | + /* Verify that the OP_Null above is exercised by tests |
| 110317 | + ** tag-20230325-2 */ |
| 110318 | + sqlite3VdbeAddOp2(v, OP_NotNull, target, 1); |
| 110319 | + VdbeCoverageNeverTaken(v); |
| 110320 | +#endif |
| 110321 | + break; |
| 110322 | + } |
| 109934 | 110323 | pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 109935 | 110324 | if( !pAggInfo->directMode ){ |
| 109936 | 110325 | return AggInfoColumnReg(pAggInfo, pExpr->iAgg); |
| 109937 | 110326 | }else if( pAggInfo->useSortingIdx ){ |
| 109938 | 110327 | Table *pTab = pCol->pTab; |
| | @@ -110103,15 +110492,12 @@ |
| 110103 | 110492 | return pExpr->iTable; |
| 110104 | 110493 | } |
| 110105 | 110494 | #ifndef SQLITE_OMIT_CAST |
| 110106 | 110495 | case TK_CAST: { |
| 110107 | 110496 | /* Expressions of the form: CAST(pLeft AS token) */ |
| 110108 | | - inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 110109 | | - if( inReg!=target ){ |
| 110110 | | - sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
| 110111 | | - inReg = target; |
| 110112 | | - } |
| 110497 | + sqlite3ExprCode(pParse, pExpr->pLeft, target); |
| 110498 | + assert( inReg==target ); |
| 110113 | 110499 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 110114 | 110500 | sqlite3VdbeAddOp2(v, OP_Cast, target, |
| 110115 | 110501 | sqlite3AffinityType(pExpr->u.zToken, 0)); |
| 110116 | 110502 | return inReg; |
| 110117 | 110503 | } |
| | @@ -110446,17 +110832,13 @@ |
| 110446 | 110832 | ** "SOFT-COLLATE" that is added to constraints that are pushed down |
| 110447 | 110833 | ** from outer queries into sub-queries by the push-down optimization. |
| 110448 | 110834 | ** Clear subtypes as subtypes may not cross a subquery boundary. |
| 110449 | 110835 | */ |
| 110450 | 110836 | assert( pExpr->pLeft ); |
| 110451 | | - inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 110452 | | - if( inReg!=target ){ |
| 110453 | | - sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
| 110454 | | - inReg = target; |
| 110455 | | - } |
| 110456 | | - sqlite3VdbeAddOp1(v, OP_ClrSubtype, inReg); |
| 110457 | | - return inReg; |
| 110837 | + sqlite3ExprCode(pParse, pExpr->pLeft, target); |
| 110838 | + sqlite3VdbeAddOp1(v, OP_ClrSubtype, target); |
| 110839 | + return target; |
| 110458 | 110840 | }else{ |
| 110459 | 110841 | pExpr = pExpr->pLeft; |
| 110460 | 110842 | goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */ |
| 110461 | 110843 | } |
| 110462 | 110844 | } |
| | @@ -110562,16 +110944,13 @@ |
| 110562 | 110944 | ** (1) Temporarily disable factoring of constant expressions |
| 110563 | 110945 | ** (2) Make sure the computed value really is stored in register |
| 110564 | 110946 | ** "target" and not someplace else. |
| 110565 | 110947 | */ |
| 110566 | 110948 | pParse->okConstFactor = 0; /* note (1) above */ |
| 110567 | | - inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 110949 | + sqlite3ExprCode(pParse, pExpr->pLeft, target); |
| 110950 | + assert( target==inReg ); |
| 110568 | 110951 | pParse->okConstFactor = okConstFactor; |
| 110569 | | - if( inReg!=target ){ /* note (2) above */ |
| 110570 | | - sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
| 110571 | | - inReg = target; |
| 110572 | | - } |
| 110573 | 110952 | sqlite3VdbeJumpHere(v, addrINR); |
| 110574 | 110953 | break; |
| 110575 | 110954 | } |
| 110576 | 110955 | |
| 110577 | 110956 | /* |
| | @@ -110805,11 +111184,13 @@ |
| 110805 | 111184 | assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); |
| 110806 | 111185 | if( pParse->pVdbe==0 ) return; |
| 110807 | 111186 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 110808 | 111187 | if( inReg!=target ){ |
| 110809 | 111188 | u8 op; |
| 110810 | | - if( ALWAYS(pExpr) && ExprHasProperty(pExpr,EP_Subquery) ){ |
| 111189 | + if( ALWAYS(pExpr) |
| 111190 | + && (ExprHasProperty(pExpr,EP_Subquery) || pExpr->op==TK_REGISTER) |
| 111191 | + ){ |
| 110811 | 111192 | op = OP_Copy; |
| 110812 | 111193 | }else{ |
| 110813 | 111194 | op = OP_SCopy; |
| 110814 | 111195 | } |
| 110815 | 111196 | sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target); |
| | @@ -111990,23 +112371,26 @@ |
| 111990 | 112371 | ){ |
| 111991 | 112372 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 111992 | 112373 | int iAgg = pExpr->iAgg; |
| 111993 | 112374 | Parse *pParse = pWalker->pParse; |
| 111994 | 112375 | sqlite3 *db = pParse->db; |
| 112376 | + assert( iAgg>=0 ); |
| 111995 | 112377 | if( pExpr->op!=TK_AGG_FUNCTION ){ |
| 111996 | | - assert( iAgg>=0 && iAgg<pAggInfo->nColumn ); |
| 111997 | | - if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){ |
| 112378 | + if( ALWAYS(iAgg<pAggInfo->nColumn) |
| 112379 | + && pAggInfo->aCol[iAgg].pCExpr==pExpr |
| 112380 | + ){ |
| 111998 | 112381 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 111999 | 112382 | if( pExpr ){ |
| 112000 | 112383 | pAggInfo->aCol[iAgg].pCExpr = pExpr; |
| 112001 | 112384 | sqlite3ExprDeferredDelete(pParse, pExpr); |
| 112002 | 112385 | } |
| 112003 | 112386 | } |
| 112004 | 112387 | }else{ |
| 112005 | 112388 | assert( pExpr->op==TK_AGG_FUNCTION ); |
| 112006 | | - assert( iAgg>=0 && iAgg<pAggInfo->nFunc ); |
| 112007 | | - if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){ |
| 112389 | + if( ALWAYS(iAgg<pAggInfo->nFunc) |
| 112390 | + && pAggInfo->aFunc[iAgg].pFExpr==pExpr |
| 112391 | + ){ |
| 112008 | 112392 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 112009 | 112393 | if( pExpr ){ |
| 112010 | 112394 | pAggInfo->aFunc[iAgg].pFExpr = pExpr; |
| 112011 | 112395 | sqlite3ExprDeferredDelete(pParse, pExpr); |
| 112012 | 112396 | } |
| | @@ -112152,11 +112536,16 @@ |
| 112152 | 112536 | if( iDataCur<0 ) continue; |
| 112153 | 112537 | if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break; |
| 112154 | 112538 | } |
| 112155 | 112539 | if( pIEpr==0 ) break; |
| 112156 | 112540 | if( NEVER(!ExprUseYTab(pExpr)) ) break; |
| 112157 | | - if( pExpr->pAggInfo!=0 ) break; /* Already resolved by outer context */ |
| 112541 | + for(i=0; i<pSrcList->nSrc; i++){ |
| 112542 | + if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break; |
| 112543 | + } |
| 112544 | + if( i>=pSrcList->nSrc ) break; |
| 112545 | + if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */ |
| 112546 | + if( pParse->nErr ){ return WRC_Abort; } |
| 112158 | 112547 | |
| 112159 | 112548 | /* If we reach this point, it means that expression pExpr can be |
| 112160 | 112549 | ** translated into a reference to an index column as described by |
| 112161 | 112550 | ** pIEpr. |
| 112162 | 112551 | */ |
| | @@ -112163,10 +112552,13 @@ |
| 112163 | 112552 | memset(&tmp, 0, sizeof(tmp)); |
| 112164 | 112553 | tmp.op = TK_AGG_COLUMN; |
| 112165 | 112554 | tmp.iTable = pIEpr->iIdxCur; |
| 112166 | 112555 | tmp.iColumn = pIEpr->iIdxCol; |
| 112167 | 112556 | findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp); |
| 112557 | + if( pParse->nErr ){ return WRC_Abort; } |
| 112558 | + assert( pAggInfo->aCol!=0 ); |
| 112559 | + assert( tmp.iAgg<pAggInfo->nColumn ); |
| 112168 | 112560 | pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr; |
| 112169 | 112561 | pExpr->pAggInfo = pAggInfo; |
| 112170 | 112562 | pExpr->iAgg = tmp.iAgg; |
| 112171 | 112563 | return WRC_Prune; |
| 112172 | 112564 | } |
| | @@ -112339,10 +112731,41 @@ |
| 112339 | 112731 | SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){ |
| 112340 | 112732 | pParse->nTempReg = 0; |
| 112341 | 112733 | pParse->nRangeReg = 0; |
| 112342 | 112734 | } |
| 112343 | 112735 | |
| 112736 | +/* |
| 112737 | +** Make sure sufficient registers have been allocated so that |
| 112738 | +** iReg is a valid register number. |
| 112739 | +*/ |
| 112740 | +SQLITE_PRIVATE void sqlite3TouchRegister(Parse *pParse, int iReg){ |
| 112741 | + if( pParse->nMem<iReg ) pParse->nMem = iReg; |
| 112742 | +} |
| 112743 | + |
| 112744 | +#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG) |
| 112745 | +/* |
| 112746 | +** Return the latest reusable register in the set of all registers. |
| 112747 | +** The value returned is no less than iMin. If any register iMin or |
| 112748 | +** greater is in permanent use, then return one more than that last |
| 112749 | +** permanent register. |
| 112750 | +*/ |
| 112751 | +SQLITE_PRIVATE int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){ |
| 112752 | + const ExprList *pList = pParse->pConstExpr; |
| 112753 | + if( pList ){ |
| 112754 | + int i; |
| 112755 | + for(i=0; i<pList->nExpr; i++){ |
| 112756 | + if( pList->a[i].u.iConstExprReg>=iMin ){ |
| 112757 | + iMin = pList->a[i].u.iConstExprReg + 1; |
| 112758 | + } |
| 112759 | + } |
| 112760 | + } |
| 112761 | + pParse->nTempReg = 0; |
| 112762 | + pParse->nRangeReg = 0; |
| 112763 | + return iMin; |
| 112764 | +} |
| 112765 | +#endif /* SQLITE_ENABLE_STAT4 || SQLITE_DEBUG */ |
| 112766 | + |
| 112344 | 112767 | /* |
| 112345 | 112768 | ** Validate that no temporary register falls within the range of |
| 112346 | 112769 | ** iFirst..iLast, inclusive. This routine is only call from within assert() |
| 112347 | 112770 | ** statements. |
| 112348 | 112771 | */ |
| | @@ -112357,10 +112780,18 @@ |
| 112357 | 112780 | } |
| 112358 | 112781 | for(i=0; i<pParse->nTempReg; i++){ |
| 112359 | 112782 | if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ |
| 112360 | 112783 | return 0; |
| 112361 | 112784 | } |
| 112785 | + } |
| 112786 | + if( pParse->pConstExpr ){ |
| 112787 | + ExprList *pList = pParse->pConstExpr; |
| 112788 | + for(i=0; i<pList->nExpr; i++){ |
| 112789 | + int iReg = pList->a[i].u.iConstExprReg; |
| 112790 | + if( iReg==0 ) continue; |
| 112791 | + if( iReg>=iFirst && iReg<=iLast ) return 0; |
| 112792 | + } |
| 112362 | 112793 | } |
| 112363 | 112794 | return 1; |
| 112364 | 112795 | } |
| 112365 | 112796 | #endif /* SQLITE_DEBUG */ |
| 112366 | 112797 | |
| | @@ -115641,15 +116072,19 @@ |
| 115641 | 116072 | int regTemp2 = iMem++; /* Second temporary use register */ |
| 115642 | 116073 | int regTabname = iMem++; /* Register containing table name */ |
| 115643 | 116074 | int regIdxname = iMem++; /* Register containing index name */ |
| 115644 | 116075 | int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ |
| 115645 | 116076 | int regPrev = iMem; /* MUST BE LAST (see below) */ |
| 116077 | +#ifdef SQLITE_ENABLE_STAT4 |
| 116078 | + int doOnce = 1; /* Flag for a one-time computation */ |
| 116079 | +#endif |
| 115646 | 116080 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 115647 | 116081 | Table *pStat1 = 0; |
| 115648 | 116082 | #endif |
| 115649 | 116083 | |
| 115650 | | - pParse->nMem = MAX(pParse->nMem, iMem); |
| 116084 | + sqlite3TouchRegister(pParse, iMem); |
| 116085 | + assert( sqlite3NoTempsInRange(pParse, regNewRowid, iMem) ); |
| 115651 | 116086 | v = sqlite3GetVdbe(pParse); |
| 115652 | 116087 | if( v==0 || NEVER(pTab==0) ){ |
| 115653 | 116088 | return; |
| 115654 | 116089 | } |
| 115655 | 116090 | if( !IsOrdinaryTable(pTab) ){ |
| | @@ -115751,11 +116186,11 @@ |
| 115751 | 116186 | |
| 115752 | 116187 | /* Make sure there are enough memory cells allocated to accommodate |
| 115753 | 116188 | ** the regPrev array and a trailing rowid (the rowid slot is required |
| 115754 | 116189 | ** when building a record to insert into the sample column of |
| 115755 | 116190 | ** the sqlite_stat4 table. */ |
| 115756 | | - pParse->nMem = MAX(pParse->nMem, regPrev+nColTest); |
| 116191 | + sqlite3TouchRegister(pParse, regPrev+nColTest); |
| 115757 | 116192 | |
| 115758 | 116193 | /* Open a read-only cursor on the index being analyzed. */ |
| 115759 | 116194 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
| 115760 | 116195 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb); |
| 115761 | 116196 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| | @@ -115923,11 +116358,39 @@ |
| 115923 | 116358 | int regSampleRowid = regCol + nCol; |
| 115924 | 116359 | int addrNext; |
| 115925 | 116360 | int addrIsNull; |
| 115926 | 116361 | u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; |
| 115927 | 116362 | |
| 115928 | | - pParse->nMem = MAX(pParse->nMem, regCol+nCol); |
| 116363 | + if( doOnce ){ |
| 116364 | + int mxCol = nCol; |
| 116365 | + Index *pX; |
| 116366 | + |
| 116367 | + /* Compute the maximum number of columns in any index */ |
| 116368 | + for(pX=pTab->pIndex; pX; pX=pX->pNext){ |
| 116369 | + int nColX; /* Number of columns in pX */ |
| 116370 | + if( !HasRowid(pTab) && IsPrimaryKeyIndex(pX) ){ |
| 116371 | + nColX = pX->nKeyCol; |
| 116372 | + }else{ |
| 116373 | + nColX = pX->nColumn; |
| 116374 | + } |
| 116375 | + if( nColX>mxCol ) mxCol = nColX; |
| 116376 | + } |
| 116377 | + |
| 116378 | + /* Allocate space to compute results for the largest index */ |
| 116379 | + sqlite3TouchRegister(pParse, regCol+mxCol); |
| 116380 | + doOnce = 0; |
| 116381 | +#ifdef SQLITE_DEBUG |
| 116382 | + /* Verify that the call to sqlite3ClearTempRegCache() below |
| 116383 | + ** really is needed. |
| 116384 | + ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25) |
| 116385 | + */ |
| 116386 | + testcase( !sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) ); |
| 116387 | +#endif |
| 116388 | + sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */ |
| 116389 | + assert( sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) ); |
| 116390 | + } |
| 116391 | + assert( sqlite3NoTempsInRange(pParse, regEq, regCol+nCol) ); |
| 115929 | 116392 | |
| 115930 | 116393 | addrNext = sqlite3VdbeCurrentAddr(v); |
| 115931 | 116394 | callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid); |
| 115932 | 116395 | addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); |
| 115933 | 116396 | VdbeCoverage(v); |
| | @@ -116004,10 +116467,15 @@ |
| 116004 | 116467 | iTab = pParse->nTab; |
| 116005 | 116468 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 116006 | 116469 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 116007 | 116470 | Table *pTab = (Table*)sqliteHashData(k); |
| 116008 | 116471 | analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab); |
| 116472 | +#ifdef SQLITE_ENABLE_STAT4 |
| 116473 | + iMem = sqlite3FirstAvailableRegister(pParse, iMem); |
| 116474 | +#else |
| 116475 | + assert( iMem==sqlite3FirstAvailableRegister(pParse,iMem) ); |
| 116476 | +#endif |
| 116009 | 116477 | } |
| 116010 | 116478 | loadAnalysis(pParse, iDb); |
| 116011 | 116479 | } |
| 116012 | 116480 | |
| 116013 | 116481 | /* |
| | @@ -118931,11 +119399,11 @@ |
| 118931 | 119399 | Hash *pHash; |
| 118932 | 119400 | sqlite3 *db = pParse->db; |
| 118933 | 119401 | if( pParse->pNewTrigger ){ |
| 118934 | 119402 | sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger"); |
| 118935 | 119403 | }else{ |
| 118936 | | - assert( pParse->bReturning==0 ); |
| 119404 | + assert( pParse->bReturning==0 || pParse->ifNotExists ); |
| 118937 | 119405 | } |
| 118938 | 119406 | pParse->bReturning = 1; |
| 118939 | 119407 | pRet = sqlite3DbMallocZero(db, sizeof(*pRet)); |
| 118940 | 119408 | if( pRet==0 ){ |
| 118941 | 119409 | sqlite3ExprListDelete(db, pList); |
| | @@ -118957,11 +119425,12 @@ |
| 118957 | 119425 | pRet->retTrig.step_list = &pRet->retTStep; |
| 118958 | 119426 | pRet->retTStep.op = TK_RETURNING; |
| 118959 | 119427 | pRet->retTStep.pTrig = &pRet->retTrig; |
| 118960 | 119428 | pRet->retTStep.pExprList = pList; |
| 118961 | 119429 | pHash = &(db->aDb[1].pSchema->trigHash); |
| 118962 | | - assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr ); |
| 119430 | + assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 |
| 119431 | + || pParse->nErr || pParse->ifNotExists ); |
| 118963 | 119432 | if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig) |
| 118964 | 119433 | ==&pRet->retTrig ){ |
| 118965 | 119434 | sqlite3OomFault(db); |
| 118966 | 119435 | } |
| 118967 | 119436 | } |
| | @@ -123830,17 +124299,19 @@ |
| 123830 | 124299 | ** |
| 123831 | 124300 | ** If pTab is not writable -> generate an error message and return 1. |
| 123832 | 124301 | ** If pTab is writable but other errors have occurred -> return 1. |
| 123833 | 124302 | ** If pTab is writable and no prior errors -> return 0; |
| 123834 | 124303 | */ |
| 123835 | | -SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ |
| 124304 | +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){ |
| 123836 | 124305 | if( tabIsReadOnly(pParse, pTab) ){ |
| 123837 | 124306 | sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); |
| 123838 | 124307 | return 1; |
| 123839 | 124308 | } |
| 123840 | 124309 | #ifndef SQLITE_OMIT_VIEW |
| 123841 | | - if( !viewOk && IsView(pTab) ){ |
| 124310 | + if( IsView(pTab) |
| 124311 | + && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0)) |
| 124312 | + ){ |
| 123842 | 124313 | sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); |
| 123843 | 124314 | return 1; |
| 123844 | 124315 | } |
| 123845 | 124316 | #endif |
| 123846 | 124317 | return 0; |
| | @@ -124090,11 +124561,11 @@ |
| 124090 | 124561 | */ |
| 124091 | 124562 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 124092 | 124563 | goto delete_from_cleanup; |
| 124093 | 124564 | } |
| 124094 | 124565 | |
| 124095 | | - if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){ |
| 124566 | + if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ |
| 124096 | 124567 | goto delete_from_cleanup; |
| 124097 | 124568 | } |
| 124098 | 124569 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 124099 | 124570 | assert( iDb<db->nDb ); |
| 124100 | 124571 | rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, |
| | @@ -124199,11 +124670,11 @@ |
| 124199 | 124670 | } |
| 124200 | 124671 | }else |
| 124201 | 124672 | #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ |
| 124202 | 124673 | { |
| 124203 | 124674 | u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK; |
| 124204 | | - if( sNC.ncFlags & NC_VarSelect ) bComplex = 1; |
| 124675 | + if( sNC.ncFlags & NC_Subquery ) bComplex = 1; |
| 124205 | 124676 | wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW); |
| 124206 | 124677 | if( HasRowid(pTab) ){ |
| 124207 | 124678 | /* For a rowid table, initialize the RowSet to an empty set */ |
| 124208 | 124679 | pPk = 0; |
| 124209 | 124680 | nPk = 1; |
| | @@ -128787,49 +129258,51 @@ |
| 128787 | 129258 | ** |
| 128788 | 129259 | ** Memory for the buffer containing the column index affinity string |
| 128789 | 129260 | ** is managed along with the rest of the Index structure. It will be |
| 128790 | 129261 | ** released when sqlite3DeleteIndex() is called. |
| 128791 | 129262 | */ |
| 128792 | | -SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ |
| 128793 | | - if( !pIdx->zColAff ){ |
| 128794 | | - /* The first time a column affinity string for a particular index is |
| 128795 | | - ** required, it is allocated and populated here. It is then stored as |
| 128796 | | - ** a member of the Index structure for subsequent use. |
| 128797 | | - ** |
| 128798 | | - ** The column affinity string will eventually be deleted by |
| 128799 | | - ** sqliteDeleteIndex() when the Index structure itself is cleaned |
| 128800 | | - ** up. |
| 128801 | | - */ |
| 128802 | | - int n; |
| 128803 | | - Table *pTab = pIdx->pTable; |
| 128804 | | - pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); |
| 128805 | | - if( !pIdx->zColAff ){ |
| 128806 | | - sqlite3OomFault(db); |
| 128807 | | - return 0; |
| 128808 | | - } |
| 128809 | | - for(n=0; n<pIdx->nColumn; n++){ |
| 128810 | | - i16 x = pIdx->aiColumn[n]; |
| 128811 | | - char aff; |
| 128812 | | - if( x>=0 ){ |
| 128813 | | - aff = pTab->aCol[x].affinity; |
| 128814 | | - }else if( x==XN_ROWID ){ |
| 128815 | | - aff = SQLITE_AFF_INTEGER; |
| 128816 | | - }else{ |
| 128817 | | - assert( x==XN_EXPR ); |
| 128818 | | - assert( pIdx->bHasExpr ); |
| 128819 | | - assert( pIdx->aColExpr!=0 ); |
| 128820 | | - aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
| 128821 | | - } |
| 128822 | | - if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB; |
| 128823 | | - if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC; |
| 128824 | | - pIdx->zColAff[n] = aff; |
| 128825 | | - } |
| 128826 | | - pIdx->zColAff[n] = 0; |
| 128827 | | - } |
| 128828 | | - |
| 129263 | +static SQLITE_NOINLINE const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){ |
| 129264 | + /* The first time a column affinity string for a particular index is |
| 129265 | + ** required, it is allocated and populated here. It is then stored as |
| 129266 | + ** a member of the Index structure for subsequent use. |
| 129267 | + ** |
| 129268 | + ** The column affinity string will eventually be deleted by |
| 129269 | + ** sqliteDeleteIndex() when the Index structure itself is cleaned |
| 129270 | + ** up. |
| 129271 | + */ |
| 129272 | + int n; |
| 129273 | + Table *pTab = pIdx->pTable; |
| 129274 | + pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); |
| 129275 | + if( !pIdx->zColAff ){ |
| 129276 | + sqlite3OomFault(db); |
| 129277 | + return 0; |
| 129278 | + } |
| 129279 | + for(n=0; n<pIdx->nColumn; n++){ |
| 129280 | + i16 x = pIdx->aiColumn[n]; |
| 129281 | + char aff; |
| 129282 | + if( x>=0 ){ |
| 129283 | + aff = pTab->aCol[x].affinity; |
| 129284 | + }else if( x==XN_ROWID ){ |
| 129285 | + aff = SQLITE_AFF_INTEGER; |
| 129286 | + }else{ |
| 129287 | + assert( x==XN_EXPR ); |
| 129288 | + assert( pIdx->bHasExpr ); |
| 129289 | + assert( pIdx->aColExpr!=0 ); |
| 129290 | + aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
| 129291 | + } |
| 129292 | + if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB; |
| 129293 | + if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC; |
| 129294 | + pIdx->zColAff[n] = aff; |
| 129295 | + } |
| 129296 | + pIdx->zColAff[n] = 0; |
| 129297 | + return pIdx->zColAff; |
| 129298 | +} |
| 129299 | +SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ |
| 129300 | + if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx); |
| 128829 | 129301 | return pIdx->zColAff; |
| 128830 | 129302 | } |
| 129303 | + |
| 128831 | 129304 | |
| 128832 | 129305 | /* |
| 128833 | 129306 | ** Compute an affinity string for a table. Space is obtained |
| 128834 | 129307 | ** from sqlite3DbMalloc(). The caller is responsible for freeing |
| 128835 | 129308 | ** the space when done. |
| | @@ -129511,11 +129984,11 @@ |
| 129511 | 129984 | goto insert_cleanup; |
| 129512 | 129985 | } |
| 129513 | 129986 | |
| 129514 | 129987 | /* Cannot insert into a read-only table. |
| 129515 | 129988 | */ |
| 129516 | | - if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 129989 | + if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ |
| 129517 | 129990 | goto insert_cleanup; |
| 129518 | 129991 | } |
| 129519 | 129992 | |
| 129520 | 129993 | /* Allocate a VDBE |
| 129521 | 129994 | */ |
| | @@ -129958,11 +130431,11 @@ |
| 129958 | 130431 | sqlite3VdbeJumpHere(v, addr1); |
| 129959 | 130432 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); |
| 129960 | 130433 | } |
| 129961 | 130434 | |
| 129962 | 130435 | /* Copy the new data already generated. */ |
| 129963 | | - assert( pTab->nNVCol>0 ); |
| 130436 | + assert( pTab->nNVCol>0 || pParse->nErr>0 ); |
| 129964 | 130437 | sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1); |
| 129965 | 130438 | |
| 129966 | 130439 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 129967 | 130440 | /* Compute the new value for generated columns after all other |
| 129968 | 130441 | ** columns have already been computed. This must be done after |
| | @@ -133321,19 +133794,25 @@ |
| 133321 | 133794 | zEntry = zProc ? zProc : "sqlite3_extension_init"; |
| 133322 | 133795 | |
| 133323 | 133796 | /* tag-20210611-1. Some dlopen() implementations will segfault if given |
| 133324 | 133797 | ** an oversize filename. Most filesystems have a pathname limit of 4K, |
| 133325 | 133798 | ** so limit the extension filename length to about twice that. |
| 133326 | | - ** https://sqlite.org/forum/forumpost/08a0d6d9bf */ |
| 133799 | + ** https://sqlite.org/forum/forumpost/08a0d6d9bf |
| 133800 | + ** |
| 133801 | + ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix. |
| 133802 | + ** See https://sqlite.org/forum/forumpost/24083b579d. |
| 133803 | + */ |
| 133327 | 133804 | if( nMsg>SQLITE_MAX_PATHLEN ) goto extension_not_found; |
| 133328 | 133805 | |
| 133329 | 133806 | handle = sqlite3OsDlOpen(pVfs, zFile); |
| 133330 | 133807 | #if SQLITE_OS_UNIX || SQLITE_OS_WIN |
| 133331 | 133808 | for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){ |
| 133332 | 133809 | char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]); |
| 133333 | 133810 | if( zAltFile==0 ) return SQLITE_NOMEM_BKPT; |
| 133334 | | - handle = sqlite3OsDlOpen(pVfs, zAltFile); |
| 133811 | + if( nMsg+strlen(azEndings[ii])+1<=SQLITE_MAX_PATHLEN ){ |
| 133812 | + handle = sqlite3OsDlOpen(pVfs, zAltFile); |
| 133813 | + } |
| 133335 | 133814 | sqlite3_free(zAltFile); |
| 133336 | 133815 | } |
| 133337 | 133816 | #endif |
| 133338 | 133817 | if( handle==0 ) goto extension_not_found; |
| 133339 | 133818 | xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
| | @@ -135824,11 +136303,11 @@ |
| 135824 | 136303 | if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue; |
| 135825 | 136304 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 135826 | 136305 | zDb = db->aDb[iDb].zDbSName; |
| 135827 | 136306 | sqlite3CodeVerifySchema(pParse, iDb); |
| 135828 | 136307 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 135829 | | - if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; |
| 136308 | + sqlite3TouchRegister(pParse, pTab->nCol+regRow); |
| 135830 | 136309 | sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); |
| 135831 | 136310 | sqlite3VdbeLoadString(v, regResult, pTab->zName); |
| 135832 | 136311 | assert( IsOrdinaryTable(pTab) ); |
| 135833 | 136312 | for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ |
| 135834 | 136313 | pParent = sqlite3FindTable(db, pFK->zTo, zDb); |
| | @@ -135865,11 +136344,11 @@ |
| 135865 | 136344 | |
| 135866 | 136345 | /* Generate code to read the child key values into registers |
| 135867 | 136346 | ** regRow..regRow+n. If any of the child key values are NULL, this |
| 135868 | 136347 | ** row cannot cause an FK violation. Jump directly to addrOk in |
| 135869 | 136348 | ** this case. */ |
| 135870 | | - if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol; |
| 136349 | + sqlite3TouchRegister(pParse, regRow + pFK->nCol); |
| 135871 | 136350 | for(j=0; j<pFK->nCol; j++){ |
| 135872 | 136351 | int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom; |
| 135873 | 136352 | sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j); |
| 135874 | 136353 | sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); |
| 135875 | 136354 | } |
| | @@ -135994,10 +136473,11 @@ |
| 135994 | 136473 | |
| 135995 | 136474 | if( OMIT_TEMPDB && i==1 ) continue; |
| 135996 | 136475 | if( iDb>=0 && i!=iDb ) continue; |
| 135997 | 136476 | |
| 135998 | 136477 | sqlite3CodeVerifySchema(pParse, i); |
| 136478 | + pParse->okConstFactor = 0; /* tag-20230327-1 */ |
| 135999 | 136479 | |
| 136000 | 136480 | /* Do an integrity check of the B-Tree |
| 136001 | 136481 | ** |
| 136002 | 136482 | ** Begin by finding the root pages numbers |
| 136003 | 136483 | ** for all tables and indices in the database. |
| | @@ -136029,11 +136509,11 @@ |
| 136029 | 136509 | } |
| 136030 | 136510 | } |
| 136031 | 136511 | aRoot[0] = cnt; |
| 136032 | 136512 | |
| 136033 | 136513 | /* Make sure sufficient number of registers have been allocated */ |
| 136034 | | - pParse->nMem = MAX( pParse->nMem, 8+mxIdx ); |
| 136514 | + sqlite3TouchRegister(pParse, 8+mxIdx); |
| 136035 | 136515 | sqlite3ClearTempRegCache(pParse); |
| 136036 | 136516 | |
| 136037 | 136517 | /* Do the b-tree integrity checks */ |
| 136038 | 136518 | sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY); |
| 136039 | 136519 | sqlite3VdbeChangeP5(v, (u8)i); |
| | @@ -136179,19 +136659,33 @@ |
| 136179 | 136659 | |
| 136180 | 136660 | labelError = sqlite3VdbeMakeLabel(pParse); |
| 136181 | 136661 | labelOk = sqlite3VdbeMakeLabel(pParse); |
| 136182 | 136662 | if( pCol->notNull ){ |
| 136183 | 136663 | /* (1) NOT NULL columns may not contain a NULL */ |
| 136664 | + int jmp3; |
| 136184 | 136665 | int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); |
| 136185 | | - sqlite3VdbeChangeP5(v, 0x0f); |
| 136186 | 136666 | VdbeCoverage(v); |
| 136667 | + if( p1<0 ){ |
| 136668 | + sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */ |
| 136669 | + jmp3 = jmp2; |
| 136670 | + }else{ |
| 136671 | + sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */ |
| 136672 | + /* OP_IsType does not detect NaN values in the database file |
| 136673 | + ** which should be treated as a NULL. So if the header type |
| 136674 | + ** is REAL, we have to load the actual data using OP_Column |
| 136675 | + ** to reliably determine if the value is a NULL. */ |
| 136676 | + sqlite3VdbeAddOp3(v, OP_Column, p1, p3, 3); |
| 136677 | + jmp3 = sqlite3VdbeAddOp2(v, OP_NotNull, 3, labelOk); |
| 136678 | + VdbeCoverage(v); |
| 136679 | + } |
| 136187 | 136680 | zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| 136188 | 136681 | pCol->zCnName); |
| 136189 | 136682 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 136190 | 136683 | if( doTypeCheck ){ |
| 136191 | 136684 | sqlite3VdbeGoto(v, labelError); |
| 136192 | 136685 | sqlite3VdbeJumpHere(v, jmp2); |
| 136686 | + sqlite3VdbeJumpHere(v, jmp3); |
| 136193 | 136687 | }else{ |
| 136194 | 136688 | /* VDBE byte code will fall thru */ |
| 136195 | 136689 | } |
| 136196 | 136690 | } |
| 136197 | 136691 | if( bStrict && doTypeCheck ){ |
| | @@ -136295,11 +136789,11 @@ |
| 136295 | 136789 | ** any extra bytes at the end. Verify that this is the case. */ |
| 136296 | 136790 | if( HasRowid(pTab) ){ |
| 136297 | 136791 | int jmp7; |
| 136298 | 136792 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur+j, 3); |
| 136299 | 136793 | jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1); |
| 136300 | | - VdbeCoverage(v); |
| 136794 | + VdbeCoverageNeverNull(v); |
| 136301 | 136795 | sqlite3VdbeLoadString(v, 3, |
| 136302 | 136796 | "rowid not at end-of-record for row "); |
| 136303 | 136797 | sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); |
| 136304 | 136798 | sqlite3VdbeLoadString(v, 4, " of index "); |
| 136305 | 136799 | sqlite3VdbeGoto(v, jmp5-1); |
| | @@ -142129,11 +142623,13 @@ |
| 142129 | 142623 | assert( pExpr->pRight==0 ); |
| 142130 | 142624 | if( sqlite3ExprIsVector(pCopy) ){ |
| 142131 | 142625 | sqlite3VectorErrorMsg(pSubst->pParse, pCopy); |
| 142132 | 142626 | }else{ |
| 142133 | 142627 | sqlite3 *db = pSubst->pParse->db; |
| 142134 | | - if( pSubst->isOuterJoin ){ |
| 142628 | + if( pSubst->isOuterJoin |
| 142629 | + && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable) |
| 142630 | + ){ |
| 142135 | 142631 | memset(&ifNullRow, 0, sizeof(ifNullRow)); |
| 142136 | 142632 | ifNullRow.op = TK_IF_NULL_ROW; |
| 142137 | 142633 | ifNullRow.pLeft = pCopy; |
| 142138 | 142634 | ifNullRow.iTable = pSubst->iNewTable; |
| 142139 | 142635 | ifNullRow.iColumn = -99; |
| | @@ -142506,12 +143002,11 @@ |
| 142506 | 143002 | ** (17f) the subquery must not be the RHS of a LEFT JOIN. |
| 142507 | 143003 | ** (17g) either the subquery is the first element of the outer |
| 142508 | 143004 | ** query or there are no RIGHT or FULL JOINs in any arm |
| 142509 | 143005 | ** of the subquery. (This is a duplicate of condition (27b).) |
| 142510 | 143006 | ** (17h) The corresponding result set expressions in all arms of the |
| 142511 | | -** compound must have the same affinity. (See restriction (9) |
| 142512 | | -** on the push-down optimization.) |
| 143007 | +** compound must have the same affinity. |
| 142513 | 143008 | ** |
| 142514 | 143009 | ** The parent and sub-query may contain WHERE clauses. Subject to |
| 142515 | 143010 | ** rules (11), (13) and (14), they may also contain ORDER BY, |
| 142516 | 143011 | ** LIMIT and OFFSET clauses. The subquery cannot use any compound |
| 142517 | 143012 | ** operator other than UNION ALL because all the other compound |
| | @@ -143375,14 +143870,10 @@ |
| 143375 | 143870 | ** |
| 143376 | 143871 | ** (8) If the subquery is a compound that uses UNION, INTERSECT, |
| 143377 | 143872 | ** or EXCEPT, then all of the result set columns for all arms of |
| 143378 | 143873 | ** the compound must use the BINARY collating sequence. |
| 143379 | 143874 | ** |
| 143380 | | -** (9) If the subquery is a compound, then all arms of the compound must |
| 143381 | | -** have the same affinity. (This is the same as restriction (17h) |
| 143382 | | -** for query flattening.) |
| 143383 | | -** |
| 143384 | 143875 | ** |
| 143385 | 143876 | ** Return 0 if no changes are made and non-zero if one or more WHERE clause |
| 143386 | 143877 | ** terms are duplicated into the subquery. |
| 143387 | 143878 | */ |
| 143388 | 143879 | static int pushDownWhereTerms( |
| | @@ -143409,13 +143900,10 @@ |
| 143409 | 143900 | } |
| 143410 | 143901 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 143411 | 143902 | if( pSel->pWin ) return 0; /* restriction (6b) */ |
| 143412 | 143903 | #endif |
| 143413 | 143904 | } |
| 143414 | | - if( compoundHasDifferentAffinities(pSubq) ){ |
| 143415 | | - return 0; /* restriction (9) */ |
| 143416 | | - } |
| 143417 | 143905 | if( notUnionAll ){ |
| 143418 | 143906 | /* If any of the compound arms are connected using UNION, INTERSECT, |
| 143419 | 143907 | ** or EXCEPT, then we must ensure that none of the columns use a |
| 143420 | 143908 | ** non-BINARY collating sequence. */ |
| 143421 | 143909 | for(pSel=pSubq; pSel; pSel=pSel->pPrior){ |
| | @@ -143502,10 +143990,80 @@ |
| 143502 | 143990 | } |
| 143503 | 143991 | } |
| 143504 | 143992 | return nChng; |
| 143505 | 143993 | } |
| 143506 | 143994 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 143995 | + |
| 143996 | +/* |
| 143997 | +** Check to see if a subquery contains result-set columns that are |
| 143998 | +** never used. If it does, change the value of those result-set columns |
| 143999 | +** to NULL so that they do not cause unnecessary work to compute. |
| 144000 | +** |
| 144001 | +** Return the number of column that were changed to NULL. |
| 144002 | +*/ |
| 144003 | +static int disableUnusedSubqueryResultColumns(SrcItem *pItem){ |
| 144004 | + int nCol; |
| 144005 | + Select *pSub; /* The subquery to be simplified */ |
| 144006 | + Select *pX; /* For looping over compound elements of pSub */ |
| 144007 | + Table *pTab; /* The table that describes the subquery */ |
| 144008 | + int j; /* Column number */ |
| 144009 | + int nChng = 0; /* Number of columns converted to NULL */ |
| 144010 | + Bitmask colUsed; /* Columns that may not be NULLed out */ |
| 144011 | + |
| 144012 | + assert( pItem!=0 ); |
| 144013 | + if( pItem->fg.isCorrelated || pItem->fg.isCte ){ |
| 144014 | + return 0; |
| 144015 | + } |
| 144016 | + assert( pItem->pTab!=0 ); |
| 144017 | + pTab = pItem->pTab; |
| 144018 | + assert( pItem->pSelect!=0 ); |
| 144019 | + pSub = pItem->pSelect; |
| 144020 | + assert( pSub->pEList->nExpr==pTab->nCol ); |
| 144021 | + if( (pSub->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){ |
| 144022 | + testcase( pSub->selFlags & SF_Distinct ); |
| 144023 | + testcase( pSub->selFlags & SF_Aggregate ); |
| 144024 | + return 0; |
| 144025 | + } |
| 144026 | + for(pX=pSub; pX; pX=pX->pPrior){ |
| 144027 | + if( pX->pPrior && pX->op!=TK_ALL ){ |
| 144028 | + /* This optimization does not work for compound subqueries that |
| 144029 | + ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */ |
| 144030 | + return 0; |
| 144031 | + } |
| 144032 | + if( pX->pWin ){ |
| 144033 | + /* This optimization does not work for subqueries that use window |
| 144034 | + ** functions. */ |
| 144035 | + return 0; |
| 144036 | + } |
| 144037 | + } |
| 144038 | + colUsed = pItem->colUsed; |
| 144039 | + if( pSub->pOrderBy ){ |
| 144040 | + ExprList *pList = pSub->pOrderBy; |
| 144041 | + for(j=0; j<pList->nExpr; j++){ |
| 144042 | + u16 iCol = pList->a[j].u.x.iOrderByCol; |
| 144043 | + if( iCol>0 ){ |
| 144044 | + iCol--; |
| 144045 | + colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); |
| 144046 | + } |
| 144047 | + } |
| 144048 | + } |
| 144049 | + nCol = pTab->nCol; |
| 144050 | + for(j=0; j<nCol; j++){ |
| 144051 | + Bitmask m = j<BMS-1 ? MASKBIT(j) : TOPBIT; |
| 144052 | + if( (m & colUsed)!=0 ) continue; |
| 144053 | + for(pX=pSub; pX; pX=pX->pPrior) { |
| 144054 | + Expr *pY = pX->pEList->a[j].pExpr; |
| 144055 | + if( pY->op==TK_NULL ) continue; |
| 144056 | + pY->op = TK_NULL; |
| 144057 | + ExprClearProperty(pY, EP_Skip|EP_Unlikely); |
| 144058 | + pX->selFlags |= SF_PushDown; |
| 144059 | + nChng++; |
| 144060 | + } |
| 144061 | + } |
| 144062 | + return nChng; |
| 144063 | +} |
| 144064 | + |
| 143507 | 144065 | |
| 143508 | 144066 | /* |
| 143509 | 144067 | ** The pFunc is the only aggregate function in the query. Check to see |
| 143510 | 144068 | ** if the query is a candidate for the min/max optimization. |
| 143511 | 144069 | ** |
| | @@ -144688,15 +145246,17 @@ |
| 144688 | 145246 | if( pExpr->pAggInfo==0 ) return WRC_Continue; |
| 144689 | 145247 | if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue; |
| 144690 | 145248 | if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue; |
| 144691 | 145249 | if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue; |
| 144692 | 145250 | pAggInfo = pExpr->pAggInfo; |
| 144693 | | - assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 145251 | + if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue; |
| 145252 | + assert( pExpr->iAgg>=0 ); |
| 144694 | 145253 | pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 144695 | 145254 | pExpr->op = TK_AGG_COLUMN; |
| 144696 | 145255 | pExpr->iTable = pCol->iTable; |
| 144697 | 145256 | pExpr->iColumn = pCol->iColumn; |
| 145257 | + ExprClearProperty(pExpr, EP_Skip|EP_Collate); |
| 144698 | 145258 | return WRC_Prune; |
| 144699 | 145259 | } |
| 144700 | 145260 | |
| 144701 | 145261 | /* |
| 144702 | 145262 | ** Convert every pAggInfo->aFunc[].pExpr such that any node within |
| | @@ -145046,11 +145606,10 @@ |
| 145046 | 145606 | sqlite3DbFree(db, p->aCol); |
| 145047 | 145607 | sqlite3DbFree(db, p->aFunc); |
| 145048 | 145608 | sqlite3DbFreeNN(db, p); |
| 145049 | 145609 | } |
| 145050 | 145610 | |
| 145051 | | -#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION |
| 145052 | 145611 | /* |
| 145053 | 145612 | ** Attempt to transform a query of the form |
| 145054 | 145613 | ** |
| 145055 | 145614 | ** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2) |
| 145056 | 145615 | ** |
| | @@ -145074,10 +145633,11 @@ |
| 145074 | 145633 | Expr *pCount; |
| 145075 | 145634 | sqlite3 *db; |
| 145076 | 145635 | if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */ |
| 145077 | 145636 | if( p->pEList->nExpr!=1 ) return 0; /* Single result column */ |
| 145078 | 145637 | if( p->pWhere ) return 0; |
| 145638 | + if( p->pHaving ) return 0; |
| 145079 | 145639 | if( p->pGroupBy ) return 0; |
| 145080 | 145640 | if( p->pOrderBy ) return 0; |
| 145081 | 145641 | pExpr = p->pEList->a[0].pExpr; |
| 145082 | 145642 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */ |
| 145083 | 145643 | assert( ExprUseUToken(pExpr) ); |
| | @@ -145093,11 +145653,12 @@ |
| 145093 | 145653 | do{ |
| 145094 | 145654 | if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */ |
| 145095 | 145655 | if( pSub->pWhere ) return 0; /* No WHERE clause */ |
| 145096 | 145656 | if( pSub->pLimit ) return 0; /* No LIMIT clause */ |
| 145097 | 145657 | if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */ |
| 145098 | | - pSub = pSub->pPrior; /* Repeat over compound */ |
| 145658 | + assert( pSub->pHaving==0 ); /* Due to the previous */ |
| 145659 | + pSub = pSub->pPrior; /* Repeat over compound */ |
| 145099 | 145660 | }while( pSub ); |
| 145100 | 145661 | |
| 145101 | 145662 | /* If we reach this point then it is OK to perform the transformation */ |
| 145102 | 145663 | |
| 145103 | 145664 | db = pParse->db; |
| | @@ -145136,11 +145697,10 @@ |
| 145136 | 145697 | sqlite3TreeViewSelect(0, p, 0); |
| 145137 | 145698 | } |
| 145138 | 145699 | #endif |
| 145139 | 145700 | return 1; |
| 145140 | 145701 | } |
| 145141 | | -#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */ |
| 145142 | 145702 | |
| 145143 | 145703 | /* |
| 145144 | 145704 | ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same |
| 145145 | 145705 | ** as pSrcItem but has the same alias as p0, then return true. |
| 145146 | 145706 | ** Otherwise return false. |
| | @@ -145525,18 +146085,16 @@ |
| 145525 | 146085 | #endif |
| 145526 | 146086 | }else{ |
| 145527 | 146087 | TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n")); |
| 145528 | 146088 | } |
| 145529 | 146089 | |
| 145530 | | -#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION |
| 145531 | 146090 | if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView) |
| 145532 | 146091 | && countOfViewOptimization(pParse, p) |
| 145533 | 146092 | ){ |
| 145534 | 146093 | if( db->mallocFailed ) goto select_end; |
| 145535 | 146094 | pTabList = p->pSrc; |
| 145536 | 146095 | } |
| 145537 | | -#endif |
| 145538 | 146096 | |
| 145539 | 146097 | /* For each term in the FROM clause, do two things: |
| 145540 | 146098 | ** (1) Authorized unreferenced tables |
| 145541 | 146099 | ** (2) Generate code for all sub-queries |
| 145542 | 146100 | */ |
| | @@ -145604,10 +146162,26 @@ |
| 145604 | 146162 | #endif |
| 145605 | 146163 | assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 ); |
| 145606 | 146164 | }else{ |
| 145607 | 146165 | TREETRACE(0x4000,pParse,p,("Push-down not possible\n")); |
| 145608 | 146166 | } |
| 146167 | + |
| 146168 | + /* Convert unused result columns of the subquery into simple NULL |
| 146169 | + ** expressions, to avoid unneeded searching and computation. |
| 146170 | + */ |
| 146171 | + if( OptimizationEnabled(db, SQLITE_NullUnusedCols) |
| 146172 | + && disableUnusedSubqueryResultColumns(pItem) |
| 146173 | + ){ |
| 146174 | +#if TREETRACE_ENABLED |
| 146175 | + if( sqlite3TreeTrace & 0x4000 ){ |
| 146176 | + TREETRACE(0x4000,pParse,p, |
| 146177 | + ("Change unused result columns to NULL for subquery %d:\n", |
| 146178 | + pSub->selId)); |
| 146179 | + sqlite3TreeViewSelect(0, p, 0); |
| 146180 | + } |
| 146181 | +#endif |
| 146182 | + } |
| 145609 | 146183 | |
| 145610 | 146184 | zSavedAuthContext = pParse->zAuthContext; |
| 145611 | 146185 | pParse->zAuthContext = pItem->zName; |
| 145612 | 146186 | |
| 145613 | 146187 | /* Generate code to implement the subquery |
| | @@ -146891,10 +147465,11 @@ |
| 146891 | 147465 | if( !noErr ){ |
| 146892 | 147466 | sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); |
| 146893 | 147467 | }else{ |
| 146894 | 147468 | assert( !db->init.busy ); |
| 146895 | 147469 | sqlite3CodeVerifySchema(pParse, iDb); |
| 147470 | + VVA_ONLY( pParse->ifNotExists = 1; ) |
| 146896 | 147471 | } |
| 146897 | 147472 | goto trigger_cleanup; |
| 146898 | 147473 | } |
| 146899 | 147474 | } |
| 146900 | 147475 | |
| | @@ -148141,10 +148716,13 @@ |
| 148141 | 148716 | const int op = pChanges ? TK_UPDATE : TK_DELETE; |
| 148142 | 148717 | u32 mask = 0; |
| 148143 | 148718 | Trigger *p; |
| 148144 | 148719 | |
| 148145 | 148720 | assert( isNew==1 || isNew==0 ); |
| 148721 | + if( IsView(pTab) ){ |
| 148722 | + return 0xffffffff; |
| 148723 | + } |
| 148146 | 148724 | for(p=pTrigger; p; p=p->pNext){ |
| 148147 | 148725 | if( p->op==op |
| 148148 | 148726 | && (tr_tm&p->tr_tm) |
| 148149 | 148727 | && checkColumnOverlap(p->pColumns,pChanges) |
| 148150 | 148728 | ){ |
| | @@ -148575,11 +149153,11 @@ |
| 148575 | 149153 | #endif |
| 148576 | 149154 | |
| 148577 | 149155 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 148578 | 149156 | goto update_cleanup; |
| 148579 | 149157 | } |
| 148580 | | - if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 149158 | + if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ |
| 148581 | 149159 | goto update_cleanup; |
| 148582 | 149160 | } |
| 148583 | 149161 | |
| 148584 | 149162 | /* Allocate a cursors for the main database table and for all indices. |
| 148585 | 149163 | ** The index cursors might not be used, but if they are used they |
| | @@ -148894,16 +149472,26 @@ |
| 148894 | 149472 | bFinishSeek = 0; |
| 148895 | 149473 | }else{ |
| 148896 | 149474 | /* Begin the database scan. |
| 148897 | 149475 | ** |
| 148898 | 149476 | ** Do not consider a single-pass strategy for a multi-row update if |
| 148899 | | - ** there are any triggers or foreign keys to process, or rows may |
| 148900 | | - ** be deleted as a result of REPLACE conflict handling. Any of these |
| 148901 | | - ** things might disturb a cursor being used to scan through the table |
| 148902 | | - ** or index, causing a single-pass approach to malfunction. */ |
| 149477 | + ** there is anything that might disrupt the cursor being used to do |
| 149478 | + ** the UPDATE: |
| 149479 | + ** (1) This is a nested UPDATE |
| 149480 | + ** (2) There are triggers |
| 149481 | + ** (3) There are FOREIGN KEY constraints |
| 149482 | + ** (4) There are REPLACE conflict handlers |
| 149483 | + ** (5) There are subqueries in the WHERE clause |
| 149484 | + */ |
| 148903 | 149485 | flags = WHERE_ONEPASS_DESIRED; |
| 148904 | | - if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){ |
| 149486 | + if( !pParse->nested |
| 149487 | + && !pTrigger |
| 149488 | + && !hasFK |
| 149489 | + && !chngKey |
| 149490 | + && !bReplace |
| 149491 | + && (sNC.ncFlags & NC_Subquery)==0 |
| 149492 | + ){ |
| 148905 | 149493 | flags |= WHERE_ONEPASS_MULTIROW; |
| 148906 | 149494 | } |
| 148907 | 149495 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur); |
| 148908 | 149496 | if( pWInfo==0 ) goto update_cleanup; |
| 148909 | 149497 | |
| | @@ -150864,11 +151452,13 @@ |
| 150864 | 151452 | sCtx.pTab = pTab; |
| 150865 | 151453 | sCtx.pVTable = pVTable; |
| 150866 | 151454 | sCtx.pPrior = db->pVtabCtx; |
| 150867 | 151455 | sCtx.bDeclared = 0; |
| 150868 | 151456 | db->pVtabCtx = &sCtx; |
| 151457 | + pTab->nTabRef++; |
| 150869 | 151458 | rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); |
| 151459 | + sqlite3DeleteTable(db, pTab); |
| 150870 | 151460 | db->pVtabCtx = sCtx.pPrior; |
| 150871 | 151461 | if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); |
| 150872 | 151462 | assert( sCtx.pTab==pTab ); |
| 150873 | 151463 | |
| 150874 | 151464 | if( SQLITE_OK!=rc ){ |
| | @@ -151582,10 +152172,14 @@ |
| 151582 | 152172 | break; |
| 151583 | 152173 | } |
| 151584 | 152174 | case SQLITE_VTAB_DIRECTONLY: { |
| 151585 | 152175 | p->pVTable->eVtabRisk = SQLITE_VTABRISK_High; |
| 151586 | 152176 | break; |
| 152177 | + } |
| 152178 | + case SQLITE_VTAB_USES_ALL_SCHEMAS: { |
| 152179 | + p->pVTable->bAllSchemas = 1; |
| 152180 | + break; |
| 151587 | 152181 | } |
| 151588 | 152182 | default: { |
| 151589 | 152183 | rc = SQLITE_MISUSE_BKPT; |
| 151590 | 152184 | break; |
| 151591 | 152185 | } |
| | @@ -152356,13 +152950,13 @@ |
| 152356 | 152950 | sqlite3_str_append(pStr, ")", 1); |
| 152357 | 152951 | } |
| 152358 | 152952 | |
| 152359 | 152953 | /* |
| 152360 | 152954 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 152361 | | -** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 152362 | | -** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 152363 | | -** is added to the output to describe the table scan strategy in pLevel. |
| 152955 | +** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG |
| 152956 | +** was defined at compile-time. If it is not a no-op, a single OP_Explain |
| 152957 | +** opcode is added to the output to describe the table scan strategy in pLevel. |
| 152364 | 152958 | ** |
| 152365 | 152959 | ** If an OP_Explain opcode is added to the VM, its address is returned. |
| 152366 | 152960 | ** Otherwise, if no OP_Explain is coded, zero is returned. |
| 152367 | 152961 | */ |
| 152368 | 152962 | SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| | @@ -152370,12 +152964,12 @@ |
| 152370 | 152964 | SrcList *pTabList, /* Table list this loop refers to */ |
| 152371 | 152965 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 152372 | 152966 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 152373 | 152967 | ){ |
| 152374 | 152968 | int ret = 0; |
| 152375 | | -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 152376 | | - if( sqlite3ParseToplevel(pParse)->explain==2 ) |
| 152969 | +#if !defined(SQLITE_DEBUG) |
| 152970 | + if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) |
| 152377 | 152971 | #endif |
| 152378 | 152972 | { |
| 152379 | 152973 | SrcItem *pItem = &pTabList->a[pLevel->iFrom]; |
| 152380 | 152974 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 152381 | 152975 | sqlite3 *db = pParse->db; /* Database handle */ |
| | @@ -152537,31 +153131,33 @@ |
| 152537 | 153131 | Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 152538 | 153132 | SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 152539 | 153133 | WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 152540 | 153134 | int addrExplain /* Address of OP_Explain (or 0) */ |
| 152541 | 153135 | ){ |
| 152542 | | - const char *zObj = 0; |
| 152543 | | - WhereLoop *pLoop = pLvl->pWLoop; |
| 152544 | | - int wsFlags = pLoop->wsFlags; |
| 152545 | | - int viaCoroutine = 0; |
| 152546 | | - |
| 152547 | | - if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 152548 | | - zObj = pLoop->u.btree.pIndex->zName; |
| 152549 | | - }else{ |
| 152550 | | - zObj = pSrclist->a[pLvl->iFrom].zName; |
| 152551 | | - viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine; |
| 152552 | | - } |
| 152553 | | - sqlite3VdbeScanStatus( |
| 152554 | | - v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 152555 | | - ); |
| 152556 | | - |
| 152557 | | - if( viaCoroutine==0 ){ |
| 152558 | | - if( (wsFlags & (WHERE_MULTI_OR|WHERE_AUTO_INDEX))==0 ){ |
| 152559 | | - sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur); |
| 152560 | | - } |
| 152561 | | - if( wsFlags & WHERE_INDEXED ){ |
| 152562 | | - sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur); |
| 153136 | + if( IS_STMT_SCANSTATUS( sqlite3VdbeDb(v) ) ){ |
| 153137 | + const char *zObj = 0; |
| 153138 | + WhereLoop *pLoop = pLvl->pWLoop; |
| 153139 | + int wsFlags = pLoop->wsFlags; |
| 153140 | + int viaCoroutine = 0; |
| 153141 | + |
| 153142 | + if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 153143 | + zObj = pLoop->u.btree.pIndex->zName; |
| 153144 | + }else{ |
| 153145 | + zObj = pSrclist->a[pLvl->iFrom].zName; |
| 153146 | + viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine; |
| 153147 | + } |
| 153148 | + sqlite3VdbeScanStatus( |
| 153149 | + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 153150 | + ); |
| 153151 | + |
| 153152 | + if( viaCoroutine==0 ){ |
| 153153 | + if( (wsFlags & (WHERE_MULTI_OR|WHERE_AUTO_INDEX))==0 ){ |
| 153154 | + sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur); |
| 153155 | + } |
| 153156 | + if( wsFlags & WHERE_INDEXED ){ |
| 153157 | + sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur); |
| 153158 | + } |
| 152563 | 153159 | } |
| 152564 | 153160 | } |
| 152565 | 153161 | } |
| 152566 | 153162 | #endif |
| 152567 | 153163 | |
| | @@ -153254,31 +153850,29 @@ |
| 153254 | 153850 | ** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into |
| 153255 | 153851 | ** an access of the index rather than the original table. |
| 153256 | 153852 | */ |
| 153257 | 153853 | static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ |
| 153258 | 153854 | int rc = WRC_Continue; |
| 153855 | + int reg; |
| 153259 | 153856 | struct CCurHint *pHint = pWalker->u.pCCurHint; |
| 153260 | 153857 | if( pExpr->op==TK_COLUMN ){ |
| 153261 | 153858 | if( pExpr->iTable!=pHint->iTabCur ){ |
| 153262 | | - int reg = ++pWalker->pParse->nMem; /* Register for column value */ |
| 153263 | | - sqlite3ExprCode(pWalker->pParse, pExpr, reg); |
| 153859 | + reg = ++pWalker->pParse->nMem; /* Register for column value */ |
| 153860 | + reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg); |
| 153264 | 153861 | pExpr->op = TK_REGISTER; |
| 153265 | 153862 | pExpr->iTable = reg; |
| 153266 | 153863 | }else if( pHint->pIdx!=0 ){ |
| 153267 | 153864 | pExpr->iTable = pHint->iIdxCur; |
| 153268 | 153865 | pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn); |
| 153269 | 153866 | assert( pExpr->iColumn>=0 ); |
| 153270 | 153867 | } |
| 153271 | | - }else if( pExpr->op==TK_AGG_FUNCTION ){ |
| 153272 | | - /* An aggregate function in the WHERE clause of a query means this must |
| 153273 | | - ** be a correlated sub-query, and expression pExpr is an aggregate from |
| 153274 | | - ** the parent context. Do not walk the function arguments in this case. |
| 153275 | | - ** |
| 153276 | | - ** todo: It should be possible to replace this node with a TK_REGISTER |
| 153277 | | - ** expression, as the result of the expression must be stored in a |
| 153278 | | - ** register at this point. The same holds for TK_AGG_COLUMN nodes. */ |
| 153868 | + }else if( pExpr->pAggInfo ){ |
| 153279 | 153869 | rc = WRC_Prune; |
| 153870 | + reg = ++pWalker->pParse->nMem; /* Register for column value */ |
| 153871 | + reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg); |
| 153872 | + pExpr->op = TK_REGISTER; |
| 153873 | + pExpr->iTable = reg; |
| 153280 | 153874 | } |
| 153281 | 153875 | return rc; |
| 153282 | 153876 | } |
| 153283 | 153877 | |
| 153284 | 153878 | /* |
| | @@ -154170,11 +154764,11 @@ |
| 154170 | 154764 | ** of entries in the tree, so basing the number of steps to try |
| 154171 | 154765 | ** on the estimated number of rows in the btree seems like a good |
| 154172 | 154766 | ** guess. */ |
| 154173 | 154767 | addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan, |
| 154174 | 154768 | (pIdx->aiRowLogEst[0]+9)/10); |
| 154175 | | - if( pRangeStart ){ |
| 154769 | + if( pRangeStart || pRangeEnd ){ |
| 154176 | 154770 | sqlite3VdbeChangeP5(v, 1); |
| 154177 | 154771 | sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1); |
| 154178 | 154772 | addrSeekScan = 0; |
| 154179 | 154773 | } |
| 154180 | 154774 | VdbeCoverage(v); |
| | @@ -154211,20 +154805,11 @@ |
| 154211 | 154805 | */ |
| 154212 | 154806 | nConstraint = nEq; |
| 154213 | 154807 | assert( pLevel->p2==0 ); |
| 154214 | 154808 | if( pRangeEnd ){ |
| 154215 | 154809 | Expr *pRight = pRangeEnd->pExpr->pRight; |
| 154216 | | - if( addrSeekScan ){ |
| 154217 | | - /* For a seek-scan that has a range on the lowest term of the index, |
| 154218 | | - ** we have to make the top of the loop be code that sets the end |
| 154219 | | - ** condition of the range. Otherwise, the OP_SeekScan might jump |
| 154220 | | - ** over that initialization, leaving the range-end value set to the |
| 154221 | | - ** range-start value, resulting in a wrong answer. |
| 154222 | | - ** See ticket 5981a8c041a3c2f3 (2021-11-02). |
| 154223 | | - */ |
| 154224 | | - pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 154225 | | - } |
| 154810 | + assert( addrSeekScan==0 ); |
| 154226 | 154811 | codeExprOrVector(pParse, pRight, regBase+nEq, nTop); |
| 154227 | 154812 | whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 154228 | 154813 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 154229 | 154814 | && sqlite3ExprCanBeNull(pRight) |
| 154230 | 154815 | ){ |
| | @@ -154254,11 +154839,11 @@ |
| 154254 | 154839 | } |
| 154255 | 154840 | if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff); |
| 154256 | 154841 | if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff); |
| 154257 | 154842 | |
| 154258 | 154843 | /* Top of the loop body */ |
| 154259 | | - if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 154844 | + pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 154260 | 154845 | |
| 154261 | 154846 | /* Check if the index cursor is past the end of the range. */ |
| 154262 | 154847 | if( nConstraint ){ |
| 154263 | 154848 | if( regBignull ){ |
| 154264 | 154849 | /* Except, skip the end-of-range check while doing the NULL-scan */ |
| | @@ -156896,13 +157481,16 @@ |
| 156896 | 157481 | pColRef->y.pTab = pTab; |
| 156897 | 157482 | pItem->colUsed |= sqlite3ExprColUsed(pColRef); |
| 156898 | 157483 | pRhs = sqlite3PExpr(pParse, TK_UPLUS, |
| 156899 | 157484 | sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); |
| 156900 | 157485 | pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); |
| 156901 | | - if( pItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){ |
| 157486 | + if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){ |
| 157487 | + testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */ |
| 157488 | + testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */ |
| 156902 | 157489 | joinType = EP_OuterON; |
| 156903 | 157490 | }else{ |
| 157491 | + testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */ |
| 156904 | 157492 | joinType = EP_InnerON; |
| 156905 | 157493 | } |
| 156906 | 157494 | sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType); |
| 156907 | 157495 | whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); |
| 156908 | 157496 | } |
| | @@ -157741,11 +158329,11 @@ |
| 157741 | 158329 | Parse *pParse, |
| 157742 | 158330 | Index *pIdx, /* Automatic index to explain */ |
| 157743 | 158331 | int bPartial, /* True if pIdx is a partial index */ |
| 157744 | 158332 | int *pAddrExplain /* OUT: Address of OP_Explain */ |
| 157745 | 158333 | ){ |
| 157746 | | - if( pParse->explain!=2 ){ |
| 158334 | + if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){ |
| 157747 | 158335 | Table *pTab = pIdx->pTable; |
| 157748 | 158336 | const char *zSep = ""; |
| 157749 | 158337 | char *zText = 0; |
| 157750 | 158338 | int ii = 0; |
| 157751 | 158339 | sqlite3_str *pStr = sqlite3_str_new(pParse->db); |
| | @@ -157802,11 +158390,12 @@ |
| 157802 | 158390 |