| | @@ -452,11 +452,11 @@ |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | 455 | #define SQLITE_VERSION "3.40.0" |
| 456 | 456 | #define SQLITE_VERSION_NUMBER 3040000 |
| 457 | | -#define SQLITE_SOURCE_ID "2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646" |
| 457 | +#define SQLITE_SOURCE_ID "2022-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -974,17 +974,21 @@ |
| 974 | 974 | /* |
| 975 | 975 | ** CAPI3REF: File Locking Levels |
| 976 | 976 | ** |
| 977 | 977 | ** SQLite uses one of these integer values as the second |
| 978 | 978 | ** argument to calls it makes to the xLock() and xUnlock() methods |
| 979 | | -** of an [sqlite3_io_methods] object. |
| 979 | +** of an [sqlite3_io_methods] object. These values are ordered from |
| 980 | +** lest restrictive to most restrictive. |
| 981 | +** |
| 982 | +** The argument to xLock() is always SHARED or higher. The argument to |
| 983 | +** xUnlock is either SHARED or NONE. |
| 980 | 984 | */ |
| 981 | | -#define SQLITE_LOCK_NONE 0 |
| 982 | | -#define SQLITE_LOCK_SHARED 1 |
| 983 | | -#define SQLITE_LOCK_RESERVED 2 |
| 984 | | -#define SQLITE_LOCK_PENDING 3 |
| 985 | | -#define SQLITE_LOCK_EXCLUSIVE 4 |
| 985 | +#define SQLITE_LOCK_NONE 0 /* xUnlock() only */ |
| 986 | +#define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */ |
| 987 | +#define SQLITE_LOCK_RESERVED 2 /* xLock() only */ |
| 988 | +#define SQLITE_LOCK_PENDING 3 /* xLock() only */ |
| 989 | +#define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */ |
| 986 | 990 | |
| 987 | 991 | /* |
| 988 | 992 | ** CAPI3REF: Synchronization Type Flags |
| 989 | 993 | ** |
| 990 | 994 | ** When SQLite invokes the xSync() method of an |
| | @@ -1058,11 +1062,18 @@ |
| 1058 | 1062 | ** <li> [SQLITE_LOCK_SHARED], |
| 1059 | 1063 | ** <li> [SQLITE_LOCK_RESERVED], |
| 1060 | 1064 | ** <li> [SQLITE_LOCK_PENDING], or |
| 1061 | 1065 | ** <li> [SQLITE_LOCK_EXCLUSIVE]. |
| 1062 | 1066 | ** </ul> |
| 1063 | | -** xLock() increases the lock. xUnlock() decreases the lock. |
| 1067 | +** xLock() upgrades the database file lock. In other words, xLock() moves the |
| 1068 | +** database file lock in the direction NONE toward EXCLUSIVE. The argument to |
| 1069 | +** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never |
| 1070 | +** SQLITE_LOCK_NONE. If the database file lock is already at or above the |
| 1071 | +** requested lock, then the call to xLock() is a no-op. |
| 1072 | +** xUnlock() downgrades the database file lock to either SHARED or NONE. |
| 1073 | +* If the lock is already at or below the requested lock state, then the call |
| 1074 | +** to xUnlock() is a no-op. |
| 1064 | 1075 | ** The xCheckReservedLock() method checks whether any database connection, |
| 1065 | 1076 | ** either in this process or in some other process, is holding a RESERVED, |
| 1066 | 1077 | ** PENDING, or EXCLUSIVE lock on the file. It returns true |
| 1067 | 1078 | ** if such a lock exists and false otherwise. |
| 1068 | 1079 | ** |
| | @@ -1163,13 +1174,12 @@ |
| 1163 | 1174 | ** <li>[[SQLITE_FCNTL_LOCKSTATE]] |
| 1164 | 1175 | ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This |
| 1165 | 1176 | ** opcode causes the xFileControl method to write the current state of |
| 1166 | 1177 | ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], |
| 1167 | 1178 | ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) |
| 1168 | | -** into an integer that the pArg argument points to. This capability |
| 1169 | | -** is used during testing and is only available when the SQLITE_TEST |
| 1170 | | -** compile-time option is used. |
| 1179 | +** into an integer that the pArg argument points to. |
| 1180 | +** This capability is only available if SQLite is compiled with [SQLITE_DEBUG]. |
| 1171 | 1181 | ** |
| 1172 | 1182 | ** <li>[[SQLITE_FCNTL_SIZE_HINT]] |
| 1173 | 1183 | ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS |
| 1174 | 1184 | ** layer a hint of how large the database file will grow to be during the |
| 1175 | 1185 | ** current transaction. This hint is not guaranteed to be accurate but it |
| | @@ -5815,10 +5825,20 @@ |
| 5815 | 5825 | ** such a conversion is possible without loss of information (in other |
| 5816 | 5826 | ** words, if the value is a string that looks like a number) |
| 5817 | 5827 | ** then the conversion is performed. Otherwise no conversion occurs. |
| 5818 | 5828 | ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ |
| 5819 | 5829 | ** |
| 5830 | +** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8], |
| 5831 | +** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding |
| 5832 | +** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X) |
| 5833 | +** returns something other than SQLITE_TEXT, then the return value from |
| 5834 | +** sqlite3_value_encoding(X) is meaningless. ^Calls to |
| 5835 | +** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X), |
| 5836 | +** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or |
| 5837 | +** sqlite3_value_bytes16(X) might change the encoding of the value X and |
| 5838 | +** thus change the return from subsequent calls to sqlite3_value_encoding(X). |
| 5839 | +** |
| 5820 | 5840 | ** ^Within the [xUpdate] method of a [virtual table], the |
| 5821 | 5841 | ** sqlite3_value_nochange(X) interface returns true if and only if |
| 5822 | 5842 | ** the column corresponding to X is unchanged by the UPDATE operation |
| 5823 | 5843 | ** that the xUpdate method call was invoked to implement and if |
| 5824 | 5844 | ** and the prior [xColumn] method call that was invoked to extracted |
| | @@ -5879,10 +5899,11 @@ |
| 5879 | 5899 | SQLITE_API int sqlite3_value_bytes16(sqlite3_value*); |
| 5880 | 5900 | SQLITE_API int sqlite3_value_type(sqlite3_value*); |
| 5881 | 5901 | SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); |
| 5882 | 5902 | SQLITE_API int sqlite3_value_nochange(sqlite3_value*); |
| 5883 | 5903 | SQLITE_API int sqlite3_value_frombind(sqlite3_value*); |
| 5904 | +SQLITE_API int sqlite3_value_encoding(sqlite3_value*); |
| 5884 | 5905 | |
| 5885 | 5906 | /* |
| 5886 | 5907 | ** CAPI3REF: Finding The Subtype Of SQL Values |
| 5887 | 5908 | ** METHOD: sqlite3_value |
| 5888 | 5909 | ** |
| | @@ -5932,11 +5953,11 @@ |
| 5932 | 5953 | ** In those cases, sqlite3_aggregate_context() might be called for the |
| 5933 | 5954 | ** first time from within xFinal().)^ |
| 5934 | 5955 | ** |
| 5935 | 5956 | ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer |
| 5936 | 5957 | ** when first called if N is less than or equal to zero or if a memory |
| 5937 | | -** allocate error occurs. |
| 5958 | +** allocation error occurs. |
| 5938 | 5959 | ** |
| 5939 | 5960 | ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is |
| 5940 | 5961 | ** determined by the N parameter on first successful call. Changing the |
| 5941 | 5962 | ** value of N in any subsequent call to sqlite3_aggregate_context() within |
| 5942 | 5963 | ** the same aggregate function instance will not resize the memory |
| | @@ -13160,11 +13181,11 @@ |
| 13160 | 13181 | /* |
| 13161 | 13182 | ** Include the configuration header output by 'configure' if we're using the |
| 13162 | 13183 | ** autoconf-based build |
| 13163 | 13184 | */ |
| 13164 | 13185 | #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H) |
| 13165 | | -#include "config.h" |
| 13186 | +#include "sqlite_cfg.h" |
| 13166 | 13187 | #define SQLITECONFIG_H 1 |
| 13167 | 13188 | #endif |
| 13168 | 13189 | |
| 13169 | 13190 | /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/ |
| 13170 | 13191 | /************** Begin file sqliteLimit.h *************************************/ |
| | @@ -14643,10 +14664,11 @@ |
| 14643 | 14664 | typedef struct FuncDestructor FuncDestructor; |
| 14644 | 14665 | typedef struct FuncDef FuncDef; |
| 14645 | 14666 | typedef struct FuncDefHash FuncDefHash; |
| 14646 | 14667 | typedef struct IdList IdList; |
| 14647 | 14668 | typedef struct Index Index; |
| 14669 | +typedef struct IndexedExpr IndexedExpr; |
| 14648 | 14670 | typedef struct IndexSample IndexSample; |
| 14649 | 14671 | typedef struct KeyClass KeyClass; |
| 14650 | 14672 | typedef struct KeyInfo KeyInfo; |
| 14651 | 14673 | typedef struct Lookaside Lookaside; |
| 14652 | 14674 | typedef struct LookasideSlot LookasideSlot; |
| | @@ -14708,10 +14730,11 @@ |
| 14708 | 14730 | #define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 14709 | 14731 | #define MASKBIT64(n) (((u64)1)<<(n)) |
| 14710 | 14732 | #define MASKBIT32(n) (((unsigned int)1)<<(n)) |
| 14711 | 14733 | #define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0) |
| 14712 | 14734 | #define ALLBITS ((Bitmask)-1) |
| 14735 | +#define TOPBIT (((Bitmask)1)<<(BMS-1)) |
| 14713 | 14736 | |
| 14714 | 14737 | /* A VList object records a mapping between parameters/variables/wildcards |
| 14715 | 14738 | ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer |
| 14716 | 14739 | ** variable number associated with that parameter. See the format description |
| 14717 | 14740 | ** on the sqlite3VListAdd() routine for more information. A VList is really |
| | @@ -14722,10 +14745,335 @@ |
| 14722 | 14745 | /* |
| 14723 | 14746 | ** Defer sourcing vdbe.h and btree.h until after the "u8" and |
| 14724 | 14747 | ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque |
| 14725 | 14748 | ** pointer types (i.e. FuncDef) defined above. |
| 14726 | 14749 | */ |
| 14750 | +/************** Include os.h in the middle of sqliteInt.h ********************/ |
| 14751 | +/************** Begin file os.h **********************************************/ |
| 14752 | +/* |
| 14753 | +** 2001 September 16 |
| 14754 | +** |
| 14755 | +** The author disclaims copyright to this source code. In place of |
| 14756 | +** a legal notice, here is a blessing: |
| 14757 | +** |
| 14758 | +** May you do good and not evil. |
| 14759 | +** May you find forgiveness for yourself and forgive others. |
| 14760 | +** May you share freely, never taking more than you give. |
| 14761 | +** |
| 14762 | +****************************************************************************** |
| 14763 | +** |
| 14764 | +** This header file (together with is companion C source-code file |
| 14765 | +** "os.c") attempt to abstract the underlying operating system so that |
| 14766 | +** the SQLite library will work on both POSIX and windows systems. |
| 14767 | +** |
| 14768 | +** This header file is #include-ed by sqliteInt.h and thus ends up |
| 14769 | +** being included by every source file. |
| 14770 | +*/ |
| 14771 | +#ifndef _SQLITE_OS_H_ |
| 14772 | +#define _SQLITE_OS_H_ |
| 14773 | + |
| 14774 | +/* |
| 14775 | +** Attempt to automatically detect the operating system and setup the |
| 14776 | +** necessary pre-processor macros for it. |
| 14777 | +*/ |
| 14778 | +/************** Include os_setup.h in the middle of os.h *********************/ |
| 14779 | +/************** Begin file os_setup.h ****************************************/ |
| 14780 | +/* |
| 14781 | +** 2013 November 25 |
| 14782 | +** |
| 14783 | +** The author disclaims copyright to this source code. In place of |
| 14784 | +** a legal notice, here is a blessing: |
| 14785 | +** |
| 14786 | +** May you do good and not evil. |
| 14787 | +** May you find forgiveness for yourself and forgive others. |
| 14788 | +** May you share freely, never taking more than you give. |
| 14789 | +** |
| 14790 | +****************************************************************************** |
| 14791 | +** |
| 14792 | +** This file contains pre-processor directives related to operating system |
| 14793 | +** detection and/or setup. |
| 14794 | +*/ |
| 14795 | +#ifndef SQLITE_OS_SETUP_H |
| 14796 | +#define SQLITE_OS_SETUP_H |
| 14797 | + |
| 14798 | +/* |
| 14799 | +** Figure out if we are dealing with Unix, Windows, or some other operating |
| 14800 | +** system. |
| 14801 | +** |
| 14802 | +** After the following block of preprocess macros, all of |
| 14803 | +** |
| 14804 | +** SQLITE_OS_KV |
| 14805 | +** SQLITE_OS_OTHER |
| 14806 | +** SQLITE_OS_UNIX |
| 14807 | +** SQLITE_OS_WIN |
| 14808 | +** |
| 14809 | +** will defined to either 1 or 0. One of them will be 1. The others will be 0. |
| 14810 | +** If none of the macros are initially defined, then select either |
| 14811 | +** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform. |
| 14812 | +** |
| 14813 | +** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application |
| 14814 | +** must provide its own VFS implementation together with sqlite3_os_init() |
| 14815 | +** and sqlite3_os_end() routines. |
| 14816 | +*/ |
| 14817 | +#if !defined(SQLITE_OS_KV) && !defined(SQLITE_OS_OTHER) && \ |
| 14818 | + !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_WIN) |
| 14819 | +# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ |
| 14820 | + defined(__MINGW32__) || defined(__BORLANDC__) |
| 14821 | +# define SQLITE_OS_WIN 1 |
| 14822 | +# define SQLITE_OS_UNIX 0 |
| 14823 | +# else |
| 14824 | +# define SQLITE_OS_WIN 0 |
| 14825 | +# define SQLITE_OS_UNIX 1 |
| 14826 | +# endif |
| 14827 | +#endif |
| 14828 | +#if SQLITE_OS_OTHER+1>1 |
| 14829 | +# undef SQLITE_OS_KV |
| 14830 | +# define SQLITE_OS_KV 0 |
| 14831 | +# undef SQLITE_OS_UNIX |
| 14832 | +# define SQLITE_OS_UNIX 0 |
| 14833 | +# undef SQLITE_OS_WIN |
| 14834 | +# define SQLITE_OS_WIN 0 |
| 14835 | +#endif |
| 14836 | +#if SQLITE_OS_KV+1>1 |
| 14837 | +# undef SQLITE_OS_OTHER |
| 14838 | +# define SQLITE_OS_OTHER 0 |
| 14839 | +# undef SQLITE_OS_UNIX |
| 14840 | +# define SQLITE_OS_UNIX 0 |
| 14841 | +# undef SQLITE_OS_WIN |
| 14842 | +# define SQLITE_OS_WIN 0 |
| 14843 | +# define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 14844 | +# define SQLITE_OMIT_WAL 1 |
| 14845 | +# define SQLITE_OMIT_DEPRECATED 1 |
| 14846 | +# undef SQLITE_TEMP_STORE |
| 14847 | +# define SQLITE_TEMP_STORE 3 /* Always use memory for temporary storage */ |
| 14848 | +# define SQLITE_DQS 0 |
| 14849 | +# define SQLITE_OMIT_SHARED_CACHE 1 |
| 14850 | +# define SQLITE_OMIT_AUTOINIT 1 |
| 14851 | +#endif |
| 14852 | +#if SQLITE_OS_UNIX+1>1 |
| 14853 | +# undef SQLITE_OS_KV |
| 14854 | +# define SQLITE_OS_KV 0 |
| 14855 | +# undef SQLITE_OS_OTHER |
| 14856 | +# define SQLITE_OS_OTHER 0 |
| 14857 | +# undef SQLITE_OS_WIN |
| 14858 | +# define SQLITE_OS_WIN 0 |
| 14859 | +#endif |
| 14860 | +#if SQLITE_OS_WIN+1>1 |
| 14861 | +# undef SQLITE_OS_KV |
| 14862 | +# define SQLITE_OS_KV 0 |
| 14863 | +# undef SQLITE_OS_OTHER |
| 14864 | +# define SQLITE_OS_OTHER 0 |
| 14865 | +# undef SQLITE_OS_UNIX |
| 14866 | +# define SQLITE_OS_UNIX 0 |
| 14867 | +#endif |
| 14868 | + |
| 14869 | + |
| 14870 | +#endif /* SQLITE_OS_SETUP_H */ |
| 14871 | + |
| 14872 | +/************** End of os_setup.h ********************************************/ |
| 14873 | +/************** Continuing where we left off in os.h *************************/ |
| 14874 | + |
| 14875 | +/* If the SET_FULLSYNC macro is not defined above, then make it |
| 14876 | +** a no-op |
| 14877 | +*/ |
| 14878 | +#ifndef SET_FULLSYNC |
| 14879 | +# define SET_FULLSYNC(x,y) |
| 14880 | +#endif |
| 14881 | + |
| 14882 | +/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h |
| 14883 | +*/ |
| 14884 | +#ifndef SQLITE_MAX_PATHLEN |
| 14885 | +# define SQLITE_MAX_PATHLEN FILENAME_MAX |
| 14886 | +#endif |
| 14887 | + |
| 14888 | +/* Maximum number of symlinks that will be resolved while trying to |
| 14889 | +** expand a filename in xFullPathname() in the VFS. |
| 14890 | +*/ |
| 14891 | +#ifndef SQLITE_MAX_SYMLINK |
| 14892 | +# define SQLITE_MAX_SYMLINK 200 |
| 14893 | +#endif |
| 14894 | + |
| 14895 | +/* |
| 14896 | +** The default size of a disk sector |
| 14897 | +*/ |
| 14898 | +#ifndef SQLITE_DEFAULT_SECTOR_SIZE |
| 14899 | +# define SQLITE_DEFAULT_SECTOR_SIZE 4096 |
| 14900 | +#endif |
| 14901 | + |
| 14902 | +/* |
| 14903 | +** Temporary files are named starting with this prefix followed by 16 random |
| 14904 | +** alphanumeric characters, and no file extension. They are stored in the |
| 14905 | +** OS's standard temporary file directory, and are deleted prior to exit. |
| 14906 | +** If sqlite is being embedded in another program, you may wish to change the |
| 14907 | +** prefix to reflect your program's name, so that if your program exits |
| 14908 | +** prematurely, old temporary files can be easily identified. This can be done |
| 14909 | +** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
| 14910 | +** |
| 14911 | +** 2006-10-31: The default prefix used to be "sqlite_". But then |
| 14912 | +** Mcafee started using SQLite in their anti-virus product and it |
| 14913 | +** started putting files with the "sqlite" name in the c:/temp folder. |
| 14914 | +** This annoyed many windows users. Those users would then do a |
| 14915 | +** Google search for "sqlite", find the telephone numbers of the |
| 14916 | +** developers and call to wake them up at night and complain. |
| 14917 | +** For this reason, the default name prefix is changed to be "sqlite" |
| 14918 | +** spelled backwards. So the temp files are still identified, but |
| 14919 | +** anybody smart enough to figure out the code is also likely smart |
| 14920 | +** enough to know that calling the developer will not help get rid |
| 14921 | +** of the file. |
| 14922 | +*/ |
| 14923 | +#ifndef SQLITE_TEMP_FILE_PREFIX |
| 14924 | +# define SQLITE_TEMP_FILE_PREFIX "etilqs_" |
| 14925 | +#endif |
| 14926 | + |
| 14927 | +/* |
| 14928 | +** The following values may be passed as the second argument to |
| 14929 | +** sqlite3OsLock(). The various locks exhibit the following semantics: |
| 14930 | +** |
| 14931 | +** SHARED: Any number of processes may hold a SHARED lock simultaneously. |
| 14932 | +** RESERVED: A single process may hold a RESERVED lock on a file at |
| 14933 | +** any time. Other processes may hold and obtain new SHARED locks. |
| 14934 | +** PENDING: A single process may hold a PENDING lock on a file at |
| 14935 | +** any one time. Existing SHARED locks may persist, but no new |
| 14936 | +** SHARED locks may be obtained by other processes. |
| 14937 | +** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. |
| 14938 | +** |
| 14939 | +** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a |
| 14940 | +** process that requests an EXCLUSIVE lock may actually obtain a PENDING |
| 14941 | +** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to |
| 14942 | +** sqlite3OsLock(). |
| 14943 | +*/ |
| 14944 | +#define NO_LOCK 0 |
| 14945 | +#define SHARED_LOCK 1 |
| 14946 | +#define RESERVED_LOCK 2 |
| 14947 | +#define PENDING_LOCK 3 |
| 14948 | +#define EXCLUSIVE_LOCK 4 |
| 14949 | + |
| 14950 | +/* |
| 14951 | +** File Locking Notes: (Mostly about windows but also some info for Unix) |
| 14952 | +** |
| 14953 | +** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because |
| 14954 | +** those functions are not available. So we use only LockFile() and |
| 14955 | +** UnlockFile(). |
| 14956 | +** |
| 14957 | +** LockFile() prevents not just writing but also reading by other processes. |
| 14958 | +** A SHARED_LOCK is obtained by locking a single randomly-chosen |
| 14959 | +** byte out of a specific range of bytes. The lock byte is obtained at |
| 14960 | +** random so two separate readers can probably access the file at the |
| 14961 | +** same time, unless they are unlucky and choose the same lock byte. |
| 14962 | +** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. |
| 14963 | +** There can only be one writer. A RESERVED_LOCK is obtained by locking |
| 14964 | +** a single byte of the file that is designated as the reserved lock byte. |
| 14965 | +** A PENDING_LOCK is obtained by locking a designated byte different from |
| 14966 | +** the RESERVED_LOCK byte. |
| 14967 | +** |
| 14968 | +** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, |
| 14969 | +** which means we can use reader/writer locks. When reader/writer locks |
| 14970 | +** are used, the lock is placed on the same range of bytes that is used |
| 14971 | +** for probabilistic locking in Win95/98/ME. Hence, the locking scheme |
| 14972 | +** will support two or more Win95 readers or two or more WinNT readers. |
| 14973 | +** But a single Win95 reader will lock out all WinNT readers and a single |
| 14974 | +** WinNT reader will lock out all other Win95 readers. |
| 14975 | +** |
| 14976 | +** The following #defines specify the range of bytes used for locking. |
| 14977 | +** SHARED_SIZE is the number of bytes available in the pool from which |
| 14978 | +** a random byte is selected for a shared lock. The pool of bytes for |
| 14979 | +** shared locks begins at SHARED_FIRST. |
| 14980 | +** |
| 14981 | +** The same locking strategy and |
| 14982 | +** byte ranges are used for Unix. This leaves open the possibility of having |
| 14983 | +** clients on win95, winNT, and unix all talking to the same shared file |
| 14984 | +** and all locking correctly. To do so would require that samba (or whatever |
| 14985 | +** tool is being used for file sharing) implements locks correctly between |
| 14986 | +** windows and unix. I'm guessing that isn't likely to happen, but by |
| 14987 | +** using the same locking range we are at least open to the possibility. |
| 14988 | +** |
| 14989 | +** Locking in windows is manditory. For this reason, we cannot store |
| 14990 | +** actual data in the bytes used for locking. The pager never allocates |
| 14991 | +** the pages involved in locking therefore. SHARED_SIZE is selected so |
| 14992 | +** that all locks will fit on a single page even at the minimum page size. |
| 14993 | +** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE |
| 14994 | +** is set high so that we don't have to allocate an unused page except |
| 14995 | +** for very large databases. But one should test the page skipping logic |
| 14996 | +** by setting PENDING_BYTE low and running the entire regression suite. |
| 14997 | +** |
| 14998 | +** Changing the value of PENDING_BYTE results in a subtly incompatible |
| 14999 | +** file format. Depending on how it is changed, you might not notice |
| 15000 | +** the incompatibility right away, even running a full regression test. |
| 15001 | +** The default location of PENDING_BYTE is the first byte past the |
| 15002 | +** 1GB boundary. |
| 15003 | +** |
| 15004 | +*/ |
| 15005 | +#ifdef SQLITE_OMIT_WSD |
| 15006 | +# define PENDING_BYTE (0x40000000) |
| 15007 | +#else |
| 15008 | +# define PENDING_BYTE sqlite3PendingByte |
| 15009 | +#endif |
| 15010 | +#define RESERVED_BYTE (PENDING_BYTE+1) |
| 15011 | +#define SHARED_FIRST (PENDING_BYTE+2) |
| 15012 | +#define SHARED_SIZE 510 |
| 15013 | + |
| 15014 | +/* |
| 15015 | +** Wrapper around OS specific sqlite3_os_init() function. |
| 15016 | +*/ |
| 15017 | +SQLITE_PRIVATE int sqlite3OsInit(void); |
| 15018 | + |
| 15019 | +/* |
| 15020 | +** Functions for accessing sqlite3_file methods |
| 15021 | +*/ |
| 15022 | +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*); |
| 15023 | +SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); |
| 15024 | +SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); |
| 15025 | +SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size); |
| 15026 | +SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int); |
| 15027 | +SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); |
| 15028 | +SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int); |
| 15029 | +SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int); |
| 15030 | +SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut); |
| 15031 | +SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*); |
| 15032 | +SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*); |
| 15033 | +#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 |
| 15034 | +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id); |
| 15035 | +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id); |
| 15036 | +#ifndef SQLITE_OMIT_WAL |
| 15037 | +SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); |
| 15038 | +SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int); |
| 15039 | +SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id); |
| 15040 | +SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int); |
| 15041 | +#endif /* SQLITE_OMIT_WAL */ |
| 15042 | +SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); |
| 15043 | +SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *); |
| 15044 | + |
| 15045 | + |
| 15046 | +/* |
| 15047 | +** Functions for accessing sqlite3_vfs methods |
| 15048 | +*/ |
| 15049 | +SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); |
| 15050 | +SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int); |
| 15051 | +SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut); |
| 15052 | +SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); |
| 15053 | +#ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 15054 | +SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); |
| 15055 | +SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *); |
| 15056 | +SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void); |
| 15057 | +SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *); |
| 15058 | +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
| 15059 | +SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *); |
| 15060 | +SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int); |
| 15061 | +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*); |
| 15062 | +SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); |
| 15063 | + |
| 15064 | +/* |
| 15065 | +** Convenience functions for opening and closing files using |
| 15066 | +** sqlite3_malloc() to obtain space for the file-handle structure. |
| 15067 | +*/ |
| 15068 | +SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); |
| 15069 | +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *); |
| 15070 | + |
| 15071 | +#endif /* _SQLITE_OS_H_ */ |
| 15072 | + |
| 15073 | +/************** End of os.h **************************************************/ |
| 15074 | +/************** Continuing where we left off in sqliteInt.h ******************/ |
| 14727 | 15075 | /************** Include pager.h in the middle of sqliteInt.h *****************/ |
| 14728 | 15076 | /************** Begin file pager.h *******************************************/ |
| 14729 | 15077 | /* |
| 14730 | 15078 | ** 2001 September 15 |
| 14731 | 15079 | ** |
| | @@ -15576,11 +15924,11 @@ |
| 15576 | 15924 | #define OP_MustBeInt 13 /* jump */ |
| 15577 | 15925 | #define OP_Jump 14 /* jump */ |
| 15578 | 15926 | #define OP_Once 15 /* jump */ |
| 15579 | 15927 | #define OP_If 16 /* jump */ |
| 15580 | 15928 | #define OP_IfNot 17 /* jump */ |
| 15581 | | -#define OP_IsNullOrType 18 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */ |
| 15929 | +#define OP_IsType 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */ |
| 15582 | 15930 | #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */ |
| 15583 | 15931 | #define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */ |
| 15584 | 15932 | #define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */ |
| 15585 | 15933 | #define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */ |
| 15586 | 15934 | #define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */ |
| | @@ -15759,11 +16107,11 @@ |
| 15759 | 16107 | #define OPFLG_OUT2 0x10 /* out2: P2 is an output */ |
| 15760 | 16108 | #define OPFLG_OUT3 0x20 /* out3: P3 is an output */ |
| 15761 | 16109 | #define OPFLG_INITIALIZER {\ |
| 15762 | 16110 | /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\ |
| 15763 | 16111 | /* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\ |
| 15764 | | -/* 16 */ 0x03, 0x03, 0x03, 0x12, 0x01, 0x09, 0x09, 0x09,\ |
| 16112 | +/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x09, 0x09, 0x09,\ |
| 15765 | 16113 | /* 24 */ 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\ |
| 15766 | 16114 | /* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\ |
| 15767 | 16115 | /* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x01, 0x23, 0x0b,\ |
| 15768 | 16116 | /* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ |
| 15769 | 16117 | /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\ |
| | @@ -15856,10 +16204,11 @@ |
| 15856 | 16204 | SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8); |
| 15857 | 16205 | SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1); |
| 15858 | 16206 | SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2); |
| 15859 | 16207 | SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3); |
| 15860 | 16208 | SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5); |
| 16209 | +SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe*, int); |
| 15861 | 16210 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); |
| 15862 | 16211 | SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr); |
| 15863 | 16212 | SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr); |
| 15864 | 16213 | SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op); |
| 15865 | 16214 | #ifdef SQLITE_DEBUG |
| | @@ -16218,301 +16567,10 @@ |
| 16218 | 16567 | #endif |
| 16219 | 16568 | |
| 16220 | 16569 | #endif /* _PCACHE_H_ */ |
| 16221 | 16570 | |
| 16222 | 16571 | /************** End of pcache.h **********************************************/ |
| 16223 | | -/************** Continuing where we left off in sqliteInt.h ******************/ |
| 16224 | | -/************** Include os.h in the middle of sqliteInt.h ********************/ |
| 16225 | | -/************** Begin file os.h **********************************************/ |
| 16226 | | -/* |
| 16227 | | -** 2001 September 16 |
| 16228 | | -** |
| 16229 | | -** The author disclaims copyright to this source code. In place of |
| 16230 | | -** a legal notice, here is a blessing: |
| 16231 | | -** |
| 16232 | | -** May you do good and not evil. |
| 16233 | | -** May you find forgiveness for yourself and forgive others. |
| 16234 | | -** May you share freely, never taking more than you give. |
| 16235 | | -** |
| 16236 | | -****************************************************************************** |
| 16237 | | -** |
| 16238 | | -** This header file (together with is companion C source-code file |
| 16239 | | -** "os.c") attempt to abstract the underlying operating system so that |
| 16240 | | -** the SQLite library will work on both POSIX and windows systems. |
| 16241 | | -** |
| 16242 | | -** This header file is #include-ed by sqliteInt.h and thus ends up |
| 16243 | | -** being included by every source file. |
| 16244 | | -*/ |
| 16245 | | -#ifndef _SQLITE_OS_H_ |
| 16246 | | -#define _SQLITE_OS_H_ |
| 16247 | | - |
| 16248 | | -/* |
| 16249 | | -** Attempt to automatically detect the operating system and setup the |
| 16250 | | -** necessary pre-processor macros for it. |
| 16251 | | -*/ |
| 16252 | | -/************** Include os_setup.h in the middle of os.h *********************/ |
| 16253 | | -/************** Begin file os_setup.h ****************************************/ |
| 16254 | | -/* |
| 16255 | | -** 2013 November 25 |
| 16256 | | -** |
| 16257 | | -** The author disclaims copyright to this source code. In place of |
| 16258 | | -** a legal notice, here is a blessing: |
| 16259 | | -** |
| 16260 | | -** May you do good and not evil. |
| 16261 | | -** May you find forgiveness for yourself and forgive others. |
| 16262 | | -** May you share freely, never taking more than you give. |
| 16263 | | -** |
| 16264 | | -****************************************************************************** |
| 16265 | | -** |
| 16266 | | -** This file contains pre-processor directives related to operating system |
| 16267 | | -** detection and/or setup. |
| 16268 | | -*/ |
| 16269 | | -#ifndef SQLITE_OS_SETUP_H |
| 16270 | | -#define SQLITE_OS_SETUP_H |
| 16271 | | - |
| 16272 | | -/* |
| 16273 | | -** Figure out if we are dealing with Unix, Windows, or some other operating |
| 16274 | | -** system. |
| 16275 | | -** |
| 16276 | | -** After the following block of preprocess macros, all of SQLITE_OS_UNIX, |
| 16277 | | -** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of |
| 16278 | | -** the three will be 1. The other two will be 0. |
| 16279 | | -*/ |
| 16280 | | -#if defined(SQLITE_OS_OTHER) |
| 16281 | | -# if SQLITE_OS_OTHER==1 |
| 16282 | | -# undef SQLITE_OS_UNIX |
| 16283 | | -# define SQLITE_OS_UNIX 0 |
| 16284 | | -# undef SQLITE_OS_WIN |
| 16285 | | -# define SQLITE_OS_WIN 0 |
| 16286 | | -# else |
| 16287 | | -# undef SQLITE_OS_OTHER |
| 16288 | | -# endif |
| 16289 | | -#endif |
| 16290 | | -#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER) |
| 16291 | | -# define SQLITE_OS_OTHER 0 |
| 16292 | | -# ifndef SQLITE_OS_WIN |
| 16293 | | -# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ |
| 16294 | | - defined(__MINGW32__) || defined(__BORLANDC__) |
| 16295 | | -# define SQLITE_OS_WIN 1 |
| 16296 | | -# define SQLITE_OS_UNIX 0 |
| 16297 | | -# else |
| 16298 | | -# define SQLITE_OS_WIN 0 |
| 16299 | | -# define SQLITE_OS_UNIX 1 |
| 16300 | | -# endif |
| 16301 | | -# else |
| 16302 | | -# define SQLITE_OS_UNIX 0 |
| 16303 | | -# endif |
| 16304 | | -#else |
| 16305 | | -# ifndef SQLITE_OS_WIN |
| 16306 | | -# define SQLITE_OS_WIN 0 |
| 16307 | | -# endif |
| 16308 | | -#endif |
| 16309 | | - |
| 16310 | | -#endif /* SQLITE_OS_SETUP_H */ |
| 16311 | | - |
| 16312 | | -/************** End of os_setup.h ********************************************/ |
| 16313 | | -/************** Continuing where we left off in os.h *************************/ |
| 16314 | | - |
| 16315 | | -/* If the SET_FULLSYNC macro is not defined above, then make it |
| 16316 | | -** a no-op |
| 16317 | | -*/ |
| 16318 | | -#ifndef SET_FULLSYNC |
| 16319 | | -# define SET_FULLSYNC(x,y) |
| 16320 | | -#endif |
| 16321 | | - |
| 16322 | | -/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h |
| 16323 | | -*/ |
| 16324 | | -#ifndef SQLITE_MAX_PATHLEN |
| 16325 | | -# define SQLITE_MAX_PATHLEN FILENAME_MAX |
| 16326 | | -#endif |
| 16327 | | - |
| 16328 | | -/* Maximum number of symlinks that will be resolved while trying to |
| 16329 | | -** expand a filename in xFullPathname() in the VFS. |
| 16330 | | -*/ |
| 16331 | | -#ifndef SQLITE_MAX_SYMLINK |
| 16332 | | -# define SQLITE_MAX_SYMLINK 200 |
| 16333 | | -#endif |
| 16334 | | - |
| 16335 | | -/* |
| 16336 | | -** The default size of a disk sector |
| 16337 | | -*/ |
| 16338 | | -#ifndef SQLITE_DEFAULT_SECTOR_SIZE |
| 16339 | | -# define SQLITE_DEFAULT_SECTOR_SIZE 4096 |
| 16340 | | -#endif |
| 16341 | | - |
| 16342 | | -/* |
| 16343 | | -** Temporary files are named starting with this prefix followed by 16 random |
| 16344 | | -** alphanumeric characters, and no file extension. They are stored in the |
| 16345 | | -** OS's standard temporary file directory, and are deleted prior to exit. |
| 16346 | | -** If sqlite is being embedded in another program, you may wish to change the |
| 16347 | | -** prefix to reflect your program's name, so that if your program exits |
| 16348 | | -** prematurely, old temporary files can be easily identified. This can be done |
| 16349 | | -** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
| 16350 | | -** |
| 16351 | | -** 2006-10-31: The default prefix used to be "sqlite_". But then |
| 16352 | | -** Mcafee started using SQLite in their anti-virus product and it |
| 16353 | | -** started putting files with the "sqlite" name in the c:/temp folder. |
| 16354 | | -** This annoyed many windows users. Those users would then do a |
| 16355 | | -** Google search for "sqlite", find the telephone numbers of the |
| 16356 | | -** developers and call to wake them up at night and complain. |
| 16357 | | -** For this reason, the default name prefix is changed to be "sqlite" |
| 16358 | | -** spelled backwards. So the temp files are still identified, but |
| 16359 | | -** anybody smart enough to figure out the code is also likely smart |
| 16360 | | -** enough to know that calling the developer will not help get rid |
| 16361 | | -** of the file. |
| 16362 | | -*/ |
| 16363 | | -#ifndef SQLITE_TEMP_FILE_PREFIX |
| 16364 | | -# define SQLITE_TEMP_FILE_PREFIX "etilqs_" |
| 16365 | | -#endif |
| 16366 | | - |
| 16367 | | -/* |
| 16368 | | -** The following values may be passed as the second argument to |
| 16369 | | -** sqlite3OsLock(). The various locks exhibit the following semantics: |
| 16370 | | -** |
| 16371 | | -** SHARED: Any number of processes may hold a SHARED lock simultaneously. |
| 16372 | | -** RESERVED: A single process may hold a RESERVED lock on a file at |
| 16373 | | -** any time. Other processes may hold and obtain new SHARED locks. |
| 16374 | | -** PENDING: A single process may hold a PENDING lock on a file at |
| 16375 | | -** any one time. Existing SHARED locks may persist, but no new |
| 16376 | | -** SHARED locks may be obtained by other processes. |
| 16377 | | -** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. |
| 16378 | | -** |
| 16379 | | -** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a |
| 16380 | | -** process that requests an EXCLUSIVE lock may actually obtain a PENDING |
| 16381 | | -** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to |
| 16382 | | -** sqlite3OsLock(). |
| 16383 | | -*/ |
| 16384 | | -#define NO_LOCK 0 |
| 16385 | | -#define SHARED_LOCK 1 |
| 16386 | | -#define RESERVED_LOCK 2 |
| 16387 | | -#define PENDING_LOCK 3 |
| 16388 | | -#define EXCLUSIVE_LOCK 4 |
| 16389 | | - |
| 16390 | | -/* |
| 16391 | | -** File Locking Notes: (Mostly about windows but also some info for Unix) |
| 16392 | | -** |
| 16393 | | -** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because |
| 16394 | | -** those functions are not available. So we use only LockFile() and |
| 16395 | | -** UnlockFile(). |
| 16396 | | -** |
| 16397 | | -** LockFile() prevents not just writing but also reading by other processes. |
| 16398 | | -** A SHARED_LOCK is obtained by locking a single randomly-chosen |
| 16399 | | -** byte out of a specific range of bytes. The lock byte is obtained at |
| 16400 | | -** random so two separate readers can probably access the file at the |
| 16401 | | -** same time, unless they are unlucky and choose the same lock byte. |
| 16402 | | -** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. |
| 16403 | | -** There can only be one writer. A RESERVED_LOCK is obtained by locking |
| 16404 | | -** a single byte of the file that is designated as the reserved lock byte. |
| 16405 | | -** A PENDING_LOCK is obtained by locking a designated byte different from |
| 16406 | | -** the RESERVED_LOCK byte. |
| 16407 | | -** |
| 16408 | | -** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, |
| 16409 | | -** which means we can use reader/writer locks. When reader/writer locks |
| 16410 | | -** are used, the lock is placed on the same range of bytes that is used |
| 16411 | | -** for probabilistic locking in Win95/98/ME. Hence, the locking scheme |
| 16412 | | -** will support two or more Win95 readers or two or more WinNT readers. |
| 16413 | | -** But a single Win95 reader will lock out all WinNT readers and a single |
| 16414 | | -** WinNT reader will lock out all other Win95 readers. |
| 16415 | | -** |
| 16416 | | -** The following #defines specify the range of bytes used for locking. |
| 16417 | | -** SHARED_SIZE is the number of bytes available in the pool from which |
| 16418 | | -** a random byte is selected for a shared lock. The pool of bytes for |
| 16419 | | -** shared locks begins at SHARED_FIRST. |
| 16420 | | -** |
| 16421 | | -** The same locking strategy and |
| 16422 | | -** byte ranges are used for Unix. This leaves open the possibility of having |
| 16423 | | -** clients on win95, winNT, and unix all talking to the same shared file |
| 16424 | | -** and all locking correctly. To do so would require that samba (or whatever |
| 16425 | | -** tool is being used for file sharing) implements locks correctly between |
| 16426 | | -** windows and unix. I'm guessing that isn't likely to happen, but by |
| 16427 | | -** using the same locking range we are at least open to the possibility. |
| 16428 | | -** |
| 16429 | | -** Locking in windows is manditory. For this reason, we cannot store |
| 16430 | | -** actual data in the bytes used for locking. The pager never allocates |
| 16431 | | -** the pages involved in locking therefore. SHARED_SIZE is selected so |
| 16432 | | -** that all locks will fit on a single page even at the minimum page size. |
| 16433 | | -** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE |
| 16434 | | -** is set high so that we don't have to allocate an unused page except |
| 16435 | | -** for very large databases. But one should test the page skipping logic |
| 16436 | | -** by setting PENDING_BYTE low and running the entire regression suite. |
| 16437 | | -** |
| 16438 | | -** Changing the value of PENDING_BYTE results in a subtly incompatible |
| 16439 | | -** file format. Depending on how it is changed, you might not notice |
| 16440 | | -** the incompatibility right away, even running a full regression test. |
| 16441 | | -** The default location of PENDING_BYTE is the first byte past the |
| 16442 | | -** 1GB boundary. |
| 16443 | | -** |
| 16444 | | -*/ |
| 16445 | | -#ifdef SQLITE_OMIT_WSD |
| 16446 | | -# define PENDING_BYTE (0x40000000) |
| 16447 | | -#else |
| 16448 | | -# define PENDING_BYTE sqlite3PendingByte |
| 16449 | | -#endif |
| 16450 | | -#define RESERVED_BYTE (PENDING_BYTE+1) |
| 16451 | | -#define SHARED_FIRST (PENDING_BYTE+2) |
| 16452 | | -#define SHARED_SIZE 510 |
| 16453 | | - |
| 16454 | | -/* |
| 16455 | | -** Wrapper around OS specific sqlite3_os_init() function. |
| 16456 | | -*/ |
| 16457 | | -SQLITE_PRIVATE int sqlite3OsInit(void); |
| 16458 | | - |
| 16459 | | -/* |
| 16460 | | -** Functions for accessing sqlite3_file methods |
| 16461 | | -*/ |
| 16462 | | -SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*); |
| 16463 | | -SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); |
| 16464 | | -SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); |
| 16465 | | -SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size); |
| 16466 | | -SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int); |
| 16467 | | -SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); |
| 16468 | | -SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int); |
| 16469 | | -SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int); |
| 16470 | | -SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut); |
| 16471 | | -SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*); |
| 16472 | | -SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*); |
| 16473 | | -#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 |
| 16474 | | -SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id); |
| 16475 | | -SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id); |
| 16476 | | -#ifndef SQLITE_OMIT_WAL |
| 16477 | | -SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); |
| 16478 | | -SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int); |
| 16479 | | -SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id); |
| 16480 | | -SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int); |
| 16481 | | -#endif /* SQLITE_OMIT_WAL */ |
| 16482 | | -SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); |
| 16483 | | -SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *); |
| 16484 | | - |
| 16485 | | - |
| 16486 | | -/* |
| 16487 | | -** Functions for accessing sqlite3_vfs methods |
| 16488 | | -*/ |
| 16489 | | -SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); |
| 16490 | | -SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int); |
| 16491 | | -SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut); |
| 16492 | | -SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); |
| 16493 | | -#ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 16494 | | -SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); |
| 16495 | | -SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *); |
| 16496 | | -SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void); |
| 16497 | | -SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *); |
| 16498 | | -#endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
| 16499 | | -SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *); |
| 16500 | | -SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int); |
| 16501 | | -SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*); |
| 16502 | | -SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); |
| 16503 | | - |
| 16504 | | -/* |
| 16505 | | -** Convenience functions for opening and closing files using |
| 16506 | | -** sqlite3_malloc() to obtain space for the file-handle structure. |
| 16507 | | -*/ |
| 16508 | | -SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); |
| 16509 | | -SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *); |
| 16510 | | - |
| 16511 | | -#endif /* _SQLITE_OS_H_ */ |
| 16512 | | - |
| 16513 | | -/************** End of os.h **************************************************/ |
| 16514 | 16572 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 16515 | 16573 | /************** Include mutex.h in the middle of sqliteInt.h *****************/ |
| 16516 | 16574 | /************** Begin file mutex.h *******************************************/ |
| 16517 | 16575 | /* |
| 16518 | 16576 | ** 2007 August 28 |
| | @@ -17100,10 +17158,11 @@ |
| 17100 | 17158 | #define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */ |
| 17101 | 17159 | #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */ |
| 17102 | 17160 | #define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */ |
| 17103 | 17161 | #define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */ |
| 17104 | 17162 | /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */ |
| 17163 | +#define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */ |
| 17105 | 17164 | #define SQLITE_AllOpts 0xffffffff /* All optimizations */ |
| 17106 | 17165 | |
| 17107 | 17166 | /* |
| 17108 | 17167 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 17109 | 17168 | */ |
| | @@ -17672,11 +17731,11 @@ |
| 17672 | 17731 | ** table support is omitted from the build. |
| 17673 | 17732 | */ |
| 17674 | 17733 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 17675 | 17734 | # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB) |
| 17676 | 17735 | # define ExprIsVtab(X) \ |
| 17677 | | - ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->eTabType==TABTYP_VTAB) |
| 17736 | + ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB) |
| 17678 | 17737 | #else |
| 17679 | 17738 | # define IsVirtual(X) 0 |
| 17680 | 17739 | # define ExprIsVtab(X) 0 |
| 17681 | 17740 | #endif |
| 17682 | 17741 | |
| | @@ -17889,13 +17948,25 @@ |
| 17889 | 17948 | ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. |
| 17890 | 17949 | ** |
| 17891 | 17950 | ** The Index.onError field determines whether or not the indexed columns |
| 17892 | 17951 | ** must be unique and what to do if they are not. When Index.onError=OE_None, |
| 17893 | 17952 | ** it means this is not a unique index. Otherwise it is a unique index |
| 17894 | | -** and the value of Index.onError indicate the which conflict resolution |
| 17895 | | -** algorithm to employ whenever an attempt is made to insert a non-unique |
| 17953 | +** and the value of Index.onError indicates which conflict resolution |
| 17954 | +** algorithm to employ when an attempt is made to insert a non-unique |
| 17896 | 17955 | ** element. |
| 17956 | +** |
| 17957 | +** The colNotIdxed bitmask is used in combination with SrcItem.colUsed |
| 17958 | +** for a fast test to see if an index can serve as a covering index. |
| 17959 | +** colNotIdxed has a 1 bit for every column of the original table that |
| 17960 | +** is *not* available in the index. Thus the expression |
| 17961 | +** "colUsed & colNotIdxed" will be non-zero if the index is not a |
| 17962 | +** covering index. The most significant bit of of colNotIdxed will always |
| 17963 | +** be true (note-20221022-a). If a column beyond the 63rd column of the |
| 17964 | +** table is used, the "colUsed & colNotIdxed" test will always be non-zero |
| 17965 | +** and we have to assume either that the index is not covering, or use |
| 17966 | +** an alternative (slower) algorithm to determine whether or not |
| 17967 | +** the index is covering. |
| 17897 | 17968 | ** |
| 17898 | 17969 | ** While parsing a CREATE TABLE or CREATE INDEX statement in order to |
| 17899 | 17970 | ** generate VDBE code (as opposed to parsing one read from an sqlite_schema |
| 17900 | 17971 | ** table as part of parsing an existing database schema), transient instances |
| 17901 | 17972 | ** of this structure may be created. In this case the Index.tnum variable is |
| | @@ -17928,19 +17999,21 @@ |
| 17928 | 17999 | unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ |
| 17929 | 18000 | unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */ |
| 17930 | 18001 | unsigned bNoQuery:1; /* Do not use this index to optimize queries */ |
| 17931 | 18002 | unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */ |
| 17932 | 18003 | unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */ |
| 18004 | + unsigned bHasExpr:1; /* Index contains an expression, either a literal |
| 18005 | + ** expression, or a reference to a VIRTUAL column */ |
| 17933 | 18006 | #ifdef SQLITE_ENABLE_STAT4 |
| 17934 | 18007 | int nSample; /* Number of elements in aSample[] */ |
| 17935 | 18008 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 17936 | 18009 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 17937 | 18010 | IndexSample *aSample; /* Samples of the left-most key */ |
| 17938 | 18011 | tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ |
| 17939 | 18012 | tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ |
| 17940 | 18013 | #endif |
| 17941 | | - Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */ |
| 18014 | + Bitmask colNotIdxed; /* Unindexed columns in pTab */ |
| 17942 | 18015 | }; |
| 17943 | 18016 | |
| 17944 | 18017 | /* |
| 17945 | 18018 | ** Allowed values for Index.idxType |
| 17946 | 18019 | */ |
| | @@ -18389,10 +18462,18 @@ |
| 18389 | 18462 | |
| 18390 | 18463 | /* |
| 18391 | 18464 | ** The SrcItem object represents a single term in the FROM clause of a query. |
| 18392 | 18465 | ** The SrcList object is mostly an array of SrcItems. |
| 18393 | 18466 | ** |
| 18467 | +** The jointype starts out showing the join type between the current table |
| 18468 | +** and the next table on the list. The parser builds the list this way. |
| 18469 | +** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each |
| 18470 | +** jointype expresses the join between the table and the previous table. |
| 18471 | +** |
| 18472 | +** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 18473 | +** contains more than 63 columns and the 64-th or later column is used. |
| 18474 | +** |
| 18394 | 18475 | ** Union member validity: |
| 18395 | 18476 | ** |
| 18396 | 18477 | ** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc |
| 18397 | 18478 | ** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy |
| 18398 | 18479 | ** u2.pIBIndex fg.isIndexedBy && !fg.isCte |
| | @@ -18428,18 +18509,18 @@ |
| 18428 | 18509 | int iCursor; /* The VDBE cursor number used to access this table */ |
| 18429 | 18510 | union { |
| 18430 | 18511 | Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */ |
| 18431 | 18512 | IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */ |
| 18432 | 18513 | } u3; |
| 18433 | | - Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */ |
| 18514 | + Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */ |
| 18434 | 18515 | union { |
| 18435 | 18516 | char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */ |
| 18436 | 18517 | ExprList *pFuncArg; /* Arguments to table-valued-function */ |
| 18437 | 18518 | } u1; |
| 18438 | 18519 | union { |
| 18439 | 18520 | Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */ |
| 18440 | | - CteUse *pCteUse; /* CTE Usage info info fg.isCte is true */ |
| 18521 | + CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */ |
| 18441 | 18522 | } u2; |
| 18442 | 18523 | }; |
| 18443 | 18524 | |
| 18444 | 18525 | /* |
| 18445 | 18526 | ** The OnOrUsing object represents either an ON clause or a USING clause. |
| | @@ -18449,27 +18530,15 @@ |
| 18449 | 18530 | Expr *pOn; /* The ON clause of a join */ |
| 18450 | 18531 | IdList *pUsing; /* The USING clause of a join */ |
| 18451 | 18532 | }; |
| 18452 | 18533 | |
| 18453 | 18534 | /* |
| 18454 | | -** The following structure describes the FROM clause of a SELECT statement. |
| 18455 | | -** Each table or subquery in the FROM clause is a separate element of |
| 18456 | | -** the SrcList.a[] array. |
| 18457 | | -** |
| 18458 | | -** With the addition of multiple database support, the following structure |
| 18459 | | -** can also be used to describe a particular table such as the table that |
| 18460 | | -** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL, |
| 18461 | | -** such a table must be a simple name: ID. But in SQLite, the table can |
| 18462 | | -** now be identified by a database name, a dot, then the table name: ID.ID. |
| 18463 | | -** |
| 18464 | | -** The jointype starts out showing the join type between the current table |
| 18465 | | -** and the next table on the list. The parser builds the list this way. |
| 18466 | | -** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each |
| 18467 | | -** jointype expresses the join between the table and the previous table. |
| 18468 | | -** |
| 18469 | | -** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 18470 | | -** contains more than 63 columns and the 64-th or later column is used. |
| 18535 | +** This object represents one or more tables that are the source of |
| 18536 | +** content for an SQL statement. For example, a single SrcList object |
| 18537 | +** is used to hold the FROM clause of a SELECT statement. SrcList also |
| 18538 | +** represents the target tables for DELETE, INSERT, and UPDATE statements. |
| 18539 | +** |
| 18471 | 18540 | */ |
| 18472 | 18541 | struct SrcList { |
| 18473 | 18542 | int nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 18474 | 18543 | u32 nAlloc; /* Number of entries allocated in a[] below */ |
| 18475 | 18544 | SrcItem a[1]; /* One entry for each identifier on the list */ |
| | @@ -18875,10 +18944,32 @@ |
| 18875 | 18944 | # define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I)) |
| 18876 | 18945 | # define DbMaskAllZero(M) (M)==0 |
| 18877 | 18946 | # define DbMaskNonZero(M) (M)!=0 |
| 18878 | 18947 | #endif |
| 18879 | 18948 | |
| 18949 | +/* |
| 18950 | +** For each index X that has as one of its arguments either an expression |
| 18951 | +** or the name of a virtual generated column, and if X is in scope such that |
| 18952 | +** the value of the expression can simply be read from the index, then |
| 18953 | +** there is an instance of this object on the Parse.pIdxExpr list. |
| 18954 | +** |
| 18955 | +** During code generation, while generating code to evaluate expressions, |
| 18956 | +** this list is consulted and if a matching expression is found, the value |
| 18957 | +** is read from the index rather than being recomputed. |
| 18958 | +*/ |
| 18959 | +struct IndexedExpr { |
| 18960 | + Expr *pExpr; /* The expression contained in the index */ |
| 18961 | + int iDataCur; /* The data cursor associated with the index */ |
| 18962 | + int iIdxCur; /* The index cursor */ |
| 18963 | + int iIdxCol; /* The index column that contains value of pExpr */ |
| 18964 | + u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */ |
| 18965 | + IndexedExpr *pIENext; /* Next in a list of all indexed expressions */ |
| 18966 | +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 18967 | + const char *zIdxName; /* Name of index, used only for bytecode comments */ |
| 18968 | +#endif |
| 18969 | +}; |
| 18970 | + |
| 18880 | 18971 | /* |
| 18881 | 18972 | ** An instance of the ParseCleanup object specifies an operation that |
| 18882 | 18973 | ** should be performed after parsing to deallocation resources obtained |
| 18883 | 18974 | ** during the parse and which are no longer needed. |
| 18884 | 18975 | */ |
| | @@ -18916,11 +19007,11 @@ |
| 18916 | 19007 | u8 isMultiWrite; /* True if statement may modify/insert multiple rows */ |
| 18917 | 19008 | u8 mayAbort; /* True if statement may throw an ABORT exception */ |
| 18918 | 19009 | u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */ |
| 18919 | 19010 | u8 okConstFactor; /* OK to factor out constants */ |
| 18920 | 19011 | u8 disableLookaside; /* Number of times lookaside has been disabled */ |
| 18921 | | - u8 disableVtab; /* Disable all virtual tables for this parse */ |
| 19012 | + u8 prepFlags; /* SQLITE_PREPARE_* flags */ |
| 18922 | 19013 | u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */ |
| 18923 | 19014 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 18924 | 19015 | u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */ |
| 18925 | 19016 | #endif |
| 18926 | 19017 | int nRangeReg; /* Size of the temporary register block */ |
| | @@ -18933,10 +19024,11 @@ |
| 18933 | 19024 | ** of the base register during check-constraint eval */ |
| 18934 | 19025 | int nLabel; /* The *negative* of the number of labels used */ |
| 18935 | 19026 | int nLabelAlloc; /* Number of slots in aLabel */ |
| 18936 | 19027 | int *aLabel; /* Space to hold the labels */ |
| 18937 | 19028 | ExprList *pConstExpr;/* Constant expressions */ |
| 19029 | + IndexedExpr *pIdxExpr;/* List of expressions used by active indexes */ |
| 18938 | 19030 | Token constraintName;/* Name of the constraint currently being parsed */ |
| 18939 | 19031 | yDbMask writeMask; /* Start a write transaction on these databases */ |
| 18940 | 19032 | yDbMask cookieMask; /* Bitmask of schema verified databases */ |
| 18941 | 19033 | int regRowid; /* Register holding rowid of CREATE TABLE entry */ |
| 18942 | 19034 | int regRoot; /* Register holding root page number for new objects */ |
| | @@ -19368,19 +19460,19 @@ |
| 19368 | 19460 | SrcList *pSrcList; /* FROM clause */ |
| 19369 | 19461 | struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ |
| 19370 | 19462 | struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */ |
| 19371 | 19463 | int *aiCol; /* array of column indexes */ |
| 19372 | 19464 | struct IdxCover *pIdxCover; /* Check for index coverage */ |
| 19373 | | - struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */ |
| 19374 | 19465 | ExprList *pGroupBy; /* GROUP BY clause */ |
| 19375 | 19466 | Select *pSelect; /* HAVING to WHERE clause ctx */ |
| 19376 | 19467 | struct WindowRewrite *pRewrite; /* Window rewrite context */ |
| 19377 | 19468 | struct WhereConst *pConst; /* WHERE clause constants */ |
| 19378 | 19469 | struct RenameCtx *pRename; /* RENAME COLUMN context */ |
| 19379 | 19470 | struct Table *pTab; /* Table of generated column */ |
| 19471 | + struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */ |
| 19380 | 19472 | SrcItem *pSrcItem; /* A single FROM clause item */ |
| 19381 | | - DbFixer *pFix; |
| 19473 | + DbFixer *pFix; /* See sqlite3FixSelect() */ |
| 19382 | 19474 | } u; |
| 19383 | 19475 | }; |
| 19384 | 19476 | |
| 19385 | 19477 | /* |
| 19386 | 19478 | ** The following structure contains information used by the sqliteFix... |
| | @@ -19703,16 +19795,20 @@ |
| 19703 | 19795 | ** The alloca() routine never returns NULL. This will cause code paths |
| 19704 | 19796 | ** that deal with sqlite3StackAlloc() failures to be unreachable. |
| 19705 | 19797 | */ |
| 19706 | 19798 | #ifdef SQLITE_USE_ALLOCA |
| 19707 | 19799 | # define sqlite3StackAllocRaw(D,N) alloca(N) |
| 19800 | +# define sqlite3StackAllocRawNN(D,N) alloca(N) |
| 19708 | 19801 | # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N) |
| 19709 | 19802 | # define sqlite3StackFree(D,P) |
| 19803 | +# define sqlite3StackFreeNN(D,P) |
| 19710 | 19804 | #else |
| 19711 | 19805 | # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N) |
| 19806 | +# define sqlite3StackAllocRawNN(D,N) sqlite3DbMallocRawNN(D,N) |
| 19712 | 19807 | # define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N) |
| 19713 | 19808 | # define sqlite3StackFree(D,P) sqlite3DbFree(D,P) |
| 19809 | +# define sqlite3StackFreeNN(D,P) sqlite3DbFreeNN(D,P) |
| 19714 | 19810 | #endif |
| 19715 | 19811 | |
| 19716 | 19812 | /* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they |
| 19717 | 19813 | ** are, disable MEMSYS3 |
| 19718 | 19814 | */ |
| | @@ -20767,10 +20863,14 @@ |
| 20767 | 20863 | SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*); |
| 20768 | 20864 | |
| 20769 | 20865 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 20770 | 20866 | SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt); |
| 20771 | 20867 | #endif |
| 20868 | + |
| 20869 | +#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) |
| 20870 | +SQLITE_PRIVATE int sqlite3KvvfsInit(void); |
| 20871 | +#endif |
| 20772 | 20872 | |
| 20773 | 20873 | #endif /* SQLITEINT_H */ |
| 20774 | 20874 | |
| 20775 | 20875 | /************** End of sqliteInt.h *******************************************/ |
| 20776 | 20876 | /************** Begin file os_common.h ***************************************/ |
| | @@ -20999,11 +21099,11 @@ |
| 20999 | 21099 | /* |
| 21000 | 21100 | ** Include the configuration header output by 'configure' if we're using the |
| 21001 | 21101 | ** autoconf-based build |
| 21002 | 21102 | */ |
| 21003 | 21103 | #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H) |
| 21004 | | -/* #include "config.h" */ |
| 21104 | +/* #include "sqlite_cfg.h" */ |
| 21005 | 21105 | #define SQLITECONFIG_H 1 |
| 21006 | 21106 | #endif |
| 21007 | 21107 | |
| 21008 | 21108 | /* These macros are provided to "stringify" the value of the define |
| 21009 | 21109 | ** for those options in which the value is meaningful. */ |
| | @@ -21164,10 +21264,13 @@ |
| 21164 | 21264 | "DISABLE_PAGECACHE_OVERFLOW_STATS", |
| 21165 | 21265 | #endif |
| 21166 | 21266 | #ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT |
| 21167 | 21267 | "DISABLE_SKIPAHEAD_DISTINCT", |
| 21168 | 21268 | #endif |
| 21269 | +#ifdef SQLITE_DQS |
| 21270 | + "DQS=" CTIMEOPT_VAL(SQLITE_DQS), |
| 21271 | +#endif |
| 21169 | 21272 | #ifdef SQLITE_ENABLE_8_3_NAMES |
| 21170 | 21273 | "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES), |
| 21171 | 21274 | #endif |
| 21172 | 21275 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 21173 | 21276 | "ENABLE_API_ARMOR", |
| | @@ -24740,13 +24843,15 @@ |
| 24740 | 24843 | DO_OS_MALLOC_TEST(id); |
| 24741 | 24844 | return id->pMethods->xFileSize(id, pSize); |
| 24742 | 24845 | } |
| 24743 | 24846 | SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){ |
| 24744 | 24847 | DO_OS_MALLOC_TEST(id); |
| 24848 | + assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE ); |
| 24745 | 24849 | return id->pMethods->xLock(id, lockType); |
| 24746 | 24850 | } |
| 24747 | 24851 | SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){ |
| 24852 | + assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED ); |
| 24748 | 24853 | return id->pMethods->xUnlock(id, lockType); |
| 24749 | 24854 | } |
| 24750 | 24855 | SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 24751 | 24856 | DO_OS_MALLOC_TEST(id); |
| 24752 | 24857 | return id->pMethods->xCheckReservedLock(id, pResOut); |
| | @@ -35382,11 +35487,11 @@ |
| 35382 | 35487 | /* 13 */ "MustBeInt" OpHelp(""), |
| 35383 | 35488 | /* 14 */ "Jump" OpHelp(""), |
| 35384 | 35489 | /* 15 */ "Once" OpHelp(""), |
| 35385 | 35490 | /* 16 */ "If" OpHelp(""), |
| 35386 | 35491 | /* 17 */ "IfNot" OpHelp(""), |
| 35387 | | - /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"), |
| 35492 | + /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"), |
| 35388 | 35493 | /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), |
| 35389 | 35494 | /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"), |
| 35390 | 35495 | /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 35391 | 35496 | /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 35392 | 35497 | /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| | @@ -35557,10 +35662,982 @@ |
| 35557 | 35662 | return azName[i]; |
| 35558 | 35663 | } |
| 35559 | 35664 | #endif |
| 35560 | 35665 | |
| 35561 | 35666 | /************** End of opcodes.c *********************************************/ |
| 35667 | +/************** Begin file os_kv.c *******************************************/ |
| 35668 | +/* |
| 35669 | +** 2022-09-06 |
| 35670 | +** |
| 35671 | +** The author disclaims copyright to this source code. In place of |
| 35672 | +** a legal notice, here is a blessing: |
| 35673 | +** |
| 35674 | +** May you do good and not evil. |
| 35675 | +** May you find forgiveness for yourself and forgive others. |
| 35676 | +** May you share freely, never taking more than you give. |
| 35677 | +** |
| 35678 | +****************************************************************************** |
| 35679 | +** |
| 35680 | +** This file contains an experimental VFS layer that operates on a |
| 35681 | +** Key/Value storage engine where both keys and values must be pure |
| 35682 | +** text. |
| 35683 | +*/ |
| 35684 | +/* #include <sqliteInt.h> */ |
| 35685 | +#if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)) |
| 35686 | + |
| 35687 | +/***************************************************************************** |
| 35688 | +** Debugging logic |
| 35689 | +*/ |
| 35690 | + |
| 35691 | +/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */ |
| 35692 | +#if 0 |
| 35693 | +#define SQLITE_KV_TRACE(X) printf X |
| 35694 | +#else |
| 35695 | +#define SQLITE_KV_TRACE(X) |
| 35696 | +#endif |
| 35697 | + |
| 35698 | +/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */ |
| 35699 | +#if 0 |
| 35700 | +#define SQLITE_KV_LOG(X) printf X |
| 35701 | +#else |
| 35702 | +#define SQLITE_KV_LOG(X) |
| 35703 | +#endif |
| 35704 | + |
| 35705 | + |
| 35706 | +/* |
| 35707 | +** Forward declaration of objects used by this VFS implementation |
| 35708 | +*/ |
| 35709 | +typedef struct KVVfsFile KVVfsFile; |
| 35710 | + |
| 35711 | +/* A single open file. There are only two files represented by this |
| 35712 | +** VFS - the database and the rollback journal. |
| 35713 | +*/ |
| 35714 | +struct KVVfsFile { |
| 35715 | + sqlite3_file base; /* IO methods */ |
| 35716 | + const char *zClass; /* Storage class */ |
| 35717 | + int isJournal; /* True if this is a journal file */ |
| 35718 | + unsigned int nJrnl; /* Space allocated for aJrnl[] */ |
| 35719 | + char *aJrnl; /* Journal content */ |
| 35720 | + int szPage; /* Last known page size */ |
| 35721 | + sqlite3_int64 szDb; /* Database file size. -1 means unknown */ |
| 35722 | +}; |
| 35723 | + |
| 35724 | +/* |
| 35725 | +** Methods for KVVfsFile |
| 35726 | +*/ |
| 35727 | +static int kvvfsClose(sqlite3_file*); |
| 35728 | +static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 35729 | +static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 35730 | +static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64); |
| 35731 | +static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64); |
| 35732 | +static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size); |
| 35733 | +static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size); |
| 35734 | +static int kvvfsSyncDb(sqlite3_file*, int flags); |
| 35735 | +static int kvvfsSyncJrnl(sqlite3_file*, int flags); |
| 35736 | +static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize); |
| 35737 | +static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize); |
| 35738 | +static int kvvfsLock(sqlite3_file*, int); |
| 35739 | +static int kvvfsUnlock(sqlite3_file*, int); |
| 35740 | +static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut); |
| 35741 | +static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg); |
| 35742 | +static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg); |
| 35743 | +static int kvvfsSectorSize(sqlite3_file*); |
| 35744 | +static int kvvfsDeviceCharacteristics(sqlite3_file*); |
| 35745 | + |
| 35746 | +/* |
| 35747 | +** Methods for sqlite3_vfs |
| 35748 | +*/ |
| 35749 | +static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 35750 | +static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 35751 | +static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 35752 | +static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
| 35753 | +static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename); |
| 35754 | +static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 35755 | +static int kvvfsSleep(sqlite3_vfs*, int microseconds); |
| 35756 | +static int kvvfsCurrentTime(sqlite3_vfs*, double*); |
| 35757 | +static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 35758 | + |
| 35759 | +static sqlite3_vfs sqlite3OsKvvfsObject = { |
| 35760 | + 1, /* iVersion */ |
| 35761 | + sizeof(KVVfsFile), /* szOsFile */ |
| 35762 | + 1024, /* mxPathname */ |
| 35763 | + 0, /* pNext */ |
| 35764 | + "kvvfs", /* zName */ |
| 35765 | + 0, /* pAppData */ |
| 35766 | + kvvfsOpen, /* xOpen */ |
| 35767 | + kvvfsDelete, /* xDelete */ |
| 35768 | + kvvfsAccess, /* xAccess */ |
| 35769 | + kvvfsFullPathname, /* xFullPathname */ |
| 35770 | + kvvfsDlOpen, /* xDlOpen */ |
| 35771 | + 0, /* xDlError */ |
| 35772 | + 0, /* xDlSym */ |
| 35773 | + 0, /* xDlClose */ |
| 35774 | + kvvfsRandomness, /* xRandomness */ |
| 35775 | + kvvfsSleep, /* xSleep */ |
| 35776 | + kvvfsCurrentTime, /* xCurrentTime */ |
| 35777 | + 0, /* xGetLastError */ |
| 35778 | + kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */ |
| 35779 | +}; |
| 35780 | + |
| 35781 | +/* Methods for sqlite3_file objects referencing a database file |
| 35782 | +*/ |
| 35783 | +static sqlite3_io_methods kvvfs_db_io_methods = { |
| 35784 | + 1, /* iVersion */ |
| 35785 | + kvvfsClose, /* xClose */ |
| 35786 | + kvvfsReadDb, /* xRead */ |
| 35787 | + kvvfsWriteDb, /* xWrite */ |
| 35788 | + kvvfsTruncateDb, /* xTruncate */ |
| 35789 | + kvvfsSyncDb, /* xSync */ |
| 35790 | + kvvfsFileSizeDb, /* xFileSize */ |
| 35791 | + kvvfsLock, /* xLock */ |
| 35792 | + kvvfsUnlock, /* xUnlock */ |
| 35793 | + kvvfsCheckReservedLock, /* xCheckReservedLock */ |
| 35794 | + kvvfsFileControlDb, /* xFileControl */ |
| 35795 | + kvvfsSectorSize, /* xSectorSize */ |
| 35796 | + kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 35797 | + 0, /* xShmMap */ |
| 35798 | + 0, /* xShmLock */ |
| 35799 | + 0, /* xShmBarrier */ |
| 35800 | + 0, /* xShmUnmap */ |
| 35801 | + 0, /* xFetch */ |
| 35802 | + 0 /* xUnfetch */ |
| 35803 | +}; |
| 35804 | + |
| 35805 | +/* Methods for sqlite3_file objects referencing a rollback journal |
| 35806 | +*/ |
| 35807 | +static sqlite3_io_methods kvvfs_jrnl_io_methods = { |
| 35808 | + 1, /* iVersion */ |
| 35809 | + kvvfsClose, /* xClose */ |
| 35810 | + kvvfsReadJrnl, /* xRead */ |
| 35811 | + kvvfsWriteJrnl, /* xWrite */ |
| 35812 | + kvvfsTruncateJrnl, /* xTruncate */ |
| 35813 | + kvvfsSyncJrnl, /* xSync */ |
| 35814 | + kvvfsFileSizeJrnl, /* xFileSize */ |
| 35815 | + kvvfsLock, /* xLock */ |
| 35816 | + kvvfsUnlock, /* xUnlock */ |
| 35817 | + kvvfsCheckReservedLock, /* xCheckReservedLock */ |
| 35818 | + kvvfsFileControlJrnl, /* xFileControl */ |
| 35819 | + kvvfsSectorSize, /* xSectorSize */ |
| 35820 | + kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 35821 | + 0, /* xShmMap */ |
| 35822 | + 0, /* xShmLock */ |
| 35823 | + 0, /* xShmBarrier */ |
| 35824 | + 0, /* xShmUnmap */ |
| 35825 | + 0, /* xFetch */ |
| 35826 | + 0 /* xUnfetch */ |
| 35827 | +}; |
| 35828 | + |
| 35829 | +/****** Storage subsystem **************************************************/ |
| 35830 | +#include <sys/types.h> |
| 35831 | +#include <sys/stat.h> |
| 35832 | +#include <unistd.h> |
| 35833 | + |
| 35834 | +/* Forward declarations for the low-level storage engine |
| 35835 | +*/ |
| 35836 | +static int kvstorageWrite(const char*, const char *zKey, const char *zData); |
| 35837 | +static int kvstorageDelete(const char*, const char *zKey); |
| 35838 | +static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf); |
| 35839 | +#define KVSTORAGE_KEY_SZ 32 |
| 35840 | + |
| 35841 | +/* Expand the key name with an appropriate prefix and put the result |
| 35842 | +** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least |
| 35843 | +** KVSTORAGE_KEY_SZ bytes. |
| 35844 | +*/ |
| 35845 | +static void kvstorageMakeKey( |
| 35846 | + const char *zClass, |
| 35847 | + const char *zKeyIn, |
| 35848 | + char *zKeyOut |
| 35849 | +){ |
| 35850 | + sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn); |
| 35851 | +} |
| 35852 | + |
| 35853 | +/* Write content into a key. zClass is the particular namespace of the |
| 35854 | +** underlying key/value store to use - either "local" or "session". |
| 35855 | +** |
| 35856 | +** Both zKey and zData are zero-terminated pure text strings. |
| 35857 | +** |
| 35858 | +** Return the number of errors. |
| 35859 | +*/ |
| 35860 | +static int kvstorageWrite( |
| 35861 | + const char *zClass, |
| 35862 | + const char *zKey, |
| 35863 | + const char *zData |
| 35864 | +){ |
| 35865 | + FILE *fd; |
| 35866 | + char zXKey[KVSTORAGE_KEY_SZ]; |
| 35867 | + kvstorageMakeKey(zClass, zKey, zXKey); |
| 35868 | + fd = fopen(zXKey, "wb"); |
| 35869 | + if( fd ){ |
| 35870 | + SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey, |
| 35871 | + (int)strlen(zData), zData, |
| 35872 | + strlen(zData)>50 ? "..." : "")); |
| 35873 | + fputs(zData, fd); |
| 35874 | + fclose(fd); |
| 35875 | + return 0; |
| 35876 | + }else{ |
| 35877 | + return 1; |
| 35878 | + } |
| 35879 | +} |
| 35880 | + |
| 35881 | +/* Delete a key (with its corresponding data) from the key/value |
| 35882 | +** namespace given by zClass. If the key does not previously exist, |
| 35883 | +** this routine is a no-op. |
| 35884 | +*/ |
| 35885 | +static int kvstorageDelete(const char *zClass, const char *zKey){ |
| 35886 | + char zXKey[KVSTORAGE_KEY_SZ]; |
| 35887 | + kvstorageMakeKey(zClass, zKey, zXKey); |
| 35888 | + unlink(zXKey); |
| 35889 | + SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey)); |
| 35890 | + return 0; |
| 35891 | +} |
| 35892 | + |
| 35893 | +/* Read the value associated with a zKey from the key/value namespace given |
| 35894 | +** by zClass and put the text data associated with that key in the first |
| 35895 | +** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large |
| 35896 | +** enough to hold it all. The value put into zBuf must always be zero |
| 35897 | +** terminated, even if it gets truncated because nBuf is not large enough. |
| 35898 | +** |
| 35899 | +** Return the total number of bytes in the data, without truncation, and |
| 35900 | +** not counting the final zero terminator. Return -1 if the key does |
| 35901 | +** not exist. |
| 35902 | +** |
| 35903 | +** If nBuf<=0 then this routine simply returns the size of the data without |
| 35904 | +** actually reading it. |
| 35905 | +*/ |
| 35906 | +static int kvstorageRead( |
| 35907 | + const char *zClass, |
| 35908 | + const char *zKey, |
| 35909 | + char *zBuf, |
| 35910 | + int nBuf |
| 35911 | +){ |
| 35912 | + FILE *fd; |
| 35913 | + struct stat buf; |
| 35914 | + char zXKey[KVSTORAGE_KEY_SZ]; |
| 35915 | + kvstorageMakeKey(zClass, zKey, zXKey); |
| 35916 | + if( access(zXKey, R_OK)!=0 |
| 35917 | + || stat(zXKey, &buf)!=0 |
| 35918 | + || !S_ISREG(buf.st_mode) |
| 35919 | + ){ |
| 35920 | + SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); |
| 35921 | + return -1; |
| 35922 | + } |
| 35923 | + if( nBuf<=0 ){ |
| 35924 | + return (int)buf.st_size; |
| 35925 | + }else if( nBuf==1 ){ |
| 35926 | + zBuf[0] = 0; |
| 35927 | + SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey, |
| 35928 | + (int)buf.st_size)); |
| 35929 | + return (int)buf.st_size; |
| 35930 | + } |
| 35931 | + if( nBuf > buf.st_size + 1 ){ |
| 35932 | + nBuf = buf.st_size + 1; |
| 35933 | + } |
| 35934 | + fd = fopen(zXKey, "rb"); |
| 35935 | + if( fd==0 ){ |
| 35936 | + SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); |
| 35937 | + return -1; |
| 35938 | + }else{ |
| 35939 | + sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd); |
| 35940 | + fclose(fd); |
| 35941 | + zBuf[n] = 0; |
| 35942 | + SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey, |
| 35943 | + n, zBuf, n>50 ? "..." : "")); |
| 35944 | + return (int)n; |
| 35945 | + } |
| 35946 | +} |
| 35947 | + |
| 35948 | +/* |
| 35949 | +** An internal level of indirection which enables us to replace the |
| 35950 | +** kvvfs i/o methods with JavaScript implementations in WASM builds. |
| 35951 | +** Maintenance reminder: if this struct changes in any way, the JSON |
| 35952 | +** rendering of its structure must be updated in |
| 35953 | +** sqlite3_wasm_enum_json(). There are no binary compatibility |
| 35954 | +** concerns, so it does not need an iVersion member. This file is |
| 35955 | +** necessarily always compiled together with sqlite3_wasm_enum_json(), |
| 35956 | +** and JS code dynamically creates the mapping of members based on |
| 35957 | +** that JSON description. |
| 35958 | +*/ |
| 35959 | +typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods; |
| 35960 | +struct sqlite3_kvvfs_methods { |
| 35961 | + int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf); |
| 35962 | + int (*xWrite)(const char *zClass, const char *zKey, const char *zData); |
| 35963 | + int (*xDelete)(const char *zClass, const char *zKey); |
| 35964 | + const int nKeySize; |
| 35965 | +}; |
| 35966 | + |
| 35967 | +/* |
| 35968 | +** This object holds the kvvfs I/O methods which may be swapped out |
| 35969 | +** for JavaScript-side implementations in WASM builds. In such builds |
| 35970 | +** it cannot be const, but in native builds it should be so that |
| 35971 | +** the compiler can hopefully optimize this level of indirection out. |
| 35972 | +** That said, kvvfs is intended primarily for use in WASM builds. |
| 35973 | +** |
| 35974 | +** Note that this is not explicitly flagged as static because the |
| 35975 | +** amalgamation build will tag it with SQLITE_PRIVATE. |
| 35976 | +*/ |
| 35977 | +#ifndef SQLITE_WASM |
| 35978 | +const |
| 35979 | +#endif |
| 35980 | +SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = { |
| 35981 | +kvstorageRead, |
| 35982 | +kvstorageWrite, |
| 35983 | +kvstorageDelete, |
| 35984 | +KVSTORAGE_KEY_SZ |
| 35985 | +}; |
| 35986 | + |
| 35987 | +/****** Utility subroutines ************************************************/ |
| 35988 | + |
| 35989 | +/* |
| 35990 | +** Encode binary into the text encoded used to persist on disk. |
| 35991 | +** The output text is stored in aOut[], which must be at least |
| 35992 | +** nData+1 bytes in length. |
| 35993 | +** |
| 35994 | +** Return the actual length of the encoded text, not counting the |
| 35995 | +** zero terminator at the end. |
| 35996 | +** |
| 35997 | +** Encoding format |
| 35998 | +** --------------- |
| 35999 | +** |
| 36000 | +** * Non-zero bytes are encoded as upper-case hexadecimal |
| 36001 | +** |
| 36002 | +** * A sequence of one or more zero-bytes that are not at the |
| 36003 | +** beginning of the buffer are encoded as a little-endian |
| 36004 | +** base-26 number using a..z. "a" means 0. "b" means 1, |
| 36005 | +** "z" means 25. "ab" means 26. "ac" means 52. And so forth. |
| 36006 | +** |
| 36007 | +** * Because there is no overlap between the encoding characters |
| 36008 | +** of hexadecimal and base-26 numbers, it is always clear where |
| 36009 | +** one stops and the next begins. |
| 36010 | +*/ |
| 36011 | +static int kvvfsEncode(const char *aData, int nData, char *aOut){ |
| 36012 | + int i, j; |
| 36013 | + const unsigned char *a = (const unsigned char*)aData; |
| 36014 | + for(i=j=0; i<nData; i++){ |
| 36015 | + unsigned char c = a[i]; |
| 36016 | + if( c!=0 ){ |
| 36017 | + aOut[j++] = "0123456789ABCDEF"[c>>4]; |
| 36018 | + aOut[j++] = "0123456789ABCDEF"[c&0xf]; |
| 36019 | + }else{ |
| 36020 | + /* A sequence of 1 or more zeros is stored as a little-endian |
| 36021 | + ** base-26 number using a..z as the digits. So one zero is "b". |
| 36022 | + ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb", |
| 36023 | + ** and so forth. |
| 36024 | + */ |
| 36025 | + int k; |
| 36026 | + for(k=1; i+k<nData && a[i+k]==0; k++){} |
| 36027 | + i += k-1; |
| 36028 | + while( k>0 ){ |
| 36029 | + aOut[j++] = 'a'+(k%26); |
| 36030 | + k /= 26; |
| 36031 | + } |
| 36032 | + } |
| 36033 | + } |
| 36034 | + aOut[j] = 0; |
| 36035 | + return j; |
| 36036 | +} |
| 36037 | + |
| 36038 | +static const signed char kvvfsHexValue[256] = { |
| 36039 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36040 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36041 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36042 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 36043 | + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36044 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36045 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36046 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36047 | + |
| 36048 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36049 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36050 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36051 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36052 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36053 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36054 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 36055 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 36056 | +}; |
| 36057 | + |
| 36058 | +/* |
| 36059 | +** Decode the text encoding back to binary. The binary content is |
| 36060 | +** written into pOut, which must be at least nOut bytes in length. |
| 36061 | +** |
| 36062 | +** The return value is the number of bytes actually written into aOut[]. |
| 36063 | +*/ |
| 36064 | +static int kvvfsDecode(const char *a, char *aOut, int nOut){ |
| 36065 | + int i, j; |
| 36066 | + int c; |
| 36067 | + const unsigned char *aIn = (const unsigned char*)a; |
| 36068 | + i = 0; |
| 36069 | + j = 0; |
| 36070 | + while( 1 ){ |
| 36071 | + c = kvvfsHexValue[aIn[i]]; |
| 36072 | + if( c<0 ){ |
| 36073 | + int n = 0; |
| 36074 | + int mult = 1; |
| 36075 | + c = aIn[i]; |
| 36076 | + if( c==0 ) break; |
| 36077 | + while( c>='a' && c<='z' ){ |
| 36078 | + n += (c - 'a')*mult; |
| 36079 | + mult *= 26; |
| 36080 | + c = aIn[++i]; |
| 36081 | + } |
| 36082 | + if( j+n>nOut ) return -1; |
| 36083 | + memset(&aOut[j], 0, n); |
| 36084 | + j += n; |
| 36085 | + c = aIn[i]; |
| 36086 | + if( c==0 ) break; |
| 36087 | + }else{ |
| 36088 | + aOut[j] = c<<4; |
| 36089 | + c = kvvfsHexValue[aIn[++i]]; |
| 36090 | + if( c<0 ) break; |
| 36091 | + aOut[j++] += c; |
| 36092 | + i++; |
| 36093 | + } |
| 36094 | + } |
| 36095 | + return j; |
| 36096 | +} |
| 36097 | + |
| 36098 | +/* |
| 36099 | +** Decode a complete journal file. Allocate space in pFile->aJrnl |
| 36100 | +** and store the decoding there. Or leave pFile->aJrnl set to NULL |
| 36101 | +** if an error is encountered. |
| 36102 | +** |
| 36103 | +** The first few characters of the text encoding will be a little-endian |
| 36104 | +** base-26 number (digits a..z) that is the total number of bytes |
| 36105 | +** in the decoded journal file image. This base-26 number is followed |
| 36106 | +** by a single space, then the encoding of the journal. The space |
| 36107 | +** separator is required to act as a terminator for the base-26 number. |
| 36108 | +*/ |
| 36109 | +static void kvvfsDecodeJournal( |
| 36110 | + KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */ |
| 36111 | + const char *zTxt, /* Text encoding. Zero-terminated */ |
| 36112 | + int nTxt /* Bytes in zTxt, excluding zero terminator */ |
| 36113 | +){ |
| 36114 | + unsigned int n = 0; |
| 36115 | + int c, i, mult; |
| 36116 | + i = 0; |
| 36117 | + mult = 1; |
| 36118 | + while( (c = zTxt[i++])>='a' && c<='z' ){ |
| 36119 | + n += (zTxt[i] - 'a')*mult; |
| 36120 | + mult *= 26; |
| 36121 | + } |
| 36122 | + sqlite3_free(pFile->aJrnl); |
| 36123 | + pFile->aJrnl = sqlite3_malloc64( n ); |
| 36124 | + if( pFile->aJrnl==0 ){ |
| 36125 | + pFile->nJrnl = 0; |
| 36126 | + return; |
| 36127 | + } |
| 36128 | + pFile->nJrnl = n; |
| 36129 | + n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl); |
| 36130 | + if( n<pFile->nJrnl ){ |
| 36131 | + sqlite3_free(pFile->aJrnl); |
| 36132 | + pFile->aJrnl = 0; |
| 36133 | + pFile->nJrnl = 0; |
| 36134 | + } |
| 36135 | +} |
| 36136 | + |
| 36137 | +/* |
| 36138 | +** Read or write the "sz" element, containing the database file size. |
| 36139 | +*/ |
| 36140 | +static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){ |
| 36141 | + char zData[50]; |
| 36142 | + zData[0] = 0; |
| 36143 | + sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1); |
| 36144 | + return strtoll(zData, 0, 0); |
| 36145 | +} |
| 36146 | +static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){ |
| 36147 | + char zData[50]; |
| 36148 | + sqlite3_snprintf(sizeof(zData), zData, "%lld", sz); |
| 36149 | + return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData); |
| 36150 | +} |
| 36151 | + |
| 36152 | +/****** sqlite3_io_methods methods ******************************************/ |
| 36153 | + |
| 36154 | +/* |
| 36155 | +** Close an kvvfs-file. |
| 36156 | +*/ |
| 36157 | +static int kvvfsClose(sqlite3_file *pProtoFile){ |
| 36158 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36159 | + |
| 36160 | + SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass, |
| 36161 | + pFile->isJournal ? "journal" : "db")); |
| 36162 | + sqlite3_free(pFile->aJrnl); |
| 36163 | + return SQLITE_OK; |
| 36164 | +} |
| 36165 | + |
| 36166 | +/* |
| 36167 | +** Read from the -journal file. |
| 36168 | +*/ |
| 36169 | +static int kvvfsReadJrnl( |
| 36170 | + sqlite3_file *pProtoFile, |
| 36171 | + void *zBuf, |
| 36172 | + int iAmt, |
| 36173 | + sqlite_int64 iOfst |
| 36174 | +){ |
| 36175 | + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 36176 | + assert( pFile->isJournal ); |
| 36177 | + SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 36178 | + if( pFile->aJrnl==0 ){ |
| 36179 | + int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0); |
| 36180 | + char *aTxt; |
| 36181 | + if( szTxt<=4 ){ |
| 36182 | + return SQLITE_IOERR; |
| 36183 | + } |
| 36184 | + aTxt = sqlite3_malloc64( szTxt+1 ); |
| 36185 | + if( aTxt==0 ) return SQLITE_NOMEM; |
| 36186 | + kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1); |
| 36187 | + kvvfsDecodeJournal(pFile, aTxt, szTxt); |
| 36188 | + sqlite3_free(aTxt); |
| 36189 | + if( pFile->aJrnl==0 ) return SQLITE_IOERR; |
| 36190 | + } |
| 36191 | + if( iOfst+iAmt>pFile->nJrnl ){ |
| 36192 | + return SQLITE_IOERR_SHORT_READ; |
| 36193 | + } |
| 36194 | + memcpy(zBuf, pFile->aJrnl+iOfst, iAmt); |
| 36195 | + return SQLITE_OK; |
| 36196 | +} |
| 36197 | + |
| 36198 | +/* |
| 36199 | +** Read from the database file. |
| 36200 | +*/ |
| 36201 | +static int kvvfsReadDb( |
| 36202 | + sqlite3_file *pProtoFile, |
| 36203 | + void *zBuf, |
| 36204 | + int iAmt, |
| 36205 | + sqlite_int64 iOfst |
| 36206 | +){ |
| 36207 | + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 36208 | + unsigned int pgno; |
| 36209 | + int got, n; |
| 36210 | + char zKey[30]; |
| 36211 | + char aData[133073]; |
| 36212 | + assert( iOfst>=0 ); |
| 36213 | + assert( iAmt>=0 ); |
| 36214 | + SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 36215 | + if( iOfst+iAmt>=512 ){ |
| 36216 | + if( (iOfst % iAmt)!=0 ){ |
| 36217 | + return SQLITE_IOERR_READ; |
| 36218 | + } |
| 36219 | + if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){ |
| 36220 | + return SQLITE_IOERR_READ; |
| 36221 | + } |
| 36222 | + pFile->szPage = iAmt; |
| 36223 | + pgno = 1 + iOfst/iAmt; |
| 36224 | + }else{ |
| 36225 | + pgno = 1; |
| 36226 | + } |
| 36227 | + sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); |
| 36228 | + got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1); |
| 36229 | + if( got<0 ){ |
| 36230 | + n = 0; |
| 36231 | + }else{ |
| 36232 | + aData[got] = 0; |
| 36233 | + if( iOfst+iAmt<512 ){ |
| 36234 | + int k = iOfst+iAmt; |
| 36235 | + aData[k*2] = 0; |
| 36236 | + n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000); |
| 36237 | + if( n>=iOfst+iAmt ){ |
| 36238 | + memcpy(zBuf, &aData[2000+iOfst], iAmt); |
| 36239 | + n = iAmt; |
| 36240 | + }else{ |
| 36241 | + n = 0; |
| 36242 | + } |
| 36243 | + }else{ |
| 36244 | + n = kvvfsDecode(aData, zBuf, iAmt); |
| 36245 | + } |
| 36246 | + } |
| 36247 | + if( n<iAmt ){ |
| 36248 | + memset(zBuf+n, 0, iAmt-n); |
| 36249 | + return SQLITE_IOERR_SHORT_READ; |
| 36250 | + } |
| 36251 | + return SQLITE_OK; |
| 36252 | +} |
| 36253 | + |
| 36254 | + |
| 36255 | +/* |
| 36256 | +** Write into the -journal file. |
| 36257 | +*/ |
| 36258 | +static int kvvfsWriteJrnl( |
| 36259 | + sqlite3_file *pProtoFile, |
| 36260 | + const void *zBuf, |
| 36261 | + int iAmt, |
| 36262 | + sqlite_int64 iOfst |
| 36263 | +){ |
| 36264 | + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 36265 | + sqlite3_int64 iEnd = iOfst+iAmt; |
| 36266 | + SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 36267 | + if( iEnd>=0x10000000 ) return SQLITE_FULL; |
| 36268 | + if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){ |
| 36269 | + char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd); |
| 36270 | + if( aNew==0 ){ |
| 36271 | + return SQLITE_IOERR_NOMEM; |
| 36272 | + } |
| 36273 | + pFile->aJrnl = aNew; |
| 36274 | + if( pFile->nJrnl<iOfst ){ |
| 36275 | + memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl); |
| 36276 | + } |
| 36277 | + pFile->nJrnl = iEnd; |
| 36278 | + } |
| 36279 | + memcpy(pFile->aJrnl+iOfst, zBuf, iAmt); |
| 36280 | + return SQLITE_OK; |
| 36281 | +} |
| 36282 | + |
| 36283 | +/* |
| 36284 | +** Write into the database file. |
| 36285 | +*/ |
| 36286 | +static int kvvfsWriteDb( |
| 36287 | + sqlite3_file *pProtoFile, |
| 36288 | + const void *zBuf, |
| 36289 | + int iAmt, |
| 36290 | + sqlite_int64 iOfst |
| 36291 | +){ |
| 36292 | + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 36293 | + unsigned int pgno; |
| 36294 | + char zKey[30]; |
| 36295 | + char aData[131073]; |
| 36296 | + SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); |
| 36297 | + assert( iAmt>=512 && iAmt<=65536 ); |
| 36298 | + assert( (iAmt & (iAmt-1))==0 ); |
| 36299 | + pgno = 1 + iOfst/iAmt; |
| 36300 | + sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); |
| 36301 | + kvvfsEncode(zBuf, iAmt, aData); |
| 36302 | + if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){ |
| 36303 | + return SQLITE_IOERR; |
| 36304 | + } |
| 36305 | + if( iOfst+iAmt > pFile->szDb ){ |
| 36306 | + pFile->szDb = iOfst + iAmt; |
| 36307 | + } |
| 36308 | + return SQLITE_OK; |
| 36309 | +} |
| 36310 | + |
| 36311 | +/* |
| 36312 | +** Truncate an kvvfs-file. |
| 36313 | +*/ |
| 36314 | +static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){ |
| 36315 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36316 | + SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size)); |
| 36317 | + assert( size==0 ); |
| 36318 | + sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl"); |
| 36319 | + sqlite3_free(pFile->aJrnl); |
| 36320 | + pFile->aJrnl = 0; |
| 36321 | + pFile->nJrnl = 0; |
| 36322 | + return SQLITE_OK; |
| 36323 | +} |
| 36324 | +static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){ |
| 36325 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36326 | + if( pFile->szDb>size |
| 36327 | + && pFile->szPage>0 |
| 36328 | + && (size % pFile->szPage)==0 |
| 36329 | + ){ |
| 36330 | + char zKey[50]; |
| 36331 | + unsigned int pgno, pgnoMax; |
| 36332 | + SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size)); |
| 36333 | + pgno = 1 + size/pFile->szPage; |
| 36334 | + pgnoMax = 2 + pFile->szDb/pFile->szPage; |
| 36335 | + while( pgno<=pgnoMax ){ |
| 36336 | + sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); |
| 36337 | + sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey); |
| 36338 | + pgno++; |
| 36339 | + } |
| 36340 | + pFile->szDb = size; |
| 36341 | + return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK; |
| 36342 | + } |
| 36343 | + return SQLITE_IOERR; |
| 36344 | +} |
| 36345 | + |
| 36346 | +/* |
| 36347 | +** Sync an kvvfs-file. |
| 36348 | +*/ |
| 36349 | +static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){ |
| 36350 | + int i, n; |
| 36351 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36352 | + char *zOut; |
| 36353 | + SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass)); |
| 36354 | + if( pFile->nJrnl<=0 ){ |
| 36355 | + return kvvfsTruncateJrnl(pProtoFile, 0); |
| 36356 | + } |
| 36357 | + zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 ); |
| 36358 | + if( zOut==0 ){ |
| 36359 | + return SQLITE_IOERR_NOMEM; |
| 36360 | + } |
| 36361 | + n = pFile->nJrnl; |
| 36362 | + i = 0; |
| 36363 | + do{ |
| 36364 | + zOut[i++] = 'a' + (n%26); |
| 36365 | + n /= 26; |
| 36366 | + }while( n>0 ); |
| 36367 | + zOut[i++] = ' '; |
| 36368 | + kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]); |
| 36369 | + i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut); |
| 36370 | + sqlite3_free(zOut); |
| 36371 | + return i ? SQLITE_IOERR : SQLITE_OK; |
| 36372 | +} |
| 36373 | +static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){ |
| 36374 | + return SQLITE_OK; |
| 36375 | +} |
| 36376 | + |
| 36377 | +/* |
| 36378 | +** Return the current file-size of an kvvfs-file. |
| 36379 | +*/ |
| 36380 | +static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){ |
| 36381 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36382 | + SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass)); |
| 36383 | + *pSize = pFile->nJrnl; |
| 36384 | + return SQLITE_OK; |
| 36385 | +} |
| 36386 | +static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){ |
| 36387 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36388 | + SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass)); |
| 36389 | + if( pFile->szDb>=0 ){ |
| 36390 | + *pSize = pFile->szDb; |
| 36391 | + }else{ |
| 36392 | + *pSize = kvvfsReadFileSize(pFile); |
| 36393 | + } |
| 36394 | + return SQLITE_OK; |
| 36395 | +} |
| 36396 | + |
| 36397 | +/* |
| 36398 | +** Lock an kvvfs-file. |
| 36399 | +*/ |
| 36400 | +static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){ |
| 36401 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36402 | + assert( !pFile->isJournal ); |
| 36403 | + SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock)); |
| 36404 | + |
| 36405 | + if( eLock!=SQLITE_LOCK_NONE ){ |
| 36406 | + pFile->szDb = kvvfsReadFileSize(pFile); |
| 36407 | + } |
| 36408 | + return SQLITE_OK; |
| 36409 | +} |
| 36410 | + |
| 36411 | +/* |
| 36412 | +** Unlock an kvvfs-file. |
| 36413 | +*/ |
| 36414 | +static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){ |
| 36415 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36416 | + assert( !pFile->isJournal ); |
| 36417 | + SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock)); |
| 36418 | + if( eLock==SQLITE_LOCK_NONE ){ |
| 36419 | + pFile->szDb = -1; |
| 36420 | + } |
| 36421 | + return SQLITE_OK; |
| 36422 | +} |
| 36423 | + |
| 36424 | +/* |
| 36425 | +** Check if another file-handle holds a RESERVED lock on an kvvfs-file. |
| 36426 | +*/ |
| 36427 | +static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){ |
| 36428 | + SQLITE_KV_LOG(("xCheckReservedLock\n")); |
| 36429 | + *pResOut = 0; |
| 36430 | + return SQLITE_OK; |
| 36431 | +} |
| 36432 | + |
| 36433 | +/* |
| 36434 | +** File control method. For custom operations on an kvvfs-file. |
| 36435 | +*/ |
| 36436 | +static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){ |
| 36437 | + SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op)); |
| 36438 | + return SQLITE_NOTFOUND; |
| 36439 | +} |
| 36440 | +static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){ |
| 36441 | + SQLITE_KV_LOG(("xFileControl(%d) on database\n", op)); |
| 36442 | + if( op==SQLITE_FCNTL_SYNC ){ |
| 36443 | + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; |
| 36444 | + int rc = SQLITE_OK; |
| 36445 | + SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass)); |
| 36446 | + if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){ |
| 36447 | + rc = SQLITE_IOERR; |
| 36448 | + } |
| 36449 | + return rc; |
| 36450 | + } |
| 36451 | + return SQLITE_NOTFOUND; |
| 36452 | +} |
| 36453 | + |
| 36454 | +/* |
| 36455 | +** Return the sector-size in bytes for an kvvfs-file. |
| 36456 | +*/ |
| 36457 | +static int kvvfsSectorSize(sqlite3_file *pFile){ |
| 36458 | + return 512; |
| 36459 | +} |
| 36460 | + |
| 36461 | +/* |
| 36462 | +** Return the device characteristic flags supported by an kvvfs-file. |
| 36463 | +*/ |
| 36464 | +static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){ |
| 36465 | + return 0; |
| 36466 | +} |
| 36467 | + |
| 36468 | +/****** sqlite3_vfs methods *************************************************/ |
| 36469 | + |
| 36470 | +/* |
| 36471 | +** Open an kvvfs file handle. |
| 36472 | +*/ |
| 36473 | +static int kvvfsOpen( |
| 36474 | + sqlite3_vfs *pProtoVfs, |
| 36475 | + const char *zName, |
| 36476 | + sqlite3_file *pProtoFile, |
| 36477 | + int flags, |
| 36478 | + int *pOutFlags |
| 36479 | +){ |
| 36480 | + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; |
| 36481 | + SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName)); |
| 36482 | + if( strcmp(zName, "local")==0 |
| 36483 | + || strcmp(zName, "session")==0 |
| 36484 | + ){ |
| 36485 | + pFile->isJournal = 0; |
| 36486 | + pFile->base.pMethods = &kvvfs_db_io_methods; |
| 36487 | + }else |
| 36488 | + if( strcmp(zName, "local-journal")==0 |
| 36489 | + || strcmp(zName, "session-journal")==0 |
| 36490 | + ){ |
| 36491 | + pFile->isJournal = 1; |
| 36492 | + pFile->base.pMethods = &kvvfs_jrnl_io_methods; |
| 36493 | + }else{ |
| 36494 | + return SQLITE_CANTOPEN; |
| 36495 | + } |
| 36496 | + if( zName[0]=='s' ){ |
| 36497 | + pFile->zClass = "session"; |
| 36498 | + }else{ |
| 36499 | + pFile->zClass = "local"; |
| 36500 | + } |
| 36501 | + pFile->aJrnl = 0; |
| 36502 | + pFile->nJrnl = 0; |
| 36503 | + pFile->szPage = -1; |
| 36504 | + pFile->szDb = -1; |
| 36505 | + return SQLITE_OK; |
| 36506 | +} |
| 36507 | + |
| 36508 | +/* |
| 36509 | +** Delete the file located at zPath. If the dirSync argument is true, |
| 36510 | +** ensure the file-system modifications are synced to disk before |
| 36511 | +** returning. |
| 36512 | +*/ |
| 36513 | +static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 36514 | + if( strcmp(zPath, "local-journal")==0 ){ |
| 36515 | + sqlite3KvvfsMethods.xDelete("local", "jrnl"); |
| 36516 | + }else |
| 36517 | + if( strcmp(zPath, "session-journal")==0 ){ |
| 36518 | + sqlite3KvvfsMethods.xDelete("session", "jrnl"); |
| 36519 | + } |
| 36520 | + return SQLITE_OK; |
| 36521 | +} |
| 36522 | + |
| 36523 | +/* |
| 36524 | +** Test for access permissions. Return true if the requested permission |
| 36525 | +** is available, or false otherwise. |
| 36526 | +*/ |
| 36527 | +static int kvvfsAccess( |
| 36528 | + sqlite3_vfs *pProtoVfs, |
| 36529 | + const char *zPath, |
| 36530 | + int flags, |
| 36531 | + int *pResOut |
| 36532 | +){ |
| 36533 | + SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath)); |
| 36534 | + if( strcmp(zPath, "local-journal")==0 ){ |
| 36535 | + *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0; |
| 36536 | + }else |
| 36537 | + if( strcmp(zPath, "session-journal")==0 ){ |
| 36538 | + *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0; |
| 36539 | + }else |
| 36540 | + if( strcmp(zPath, "local")==0 ){ |
| 36541 | + *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0; |
| 36542 | + }else |
| 36543 | + if( strcmp(zPath, "session")==0 ){ |
| 36544 | + *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0; |
| 36545 | + }else |
| 36546 | + { |
| 36547 | + *pResOut = 0; |
| 36548 | + } |
| 36549 | + SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut)); |
| 36550 | + return SQLITE_OK; |
| 36551 | +} |
| 36552 | + |
| 36553 | +/* |
| 36554 | +** Populate buffer zOut with the full canonical pathname corresponding |
| 36555 | +** to the pathname in zPath. zOut is guaranteed to point to a buffer |
| 36556 | +** of at least (INST_MAX_PATHNAME+1) bytes. |
| 36557 | +*/ |
| 36558 | +static int kvvfsFullPathname( |
| 36559 | + sqlite3_vfs *pVfs, |
| 36560 | + const char *zPath, |
| 36561 | + int nOut, |
| 36562 | + char *zOut |
| 36563 | +){ |
| 36564 | + size_t nPath; |
| 36565 | +#ifdef SQLITE_OS_KV_ALWAYS_LOCAL |
| 36566 | + zPath = "local"; |
| 36567 | +#endif |
| 36568 | + nPath = strlen(zPath); |
| 36569 | + SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath)); |
| 36570 | + if( nOut<nPath+1 ) nPath = nOut - 1; |
| 36571 | + memcpy(zOut, zPath, nPath); |
| 36572 | + zOut[nPath] = 0; |
| 36573 | + return SQLITE_OK; |
| 36574 | +} |
| 36575 | + |
| 36576 | +/* |
| 36577 | +** Open the dynamic library located at zPath and return a handle. |
| 36578 | +*/ |
| 36579 | +static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 36580 | + return 0; |
| 36581 | +} |
| 36582 | + |
| 36583 | +/* |
| 36584 | +** Populate the buffer pointed to by zBufOut with nByte bytes of |
| 36585 | +** random data. |
| 36586 | +*/ |
| 36587 | +static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 36588 | + memset(zBufOut, 0, nByte); |
| 36589 | + return nByte; |
| 36590 | +} |
| 36591 | + |
| 36592 | +/* |
| 36593 | +** Sleep for nMicro microseconds. Return the number of microseconds |
| 36594 | +** actually slept. |
| 36595 | +*/ |
| 36596 | +static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 36597 | + return SQLITE_OK; |
| 36598 | +} |
| 36599 | + |
| 36600 | +/* |
| 36601 | +** Return the current time as a Julian Day number in *pTimeOut. |
| 36602 | +*/ |
| 36603 | +static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 36604 | + sqlite3_int64 i = 0; |
| 36605 | + int rc; |
| 36606 | + rc = kvvfsCurrentTimeInt64(0, &i); |
| 36607 | + *pTimeOut = i/86400000.0; |
| 36608 | + return rc; |
| 36609 | +} |
| 36610 | +#include <sys/time.h> |
| 36611 | +static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 36612 | + static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 36613 | + struct timeval sNow; |
| 36614 | + (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 36615 | + *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 36616 | + return SQLITE_OK; |
| 36617 | +} |
| 36618 | +#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */ |
| 36619 | + |
| 36620 | +#if SQLITE_OS_KV |
| 36621 | +/* |
| 36622 | +** This routine is called initialize the KV-vfs as the default VFS. |
| 36623 | +*/ |
| 36624 | +SQLITE_API int sqlite3_os_init(void){ |
| 36625 | + return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1); |
| 36626 | +} |
| 36627 | +SQLITE_API int sqlite3_os_end(void){ |
| 36628 | + return SQLITE_OK; |
| 36629 | +} |
| 36630 | +#endif /* SQLITE_OS_KV */ |
| 36631 | + |
| 36632 | +#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) |
| 36633 | +SQLITE_PRIVATE int sqlite3KvvfsInit(void){ |
| 36634 | + return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0); |
| 36635 | +} |
| 36636 | +#endif |
| 36637 | + |
| 36638 | +/************** End of os_kv.c ***********************************************/ |
| 35562 | 36639 | /************** Begin file os_unix.c *****************************************/ |
| 35563 | 36640 | /* |
| 35564 | 36641 | ** 2004 May 22 |
| 35565 | 36642 | ** |
| 35566 | 36643 | ** The author disclaims copyright to this source code. In place of |
| | @@ -35647,17 +36724,17 @@ |
| 35647 | 36724 | #endif |
| 35648 | 36725 | |
| 35649 | 36726 | /* |
| 35650 | 36727 | ** standard include files. |
| 35651 | 36728 | */ |
| 35652 | | -#include <sys/types.h> |
| 35653 | | -#include <sys/stat.h> |
| 36729 | +#include <sys/types.h> /* amalgamator: keep */ |
| 36730 | +#include <sys/stat.h> /* amalgamator: keep */ |
| 35654 | 36731 | #include <fcntl.h> |
| 35655 | 36732 | #include <sys/ioctl.h> |
| 35656 | | -#include <unistd.h> |
| 36733 | +#include <unistd.h> /* amalgamator: keep */ |
| 35657 | 36734 | /* #include <time.h> */ |
| 35658 | | -#include <sys/time.h> |
| 36735 | +#include <sys/time.h> /* amalgamator: keep */ |
| 35659 | 36736 | #include <errno.h> |
| 35660 | 36737 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 35661 | 36738 | # include <sys/mman.h> |
| 35662 | 36739 | #endif |
| 35663 | 36740 | |
| | @@ -43625,10 +44702,13 @@ |
| 43625 | 44702 | 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS)); |
| 43626 | 44703 | #else |
| 43627 | 44704 | sqlite3_vfs_register(&aVfs[i], i==0); |
| 43628 | 44705 | #endif |
| 43629 | 44706 | } |
| 44707 | +#ifdef SQLITE_OS_KV_OPTIONAL |
| 44708 | + sqlite3KvvfsInit(); |
| 44709 | +#endif |
| 43630 | 44710 | unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
| 43631 | 44711 | |
| 43632 | 44712 | #ifndef SQLITE_OMIT_WAL |
| 43633 | 44713 | /* Validate lock assumptions */ |
| 43634 | 44714 | assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */ |
| | @@ -70722,10 +71802,13 @@ |
| 70722 | 71802 | put4byte(pCell+info.nSize-4, iTo); |
| 70723 | 71803 | break; |
| 70724 | 71804 | } |
| 70725 | 71805 | } |
| 70726 | 71806 | }else{ |
| 71807 | + if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){ |
| 71808 | + return SQLITE_CORRUPT_PAGE(pPage); |
| 71809 | + } |
| 70727 | 71810 | if( get4byte(pCell)==iFrom ){ |
| 70728 | 71811 | put4byte(pCell, iTo); |
| 70729 | 71812 | break; |
| 70730 | 71813 | } |
| 70731 | 71814 | } |
| | @@ -73056,18 +74139,11 @@ |
| 73056 | 74139 | } |
| 73057 | 74140 | } |
| 73058 | 74141 | |
| 73059 | 74142 | pPage = pCur->pPage; |
| 73060 | 74143 | idx = ++pCur->ix; |
| 73061 | | - if( !pPage->isInit || sqlite3FaultSim(412) ){ |
| 73062 | | - /* The only known way for this to happen is for there to be a |
| 73063 | | - ** recursive SQL function that does a DELETE operation as part of a |
| 73064 | | - ** SELECT which deletes content out from under an active cursor |
| 73065 | | - ** in a corrupt database file where the table being DELETE-ed from |
| 73066 | | - ** has pages in common with the table being queried. See TH3 |
| 73067 | | - ** module cov1/btree78.test testcase 220 (2018-06-08) for an |
| 73068 | | - ** example. */ |
| 74144 | + if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){ |
| 73069 | 74145 | return SQLITE_CORRUPT_BKPT; |
| 73070 | 74146 | } |
| 73071 | 74147 | |
| 73072 | 74148 | if( idx>=pPage->nCell ){ |
| 73073 | 74149 | if( !pPage->leaf ){ |
| | @@ -75753,10 +76829,15 @@ |
| 75753 | 76829 | assert( pCur->pPage->nOverflow ); |
| 75754 | 76830 | } |
| 75755 | 76831 | }else{ |
| 75756 | 76832 | break; |
| 75757 | 76833 | } |
| 76834 | + }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){ |
| 76835 | + /* The page being written is not a root page, and there is currently |
| 76836 | + ** more than one reference to it. This only happens if the page is one |
| 76837 | + ** of its own ancestor pages. Corruption. */ |
| 76838 | + rc = SQLITE_CORRUPT_BKPT; |
| 75758 | 76839 | }else{ |
| 75759 | 76840 | MemPage * const pParent = pCur->apPage[iPage-1]; |
| 75760 | 76841 | int const iIdx = pCur->aiIdx[iPage-1]; |
| 75761 | 76842 | |
| 75762 | 76843 | rc = sqlite3PagerWrite(pParent->pDbPage); |
| | @@ -79679,10 +80760,11 @@ |
| 79679 | 80760 | assert( MEM_Str==(MEM_Blob>>3) ); |
| 79680 | 80761 | pMem->flags |= (pMem->flags&MEM_Blob)>>3; |
| 79681 | 80762 | sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); |
| 79682 | 80763 | assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); |
| 79683 | 80764 | pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero); |
| 80765 | + if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1; |
| 79684 | 80766 | return sqlite3VdbeChangeEncoding(pMem, encoding); |
| 79685 | 80767 | } |
| 79686 | 80768 | } |
| 79687 | 80769 | return SQLITE_OK; |
| 79688 | 80770 | } |
| | @@ -80813,10 +81895,13 @@ |
| 80813 | 81895 | SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ |
| 80814 | 81896 | Mem *p = (Mem*)pVal; |
| 80815 | 81897 | assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 ); |
| 80816 | 81898 | if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){ |
| 80817 | 81899 | return p->n; |
| 81900 | + } |
| 81901 | + if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){ |
| 81902 | + return p->n; |
| 80818 | 81903 | } |
| 80819 | 81904 | if( (p->flags & MEM_Blob)!=0 ){ |
| 80820 | 81905 | if( p->flags & MEM_Zero ){ |
| 80821 | 81906 | return p->n + p->u.nZero; |
| 80822 | 81907 | }else{ |
| | @@ -81983,10 +83068,22 @@ |
| 81983 | 83068 | } |
| 81984 | 83069 | SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){ |
| 81985 | 83070 | assert( p->nOp>0 || p->db->mallocFailed ); |
| 81986 | 83071 | if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5; |
| 81987 | 83072 | } |
| 83073 | + |
| 83074 | +/* |
| 83075 | +** If the previous opcode is an OP_Column that delivers results |
| 83076 | +** into register iDest, then add the OPFLAG_TYPEOFARG flag to that |
| 83077 | +** opcode. |
| 83078 | +*/ |
| 83079 | +SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){ |
| 83080 | + VdbeOp *pOp = sqlite3VdbeGetLastOp(p); |
| 83081 | + if( pOp->p3==iDest && pOp->opcode==OP_Column ){ |
| 83082 | + pOp->p5 |= OPFLAG_TYPEOFARG; |
| 83083 | + } |
| 83084 | +} |
| 81988 | 83085 | |
| 81989 | 83086 | /* |
| 81990 | 83087 | ** Change the P2 operand of instruction addr so that it points to |
| 81991 | 83088 | ** the address of the next instruction to be coded. |
| 81992 | 83089 | */ |
| | @@ -85402,11 +86499,11 @@ |
| 85402 | 86499 | testcase( pRhs->flags & MEM_Int ); |
| 85403 | 86500 | testcase( pRhs->flags & MEM_IntReal ); |
| 85404 | 86501 | serial_type = aKey1[idx1]; |
| 85405 | 86502 | testcase( serial_type==12 ); |
| 85406 | 86503 | if( serial_type>=10 ){ |
| 85407 | | - rc = +1; |
| 86504 | + rc = serial_type==10 ? -1 : +1; |
| 85408 | 86505 | }else if( serial_type==0 ){ |
| 85409 | 86506 | rc = -1; |
| 85410 | 86507 | }else if( serial_type==7 ){ |
| 85411 | 86508 | sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 85412 | 86509 | rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r); |
| | @@ -85427,11 +86524,11 @@ |
| 85427 | 86524 | if( serial_type>=10 ){ |
| 85428 | 86525 | /* Serial types 12 or greater are strings and blobs (greater than |
| 85429 | 86526 | ** numbers). Types 10 and 11 are currently "reserved for future |
| 85430 | 86527 | ** use", so it doesn't really matter what the results of comparing |
| 85431 | 86528 | ** them to numberic values are. */ |
| 85432 | | - rc = +1; |
| 86529 | + rc = serial_type==10 ? -1 : +1; |
| 85433 | 86530 | }else if( serial_type==0 ){ |
| 85434 | 86531 | rc = -1; |
| 85435 | 86532 | }else{ |
| 85436 | 86533 | sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 85437 | 86534 | if( serial_type==7 ){ |
| | @@ -85508,11 +86605,11 @@ |
| 85508 | 86605 | } |
| 85509 | 86606 | |
| 85510 | 86607 | /* RHS is null */ |
| 85511 | 86608 | else{ |
| 85512 | 86609 | serial_type = aKey1[idx1]; |
| 85513 | | - rc = (serial_type!=0); |
| 86610 | + rc = (serial_type!=0 && serial_type!=10); |
| 85514 | 86611 | } |
| 85515 | 86612 | |
| 85516 | 86613 | if( rc!=0 ){ |
| 85517 | 86614 | int sortFlags = pPKey2->pKeyInfo->aSortFlags[i]; |
| 85518 | 86615 | if( sortFlags ){ |
| | @@ -86457,10 +87554,13 @@ |
| 86457 | 87554 | } |
| 86458 | 87555 | assert( eType == aType[pVal->flags&MEM_AffMask] ); |
| 86459 | 87556 | } |
| 86460 | 87557 | #endif |
| 86461 | 87558 | return aType[pVal->flags&MEM_AffMask]; |
| 87559 | +} |
| 87560 | +SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){ |
| 87561 | + return pVal->enc; |
| 86462 | 87562 | } |
| 86463 | 87563 | |
| 86464 | 87564 | /* Return true if a parameter to xUpdate represents an unchanged column */ |
| 86465 | 87565 | SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){ |
| 86466 | 87566 | return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); |
| | @@ -91094,23 +92194,94 @@ |
| 91094 | 92194 | goto jump_to_p2; |
| 91095 | 92195 | } |
| 91096 | 92196 | break; |
| 91097 | 92197 | } |
| 91098 | 92198 | |
| 91099 | | -/* Opcode: IsNullOrType P1 P2 P3 * * |
| 91100 | | -** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2 |
| 92199 | +/* Opcode: IsType P1 P2 P3 P4 P5 |
| 92200 | +** Synopsis: if typeof(P1.P3) in P5 goto P2 |
| 91101 | 92201 | ** |
| 91102 | | -** Jump to P2 if the value in register P1 is NULL or has a datatype P3. |
| 91103 | | -** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT, |
| 91104 | | -** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT. |
| 92202 | +** Jump to P2 if the type of a column in a btree is one of the types specified |
| 92203 | +** by the P5 bitmask. |
| 92204 | +** |
| 92205 | +** P1 is normally a cursor on a btree for which the row decode cache is |
| 92206 | +** valid through at least column P3. In other words, there should have been |
| 92207 | +** a prior OP_Column for column P3 or greater. If the cursor is not valid, |
| 92208 | +** then this opcode might give spurious results. |
| 92209 | +** The the btree row has fewer than P3 columns, then use P4 as the |
| 92210 | +** datatype. |
| 92211 | +** |
| 92212 | +** If P1 is -1, then P3 is a register number and the datatype is taken |
| 92213 | +** from the value in that register. |
| 92214 | +** |
| 92215 | +** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant |
| 92216 | +** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. |
| 92217 | +** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. |
| 92218 | +** |
| 92219 | +** Take the jump to address P2 if and only if the datatype of the |
| 92220 | +** value determined by P1 and P3 corresponds to one of the bits in the |
| 92221 | +** P5 bitmask. |
| 92222 | +** |
| 91105 | 92223 | */ |
| 91106 | | -case OP_IsNullOrType: { /* jump, in1 */ |
| 91107 | | - int doTheJump; |
| 91108 | | - pIn1 = &aMem[pOp->p1]; |
| 91109 | | - doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3; |
| 91110 | | - VdbeBranchTaken( doTheJump, 2); |
| 91111 | | - if( doTheJump ) goto jump_to_p2; |
| 92224 | +case OP_IsType: { /* jump */ |
| 92225 | + VdbeCursor *pC; |
| 92226 | + u16 typeMask; |
| 92227 | + u32 serialType; |
| 92228 | + |
| 92229 | + assert( pOp->p1>=(-1) && pOp->p1<p->nCursor ); |
| 92230 | + assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) ); |
| 92231 | + if( pOp->p1>=0 ){ |
| 92232 | + pC = p->apCsr[pOp->p1]; |
| 92233 | + assert( pC!=0 ); |
| 92234 | + assert( pOp->p3>=0 ); |
| 92235 | + if( pOp->p3<pC->nHdrParsed ){ |
| 92236 | + serialType = pC->aType[pOp->p3]; |
| 92237 | + if( serialType>=12 ){ |
| 92238 | + if( serialType&1 ){ |
| 92239 | + typeMask = 0x04; /* SQLITE_TEXT */ |
| 92240 | + }else{ |
| 92241 | + typeMask = 0x08; /* SQLITE_BLOB */ |
| 92242 | + } |
| 92243 | + }else{ |
| 92244 | + static const unsigned char aMask[] = { |
| 92245 | + 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2, |
| 92246 | + 0x01, 0x01, 0x10, 0x10 |
| 92247 | + }; |
| 92248 | + testcase( serialType==0 ); |
| 92249 | + testcase( serialType==1 ); |
| 92250 | + testcase( serialType==2 ); |
| 92251 | + testcase( serialType==3 ); |
| 92252 | + testcase( serialType==4 ); |
| 92253 | + testcase( serialType==5 ); |
| 92254 | + testcase( serialType==6 ); |
| 92255 | + testcase( serialType==7 ); |
| 92256 | + testcase( serialType==8 ); |
| 92257 | + testcase( serialType==9 ); |
| 92258 | + testcase( serialType==10 ); |
| 92259 | + testcase( serialType==11 ); |
| 92260 | + typeMask = aMask[serialType]; |
| 92261 | + } |
| 92262 | + }else{ |
| 92263 | + typeMask = 1 << (pOp->p4.i - 1); |
| 92264 | + testcase( typeMask==0x01 ); |
| 92265 | + testcase( typeMask==0x02 ); |
| 92266 | + testcase( typeMask==0x04 ); |
| 92267 | + testcase( typeMask==0x08 ); |
| 92268 | + testcase( typeMask==0x10 ); |
| 92269 | + } |
| 92270 | + }else{ |
| 92271 | + assert( memIsValid(&aMem[pOp->p3]) ); |
| 92272 | + typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1); |
| 92273 | + testcase( typeMask==0x01 ); |
| 92274 | + testcase( typeMask==0x02 ); |
| 92275 | + testcase( typeMask==0x04 ); |
| 92276 | + testcase( typeMask==0x08 ); |
| 92277 | + testcase( typeMask==0x10 ); |
| 92278 | + } |
| 92279 | + VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2); |
| 92280 | + if( typeMask & pOp->p5 ){ |
| 92281 | + goto jump_to_p2; |
| 92282 | + } |
| 91112 | 92283 | break; |
| 91113 | 92284 | } |
| 91114 | 92285 | |
| 91115 | 92286 | /* Opcode: ZeroOrNull P1 P2 P3 * * |
| 91116 | 92287 | ** Synopsis: r[P2] = 0 OR NULL |
| | @@ -91207,23 +92378,25 @@ |
| 91207 | 92378 | ** Synopsis: r[P3]=PX cursor P1 column P2 |
| 91208 | 92379 | ** |
| 91209 | 92380 | ** Interpret the data that cursor P1 points to as a structure built using |
| 91210 | 92381 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
| 91211 | 92382 | ** information about the format of the data.) Extract the P2-th column |
| 91212 | | -** from this record. If there are less that (P2+1) |
| 92383 | +** from this record. If there are less than (P2+1) |
| 91213 | 92384 | ** values in the record, extract a NULL. |
| 91214 | 92385 | ** |
| 91215 | 92386 | ** The value extracted is stored in register P3. |
| 91216 | 92387 | ** |
| 91217 | 92388 | ** If the record contains fewer than P2 fields, then extract a NULL. Or, |
| 91218 | 92389 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 91219 | 92390 | ** the result. |
| 91220 | 92391 | ** |
| 91221 | | -** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then |
| 91222 | | -** the result is guaranteed to only be used as the argument of a length() |
| 91223 | | -** or typeof() function, respectively. The loading of large blobs can be |
| 91224 | | -** skipped for length() and all content loading can be skipped for typeof(). |
| 92392 | +** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed |
| 92393 | +** to only be used by the length() function or the equivalent. The content |
| 92394 | +** of large blobs is not loaded, thus saving CPU cycles. If the |
| 92395 | +** OPFLAG_TYPEOFARG bit is set then the result will only be used by the |
| 92396 | +** typeof() function or the IS NULL or IS NOT NULL operators or the |
| 92397 | +** equivalent. In this case, all content loading can be omitted. |
| 91225 | 92398 | */ |
| 91226 | 92399 | case OP_Column: { |
| 91227 | 92400 | u32 p2; /* column number to retrieve */ |
| 91228 | 92401 | VdbeCursor *pC; /* The VDBE cursor */ |
| 91229 | 92402 | BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ |
| | @@ -93158,11 +94331,17 @@ |
| 93158 | 94331 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 93159 | 94332 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
| 93160 | 94333 | |
| 93161 | 94334 | r.aMem = &aMem[pOp->p3]; |
| 93162 | 94335 | #ifdef SQLITE_DEBUG |
| 93163 | | - { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 94336 | + { |
| 94337 | + int i; |
| 94338 | + for(i=0; i<r.nField; i++){ |
| 94339 | + assert( memIsValid(&r.aMem[i]) ); |
| 94340 | + if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]); |
| 94341 | + } |
| 94342 | + } |
| 93164 | 94343 | #endif |
| 93165 | 94344 | r.eqSeen = 0; |
| 93166 | 94345 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res); |
| 93167 | 94346 | if( rc!=SQLITE_OK ){ |
| 93168 | 94347 | goto abort_due_to_error; |
| | @@ -93221,54 +94400,76 @@ |
| 93221 | 94400 | } |
| 93222 | 94401 | break; |
| 93223 | 94402 | } |
| 93224 | 94403 | |
| 93225 | 94404 | |
| 93226 | | -/* Opcode: SeekScan P1 P2 * * * |
| 94405 | +/* Opcode: SeekScan P1 P2 * * P5 |
| 93227 | 94406 | ** Synopsis: Scan-ahead up to P1 rows |
| 93228 | 94407 | ** |
| 93229 | 94408 | ** This opcode is a prefix opcode to OP_SeekGE. In other words, this |
| 93230 | 94409 | ** opcode must be immediately followed by OP_SeekGE. This constraint is |
| 93231 | 94410 | ** checked by assert() statements. |
| 93232 | 94411 | ** |
| 93233 | 94412 | ** This opcode uses the P1 through P4 operands of the subsequent |
| 93234 | 94413 | ** OP_SeekGE. In the text that follows, the operands of the subsequent |
| 93235 | 94414 | ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only |
| 93236 | | -** the P1 and P2 operands of this opcode are also used, and are called |
| 93237 | | -** This.P1 and This.P2. |
| 94415 | +** the P1, P2 and P5 operands of this opcode are also used, and are called |
| 94416 | +** This.P1, This.P2 and This.P5. |
| 93238 | 94417 | ** |
| 93239 | 94418 | ** This opcode helps to optimize IN operators on a multi-column index |
| 93240 | 94419 | ** where the IN operator is on the later terms of the index by avoiding |
| 93241 | 94420 | ** unnecessary seeks on the btree, substituting steps to the next row |
| 93242 | 94421 | ** of the b-tree instead. A correct answer is obtained if this opcode |
| 93243 | 94422 | ** is omitted or is a no-op. |
| 93244 | 94423 | ** |
| 93245 | 94424 | ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which |
| 93246 | 94425 | ** is the desired entry that we want the cursor SeekGE.P1 to be pointing |
| 93247 | | -** to. Call this SeekGE.P4/P5 row the "target". |
| 94426 | +** to. Call this SeekGE.P3/P4 row the "target". |
| 93248 | 94427 | ** |
| 93249 | 94428 | ** If the SeekGE.P1 cursor is not currently pointing to a valid row, |
| 93250 | 94429 | ** then this opcode is a no-op and control passes through into the OP_SeekGE. |
| 93251 | 94430 | ** |
| 93252 | 94431 | ** If the SeekGE.P1 cursor is pointing to a valid row, then that row |
| 93253 | 94432 | ** might be the target row, or it might be near and slightly before the |
| 93254 | | -** target row. This opcode attempts to position the cursor on the target |
| 93255 | | -** row by, perhaps by invoking sqlite3BtreeStep() on the cursor |
| 93256 | | -** between 0 and This.P1 times. |
| 93257 | | -** |
| 93258 | | -** There are three possible outcomes from this opcode:<ol> |
| 93259 | | -** |
| 93260 | | -** <li> If after This.P1 steps, the cursor is still pointing to a place that |
| 93261 | | -** is earlier in the btree than the target row, then fall through |
| 93262 | | -** into the subsquence OP_SeekGE opcode. |
| 93263 | | -** |
| 93264 | | -** <li> If the cursor is successfully moved to the target row by 0 or more |
| 93265 | | -** sqlite3BtreeNext() calls, then jump to This.P2, which will land just |
| 93266 | | -** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE. |
| 93267 | | -** |
| 93268 | | -** <li> If the cursor ends up past the target row (indicating that the target |
| 93269 | | -** row does not exist in the btree) then jump to SeekOP.P2. |
| 94433 | +** target row, or it might be after the target row. If the cursor is |
| 94434 | +** currently before the target row, then this opcode attempts to position |
| 94435 | +** the cursor on or after the target row by invoking sqlite3BtreeStep() |
| 94436 | +** on the cursor between 1 and This.P1 times. |
| 94437 | +** |
| 94438 | +** The This.P5 parameter is a flag that indicates what to do if the |
| 94439 | +** cursor ends up pointing at a valid row that is past the target |
| 94440 | +** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If |
| 94441 | +** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0 |
| 94442 | +** case occurs when there are no inequality constraints to the right of |
| 94443 | +** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case |
| 94444 | +** occurs when there are inequality constraints to the right of the IN |
| 94445 | +** operator. In that case, the This.P2 will point either directly to or |
| 94446 | +** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for |
| 94447 | +** loop terminate. |
| 94448 | +** |
| 94449 | +** Possible outcomes from this opcode:<ol> |
| 94450 | +** |
| 94451 | +** <li> If the cursor is initally not pointed to any valid row, then |
| 94452 | +** fall through into the subsequent OP_SeekGE opcode. |
| 94453 | +** |
| 94454 | +** <li> If the cursor is left pointing to a row that is before the target |
| 94455 | +** row, even after making as many as This.P1 calls to |
| 94456 | +** sqlite3BtreeNext(), then also fall through into OP_SeekGE. |
| 94457 | +** |
| 94458 | +** <li> If the cursor is left pointing at the target row, either because it |
| 94459 | +** was at the target row to begin with or because one or more |
| 94460 | +** sqlite3BtreeNext() calls moved the cursor to the target row, |
| 94461 | +** then jump to This.P2.., |
| 94462 | +** |
| 94463 | +** <li> If the cursor started out before the target row and a call to |
| 94464 | +** to sqlite3BtreeNext() moved the cursor off the end of the index |
| 94465 | +** (indicating that the target row definitely does not exist in the |
| 94466 | +** btree) then jump to SeekGE.P2, ending the loop. |
| 94467 | +** |
| 94468 | +** <li> If the cursor ends up on a valid row that is past the target row |
| 94469 | +** (indicating that the target row does not exist in the btree) then |
| 94470 | +** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0. |
| 93270 | 94471 | ** </ol> |
| 93271 | 94472 | */ |
| 93272 | 94473 | case OP_SeekScan: { |
| 93273 | 94474 | VdbeCursor *pC; |
| 93274 | 94475 | int res; |
| | @@ -93275,18 +94476,29 @@ |
| 93275 | 94476 | int nStep; |
| 93276 | 94477 | UnpackedRecord r; |
| 93277 | 94478 | |
| 93278 | 94479 | assert( pOp[1].opcode==OP_SeekGE ); |
| 93279 | 94480 | |
| 93280 | | - /* pOp->p2 points to the first instruction past the OP_IdxGT that |
| 93281 | | - ** follows the OP_SeekGE. */ |
| 94481 | + /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the |
| 94482 | + ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first |
| 94483 | + ** opcode past the OP_SeekGE itself. */ |
| 93282 | 94484 | assert( pOp->p2>=(int)(pOp-aOp)+2 ); |
| 93283 | | - assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 93284 | | - testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 93285 | | - assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); |
| 93286 | | - assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); |
| 93287 | | - assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); |
| 94485 | +#ifdef SQLITE_DEBUG |
| 94486 | + if( pOp->p5==0 ){ |
| 94487 | + /* There are no inequality constraints following the IN constraint. */ |
| 94488 | + assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); |
| 94489 | + assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); |
| 94490 | + assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); |
| 94491 | + assert( aOp[pOp->p2-1].opcode==OP_IdxGT |
| 94492 | + || aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 94493 | + testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 94494 | + }else{ |
| 94495 | + /* There are inequality constraints. */ |
| 94496 | + assert( pOp->p2==(int)(pOp-aOp)+2 ); |
| 94497 | + assert( aOp[pOp->p2-1].opcode==OP_SeekGE ); |
| 94498 | + } |
| 94499 | +#endif |
| 93288 | 94500 | |
| 93289 | 94501 | assert( pOp->p1>0 ); |
| 93290 | 94502 | pC = p->apCsr[pOp[1].p1]; |
| 93291 | 94503 | assert( pC!=0 ); |
| 93292 | 94504 | assert( pC->eCurType==CURTYPE_BTREE ); |
| | @@ -93316,22 +94528,24 @@ |
| 93316 | 94528 | #endif |
| 93317 | 94529 | res = 0; /* Not needed. Only used to silence a warning. */ |
| 93318 | 94530 | while(1){ |
| 93319 | 94531 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
| 93320 | 94532 | if( rc ) goto abort_due_to_error; |
| 93321 | | - if( res>0 ){ |
| 94533 | + if( res>0 && pOp->p5==0 ){ |
| 93322 | 94534 | seekscan_search_fail: |
| 94535 | + /* Jump to SeekGE.P2, ending the loop */ |
| 93323 | 94536 | #ifdef SQLITE_DEBUG |
| 93324 | 94537 | if( db->flags&SQLITE_VdbeTrace ){ |
| 93325 | 94538 | printf("... %d steps and then skip\n", pOp->p1 - nStep); |
| 93326 | 94539 | } |
| 93327 | 94540 | #endif |
| 93328 | 94541 | VdbeBranchTaken(1,3); |
| 93329 | 94542 | pOp++; |
| 93330 | 94543 | goto jump_to_p2; |
| 93331 | 94544 | } |
| 93332 | | - if( res==0 ){ |
| 94545 | + if( res>=0 ){ |
| 94546 | + /* Jump to This.P2, bypassing the OP_SeekGE opcode */ |
| 93333 | 94547 | #ifdef SQLITE_DEBUG |
| 93334 | 94548 | if( db->flags&SQLITE_VdbeTrace ){ |
| 93335 | 94549 | printf("... %d steps and then success\n", pOp->p1 - nStep); |
| 93336 | 94550 | } |
| 93337 | 94551 | #endif |
| | @@ -103867,13 +105081,12 @@ |
| 103867 | 105081 | } |
| 103868 | 105082 | op = pExpr->op; |
| 103869 | 105083 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 103870 | 105084 | if( op==TK_COLUMN || op==TK_AGG_COLUMN ){ |
| 103871 | 105085 | assert( ExprUseYTab(pExpr) ); |
| 103872 | | - if( pExpr->y.pTab ){ |
| 103873 | | - return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
| 103874 | | - } |
| 105086 | + assert( pExpr->y.pTab!=0 ); |
| 105087 | + return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
| 103875 | 105088 | } |
| 103876 | 105089 | if( op==TK_SELECT ){ |
| 103877 | 105090 | assert( ExprUseXSelect(pExpr) ); |
| 103878 | 105091 | assert( pExpr->x.pSelect!=0 ); |
| 103879 | 105092 | assert( pExpr->x.pSelect->pEList!=0 ); |
| | @@ -103987,21 +105200,18 @@ |
| 103987 | 105200 | const Expr *p = pExpr; |
| 103988 | 105201 | while( p ){ |
| 103989 | 105202 | int op = p->op; |
| 103990 | 105203 | if( op==TK_REGISTER ) op = p->op2; |
| 103991 | 105204 | if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){ |
| 105205 | + int j; |
| 103992 | 105206 | assert( ExprUseYTab(p) ); |
| 103993 | | - if( p->y.pTab!=0 ){ |
| 103994 | | - /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally |
| 103995 | | - ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 103996 | | - int j = p->iColumn; |
| 103997 | | - if( j>=0 ){ |
| 103998 | | - const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]); |
| 103999 | | - pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
| 104000 | | - } |
| 104001 | | - break; |
| 104002 | | - } |
| 105207 | + assert( p->y.pTab!=0 ); |
| 105208 | + if( (j = p->iColumn)>=0 ){ |
| 105209 | + const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]); |
| 105210 | + pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
| 105211 | + } |
| 105212 | + break; |
| 104003 | 105213 | } |
| 104004 | 105214 | if( op==TK_CAST || op==TK_UPLUS ){ |
| 104005 | 105215 | p = p->pLeft; |
| 104006 | 105216 | continue; |
| 104007 | 105217 | } |
| | @@ -107602,14 +108812,11 @@ |
| 107602 | 108812 | int iCol, /* Index of the column to extract */ |
| 107603 | 108813 | int regOut /* Extract the value into this register */ |
| 107604 | 108814 | ){ |
| 107605 | 108815 | Column *pCol; |
| 107606 | 108816 | assert( v!=0 ); |
| 107607 | | - if( pTab==0 ){ |
| 107608 | | - sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut); |
| 107609 | | - return; |
| 107610 | | - } |
| 108817 | + assert( pTab!=0 ); |
| 107611 | 108818 | if( iCol<0 || iCol==pTab->iPKey ){ |
| 107612 | 108819 | sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); |
| 107613 | 108820 | VdbeComment((v, "%s.rowid", pTab->zName)); |
| 107614 | 108821 | }else{ |
| 107615 | 108822 | int op; |
| | @@ -107855,10 +109062,57 @@ |
| 107855 | 109062 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
| 107856 | 109063 | } |
| 107857 | 109064 | return target; |
| 107858 | 109065 | } |
| 107859 | 109066 | |
| 109067 | +/* |
| 109068 | +** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr. |
| 109069 | +** If it is, then resolve the expression by reading from the index and |
| 109070 | +** return the register into which the value has been read. If pExpr is |
| 109071 | +** not an indexed expression, then return negative. |
| 109072 | +*/ |
| 109073 | +static SQLITE_NOINLINE int sqlite3IndexedExprLookup( |
| 109074 | + Parse *pParse, /* The parsing context */ |
| 109075 | + Expr *pExpr, /* The expression to potentially bypass */ |
| 109076 | + int target /* Where to store the result of the expression */ |
| 109077 | +){ |
| 109078 | + IndexedExpr *p; |
| 109079 | + Vdbe *v; |
| 109080 | + for(p=pParse->pIdxExpr; p; p=p->pIENext){ |
| 109081 | + int iDataCur = p->iDataCur; |
| 109082 | + if( iDataCur<0 ) continue; |
| 109083 | + if( pParse->iSelfTab ){ |
| 109084 | + if( p->iDataCur!=pParse->iSelfTab-1 ) continue; |
| 109085 | + iDataCur = -1; |
| 109086 | + } |
| 109087 | + if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue; |
| 109088 | + v = pParse->pVdbe; |
| 109089 | + assert( v!=0 ); |
| 109090 | + if( p->bMaybeNullRow ){ |
| 109091 | + /* If the index is on a NULL row due to an outer join, then we |
| 109092 | + ** cannot extract the value from the index. The value must be |
| 109093 | + ** computed using the original expression. */ |
| 109094 | + int addr = sqlite3VdbeCurrentAddr(v); |
| 109095 | + sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target); |
| 109096 | + VdbeCoverage(v); |
| 109097 | + sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); |
| 109098 | + VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); |
| 109099 | + sqlite3VdbeGoto(v, 0); |
| 109100 | + p = pParse->pIdxExpr; |
| 109101 | + pParse->pIdxExpr = 0; |
| 109102 | + sqlite3ExprCode(pParse, pExpr, target); |
| 109103 | + pParse->pIdxExpr = p; |
| 109104 | + sqlite3VdbeJumpHere(v, addr+2); |
| 109105 | + }else{ |
| 109106 | + sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); |
| 109107 | + VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); |
| 109108 | + } |
| 109109 | + return target; |
| 109110 | + } |
| 109111 | + return -1; /* Not found */ |
| 109112 | +} |
| 109113 | + |
| 107860 | 109114 | |
| 107861 | 109115 | /* |
| 107862 | 109116 | ** Generate code into the current Vdbe to evaluate the given |
| 107863 | 109117 | ** expression. Attempt to store the results in register "target". |
| 107864 | 109118 | ** Return the register where results are stored. |
| | @@ -107883,10 +109137,15 @@ |
| 107883 | 109137 | assert( v!=0 ); |
| 107884 | 109138 | |
| 107885 | 109139 | expr_code_doover: |
| 107886 | 109140 | if( pExpr==0 ){ |
| 107887 | 109141 | op = TK_NULL; |
| 109142 | + }else if( pParse->pIdxExpr!=0 |
| 109143 | + && !ExprHasProperty(pExpr, EP_Leaf) |
| 109144 | + && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0 |
| 109145 | + ){ |
| 109146 | + return r1; |
| 107888 | 109147 | }else{ |
| 107889 | 109148 | assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); |
| 107890 | 109149 | op = pExpr->op; |
| 107891 | 109150 | } |
| 107892 | 109151 | switch( op ){ |
| | @@ -107928,15 +109187,12 @@ |
| 107928 | 109187 | ** constant. |
| 107929 | 109188 | */ |
| 107930 | 109189 | int aff; |
| 107931 | 109190 | iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target); |
| 107932 | 109191 | assert( ExprUseYTab(pExpr) ); |
| 107933 | | - if( pExpr->y.pTab ){ |
| 107934 | | - aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
| 107935 | | - }else{ |
| 107936 | | - aff = pExpr->affExpr; |
| 107937 | | - } |
| 109192 | + assert( pExpr->y.pTab!=0 ); |
| 109193 | + aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
| 107938 | 109194 | if( aff>SQLITE_AFF_BLOB ){ |
| 107939 | 109195 | static const char zAff[] = "B\000C\000D\000E"; |
| 107940 | 109196 | assert( SQLITE_AFF_BLOB=='A' ); |
| 107941 | 109197 | assert( SQLITE_AFF_TEXT=='B' ); |
| 107942 | 109198 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0, |
| | @@ -107994,16 +109250,14 @@ |
| 107994 | 109250 | ** in the index refer to the table to which the index belongs */ |
| 107995 | 109251 | iTab = pParse->iSelfTab - 1; |
| 107996 | 109252 | } |
| 107997 | 109253 | } |
| 107998 | 109254 | assert( ExprUseYTab(pExpr) ); |
| 109255 | + assert( pExpr->y.pTab!=0 ); |
| 107999 | 109256 | iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab, |
| 108000 | 109257 | pExpr->iColumn, iTab, target, |
| 108001 | 109258 | pExpr->op2); |
| 108002 | | - if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){ |
| 108003 | | - sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 108004 | | - } |
| 108005 | 109259 | return iReg; |
| 108006 | 109260 | } |
| 108007 | 109261 | case TK_INTEGER: { |
| 108008 | 109262 | codeInteger(pParse, pExpr, 0, target); |
| 108009 | 109263 | return target; |
| | @@ -109053,10 +110307,11 @@ |
| 109053 | 110307 | case TK_ISNULL: |
| 109054 | 110308 | case TK_NOTNULL: { |
| 109055 | 110309 | assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
| 109056 | 110310 | assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
| 109057 | 110311 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 110312 | + sqlite3VdbeTypeofColumn(v, r1); |
| 109058 | 110313 | sqlite3VdbeAddOp2(v, op, r1, dest); |
| 109059 | 110314 | VdbeCoverageIf(v, op==TK_ISNULL); |
| 109060 | 110315 | VdbeCoverageIf(v, op==TK_NOTNULL); |
| 109061 | 110316 | testcase( regFree1==0 ); |
| 109062 | 110317 | break; |
| | @@ -109227,10 +110482,11 @@ |
| 109227 | 110482 | break; |
| 109228 | 110483 | } |
| 109229 | 110484 | case TK_ISNULL: |
| 109230 | 110485 | case TK_NOTNULL: { |
| 109231 | 110486 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 110487 | + sqlite3VdbeTypeofColumn(v, r1); |
| 109232 | 110488 | sqlite3VdbeAddOp2(v, op, r1, dest); |
| 109233 | 110489 | testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); |
| 109234 | 110490 | testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); |
| 109235 | 110491 | testcase( regFree1==0 ); |
| 109236 | 110492 | break; |
| | @@ -109380,11 +110636,17 @@ |
| 109380 | 110636 | return 1; |
| 109381 | 110637 | } |
| 109382 | 110638 | if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){ |
| 109383 | 110639 | return 1; |
| 109384 | 110640 | } |
| 109385 | | - return 2; |
| 110641 | + if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN |
| 110642 | + && pB->iTable<0 && pA->iTable==iTab |
| 110643 | + ){ |
| 110644 | + /* fall through */ |
| 110645 | + }else{ |
| 110646 | + return 2; |
| 110647 | + } |
| 109386 | 110648 | } |
| 109387 | 110649 | assert( !ExprHasProperty(pA, EP_IntValue) ); |
| 109388 | 110650 | assert( !ExprHasProperty(pB, EP_IntValue) ); |
| 109389 | 110651 | if( pA->u.zToken ){ |
| 109390 | 110652 | if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){ |
| | @@ -109682,14 +110944,14 @@ |
| 109682 | 110944 | /* The y.pTab=0 assignment in wherecode.c always happens after the |
| 109683 | 110945 | ** impliesNotNullRow() test */ |
| 109684 | 110946 | assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) ); |
| 109685 | 110947 | assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) ); |
| 109686 | 110948 | if( (pLeft->op==TK_COLUMN |
| 109687 | | - && pLeft->y.pTab!=0 |
| 110949 | + && ALWAYS(pLeft->y.pTab!=0) |
| 109688 | 110950 | && IsVirtual(pLeft->y.pTab)) |
| 109689 | 110951 | || (pRight->op==TK_COLUMN |
| 109690 | | - && pRight->y.pTab!=0 |
| 110952 | + && ALWAYS(pRight->y.pTab!=0) |
| 109691 | 110953 | && IsVirtual(pRight->y.pTab)) |
| 109692 | 110954 | ){ |
| 109693 | 110955 | return WRC_Prune; |
| 109694 | 110956 | } |
| 109695 | 110957 | /* no break */ deliberate_fall_through |
| | @@ -113510,10 +114772,11 @@ |
| 113510 | 114772 | assert( k>=0 && k<pIdx->nColumn ); |
| 113511 | 114773 | i = pIdx->aiColumn[k]; |
| 113512 | 114774 | if( NEVER(i==XN_ROWID) ){ |
| 113513 | 114775 | VdbeComment((v,"%s.rowid",pIdx->zName)); |
| 113514 | 114776 | }else if( i==XN_EXPR ){ |
| 114777 | + assert( pIdx->bHasExpr ); |
| 113515 | 114778 | VdbeComment((v,"%s.expr(%d)",pIdx->zName, k)); |
| 113516 | 114779 | }else{ |
| 113517 | 114780 | VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName)); |
| 113518 | 114781 | } |
| 113519 | 114782 | } |
| | @@ -115825,11 +117088,11 @@ |
| 115825 | 117088 | if( p==0 ){ |
| 115826 | 117089 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115827 | 117090 | /* If zName is the not the name of a table in the schema created using |
| 115828 | 117091 | ** CREATE, then check to see if it is the name of an virtual table that |
| 115829 | 117092 | ** can be an eponymous virtual table. */ |
| 115830 | | - if( pParse->disableVtab==0 && db->init.busy==0 ){ |
| 117093 | + if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){ |
| 115831 | 117094 | Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 115832 | 117095 | if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 115833 | 117096 | pMod = sqlite3PragmaVtabRegister(db, zName); |
| 115834 | 117097 | } |
| 115835 | 117098 | if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
| | @@ -115838,11 +117101,11 @@ |
| 115838 | 117101 | } |
| 115839 | 117102 | } |
| 115840 | 117103 | #endif |
| 115841 | 117104 | if( flags & LOCATE_NOERR ) return 0; |
| 115842 | 117105 | pParse->checkSchema = 1; |
| 115843 | | - }else if( IsVirtual(p) && pParse->disableVtab ){ |
| 117106 | + }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){ |
| 115844 | 117107 | p = 0; |
| 115845 | 117108 | } |
| 115846 | 117109 | |
| 115847 | 117110 | if( p==0 ){ |
| 115848 | 117111 | const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| | @@ -117648,11 +118911,12 @@ |
| 117648 | 118911 | } |
| 117649 | 118912 | |
| 117650 | 118913 | /* Recompute the colNotIdxed field of the Index. |
| 117651 | 118914 | ** |
| 117652 | 118915 | ** colNotIdxed is a bitmask that has a 0 bit representing each indexed |
| 117653 | | -** columns that are within the first 63 columns of the table. The |
| 118916 | +** columns that are within the first 63 columns of the table and a 1 for |
| 118917 | +** all other bits (all columns that are not in the index). The |
| 117654 | 118918 | ** high-order bit of colNotIdxed is always 1. All unindexed columns |
| 117655 | 118919 | ** of the table have a 1. |
| 117656 | 118920 | ** |
| 117657 | 118921 | ** 2019-10-24: For the purpose of this computation, virtual columns are |
| 117658 | 118922 | ** not considered to be covered by the index, even if they are in the |
| | @@ -117676,11 +118940,11 @@ |
| 117676 | 118940 | testcase( x==BMS-2 ); |
| 117677 | 118941 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 117678 | 118942 | } |
| 117679 | 118943 | } |
| 117680 | 118944 | pIdx->colNotIdxed = ~m; |
| 117681 | | - assert( (pIdx->colNotIdxed>>63)==1 ); |
| 118945 | + assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */ |
| 117682 | 118946 | } |
| 117683 | 118947 | |
| 117684 | 118948 | /* |
| 117685 | 118949 | ** This routine runs at the end of parsing a CREATE TABLE statement that |
| 117686 | 118950 | ** has a WITHOUT ROWID clause. The job of this routine is to convert both |
| | @@ -119564,10 +120828,11 @@ |
| 119564 | 120828 | pList = 0; |
| 119565 | 120829 | } |
| 119566 | 120830 | j = XN_EXPR; |
| 119567 | 120831 | pIndex->aiColumn[i] = XN_EXPR; |
| 119568 | 120832 | pIndex->uniqNotNull = 0; |
| 120833 | + pIndex->bHasExpr = 1; |
| 119569 | 120834 | }else{ |
| 119570 | 120835 | j = pCExpr->iColumn; |
| 119571 | 120836 | assert( j<=0x7fff ); |
| 119572 | 120837 | if( j<0 ){ |
| 119573 | 120838 | j = pTab->iPKey; |
| | @@ -119575,10 +120840,11 @@ |
| 119575 | 120840 | if( pTab->aCol[j].notNull==0 ){ |
| 119576 | 120841 | pIndex->uniqNotNull = 0; |
| 119577 | 120842 | } |
| 119578 | 120843 | if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ |
| 119579 | 120844 | pIndex->bHasVCol = 1; |
| 120845 | + pIndex->bHasExpr = 1; |
| 119580 | 120846 | } |
| 119581 | 120847 | } |
| 119582 | 120848 | pIndex->aiColumn[i] = (i16)j; |
| 119583 | 120849 | } |
| 119584 | 120850 | zColl = 0; |
| | @@ -123352,11 +124618,11 @@ |
| 123352 | 124618 | ** |
| 123353 | 124619 | ** For a case-insensitive search, set variable cx to be the same as |
| 123354 | 124620 | ** c but in the other case and search the input string for either |
| 123355 | 124621 | ** c or cx. |
| 123356 | 124622 | */ |
| 123357 | | - if( c<=0x80 ){ |
| 124623 | + if( c<0x80 ){ |
| 123358 | 124624 | char zStop[3]; |
| 123359 | 124625 | int bMatch; |
| 123360 | 124626 | if( noCase ){ |
| 123361 | 124627 | zStop[0] = sqlite3Toupper(c); |
| 123362 | 124628 | zStop[1] = sqlite3Tolower(c); |
| | @@ -123435,19 +124701,31 @@ |
| 123435 | 124701 | /* |
| 123436 | 124702 | ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and |
| 123437 | 124703 | ** non-zero if there is no match. |
| 123438 | 124704 | */ |
| 123439 | 124705 | SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){ |
| 123440 | | - return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); |
| 124706 | + if( zString==0 ){ |
| 124707 | + return zGlobPattern!=0; |
| 124708 | + }else if( zGlobPattern==0 ){ |
| 124709 | + return 1; |
| 124710 | + }else { |
| 124711 | + return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); |
| 124712 | + } |
| 123441 | 124713 | } |
| 123442 | 124714 | |
| 123443 | 124715 | /* |
| 123444 | 124716 | ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for |
| 123445 | 124717 | ** a miss - like strcmp(). |
| 123446 | 124718 | */ |
| 123447 | 124719 | SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ |
| 123448 | | - return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); |
| 124720 | + if( zStr==0 ){ |
| 124721 | + return zPattern!=0; |
| 124722 | + }else if( zPattern==0 ){ |
| 124723 | + return 1; |
| 124724 | + }else{ |
| 124725 | + return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); |
| 124726 | + } |
| 123449 | 124727 | } |
| 123450 | 124728 | |
| 123451 | 124729 | /* |
| 123452 | 124730 | ** Count the number of times that the LIKE operator (or GLOB which is |
| 123453 | 124731 | ** just a variation of LIKE) gets called. This is used for testing |
| | @@ -126572,10 +127850,11 @@ |
| 126572 | 127850 | aff = pTab->aCol[x].affinity; |
| 126573 | 127851 | }else if( x==XN_ROWID ){ |
| 126574 | 127852 | aff = SQLITE_AFF_INTEGER; |
| 126575 | 127853 | }else{ |
| 126576 | 127854 | assert( x==XN_EXPR ); |
| 127855 | + assert( pIdx->bHasExpr ); |
| 126577 | 127856 | assert( pIdx->aColExpr!=0 ); |
| 126578 | 127857 | aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
| 126579 | 127858 | } |
| 126580 | 127859 | if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB; |
| 126581 | 127860 | if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC; |
| | @@ -130146,10 +131425,12 @@ |
| 130146 | 131425 | int (*deserialize)(sqlite3*,const char*,unsigned char*, |
| 130147 | 131426 | sqlite3_int64,sqlite3_int64,unsigned); |
| 130148 | 131427 | unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*, |
| 130149 | 131428 | unsigned int); |
| 130150 | 131429 | const char *(*db_name)(sqlite3*,int); |
| 131430 | + /* Version 3.40.0 and later */ |
| 131431 | + int (*value_encoding)(sqlite3_value*); |
| 130151 | 131432 | }; |
| 130152 | 131433 | |
| 130153 | 131434 | /* |
| 130154 | 131435 | ** This is the function signature used for all extension entry points. It |
| 130155 | 131436 | ** is also defined in the file "loadext.c". |
| | @@ -130470,10 +131751,12 @@ |
| 130470 | 131751 | #ifndef SQLITE_OMIT_DESERIALIZE |
| 130471 | 131752 | #define sqlite3_deserialize sqlite3_api->deserialize |
| 130472 | 131753 | #define sqlite3_serialize sqlite3_api->serialize |
| 130473 | 131754 | #endif |
| 130474 | 131755 | #define sqlite3_db_name sqlite3_api->db_name |
| 131756 | +/* Version 3.40.0 and later */ |
| 131757 | +#define sqlite3_value_encoding sqlite3_api->value_encoding |
| 130475 | 131758 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 130476 | 131759 | |
| 130477 | 131760 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 130478 | 131761 | /* This case when the file really is being compiled as a loadable |
| 130479 | 131762 | ** extension */ |
| | @@ -130982,11 +132265,13 @@ |
| 130982 | 132265 | sqlite3_serialize, |
| 130983 | 132266 | #else |
| 130984 | 132267 | 0, |
| 130985 | 132268 | 0, |
| 130986 | 132269 | #endif |
| 130987 | | - sqlite3_db_name |
| 132270 | + sqlite3_db_name, |
| 132271 | + /* Version 3.40.0 and later */ |
| 132272 | + sqlite3_value_type |
| 130988 | 132273 | }; |
| 130989 | 132274 | |
| 130990 | 132275 | /* True if x is the directory separator character |
| 130991 | 132276 | */ |
| 130992 | 132277 | #if SQLITE_OS_WIN |
| | @@ -133786,12 +135071,13 @@ |
| 133786 | 135071 | Index *pIdx, *pPk; |
| 133787 | 135072 | Index *pPrior = 0; /* Previous index */ |
| 133788 | 135073 | int loopTop; |
| 133789 | 135074 | int iDataCur, iIdxCur; |
| 133790 | 135075 | int r1 = -1; |
| 133791 | | - int bStrict; |
| 135076 | + int bStrict; /* True for a STRICT table */ |
| 133792 | 135077 | int r2; /* Previous key for WITHOUT ROWID tables */ |
| 135078 | + int mxCol; /* Maximum non-virtual column number */ |
| 133793 | 135079 | |
| 133794 | 135080 | if( !IsOrdinaryTable(pTab) ) continue; |
| 133795 | 135081 | if( pObjTab && pObjTab!=pTab ) continue; |
| 133796 | 135082 | if( isQuick || HasRowid(pTab) ){ |
| 133797 | 135083 | pPk = 0; |
| | @@ -133812,15 +135098,26 @@ |
| 133812 | 135098 | } |
| 133813 | 135099 | assert( pParse->nMem>=8+j ); |
| 133814 | 135100 | assert( sqlite3NoTempsInRange(pParse,1,7+j) ); |
| 133815 | 135101 | sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); |
| 133816 | 135102 | loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); |
| 135103 | + |
| 135104 | + /* Fetch the right-most column from the table. This will cause |
| 135105 | + ** the entire record header to be parsed and sanity checked. It |
| 135106 | + ** will also prepopulate the cursor column cache that is used |
| 135107 | + ** by the OP_IsType code, so it is a required step. |
| 135108 | + */ |
| 135109 | + mxCol = pTab->nCol-1; |
| 135110 | + while( mxCol>=0 |
| 135111 | + && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0 |
| 135112 | + || pTab->iPKey==mxCol) ) mxCol--; |
| 135113 | + if( mxCol>=0 ){ |
| 135114 | + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3); |
| 135115 | + sqlite3VdbeTypeofColumn(v, 3); |
| 135116 | + } |
| 135117 | + |
| 133817 | 135118 | if( !isQuick ){ |
| 133818 | | - /* Sanity check on record header decoding */ |
| 133819 | | - sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3); |
| 133820 | | - sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 133821 | | - VdbeComment((v, "(right-most column)")); |
| 133822 | 135119 | if( pPk ){ |
| 133823 | 135120 | /* Verify WITHOUT ROWID keys are in ascending order */ |
| 133824 | 135121 | int a1; |
| 133825 | 135122 | char *zErr; |
| 133826 | 135123 | a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol); |
| | @@ -133836,48 +135133,126 @@ |
| 133836 | 135133 | for(j=0; j<pPk->nKeyCol; j++){ |
| 133837 | 135134 | sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j); |
| 133838 | 135135 | } |
| 133839 | 135136 | } |
| 133840 | 135137 | } |
| 133841 | | - /* Verify that all NOT NULL columns really are NOT NULL. At the |
| 133842 | | - ** same time verify the type of the content of STRICT tables */ |
| 135138 | + /* Verify datatypes for all columns: |
| 135139 | + ** |
| 135140 | + ** (1) NOT NULL columns may not contain a NULL |
| 135141 | + ** (2) Datatype must be exact for non-ANY columns in STRICT tables |
| 135142 | + ** (3) Datatype for TEXT columns in non-STRICT tables must be |
| 135143 | + ** NULL, TEXT, or BLOB. |
| 135144 | + ** (4) Datatype for numeric columns in non-STRICT tables must not |
| 135145 | + ** be a TEXT value that can be losslessly converted to numeric. |
| 135146 | + */ |
| 133843 | 135147 | bStrict = (pTab->tabFlags & TF_Strict)!=0; |
| 133844 | 135148 | for(j=0; j<pTab->nCol; j++){ |
| 133845 | 135149 | char *zErr; |
| 133846 | | - Column *pCol = pTab->aCol + j; |
| 133847 | | - int doError, jmp2; |
| 135150 | + Column *pCol = pTab->aCol + j; /* The column to be checked */ |
| 135151 | + int labelError; /* Jump here to report an error */ |
| 135152 | + int labelOk; /* Jump here if all looks ok */ |
| 135153 | + int p1, p3, p4; /* Operands to the OP_IsType opcode */ |
| 135154 | + int doTypeCheck; /* Check datatypes (besides NOT NULL) */ |
| 135155 | + |
| 133848 | 135156 | if( j==pTab->iPKey ) continue; |
| 133849 | | - if( pCol->notNull==0 && !bStrict ) continue; |
| 133850 | | - doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0; |
| 133851 | | - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); |
| 133852 | | - if( sqlite3VdbeGetLastOp(v)->opcode==OP_Column ){ |
| 133853 | | - sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 135157 | + if( bStrict ){ |
| 135158 | + doTypeCheck = pCol->eCType>COLTYPE_ANY; |
| 135159 | + }else{ |
| 135160 | + doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB; |
| 133854 | 135161 | } |
| 135162 | + if( pCol->notNull==0 && !doTypeCheck ) continue; |
| 135163 | + |
| 135164 | + /* Compute the operands that will be needed for OP_IsType */ |
| 135165 | + p4 = SQLITE_NULL; |
| 135166 | + if( pCol->colFlags & COLFLAG_VIRTUAL ){ |
| 135167 | + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); |
| 135168 | + p1 = -1; |
| 135169 | + p3 = 3; |
| 135170 | + }else{ |
| 135171 | + if( pCol->iDflt ){ |
| 135172 | + sqlite3_value *pDfltValue = 0; |
| 135173 | + sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db), |
| 135174 | + pCol->affinity, &pDfltValue); |
| 135175 | + if( pDfltValue ){ |
| 135176 | + p4 = sqlite3_value_type(pDfltValue); |
| 135177 | + sqlite3ValueFree(pDfltValue); |
| 135178 | + } |
| 135179 | + } |
| 135180 | + p1 = iDataCur; |
| 135181 | + if( !HasRowid(pTab) ){ |
| 135182 | + testcase( j!=sqlite3TableColumnToStorage(pTab, j) ); |
| 135183 | + p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j); |
| 135184 | + }else{ |
| 135185 | + p3 = sqlite3TableColumnToStorage(pTab,j); |
| 135186 | + testcase( p3!=j); |
| 135187 | + } |
| 135188 | + } |
| 135189 | + |
| 135190 | + labelError = sqlite3VdbeMakeLabel(pParse); |
| 135191 | + labelOk = sqlite3VdbeMakeLabel(pParse); |
| 133855 | 135192 | if( pCol->notNull ){ |
| 133856 | | - jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); |
| 135193 | + /* (1) NOT NULL columns may not contain a NULL */ |
| 135194 | + int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); |
| 135195 | + sqlite3VdbeChangeP5(v, 0x0f); |
| 135196 | + VdbeCoverage(v); |
| 133857 | 135197 | zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| 133858 | 135198 | pCol->zCnName); |
| 133859 | 135199 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 133860 | | - if( bStrict && pCol->eCType!=COLTYPE_ANY ){ |
| 133861 | | - sqlite3VdbeGoto(v, doError); |
| 135200 | + if( doTypeCheck ){ |
| 135201 | + sqlite3VdbeGoto(v, labelError); |
| 135202 | + sqlite3VdbeJumpHere(v, jmp2); |
| 133862 | 135203 | }else{ |
| 133863 | | - integrityCheckResultRow(v); |
| 135204 | + /* VDBE byte code will fall thru */ |
| 133864 | 135205 | } |
| 133865 | | - sqlite3VdbeJumpHere(v, jmp2); |
| 133866 | 135206 | } |
| 133867 | | - if( bStrict && pCol->eCType!=COLTYPE_ANY ){ |
| 133868 | | - jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0, |
| 133869 | | - sqlite3StdTypeMap[pCol->eCType-1]); |
| 135207 | + if( bStrict && doTypeCheck ){ |
| 135208 | + /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/ |
| 135209 | + static unsigned char aStdTypeMask[] = { |
| 135210 | + 0x1f, /* ANY */ |
| 135211 | + 0x18, /* BLOB */ |
| 135212 | + 0x11, /* INT */ |
| 135213 | + 0x11, /* INTEGER */ |
| 135214 | + 0x13, /* REAL */ |
| 135215 | + 0x14 /* TEXT */ |
| 135216 | + }; |
| 135217 | + sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); |
| 135218 | + assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) ); |
| 135219 | + sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]); |
| 133870 | 135220 | VdbeCoverage(v); |
| 133871 | 135221 | zErr = sqlite3MPrintf(db, "non-%s value in %s.%s", |
| 133872 | 135222 | sqlite3StdType[pCol->eCType-1], |
| 133873 | 135223 | pTab->zName, pTab->aCol[j].zCnName); |
| 133874 | 135224 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 133875 | | - sqlite3VdbeResolveLabel(v, doError); |
| 133876 | | - integrityCheckResultRow(v); |
| 133877 | | - sqlite3VdbeJumpHere(v, jmp2); |
| 135225 | + }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){ |
| 135226 | + /* (3) Datatype for TEXT columns in non-STRICT tables must be |
| 135227 | + ** NULL, TEXT, or BLOB. */ |
| 135228 | + sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); |
| 135229 | + sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */ |
| 135230 | + VdbeCoverage(v); |
| 135231 | + zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s", |
| 135232 | + pTab->zName, pTab->aCol[j].zCnName); |
| 135233 | + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 135234 | + }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){ |
| 135235 | + /* (4) Datatype for numeric columns in non-STRICT tables must not |
| 135236 | + ** be a TEXT value that can be converted to numeric. */ |
| 135237 | + sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); |
| 135238 | + sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */ |
| 135239 | + VdbeCoverage(v); |
| 135240 | + if( p1>=0 ){ |
| 135241 | + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); |
| 135242 | + } |
| 135243 | + sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC); |
| 135244 | + sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4); |
| 135245 | + sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */ |
| 135246 | + VdbeCoverage(v); |
| 135247 | + zErr = sqlite3MPrintf(db, "TEXT value in %s.%s", |
| 135248 | + pTab->zName, pTab->aCol[j].zCnName); |
| 135249 | + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 133878 | 135250 | } |
| 135251 | + sqlite3VdbeResolveLabel(v, labelError); |
| 135252 | + integrityCheckResultRow(v); |
| 135253 | + sqlite3VdbeResolveLabel(v, labelOk); |
| 133879 | 135254 | } |
| 133880 | 135255 | /* Verify CHECK constraints */ |
| 133881 | 135256 | if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
| 133882 | 135257 | ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0); |
| 133883 | 135258 | if( db->mallocFailed==0 ){ |
| | @@ -135493,11 +136868,11 @@ |
| 135493 | 136868 | */ |
| 135494 | 136869 | if( prepFlags & SQLITE_PREPARE_PERSISTENT ){ |
| 135495 | 136870 | sParse.disableLookaside++; |
| 135496 | 136871 | DisableLookaside; |
| 135497 | 136872 | } |
| 135498 | | - sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0; |
| 136873 | + sParse.prepFlags = prepFlags & 0xff; |
| 135499 | 136874 | |
| 135500 | 136875 | /* Check to verify that it is possible to get a read lock on all |
| 135501 | 136876 | ** database schemas. The inability to get a read lock indicates that |
| 135502 | 136877 | ** some other database connection is holding a write-lock, which in |
| 135503 | 136878 | ** turn means that the other connection has made uncommitted changes |
| | @@ -135534,11 +136909,13 @@ |
| 135534 | 136909 | } |
| 135535 | 136910 | } |
| 135536 | 136911 | } |
| 135537 | 136912 | } |
| 135538 | 136913 | |
| 135539 | | - sqlite3VtabUnlockList(db); |
| 136914 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 136915 | + if( db->pDisconnect ) sqlite3VtabUnlockList(db); |
| 136916 | +#endif |
| 135540 | 136917 | |
| 135541 | 136918 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 135542 | 136919 | char *zSqlCopy; |
| 135543 | 136920 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 135544 | 136921 | testcase( nBytes==mxLen ); |
| | @@ -139603,10 +140980,11 @@ |
| 139603 | 140980 | Parse *pParse; /* The parsing context */ |
| 139604 | 140981 | int iTable; /* Replace references to this table */ |
| 139605 | 140982 | int iNewTable; /* New table number */ |
| 139606 | 140983 | int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */ |
| 139607 | 140984 | ExprList *pEList; /* Replacement expressions */ |
| 140985 | + ExprList *pCList; /* Collation sequences for replacement expr */ |
| 139608 | 140986 | } SubstContext; |
| 139609 | 140987 | |
| 139610 | 140988 | /* Forward Declarations */ |
| 139611 | 140989 | static void substExprList(SubstContext*, ExprList*); |
| 139612 | 140990 | static void substSelect(SubstContext*, Select*, int); |
| | @@ -139644,13 +141022,14 @@ |
| 139644 | 141022 | pExpr->op = TK_NULL; |
| 139645 | 141023 | }else |
| 139646 | 141024 | #endif |
| 139647 | 141025 | { |
| 139648 | 141026 | Expr *pNew; |
| 139649 | | - Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr; |
| 141027 | + int iColumn = pExpr->iColumn; |
| 141028 | + Expr *pCopy = pSubst->pEList->a[iColumn].pExpr; |
| 139650 | 141029 | Expr ifNullRow; |
| 139651 | | - assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr ); |
| 141030 | + assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr ); |
| 139652 | 141031 | assert( pExpr->pRight==0 ); |
| 139653 | 141032 | if( sqlite3ExprIsVector(pCopy) ){ |
| 139654 | 141033 | sqlite3VectorErrorMsg(pSubst->pParse, pCopy); |
| 139655 | 141034 | }else{ |
| 139656 | 141035 | sqlite3 *db = pSubst->pParse->db; |
| | @@ -139684,15 +141063,20 @@ |
| 139684 | 141063 | ExprSetProperty(pExpr, EP_IntValue); |
| 139685 | 141064 | } |
| 139686 | 141065 | |
| 139687 | 141066 | /* Ensure that the expression now has an implicit collation sequence, |
| 139688 | 141067 | ** just as it did when it was a column of a view or sub-query. */ |
| 139689 | | - if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){ |
| 139690 | | - CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr); |
| 139691 | | - pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr, |
| 139692 | | - (pColl ? pColl->zName : "BINARY") |
| 141068 | + { |
| 141069 | + CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr); |
| 141070 | + CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, |
| 141071 | + pSubst->pCList->a[iColumn].pExpr |
| 139693 | 141072 | ); |
| 141073 | + if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){ |
| 141074 | + pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr, |
| 141075 | + (pColl ? pColl->zName : "BINARY") |
| 141076 | + ); |
| 141077 | + } |
| 139694 | 141078 | } |
| 139695 | 141079 | ExprClearProperty(pExpr, EP_Collate); |
| 139696 | 141080 | } |
| 139697 | 141081 | } |
| 139698 | 141082 | }else{ |
| | @@ -139880,10 +141264,22 @@ |
| 139880 | 141264 | w.xExprCallback = renumberCursorsCb; |
| 139881 | 141265 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 139882 | 141266 | sqlite3WalkSelect(&w, p); |
| 139883 | 141267 | } |
| 139884 | 141268 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 141269 | + |
| 141270 | +/* |
| 141271 | +** If pSel is not part of a compound SELECT, return a pointer to its |
| 141272 | +** expression list. Otherwise, return a pointer to the expression list |
| 141273 | +** of the leftmost SELECT in the compound. |
| 141274 | +*/ |
| 141275 | +static ExprList *findLeftmostExprlist(Select *pSel){ |
| 141276 | + while( pSel->pPrior ){ |
| 141277 | + pSel = pSel->pPrior; |
| 141278 | + } |
| 141279 | + return pSel->pEList; |
| 141280 | +} |
| 139885 | 141281 | |
| 139886 | 141282 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 139887 | 141283 | /* |
| 139888 | 141284 | ** This routine attempts to flatten subqueries as a performance optimization. |
| 139889 | 141285 | ** This routine returns 1 if it makes changes and 0 if no flattening occurs. |
| | @@ -140433,10 +141829,11 @@ |
| 140433 | 141829 | x.pParse = pParse; |
| 140434 | 141830 | x.iTable = iParent; |
| 140435 | 141831 | x.iNewTable = iNewParent; |
| 140436 | 141832 | x.isOuterJoin = isOuterJoin; |
| 140437 | 141833 | x.pEList = pSub->pEList; |
| 141834 | + x.pCList = findLeftmostExprlist(pSub); |
| 140438 | 141835 | substSelect(&x, pParent, 0); |
| 140439 | 141836 | } |
| 140440 | 141837 | |
| 140441 | 141838 | /* The flattened query is a compound if either the inner or the |
| 140442 | 141839 | ** outer query is a compound. */ |
| | @@ -140452,11 +141849,11 @@ |
| 140452 | 141849 | if( pSub->pLimit ){ |
| 140453 | 141850 | pParent->pLimit = pSub->pLimit; |
| 140454 | 141851 | pSub->pLimit = 0; |
| 140455 | 141852 | } |
| 140456 | 141853 | |
| 140457 | | - /* Recompute the SrcList_item.colUsed masks for the flattened |
| 141854 | + /* Recompute the SrcItem.colUsed masks for the flattened |
| 140458 | 141855 | ** tables. */ |
| 140459 | 141856 | for(i=0; i<nSubSrc; i++){ |
| 140460 | 141857 | recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]); |
| 140461 | 141858 | } |
| 140462 | 141859 | } |
| | @@ -140915,10 +142312,11 @@ |
| 140915 | 142312 | x.pParse = pParse; |
| 140916 | 142313 | x.iTable = pSrc->iCursor; |
| 140917 | 142314 | x.iNewTable = pSrc->iCursor; |
| 140918 | 142315 | x.isOuterJoin = 0; |
| 140919 | 142316 | x.pEList = pSubq->pEList; |
| 142317 | + x.pCList = findLeftmostExprlist(pSubq); |
| 140920 | 142318 | pNew = substExpr(&x, pNew); |
| 140921 | 142319 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 140922 | 142320 | if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){ |
| 140923 | 142321 | /* Restriction 6c has prevented push-down in this case */ |
| 140924 | 142322 | sqlite3ExprDelete(pParse->db, pNew); |
| | @@ -141439,13 +142837,13 @@ |
| 141439 | 142837 | } |
| 141440 | 142838 | } |
| 141441 | 142839 | #endif |
| 141442 | 142840 | |
| 141443 | 142841 | /* |
| 141444 | | -** The SrcList_item structure passed as the second argument represents a |
| 142842 | +** The SrcItem structure passed as the second argument represents a |
| 141445 | 142843 | ** sub-query in the FROM clause of a SELECT statement. This function |
| 141446 | | -** allocates and populates the SrcList_item.pTab object. If successful, |
| 142844 | +** allocates and populates the SrcItem.pTab object. If successful, |
| 141447 | 142845 | ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered, |
| 141448 | 142846 | ** SQLITE_NOMEM. |
| 141449 | 142847 | */ |
| 141450 | 142848 | SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){ |
| 141451 | 142849 | Select *pSel = pFrom->pSelect; |
| | @@ -142274,11 +143672,11 @@ |
| 142274 | 143672 | #endif |
| 142275 | 143673 | } |
| 142276 | 143674 | |
| 142277 | 143675 | /* |
| 142278 | 143676 | ** Check to see if the pThis entry of pTabList is a self-join of a prior view. |
| 142279 | | -** If it is, then return the SrcList_item for the prior view. If it is not, |
| 143677 | +** If it is, then return the SrcItem for the prior view. If it is not, |
| 142280 | 143678 | ** then return 0. |
| 142281 | 143679 | */ |
| 142282 | 143680 | static SrcItem *isSelfJoinView( |
| 142283 | 143681 | SrcList *pTabList, /* Search for self-joins in this FROM clause */ |
| 142284 | 143682 | SrcItem *pThis /* Search for prior reference to this subquery */ |
| | @@ -143318,11 +144716,11 @@ |
| 143318 | 144716 | ** in the right order to begin with. |
| 143319 | 144717 | */ |
| 143320 | 144718 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 143321 | 144719 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 143322 | 144720 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct, |
| 143323 | | - 0, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY) |
| 144721 | + p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY) |
| 143324 | 144722 | | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 |
| 143325 | 144723 | ); |
| 143326 | 144724 | if( pWInfo==0 ){ |
| 143327 | 144725 | sqlite3ExprListDelete(db, pDistinct); |
| 143328 | 144726 | goto select_end; |
| | @@ -143617,11 +145015,11 @@ |
| 143617 | 145015 | assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 ); |
| 143618 | 145016 | assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 ); |
| 143619 | 145017 | |
| 143620 | 145018 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 143621 | 145019 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy, |
| 143622 | | - pDistinct, 0, minMaxFlag|distFlag, 0); |
| 145020 | + pDistinct, p, minMaxFlag|distFlag, 0); |
| 143623 | 145021 | if( pWInfo==0 ){ |
| 143624 | 145022 | goto select_end; |
| 143625 | 145023 | } |
| 143626 | 145024 | SELECTTRACE(1,pParse,p,("WhereBegin returns\n")); |
| 143627 | 145025 | eDist = sqlite3WhereIsDistinct(pWInfo); |
| | @@ -145101,11 +146499,11 @@ |
| 145101 | 146499 | sSubParse.pTriggerTab = pTab; |
| 145102 | 146500 | sSubParse.pToplevel = pTop; |
| 145103 | 146501 | sSubParse.zAuthContext = pTrigger->zName; |
| 145104 | 146502 | sSubParse.eTriggerOp = pTrigger->op; |
| 145105 | 146503 | sSubParse.nQueryLoop = pParse->nQueryLoop; |
| 145106 | | - sSubParse.disableVtab = pParse->disableVtab; |
| 146504 | + sSubParse.prepFlags = pParse->prepFlags; |
| 145107 | 146505 | |
| 145108 | 146506 | v = sqlite3GetVdbe(&sSubParse); |
| 145109 | 146507 | if( v ){ |
| 145110 | 146508 | VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", |
| 145111 | 146509 | pTrigger->zName, onErrorText(orconf), |
| | @@ -145447,15 +146845,18 @@ |
| 145447 | 146845 | ** (not a virtual table) then the value might have been stored as an |
| 145448 | 146846 | ** integer. In that case, add an OP_RealAffinity opcode to make sure |
| 145449 | 146847 | ** it has been converted into REAL. |
| 145450 | 146848 | */ |
| 145451 | 146849 | SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ |
| 146850 | + Column *pCol; |
| 145452 | 146851 | assert( pTab!=0 ); |
| 145453 | | - if( !IsView(pTab) ){ |
| 146852 | + assert( pTab->nCol>i ); |
| 146853 | + pCol = &pTab->aCol[i]; |
| 146854 | + if( pCol->iDflt ){ |
| 145454 | 146855 | sqlite3_value *pValue = 0; |
| 145455 | 146856 | u8 enc = ENC(sqlite3VdbeDb(v)); |
| 145456 | | - Column *pCol = &pTab->aCol[i]; |
| 146857 | + assert( !IsView(pTab) ); |
| 145457 | 146858 | VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName)); |
| 145458 | 146859 | assert( i<pTab->nCol ); |
| 145459 | 146860 | sqlite3ValueFromExpr(sqlite3VdbeDb(v), |
| 145460 | 146861 | sqlite3ColumnExpr(pTab,pCol), enc, |
| 145461 | 146862 | pCol->affinity, &pValue); |
| | @@ -145462,11 +146863,11 @@ |
| 145462 | 146863 | if( pValue ){ |
| 145463 | 146864 | sqlite3VdbeAppendP4(v, pValue, P4_MEM); |
| 145464 | 146865 | } |
| 145465 | 146866 | } |
| 145466 | 146867 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 145467 | | - if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){ |
| 146868 | + if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){ |
| 145468 | 146869 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 145469 | 146870 | } |
| 145470 | 146871 | #endif |
| 145471 | 146872 | } |
| 145472 | 146873 | |
| | @@ -146902,10 +148303,11 @@ |
| 146902 | 148303 | Expr *pExpr; |
| 146903 | 148304 | sCol[0].u.zToken = (char*)pIdx->azColl[ii]; |
| 146904 | 148305 | if( pIdx->aiColumn[ii]==XN_EXPR ){ |
| 146905 | 148306 | assert( pIdx->aColExpr!=0 ); |
| 146906 | 148307 | assert( pIdx->aColExpr->nExpr>ii ); |
| 148308 | + assert( pIdx->bHasExpr ); |
| 146907 | 148309 | pExpr = pIdx->aColExpr->a[ii].pExpr; |
| 146908 | 148310 | if( pExpr->op!=TK_COLLATE ){ |
| 146909 | 148311 | sCol[0].pLeft = pExpr; |
| 146910 | 148312 | pExpr = &sCol[0]; |
| 146911 | 148313 | } |
| | @@ -147215,10 +148617,11 @@ |
| 147215 | 148617 | int isMemDb; /* True if vacuuming a :memory: database */ |
| 147216 | 148618 | int nRes; /* Bytes of reserved space at the end of each page */ |
| 147217 | 148619 | int nDb; /* Number of attached databases */ |
| 147218 | 148620 | const char *zDbMain; /* Schema name of database to vacuum */ |
| 147219 | 148621 | const char *zOut; /* Name of output file */ |
| 148622 | + u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */ |
| 147220 | 148623 | |
| 147221 | 148624 | if( !db->autoCommit ){ |
| 147222 | 148625 | sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); |
| 147223 | 148626 | return SQLITE_ERROR; /* IMP: R-12218-18073 */ |
| 147224 | 148627 | } |
| | @@ -147286,16 +148689,21 @@ |
| 147286 | 148689 | rc = SQLITE_ERROR; |
| 147287 | 148690 | sqlite3SetString(pzErrMsg, db, "output file already exists"); |
| 147288 | 148691 | goto end_of_vacuum; |
| 147289 | 148692 | } |
| 147290 | 148693 | db->mDbFlags |= DBFLAG_VacuumInto; |
| 148694 | + |
| 148695 | + /* For a VACUUM INTO, the pager-flags are set to the same values as |
| 148696 | + ** they are for the database being vacuumed, except that PAGER_CACHESPILL |
| 148697 | + ** is always set. */ |
| 148698 | + pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK); |
| 147291 | 148699 | } |
| 147292 | 148700 | nRes = sqlite3BtreeGetRequestedReserve(pMain); |
| 147293 | 148701 | |
| 147294 | 148702 | sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size); |
| 147295 | 148703 | sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0)); |
| 147296 | | - sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL); |
| 148704 | + sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL); |
| 147297 | 148705 | |
| 147298 | 148706 | /* Begin a transaction and take an exclusive lock on the main database |
| 147299 | 148707 | ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below, |
| 147300 | 148708 | ** to ensure that we do not try to change the page-size on a WAL database. |
| 147301 | 148709 | */ |
| | @@ -148605,11 +150013,11 @@ |
| 148605 | 150013 | /* Check to see the left operand is a column in a virtual table */ |
| 148606 | 150014 | if( NEVER(pExpr==0) ) return pDef; |
| 148607 | 150015 | if( pExpr->op!=TK_COLUMN ) return pDef; |
| 148608 | 150016 | assert( ExprUseYTab(pExpr) ); |
| 148609 | 150017 | pTab = pExpr->y.pTab; |
| 148610 | | - if( pTab==0 ) return pDef; |
| 150018 | + if( NEVER(pTab==0) ) return pDef; |
| 148611 | 150019 | if( !IsVirtual(pTab) ) return pDef; |
| 148612 | 150020 | pVtab = sqlite3GetVTable(db, pTab)->pVtab; |
| 148613 | 150021 | assert( pVtab!=0 ); |
| 148614 | 150022 | assert( pVtab->pModule!=0 ); |
| 148615 | 150023 | pMod = (sqlite3_module *)pVtab->pModule; |
| | @@ -149212,11 +150620,11 @@ |
| 149212 | 150620 | /* |
| 149213 | 150621 | ** An instance of the following structure keeps track of a mapping |
| 149214 | 150622 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
| 149215 | 150623 | ** |
| 149216 | 150624 | ** The VDBE cursor numbers are small integers contained in |
| 149217 | | -** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 150625 | +** SrcItem.iCursor and Expr.iTable fields. For any given WHERE |
| 149218 | 150626 | ** clause, the cursor numbers might not begin with 0 and they might |
| 149219 | 150627 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 149220 | 150628 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 149221 | 150629 | ** from the sparse cursor numbers into consecutive integers beginning |
| 149222 | 150630 | ** with 0. |
| | @@ -149283,24 +150691,10 @@ |
| 149283 | 150691 | #endif |
| 149284 | 150692 | #ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR |
| 149285 | 150693 | # define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000 |
| 149286 | 150694 | #endif |
| 149287 | 150695 | |
| 149288 | | -/* |
| 149289 | | -** Each instance of this object records a change to a single node |
| 149290 | | -** in an expression tree to cause that node to point to a column |
| 149291 | | -** of an index rather than an expression or a virtual column. All |
| 149292 | | -** such transformations need to be undone at the end of WHERE clause |
| 149293 | | -** processing. |
| 149294 | | -*/ |
| 149295 | | -typedef struct WhereExprMod WhereExprMod; |
| 149296 | | -struct WhereExprMod { |
| 149297 | | - WhereExprMod *pNext; /* Next translation on a list of them all */ |
| 149298 | | - Expr *pExpr; /* The Expr node that was transformed */ |
| 149299 | | - Expr orig; /* Original value of the Expr node */ |
| 149300 | | -}; |
| 149301 | | - |
| 149302 | 150696 | /* |
| 149303 | 150697 | ** The WHERE clause processing routine has two halves. The |
| 149304 | 150698 | ** first part does the start of the WHERE loop and the second |
| 149305 | 150699 | ** half does the tail of the WHERE loop. An instance of |
| 149306 | 150700 | ** this structure is returned by the first half and passed |
| | @@ -149312,14 +150706,14 @@ |
| 149312 | 150706 | struct WhereInfo { |
| 149313 | 150707 | Parse *pParse; /* Parsing and code generating context */ |
| 149314 | 150708 | SrcList *pTabList; /* List of tables in the join */ |
| 149315 | 150709 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 149316 | 150710 | ExprList *pResultSet; /* Result set of the query */ |
| 150711 | +#if WHERETRACE_ENABLED |
| 149317 | 150712 | Expr *pWhere; /* The complete WHERE clause */ |
| 149318 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 149319 | | - Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */ |
| 149320 | 150713 | #endif |
| 150714 | + Select *pSelect; /* The entire SELECT statement containing WHERE */ |
| 149321 | 150715 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 149322 | 150716 | int iContinue; /* Jump here to continue with next record */ |
| 149323 | 150717 | int iBreak; /* Jump here to break out of the loop */ |
| 149324 | 150718 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 149325 | 150719 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| | @@ -149334,11 +150728,10 @@ |
| 149334 | 150728 | unsigned sorted :1; /* True if really sorted (not just grouped) */ |
| 149335 | 150729 | LogEst nRowOut; /* Estimated number of output rows */ |
| 149336 | 150730 | int iTop; /* The very beginning of the WHERE loop */ |
| 149337 | 150731 | int iEndWhere; /* End of the WHERE clause itself */ |
| 149338 | 150732 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
| 149339 | | - WhereExprMod *pExprMods; /* Expression modifications */ |
| 149340 | 150733 | WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */ |
| 149341 | 150734 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
| 149342 | 150735 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
| 149343 | 150736 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 149344 | 150737 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| | @@ -150686,147 +152079,10 @@ |
| 150686 | 152079 | assert( nReg==1 || pParse->nErr ); |
| 150687 | 152080 | sqlite3ExprCode(pParse, p, iReg); |
| 150688 | 152081 | } |
| 150689 | 152082 | } |
| 150690 | 152083 | |
| 150691 | | -/* An instance of the IdxExprTrans object carries information about a |
| 150692 | | -** mapping from an expression on table columns into a column in an index |
| 150693 | | -** down through the Walker. |
| 150694 | | -*/ |
| 150695 | | -typedef struct IdxExprTrans { |
| 150696 | | - Expr *pIdxExpr; /* The index expression */ |
| 150697 | | - int iTabCur; /* The cursor of the corresponding table */ |
| 150698 | | - int iIdxCur; /* The cursor for the index */ |
| 150699 | | - int iIdxCol; /* The column for the index */ |
| 150700 | | - int iTabCol; /* The column for the table */ |
| 150701 | | - WhereInfo *pWInfo; /* Complete WHERE clause information */ |
| 150702 | | - sqlite3 *db; /* Database connection (for malloc()) */ |
| 150703 | | -} IdxExprTrans; |
| 150704 | | - |
| 150705 | | -/* |
| 150706 | | -** Preserve pExpr on the WhereETrans list of the WhereInfo. |
| 150707 | | -*/ |
| 150708 | | -static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){ |
| 150709 | | - WhereExprMod *pNew; |
| 150710 | | - pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew)); |
| 150711 | | - if( pNew==0 ) return; |
| 150712 | | - pNew->pNext = pTrans->pWInfo->pExprMods; |
| 150713 | | - pTrans->pWInfo->pExprMods = pNew; |
| 150714 | | - pNew->pExpr = pExpr; |
| 150715 | | - memcpy(&pNew->orig, pExpr, sizeof(*pExpr)); |
| 150716 | | -} |
| 150717 | | - |
| 150718 | | -/* The walker node callback used to transform matching expressions into |
| 150719 | | -** a reference to an index column for an index on an expression. |
| 150720 | | -** |
| 150721 | | -** If pExpr matches, then transform it into a reference to the index column |
| 150722 | | -** that contains the value of pExpr. |
| 150723 | | -*/ |
| 150724 | | -static int whereIndexExprTransNode(Walker *p, Expr *pExpr){ |
| 150725 | | - IdxExprTrans *pX = p->u.pIdxTrans; |
| 150726 | | - if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){ |
| 150727 | | - pExpr = sqlite3ExprSkipCollate(pExpr); |
| 150728 | | - preserveExpr(pX, pExpr); |
| 150729 | | - pExpr->affExpr = sqlite3ExprAffinity(pExpr); |
| 150730 | | - pExpr->op = TK_COLUMN; |
| 150731 | | - pExpr->iTable = pX->iIdxCur; |
| 150732 | | - pExpr->iColumn = pX->iIdxCol; |
| 150733 | | - testcase( ExprHasProperty(pExpr, EP_Unlikely) ); |
| 150734 | | - ExprClearProperty(pExpr, EP_Skip|EP_Unlikely|EP_WinFunc|EP_Subrtn); |
| 150735 | | - pExpr->y.pTab = 0; |
| 150736 | | - return WRC_Prune; |
| 150737 | | - }else{ |
| 150738 | | - return WRC_Continue; |
| 150739 | | - } |
| 150740 | | -} |
| 150741 | | - |
| 150742 | | -#ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 150743 | | -/* A walker node callback that translates a column reference to a table |
| 150744 | | -** into a corresponding column reference of an index. |
| 150745 | | -*/ |
| 150746 | | -static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){ |
| 150747 | | - if( pExpr->op==TK_COLUMN ){ |
| 150748 | | - IdxExprTrans *pX = p->u.pIdxTrans; |
| 150749 | | - if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){ |
| 150750 | | - assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 ); |
| 150751 | | - preserveExpr(pX, pExpr); |
| 150752 | | - pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn); |
| 150753 | | - pExpr->iTable = pX->iIdxCur; |
| 150754 | | - pExpr->iColumn = pX->iIdxCol; |
| 150755 | | - pExpr->y.pTab = 0; |
| 150756 | | - } |
| 150757 | | - } |
| 150758 | | - return WRC_Continue; |
| 150759 | | -} |
| 150760 | | -#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ |
| 150761 | | - |
| 150762 | | -/* |
| 150763 | | -** For an indexes on expression X, locate every instance of expression X |
| 150764 | | -** in pExpr and change that subexpression into a reference to the appropriate |
| 150765 | | -** column of the index. |
| 150766 | | -** |
| 150767 | | -** 2019-10-24: Updated to also translate references to a VIRTUAL column in |
| 150768 | | -** the table into references to the corresponding (stored) column of the |
| 150769 | | -** index. |
| 150770 | | -*/ |
| 150771 | | -static void whereIndexExprTrans( |
| 150772 | | - Index *pIdx, /* The Index */ |
| 150773 | | - int iTabCur, /* Cursor of the table that is being indexed */ |
| 150774 | | - int iIdxCur, /* Cursor of the index itself */ |
| 150775 | | - WhereInfo *pWInfo /* Transform expressions in this WHERE clause */ |
| 150776 | | -){ |
| 150777 | | - int iIdxCol; /* Column number of the index */ |
| 150778 | | - ExprList *aColExpr; /* Expressions that are indexed */ |
| 150779 | | - Table *pTab; |
| 150780 | | - Walker w; |
| 150781 | | - IdxExprTrans x; |
| 150782 | | - aColExpr = pIdx->aColExpr; |
| 150783 | | - if( aColExpr==0 && !pIdx->bHasVCol ){ |
| 150784 | | - /* The index does not reference any expressions or virtual columns |
| 150785 | | - ** so no translations are needed. */ |
| 150786 | | - return; |
| 150787 | | - } |
| 150788 | | - pTab = pIdx->pTable; |
| 150789 | | - memset(&w, 0, sizeof(w)); |
| 150790 | | - w.u.pIdxTrans = &x; |
| 150791 | | - x.iTabCur = iTabCur; |
| 150792 | | - x.iIdxCur = iIdxCur; |
| 150793 | | - x.pWInfo = pWInfo; |
| 150794 | | - x.db = pWInfo->pParse->db; |
| 150795 | | - for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){ |
| 150796 | | - i16 iRef = pIdx->aiColumn[iIdxCol]; |
| 150797 | | - if( iRef==XN_EXPR ){ |
| 150798 | | - assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 ); |
| 150799 | | - x.pIdxExpr = aColExpr->a[iIdxCol].pExpr; |
| 150800 | | - if( sqlite3ExprIsConstant(x.pIdxExpr) ) continue; |
| 150801 | | - w.xExprCallback = whereIndexExprTransNode; |
| 150802 | | -#ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 150803 | | - }else if( iRef>=0 |
| 150804 | | - && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0 |
| 150805 | | - && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL)==0 |
| 150806 | | - || sqlite3StrICmp(sqlite3ColumnColl(&pTab->aCol[iRef]), |
| 150807 | | - sqlite3StrBINARY)==0) |
| 150808 | | - ){ |
| 150809 | | - /* Check to see if there are direct references to generated columns |
| 150810 | | - ** that are contained in the index. Pulling the generated column |
| 150811 | | - ** out of the index is an optimization only - the main table is always |
| 150812 | | - ** available if the index cannot be used. To avoid unnecessary |
| 150813 | | - ** complication, omit this optimization if the collating sequence for |
| 150814 | | - ** the column is non-standard */ |
| 150815 | | - x.iTabCol = iRef; |
| 150816 | | - w.xExprCallback = whereIndexExprTransColumn; |
| 150817 | | -#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ |
| 150818 | | - }else{ |
| 150819 | | - continue; |
| 150820 | | - } |
| 150821 | | - x.iIdxCol = iIdxCol; |
| 150822 | | - sqlite3WalkExpr(&w, pWInfo->pWhere); |
| 150823 | | - sqlite3WalkExprList(&w, pWInfo->pOrderBy); |
| 150824 | | - sqlite3WalkExprList(&w, pWInfo->pResultSet); |
| 150825 | | - } |
| 150826 | | -} |
| 150827 | | - |
| 150828 | 152084 | /* |
| 150829 | 152085 | ** The pTruth expression is always true because it is the WHERE clause |
| 150830 | 152086 | ** a partial index that is driving a query loop. Look through all of the |
| 150831 | 152087 | ** WHERE clause terms on the query, and if any of those terms must be |
| 150832 | 152088 | ** true because pTruth is true, then mark those WHERE clause terms as |
| | @@ -150891,10 +152147,12 @@ |
| 150891 | 152147 | assert( pTerm!=0 ); |
| 150892 | 152148 | assert( pTerm->pExpr!=0 ); |
| 150893 | 152149 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 150894 | 152150 | regRowid = sqlite3GetTempReg(pParse); |
| 150895 | 152151 | regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid); |
| 152152 | + sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt); |
| 152153 | + VdbeCoverage(pParse->pVdbe); |
| 150896 | 152154 | sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, |
| 150897 | 152155 | addrNxt, regRowid, 1); |
| 150898 | 152156 | VdbeCoverage(pParse->pVdbe); |
| 150899 | 152157 | }else{ |
| 150900 | 152158 | u16 nEq = pLoop->u.btree.nEq; |
| | @@ -151042,13 +152300,13 @@ |
| 151042 | 152300 | codeExprOrVector(pParse, pRight, iTarget, 1); |
| 151043 | 152301 | if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET |
| 151044 | 152302 | && pLoop->u.vtab.bOmitOffset |
| 151045 | 152303 | ){ |
| 151046 | 152304 | assert( pTerm->eOperator==WO_AUX ); |
| 151047 | | - assert( pWInfo->pLimit!=0 ); |
| 151048 | | - assert( pWInfo->pLimit->iOffset>0 ); |
| 151049 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset); |
| 152305 | + assert( pWInfo->pSelect!=0 ); |
| 152306 | + assert( pWInfo->pSelect->iOffset>0 ); |
| 152307 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset); |
| 151050 | 152308 | VdbeComment((v,"Zero OFFSET counter")); |
| 151051 | 152309 | } |
| 151052 | 152310 | } |
| 151053 | 152311 | } |
| 151054 | 152312 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| | @@ -151152,10 +152410,12 @@ |
| 151152 | 152410 | iReleaseReg = ++pParse->nMem; |
| 151153 | 152411 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 151154 | 152412 | if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 151155 | 152413 | addrNxt = pLevel->addrNxt; |
| 151156 | 152414 | if( pLevel->regFilter ){ |
| 152415 | + sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 152416 | + VdbeCoverage(v); |
| 151157 | 152417 | sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt, |
| 151158 | 152418 | iRowidReg, 1); |
| 151159 | 152419 | VdbeCoverage(v); |
| 151160 | 152420 | filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady); |
| 151161 | 152421 | } |
| | @@ -151503,10 +152763,15 @@ |
| 151503 | 152763 | ** of entries in the tree, so basing the number of steps to try |
| 151504 | 152764 | ** on the estimated number of rows in the btree seems like a good |
| 151505 | 152765 | ** guess. */ |
| 151506 | 152766 | addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan, |
| 151507 | 152767 | (pIdx->aiRowLogEst[0]+9)/10); |
| 152768 | + if( pRangeStart ){ |
| 152769 | + sqlite3VdbeChangeP5(v, 1); |
| 152770 | + sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1); |
| 152771 | + addrSeekScan = 0; |
| 152772 | + } |
| 151508 | 152773 | VdbeCoverage(v); |
| 151509 | 152774 | } |
| 151510 | 152775 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 151511 | 152776 | VdbeCoverage(v); |
| 151512 | 152777 | VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| | @@ -151641,31 +152906,10 @@ |
| 151641 | 152906 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
| 151642 | 152907 | iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
| 151643 | 152908 | } |
| 151644 | 152909 | |
| 151645 | 152910 | if( pLevel->iLeftJoin==0 ){ |
| 151646 | | - /* If pIdx is an index on one or more expressions, then look through |
| 151647 | | - ** all the expressions in pWInfo and try to transform matching expressions |
| 151648 | | - ** into reference to index columns. Also attempt to translate references |
| 151649 | | - ** to virtual columns in the table into references to (stored) columns |
| 151650 | | - ** of the index. |
| 151651 | | - ** |
| 151652 | | - ** Do not do this for the RHS of a LEFT JOIN. This is because the |
| 151653 | | - ** expression may be evaluated after OP_NullRow has been executed on |
| 151654 | | - ** the cursor. In this case it is important to do the full evaluation, |
| 151655 | | - ** as the result of the expression may not be NULL, even if all table |
| 151656 | | - ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a |
| 151657 | | - ** |
| 151658 | | - ** Also, do not do this when processing one index an a multi-index |
| 151659 | | - ** OR clause, since the transformation will become invalid once we |
| 151660 | | - ** move forward to the next index. |
| 151661 | | - ** https://sqlite.org/src/info/4e8e4857d32d401f |
| 151662 | | - */ |
| 151663 | | - if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ){ |
| 151664 | | - whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo); |
| 151665 | | - } |
| 151666 | | - |
| 151667 | 152911 | /* If a partial index is driving the loop, try to eliminate WHERE clause |
| 151668 | 152912 | ** terms from the query that must be true due to the WHERE clause of |
| 151669 | 152913 | ** the partial index. |
| 151670 | 152914 | ** |
| 151671 | 152915 | ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work |
| | @@ -151774,11 +153018,11 @@ |
| 151774 | 153018 | */ |
| 151775 | 153019 | if( pWInfo->nLevel>1 ){ |
| 151776 | 153020 | int nNotReady; /* The number of notReady tables */ |
| 151777 | 153021 | SrcItem *origSrc; /* Original list of tables */ |
| 151778 | 153022 | nNotReady = pWInfo->nLevel - iLevel - 1; |
| 151779 | | - pOrTab = sqlite3StackAllocRaw(db, |
| 153023 | + pOrTab = sqlite3DbMallocRawNN(db, |
| 151780 | 153024 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 151781 | 153025 | if( pOrTab==0 ) return notReady; |
| 151782 | 153026 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
| 151783 | 153027 | pOrTab->nSrc = pOrTab->nAlloc; |
| 151784 | 153028 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| | @@ -152027,11 +153271,11 @@ |
| 152027 | 153271 | ** indent everything in between the this point and the final OP_Return. |
| 152028 | 153272 | ** See tag-20220407a in vdbe.c and shell.c */ |
| 152029 | 153273 | assert( pLevel->op==OP_Return ); |
| 152030 | 153274 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 152031 | 153275 | |
| 152032 | | - if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); } |
| 153276 | + if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); } |
| 152033 | 153277 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 152034 | 153278 | }else |
| 152035 | 153279 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 152036 | 153280 | |
| 152037 | 153281 | { |
| | @@ -152655,11 +153899,11 @@ |
| 152655 | 153899 | ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a |
| 152656 | 153900 | */ |
| 152657 | 153901 | if( pLeft->op!=TK_COLUMN |
| 152658 | 153902 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 152659 | 153903 | || (ALWAYS( ExprUseYTab(pLeft) ) |
| 152660 | | - && pLeft->y.pTab |
| 153904 | + && ALWAYS(pLeft->y.pTab) |
| 152661 | 153905 | && IsVirtual(pLeft->y.pTab)) /* Might be numeric */ |
| 152662 | 153906 | ){ |
| 152663 | 153907 | int isNum; |
| 152664 | 153908 | double rDummy; |
| 152665 | 153909 | isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8); |
| | @@ -152772,12 +154016,11 @@ |
| 152772 | 154016 | ** |
| 152773 | 154017 | ** vtab_column MATCH expression |
| 152774 | 154018 | ** MATCH(expression,vtab_column) |
| 152775 | 154019 | */ |
| 152776 | 154020 | pCol = pList->a[1].pExpr; |
| 152777 | | - assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) ); |
| 152778 | | - testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 ); |
| 154021 | + assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); |
| 152779 | 154022 | if( ExprIsVtab(pCol) ){ |
| 152780 | 154023 | for(i=0; i<ArraySize(aOp); i++){ |
| 152781 | 154024 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 152782 | 154025 | if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ |
| 152783 | 154026 | *peOp2 = aOp[i].eOp2; |
| | @@ -152798,11 +154041,11 @@ |
| 152798 | 154041 | ** names. But for this use case, xFindFunction is expected to deal |
| 152799 | 154042 | ** with function names in an arbitrary case. |
| 152800 | 154043 | */ |
| 152801 | 154044 | pCol = pList->a[0].pExpr; |
| 152802 | 154045 | assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) ); |
| 152803 | | - testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 ); |
| 154046 | + assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); |
| 152804 | 154047 | if( ExprIsVtab(pCol) ){ |
| 152805 | 154048 | sqlite3_vtab *pVtab; |
| 152806 | 154049 | sqlite3_module *pMod; |
| 152807 | 154050 | void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**); |
| 152808 | 154051 | void *pNotUsed; |
| | @@ -152823,17 +154066,16 @@ |
| 152823 | 154066 | } |
| 152824 | 154067 | }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){ |
| 152825 | 154068 | int res = 0; |
| 152826 | 154069 | Expr *pLeft = pExpr->pLeft; |
| 152827 | 154070 | Expr *pRight = pExpr->pRight; |
| 152828 | | - assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) ); |
| 152829 | | - testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 ); |
| 154071 | + assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) ); |
| 152830 | 154072 | if( ExprIsVtab(pLeft) ){ |
| 152831 | 154073 | res++; |
| 152832 | 154074 | } |
| 152833 | | - assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) ); |
| 152834 | | - testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 ); |
| 154075 | + assert( pRight==0 || pRight->op!=TK_COLUMN |
| 154076 | + || (ExprUseYTab(pRight) && pRight->y.pTab!=0) ); |
| 152835 | 154077 | if( pRight && ExprIsVtab(pRight) ){ |
| 152836 | 154078 | res++; |
| 152837 | 154079 | SWAP(Expr*, pLeft, pRight); |
| 152838 | 154080 | } |
| 152839 | 154081 | *ppLeft = pLeft; |
| | @@ -153378,10 +154620,11 @@ |
| 153378 | 154620 | iCur = pFrom->a[i].iCursor; |
| 153379 | 154621 | for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 153380 | 154622 | if( pIdx->aColExpr==0 ) continue; |
| 153381 | 154623 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 153382 | 154624 | if( pIdx->aiColumn[i]!=XN_EXPR ) continue; |
| 154625 | + assert( pIdx->bHasExpr ); |
| 153383 | 154626 | if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){ |
| 153384 | 154627 | aiCurCol[0] = iCur; |
| 153385 | 154628 | aiCurCol[1] = XN_EXPR; |
| 153386 | 154629 | return 1; |
| 153387 | 154630 | } |
| | @@ -153991,13 +155234,13 @@ |
| 153991 | 155234 | ** |
| 153992 | 155235 | ** LIMIT and OFFSET terms are ignored by most of the planner code. They |
| 153993 | 155236 | ** exist only so that they may be passed to the xBestIndex method of the |
| 153994 | 155237 | ** single virtual table in the FROM clause of the SELECT. |
| 153995 | 155238 | */ |
| 153996 | | -SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ |
| 153997 | | - assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) ); |
| 153998 | | - if( (p && p->pLimit) /* 1 */ |
| 155239 | +SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ |
| 155240 | + assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */ |
| 155241 | + if( p->pGroupBy==0 |
| 153999 | 155242 | && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */ |
| 154000 | 155243 | && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */ |
| 154001 | 155244 | ){ |
| 154002 | 155245 | ExprList *pOrderBy = p->pOrderBy; |
| 154003 | 155246 | int iCsr = p->pSrc->a[0].iCursor; |
| | @@ -156530,30 +157773,18 @@ |
| 156530 | 157773 | while( pWInfo->pLoops ){ |
| 156531 | 157774 | WhereLoop *p = pWInfo->pLoops; |
| 156532 | 157775 | pWInfo->pLoops = p->pNextLoop; |
| 156533 | 157776 | whereLoopDelete(db, p); |
| 156534 | 157777 | } |
| 156535 | | - assert( pWInfo->pExprMods==0 ); |
| 156536 | 157778 | while( pWInfo->pMemToFree ){ |
| 156537 | 157779 | WhereMemBlock *pNext = pWInfo->pMemToFree->pNext; |
| 156538 | 157780 | sqlite3DbNNFreeNN(db, pWInfo->pMemToFree); |
| 156539 | 157781 | pWInfo->pMemToFree = pNext; |
| 156540 | 157782 | } |
| 156541 | 157783 | sqlite3DbNNFreeNN(db, pWInfo); |
| 156542 | 157784 | } |
| 156543 | 157785 | |
| 156544 | | -/* Undo all Expr node modifications |
| 156545 | | -*/ |
| 156546 | | -static void whereUndoExprMods(WhereInfo *pWInfo){ |
| 156547 | | - while( pWInfo->pExprMods ){ |
| 156548 | | - WhereExprMod *p = pWInfo->pExprMods; |
| 156549 | | - pWInfo->pExprMods = p->pNext; |
| 156550 | | - memcpy(p->pExpr, &p->orig, sizeof(p->orig)); |
| 156551 | | - sqlite3DbFree(pWInfo->pParse->db, p); |
| 156552 | | - } |
| 156553 | | -} |
| 156554 | | - |
| 156555 | 157786 | /* |
| 156556 | 157787 | ** Return TRUE if all of the following are true: |
| 156557 | 157788 | ** |
| 156558 | 157789 | ** (1) X has the same or lower cost, or returns the same or fewer rows, |
| 156559 | 157790 | ** than Y. |
| | @@ -157501,10 +158732,98 @@ |
| 157501 | 158732 | return 1; |
| 157502 | 158733 | } |
| 157503 | 158734 | } |
| 157504 | 158735 | return 0; |
| 157505 | 158736 | } |
| 158737 | + |
| 158738 | +/* |
| 158739 | +** Structure passed to the whereIsCoveringIndex Walker callback. |
| 158740 | +*/ |
| 158741 | +struct CoveringIndexCheck { |
| 158742 | + Index *pIdx; /* The index */ |
| 158743 | + int iTabCur; /* Cursor number for the corresponding table */ |
| 158744 | +}; |
| 158745 | + |
| 158746 | +/* |
| 158747 | +** Information passed in is pWalk->u.pCovIdxCk. Call is pCk. |
| 158748 | +** |
| 158749 | +** If the Expr node references the table with cursor pCk->iTabCur, then |
| 158750 | +** make sure that column is covered by the index pCk->pIdx. We know that |
| 158751 | +** all columns less than 63 (really BMS-1) are covered, so we don't need |
| 158752 | +** to check them. But we do need to check any column at 63 or greater. |
| 158753 | +** |
| 158754 | +** If the index does not cover the column, then set pWalk->eCode to |
| 158755 | +** non-zero and return WRC_Abort to stop the search. |
| 158756 | +** |
| 158757 | +** If this node does not disprove that the index can be a covering index, |
| 158758 | +** then just return WRC_Continue, to continue the search. |
| 158759 | +*/ |
| 158760 | +static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){ |
| 158761 | + int i; /* Loop counter */ |
| 158762 | + const Index *pIdx; /* The index of interest */ |
| 158763 | + const i16 *aiColumn; /* Columns contained in the index */ |
| 158764 | + u16 nColumn; /* Number of columns in the index */ |
| 158765 | + if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue; |
| 158766 | + if( pExpr->iColumn<(BMS-1) ) return WRC_Continue; |
| 158767 | + if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue; |
| 158768 | + pIdx = pWalk->u.pCovIdxCk->pIdx; |
| 158769 | + aiColumn = pIdx->aiColumn; |
| 158770 | + nColumn = pIdx->nColumn; |
| 158771 | + for(i=0; i<nColumn; i++){ |
| 158772 | + if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue; |
| 158773 | + } |
| 158774 | + pWalk->eCode = 1; |
| 158775 | + return WRC_Abort; |
| 158776 | +} |
| 158777 | + |
| 158778 | + |
| 158779 | +/* |
| 158780 | +** pIdx is an index that covers all of the low-number columns used by |
| 158781 | +** pWInfo->pSelect (columns from 0 through 62). But there are columns |
| 158782 | +** in pWInfo->pSelect beyond 62. This routine tries to answer the question |
| 158783 | +** of whether pIdx covers *all* columns in the query. |
| 158784 | +** |
| 158785 | +** Return 0 if pIdx is a covering index. Return non-zero if pIdx is |
| 158786 | +** not a covering index or if we are unable to determine if pIdx is a |
| 158787 | +** covering index. |
| 158788 | +** |
| 158789 | +** This routine is an optimization. It is always safe to return non-zero. |
| 158790 | +** But returning zero when non-zero should have been returned can lead to |
| 158791 | +** incorrect bytecode and assertion faults. |
| 158792 | +*/ |
| 158793 | +static SQLITE_NOINLINE u32 whereIsCoveringIndex( |
| 158794 | + WhereInfo *pWInfo, /* The WHERE clause context */ |
| 158795 | + Index *pIdx, /* Index that is being tested */ |
| 158796 | + int iTabCur /* Cursor for the table being indexed */ |
| 158797 | +){ |
| 158798 | + int i; |
| 158799 | + struct CoveringIndexCheck ck; |
| 158800 | + Walker w; |
| 158801 | + if( pWInfo->pSelect==0 ){ |
| 158802 | + /* We don't have access to the full query, so we cannot check to see |
| 158803 | + ** if pIdx is covering. Assume it is not. */ |
| 158804 | + return 1; |
| 158805 | + } |
| 158806 | + for(i=0; i<pIdx->nColumn; i++){ |
| 158807 | + if( pIdx->aiColumn[i]>=BMS-1 ) break; |
| 158808 | + } |
| 158809 | + if( i>=pIdx->nColumn ){ |
| 158810 | + /* pIdx does not index any columns greater than 62, but we know from |
| 158811 | + ** colMask that columns greater than 62 are used, so this is not a |
| 158812 | + ** covering index */ |
| 158813 | + return 1; |
| 158814 | + } |
| 158815 | + ck.pIdx = pIdx; |
| 158816 | + ck.iTabCur = iTabCur; |
| 158817 | + memset(&w, 0, sizeof(w)); |
| 158818 | + w.xExprCallback = whereIsCoveringIndexWalkCallback; |
| 158819 | + w.xSelectCallback = sqlite3SelectWalkNoop; |
| 158820 | + w.u.pCovIdxCk = &ck; |
| 158821 | + w.eCode = 0; |
| 158822 | + sqlite3WalkSelect(&w, pWInfo->pSelect); |
| 158823 | + return w.eCode; |
| 158824 | +} |
| 157506 | 158825 | |
| 157507 | 158826 | /* |
| 157508 | 158827 | ** Add all WhereLoop objects for a single table of the join where the table |
| 157509 | 158828 | ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be |
| 157510 | 158829 | ** a b-tree table, not a virtual table. |
| | @@ -157719,10 +159038,13 @@ |
| 157719 | 159038 | if( pProbe->isCovering ){ |
| 157720 | 159039 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 157721 | 159040 | m = 0; |
| 157722 | 159041 | }else{ |
| 157723 | 159042 | m = pSrc->colUsed & pProbe->colNotIdxed; |
| 159043 | + if( m==TOPBIT ){ |
| 159044 | + m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor); |
| 159045 | + } |
| 157724 | 159046 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 157725 | 159047 | } |
| 157726 | 159048 | |
| 157727 | 159049 | /* Full scan via index */ |
| 157728 | 159050 | if( b |
| | @@ -158944,11 +160266,10 @@ |
| 158944 | 160266 | */ |
| 158945 | 160267 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
| 158946 | 160268 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
| 158947 | 160269 | int nLoop; /* Number of terms in the join */ |
| 158948 | 160270 | Parse *pParse; /* Parsing context */ |
| 158949 | | - sqlite3 *db; /* The database connection */ |
| 158950 | 160271 | int iLoop; /* Loop counter over the terms of the join */ |
| 158951 | 160272 | int ii, jj; /* Loop counters */ |
| 158952 | 160273 | int mxI = 0; /* Index of next entry to replace */ |
| 158953 | 160274 | int nOrderBy; /* Number of ORDER BY clause terms */ |
| 158954 | 160275 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
| | @@ -158963,11 +160284,10 @@ |
| 158963 | 160284 | LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ |
| 158964 | 160285 | char *pSpace; /* Temporary memory used by this routine */ |
| 158965 | 160286 | int nSpace; /* Bytes of space allocated at pSpace */ |
| 158966 | 160287 | |
| 158967 | 160288 | pParse = pWInfo->pParse; |
| 158968 | | - db = pParse->db; |
| 158969 | 160289 | nLoop = pWInfo->nLevel; |
| 158970 | 160290 | /* TUNING: For simple queries, only the best path is tracked. |
| 158971 | 160291 | ** For 2-way joins, the 5 best paths are followed. |
| 158972 | 160292 | ** For joins of 3 or more tables, track the 10 best paths */ |
| 158973 | 160293 | mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); |
| | @@ -158986,11 +160306,11 @@ |
| 158986 | 160306 | } |
| 158987 | 160307 | |
| 158988 | 160308 | /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ |
| 158989 | 160309 | nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 158990 | 160310 | nSpace += sizeof(LogEst) * nOrderBy; |
| 158991 | | - pSpace = sqlite3DbMallocRawNN(db, nSpace); |
| 160311 | + pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace); |
| 158992 | 160312 | if( pSpace==0 ) return SQLITE_NOMEM_BKPT; |
| 158993 | 160313 | aTo = (WherePath*)pSpace; |
| 158994 | 160314 | aFrom = aTo+mxChoice; |
| 158995 | 160315 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 158996 | 160316 | pX = (WhereLoop**)(aFrom+mxChoice); |
| | @@ -159244,11 +160564,11 @@ |
| 159244 | 160564 | nFrom = nTo; |
| 159245 | 160565 | } |
| 159246 | 160566 | |
| 159247 | 160567 | if( nFrom==0 ){ |
| 159248 | 160568 | sqlite3ErrorMsg(pParse, "no query solution"); |
| 159249 | | - sqlite3DbFreeNN(db, pSpace); |
| 160569 | + sqlite3StackFreeNN(pParse->db, pSpace); |
| 159250 | 160570 | return SQLITE_ERROR; |
| 159251 | 160571 | } |
| 159252 | 160572 | |
| 159253 | 160573 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
| 159254 | 160574 | pFrom = aFrom; |
| | @@ -159326,12 +160646,11 @@ |
| 159326 | 160646 | |
| 159327 | 160647 | |
| 159328 | 160648 | pWInfo->nRowOut = pFrom->nRow; |
| 159329 | 160649 | |
| 159330 | 160650 | /* Free temporary memory and return success */ |
| 159331 | | - assert( db!=0 ); |
| 159332 | | - sqlite3DbNNFreeNN(db, pSpace); |
| 160651 | + sqlite3StackFreeNN(pParse->db, pSpace); |
| 159333 | 160652 | return SQLITE_OK; |
| 159334 | 160653 | } |
| 159335 | 160654 | |
| 159336 | 160655 | /* |
| 159337 | 160656 | ** Most queries use only a single table (they are not joins) and have |
| | @@ -159625,10 +160944,81 @@ |
| 159625 | 160944 | } |
| 159626 | 160945 | } |
| 159627 | 160946 | nSearch += pLoop->nOut; |
| 159628 | 160947 | } |
| 159629 | 160948 | } |
| 160949 | + |
| 160950 | +/* |
| 160951 | +** This is an sqlite3ParserAddCleanup() callback that is invoked to |
| 160952 | +** free the Parse->pIdxExpr list when the Parse object is destroyed. |
| 160953 | +*/ |
| 160954 | +static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){ |
| 160955 | + Parse *pParse = (Parse*)pObject; |
| 160956 | + while( pParse->pIdxExpr!=0 ){ |
| 160957 | + IndexedExpr *p = pParse->pIdxExpr; |
| 160958 | + pParse->pIdxExpr = p->pIENext; |
| 160959 | + sqlite3ExprDelete(db, p->pExpr); |
| 160960 | + sqlite3DbFreeNN(db, p); |
| 160961 | + } |
| 160962 | +} |
| 160963 | + |
| 160964 | +/* |
| 160965 | +** The index pIdx is used by a query and contains one or more expressions. |
| 160966 | +** In other words pIdx is an index on an expression. iIdxCur is the cursor |
| 160967 | +** number for the index and iDataCur is the cursor number for the corresponding |
| 160968 | +** table. |
| 160969 | +** |
| 160970 | +** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for |
| 160971 | +** each of the expressions in the index so that the expression code generator |
| 160972 | +** will know to replace occurrences of the indexed expression with |
| 160973 | +** references to the corresponding column of the index. |
| 160974 | +*/ |
| 160975 | +static SQLITE_NOINLINE void whereAddIndexedExpr( |
| 160976 | + Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */ |
| 160977 | + Index *pIdx, /* The index-on-expression that contains the expressions */ |
| 160978 | + int iIdxCur, /* Cursor number for pIdx */ |
| 160979 | + SrcItem *pTabItem /* The FROM clause entry for the table */ |
| 160980 | +){ |
| 160981 | + int i; |
| 160982 | + IndexedExpr *p; |
| 160983 | + Table *pTab; |
| 160984 | + assert( pIdx->bHasExpr ); |
| 160985 | + pTab = pIdx->pTable; |
| 160986 | + for(i=0; i<pIdx->nColumn; i++){ |
| 160987 | + Expr *pExpr; |
| 160988 | + int j = pIdx->aiColumn[i]; |
| 160989 | + int bMaybeNullRow; |
| 160990 | + if( j==XN_EXPR ){ |
| 160991 | + pExpr = pIdx->aColExpr->a[i].pExpr; |
| 160992 | + testcase( pTabItem->fg.jointype & JT_LEFT ); |
| 160993 | + testcase( pTabItem->fg.jointype & JT_RIGHT ); |
| 160994 | + testcase( pTabItem->fg.jointype & JT_LTORJ ); |
| 160995 | + bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0; |
| 160996 | + }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){ |
| 160997 | + pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]); |
| 160998 | + bMaybeNullRow = 0; |
| 160999 | + }else{ |
| 161000 | + continue; |
| 161001 | + } |
| 161002 | + if( sqlite3ExprIsConstant(pExpr) ) continue; |
| 161003 | + p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr)); |
| 161004 | + if( p==0 ) break; |
| 161005 | + p->pIENext = pParse->pIdxExpr; |
| 161006 | + p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 161007 | + p->iDataCur = pTabItem->iCursor; |
| 161008 | + p->iIdxCur = iIdxCur; |
| 161009 | + p->iIdxCol = i; |
| 161010 | + p->bMaybeNullRow = bMaybeNullRow; |
| 161011 | +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 161012 | + p->zIdxName = pIdx->zName; |
| 161013 | +#endif |
| 161014 | + pParse->pIdxExpr = p; |
| 161015 | + if( p->pIENext==0 ){ |
| 161016 | + sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse); |
| 161017 | + } |
| 161018 | + } |
| 161019 | +} |
| 159630 | 161020 | |
| 159631 | 161021 | /* |
| 159632 | 161022 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 159633 | 161023 | ** The return value is a pointer to an opaque structure that contains |
| 159634 | 161024 | ** information needed to terminate the loop. Later, the calling routine |
| | @@ -159720,11 +161110,11 @@ |
| 159720 | 161110 | Parse *pParse, /* The parser context */ |
| 159721 | 161111 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
| 159722 | 161112 | Expr *pWhere, /* The WHERE clause */ |
| 159723 | 161113 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
| 159724 | 161114 | ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */ |
| 159725 | | - Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */ |
| 161115 | + Select *pSelect, /* The entire SELECT statement */ |
| 159726 | 161116 | u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ |
| 159727 | 161117 | int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number |
| 159728 | 161118 | ** If WHERE_USE_LIMIT, then the limit amount */ |
| 159729 | 161119 | ){ |
| 159730 | 161120 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| | @@ -159789,21 +161179,21 @@ |
| 159789 | 161179 | goto whereBeginError; |
| 159790 | 161180 | } |
| 159791 | 161181 | pWInfo->pParse = pParse; |
| 159792 | 161182 | pWInfo->pTabList = pTabList; |
| 159793 | 161183 | pWInfo->pOrderBy = pOrderBy; |
| 161184 | +#if WHERETRACE_ENABLED |
| 159794 | 161185 | pWInfo->pWhere = pWhere; |
| 161186 | +#endif |
| 159795 | 161187 | pWInfo->pResultSet = pResultSet; |
| 159796 | 161188 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
| 159797 | 161189 | pWInfo->nLevel = nTabList; |
| 159798 | 161190 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse); |
| 159799 | 161191 | pWInfo->wctrlFlags = wctrlFlags; |
| 159800 | 161192 | pWInfo->iLimit = iAuxArg; |
| 159801 | 161193 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 159802 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 159803 | | - pWInfo->pLimit = pLimit; |
| 159804 | | -#endif |
| 161194 | + pWInfo->pSelect = pSelect; |
| 159805 | 161195 | memset(&pWInfo->nOBSat, 0, |
| 159806 | 161196 | offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); |
| 159807 | 161197 | memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); |
| 159808 | 161198 | assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ |
| 159809 | 161199 | pMaskSet = &pWInfo->sMaskSet; |
| | @@ -159868,11 +161258,13 @@ |
| 159868 | 161258 | #endif |
| 159869 | 161259 | } |
| 159870 | 161260 | |
| 159871 | 161261 | /* Analyze all of the subexpressions. */ |
| 159872 | 161262 | sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 159873 | | - sqlite3WhereAddLimit(&pWInfo->sWC, pLimit); |
| 161263 | + if( pSelect && pSelect->pLimit ){ |
| 161264 | + sqlite3WhereAddLimit(&pWInfo->sWC, pSelect); |
| 161265 | + } |
| 159874 | 161266 | if( pParse->nErr ) goto whereBeginError; |
| 159875 | 161267 | |
| 159876 | 161268 | /* Special case: WHERE terms that do not refer to any tables in the join |
| 159877 | 161269 | ** (constant expressions). Evaluate each such term, and jump over all the |
| 159878 | 161270 | ** generated code if the result is not true. |
| | @@ -160171,10 +161563,13 @@ |
| 160171 | 161563 | }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){ |
| 160172 | 161564 | iIndexCur = iAuxArg; |
| 160173 | 161565 | op = OP_ReopenIdx; |
| 160174 | 161566 | }else{ |
| 160175 | 161567 | iIndexCur = pParse->nTab++; |
| 161568 | + if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){ |
| 161569 | + whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem); |
| 161570 | + } |
| 160176 | 161571 | } |
| 160177 | 161572 | pLevel->iIdxCur = iIndexCur; |
| 160178 | 161573 | assert( pIx!=0 ); |
| 160179 | 161574 | assert( pIx->pSchema==pTab->pSchema ); |
| 160180 | 161575 | assert( iIndexCur>=0 ); |
| | @@ -160293,12 +161688,10 @@ |
| 160293 | 161688 | return pWInfo; |
| 160294 | 161689 | |
| 160295 | 161690 | /* Jump here if malloc fails */ |
| 160296 | 161691 | whereBeginError: |
| 160297 | 161692 | if( pWInfo ){ |
| 160298 | | - testcase( pWInfo->pExprMods!=0 ); |
| 160299 | | - whereUndoExprMods(pWInfo); |
| 160300 | 161693 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 160301 | 161694 | whereInfoFree(db, pWInfo); |
| 160302 | 161695 | } |
| 160303 | 161696 | return 0; |
| 160304 | 161697 | } |
| | @@ -160513,11 +161906,10 @@ |
| 160513 | 161906 | VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
| 160514 | 161907 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
| 160515 | 161908 | } |
| 160516 | 161909 | |
| 160517 | 161910 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
| 160518 | | - if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo); |
| 160519 | 161911 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 160520 | 161912 | int k, last; |
| 160521 | 161913 | VdbeOp *pOp, *pLastOp; |
| 160522 | 161914 | Index *pIdx = 0; |
| 160523 | 161915 | SrcItem *pTabItem = &pTabList->a[pLevel->iFrom]; |
| | @@ -160566,10 +161958,20 @@ |
| 160566 | 161958 | ){ |
| 160567 | 161959 | if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){ |
| 160568 | 161960 | last = iEnd; |
| 160569 | 161961 | }else{ |
| 160570 | 161962 | last = pWInfo->iEndWhere; |
| 161963 | + } |
| 161964 | + if( pIdx->bHasExpr ){ |
| 161965 | + IndexedExpr *p = pParse->pIdxExpr; |
| 161966 | + while( p ){ |
| 161967 | + if( p->iIdxCur==pLevel->iIdxCur ){ |
| 161968 | + p->iDataCur = -1; |
| 161969 | + p->iIdxCur = -1; |
| 161970 | + } |
| 161971 | + p = p->pIENext; |
| 161972 | + } |
| 160571 | 161973 | } |
| 160572 | 161974 | k = pLevel->addrBody + 1; |
| 160573 | 161975 | #ifdef SQLITE_DEBUG |
| 160574 | 161976 | if( db->flags & SQLITE_VdbeAddopTrace ){ |
| 160575 | 161977 | printf("TRANSLATE opcodes in range %d..%d\n", k, last-1); |
| | @@ -174293,10 +175695,23 @@ |
| 174293 | 175695 | createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0); |
| 174294 | 175696 | if( db->mallocFailed ){ |
| 174295 | 175697 | goto opendb_out; |
| 174296 | 175698 | } |
| 174297 | 175699 | |
| 175700 | +#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) |
| 175701 | + /* Process magic filenames ":localStorage:" and ":sessionStorage:" */ |
| 175702 | + if( zFilename && zFilename[0]==':' ){ |
| 175703 | + if( strcmp(zFilename, ":localStorage:")==0 ){ |
| 175704 | + zFilename = "file:local?vfs=kvvfs"; |
| 175705 | + flags |= SQLITE_OPEN_URI; |
| 175706 | + }else if( strcmp(zFilename, ":sessionStorage:")==0 ){ |
| 175707 | + zFilename = "file:session?vfs=kvvfs"; |
| 175708 | + flags |= SQLITE_OPEN_URI; |
| 175709 | + } |
| 175710 | + } |
| 175711 | +#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */ |
| 175712 | + |
| 174298 | 175713 | /* Parse the filename/URI argument |
| 174299 | 175714 | ** |
| 174300 | 175715 | ** Only allow sensible combinations of bits in the flags argument. |
| 174301 | 175716 | ** Throw an error if any non-sense combination is used. If we |
| 174302 | 175717 | ** do not block illegal combinations here, it could trigger |
| | @@ -175722,12 +177137,12 @@ |
| 175722 | 177137 | ** Recover as many snapshots as possible from the wal file associated with |
| 175723 | 177138 | ** schema zDb of database db. |
| 175724 | 177139 | */ |
| 175725 | 177140 | SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){ |
| 175726 | 177141 | int rc = SQLITE_ERROR; |
| 175727 | | - int iDb; |
| 175728 | 177142 | #ifndef SQLITE_OMIT_WAL |
| 177143 | + int iDb; |
| 175729 | 177144 | |
| 175730 | 177145 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 175731 | 177146 | if( !sqlite3SafetyCheckOk(db) ){ |
| 175732 | 177147 | return SQLITE_MISUSE_BKPT; |
| 175733 | 177148 | } |
| | @@ -201626,11 +203041,11 @@ |
| 201626 | 203041 | return SQLITE_LOCKED_VTAB; |
| 201627 | 203042 | } |
| 201628 | 203043 | rtreeReference(pRtree); |
| 201629 | 203044 | assert(nData>=1); |
| 201630 | 203045 | |
| 201631 | | - cell.iRowid = 0; /* Used only to suppress a compiler warning */ |
| 203046 | + memset(&cell, 0, sizeof(cell)); |
| 201632 | 203047 | |
| 201633 | 203048 | /* Constraint handling. A write operation on an r-tree table may return |
| 201634 | 203049 | ** SQLITE_CONSTRAINT for two reasons: |
| 201635 | 203050 | ** |
| 201636 | 203051 | ** 1. A duplicate rowid value, or |
| | @@ -236984,11 +238399,11 @@ |
| 236984 | 238399 | int nArg, /* Number of args */ |
| 236985 | 238400 | sqlite3_value **apUnused /* Function arguments */ |
| 236986 | 238401 | ){ |
| 236987 | 238402 | assert( nArg==0 ); |
| 236988 | 238403 | UNUSED_PARAM2(nArg, apUnused); |
| 236989 | | - sqlite3_result_text(pCtx, "fts5: 2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646", -1, SQLITE_TRANSIENT); |
| 238404 | + sqlite3_result_text(pCtx, "fts5: 2022-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910", -1, SQLITE_TRANSIENT); |
| 236990 | 238405 | } |
| 236991 | 238406 | |
| 236992 | 238407 | /* |
| 236993 | 238408 | ** Return true if zName is the extension on one of the shadow tables used |
| 236994 | 238409 | ** by this module. |
| 236995 | 238410 | |