| | @@ -16,11 +16,11 @@ |
| 16 | 16 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 17 | 17 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 18 | 18 | ** separate file. This file contains only code for the core SQLite library. |
| 19 | 19 | ** |
| 20 | 20 | ** The content in this amalgamation comes from Fossil check-in |
| 21 | | -** d08cabd778f7844d04f3852d93197e80c0d0 with changes in files: |
| 21 | +** ad4ee20509dc4e2803f35093b03df55cb3e3 with changes in files: |
| 22 | 22 | ** |
| 23 | 23 | ** |
| 24 | 24 | */ |
| 25 | 25 | #ifndef SQLITE_AMALGAMATION |
| 26 | 26 | #define SQLITE_CORE 1 |
| | @@ -467,14 +467,14 @@ |
| 467 | 467 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 468 | 468 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 469 | 469 | */ |
| 470 | 470 | #define SQLITE_VERSION "3.55.0" |
| 471 | 471 | #define SQLITE_VERSION_NUMBER 3055000 |
| 472 | | -#define SQLITE_SOURCE_ID "2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b" |
| 472 | +#define SQLITE_SOURCE_ID "2026-11-15 00:55:04 ad4ee20509dc4e2803f35093b03df55cb3e39c8083b0d4ad231e1432516c7841" |
| 473 | 473 | #define SQLITE_SCM_BRANCH "trunk" |
| 474 | 474 | #define SQLITE_SCM_TAGS "" |
| 475 | | -#define SQLITE_SCM_DATETIME "2026-10-24T22:25:07.764Z" |
| 475 | +#define SQLITE_SCM_DATETIME "2026-11-15T00:55:04.503Z" |
| 476 | 476 | |
| 477 | 477 | /* |
| 478 | 478 | ** CAPI3REF: Run-Time Library Version Numbers |
| 479 | 479 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 480 | 480 | ** |
| | @@ -795,10 +795,11 @@ |
| 795 | 795 | #define SQLITE_FORMAT 24 /* Not used */ |
| 796 | 796 | #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ |
| 797 | 797 | #define SQLITE_NOTADB 26 /* File opened that is not a database file */ |
| 798 | 798 | #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ |
| 799 | 799 | #define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */ |
| 800 | +#define SQLITE_MOVED 29 /* Internal use only: Cursor moved */ |
| 800 | 801 | #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ |
| 801 | 802 | #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ |
| 802 | 803 | /* end-of-error-codes */ |
| 803 | 804 | |
| 804 | 805 | /* |
| | @@ -2983,16 +2984,23 @@ |
| 2983 | 2984 | ** the number of significant digits that SQLite will attempt to preserve when |
| 2984 | 2985 | ** converting floating point numbers (IEEE 754 "doubles") into text. The |
| 2985 | 2986 | ** default value 17, as of SQLite version 3.52.0. The value was 15 in all |
| 2986 | 2987 | ** prior versions.<p> |
| 2987 | 2988 | ** This option takes two arguments which are an integer and a pointer |
| 2988 | | -** to an integer. The first argument is a small integer, between 3 and 23, or |
| 2989 | +** to an integer. The first argument is a small integer, between 4 and 23, or |
| 2989 | 2990 | ** zero. The FP_DIGITS setting is changed to that small integer, or left |
| 2990 | 2991 | ** unaltered if the first argument is zero or out of range. The second argument |
| 2991 | 2992 | ** is a pointer to an integer. If the pointer is not NULL, then the value of |
| 2992 | 2993 | ** the FP_DIGITS setting, after possibly being modified by the first |
| 2993 | 2994 | ** arguments, is written into the integer to which the second argument points. |
| 2995 | +** |
| 2996 | +** [[SQLITE_DBCONFIG_VDBE_TRACE]] |
| 2997 | +** <dt>SQLITE_DBCONFIG_VDBE_TRACE</dt> |
| 2998 | +** <dd>The SQLITE_DBCONFIG_VDBE_TRACE setting enables or disables tracing |
| 2999 | +** of the [bytecode engine]. This setting is only available when SQLite has |
| 3000 | +** be compiled with -DSQLITE_DEBUG. It is the equivalent of using |
| 3001 | +** [PRAGMA vdbe_trace]. |
| 2994 | 3002 | ** </dd> |
| 2995 | 3003 | ** |
| 2996 | 3004 | ** </dl> |
| 2997 | 3005 | ** |
| 2998 | 3006 | ** [[DBCONFIG arguments]] <h3>Arguments To SQLITE_DBCONFIG Options</h3> |
| | @@ -3039,11 +3047,12 @@ |
| 3039 | 3047 | #define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */ |
| 3040 | 3048 | #define SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE 1020 /* int int* */ |
| 3041 | 3049 | #define SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE 1021 /* int int* */ |
| 3042 | 3050 | #define SQLITE_DBCONFIG_ENABLE_COMMENTS 1022 /* int int* */ |
| 3043 | 3051 | #define SQLITE_DBCONFIG_FP_DIGITS 1023 /* int int* */ |
| 3044 | | -#define SQLITE_DBCONFIG_MAX 1023 /* Largest DBCONFIG */ |
| 3052 | +#define SQLITE_DBCONFIG_VDBE_TRACE 1024 /* int int* */ |
| 3053 | +#define SQLITE_DBCONFIG_MAX 1024 /* Largest DBCONFIG */ |
| 3045 | 3054 | |
| 3046 | 3055 | /* |
| 3047 | 3056 | ** CAPI3REF: Enable Or Disable Extended Result Codes |
| 3048 | 3057 | ** METHOD: sqlite3 |
| 3049 | 3058 | ** |
| | @@ -5320,11 +5329,11 @@ |
| 5320 | 5329 | ** or indicates the lifetime of the object referenced by the third parameter. |
| 5321 | 5330 | ** These three options exist: |
| 5322 | 5331 | ** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished |
| 5323 | 5332 | ** with it may be passed. ^It is called to dispose of the BLOB or string even |
| 5324 | 5333 | ** if the call to the bind API fails, except the destructor is not called if |
| 5325 | | -** the third parameter is a NULL pointer or the fourth parameter is negative. |
| 5334 | +** the third parameter is a NULL pointer or the fifth parameter is negative. |
| 5326 | 5335 | ** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that |
| 5327 | 5336 | ** the application remains responsible for disposing of the object. ^In this |
| 5328 | 5337 | ** case, the object and the provided pointer to it must remain valid until |
| 5329 | 5338 | ** either the prepared statement is finalized or the same SQL parameter is |
| 5330 | 5339 | ** bound to something else, whichever occurs sooner. |
| | @@ -9112,11 +9121,12 @@ |
| 9112 | 9121 | #define SQLITE_TESTCTRL_TRACEFLAGS 31 |
| 9113 | 9122 | #define SQLITE_TESTCTRL_TUNE 32 |
| 9114 | 9123 | #define SQLITE_TESTCTRL_LOGEST 33 |
| 9115 | 9124 | #define SQLITE_TESTCTRL_USELONGDOUBLE 34 /* NOT USED */ |
| 9116 | 9125 | #define SQLITE_TESTCTRL_ATOF 34 |
| 9117 | | -#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */ |
| 9126 | +#define SQLITE_TESTCTRL_EXPIRE_STMTS 35 |
| 9127 | +#define SQLITE_TESTCTRL_LAST 35 /* Largest TESTCTRL */ |
| 9118 | 9128 | |
| 9119 | 9129 | /* |
| 9120 | 9130 | ** CAPI3REF: SQL Keyword Checking |
| 9121 | 9131 | ** |
| 9122 | 9132 | ** These routines provide access to the set of SQL language keywords |
| | @@ -18041,64 +18051,66 @@ |
| 18041 | 18051 | #define OP_ResetCount 132 |
| 18042 | 18052 | #define OP_SorterCompare 133 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ |
| 18043 | 18053 | #define OP_SorterData 134 /* synopsis: r[P2]=data */ |
| 18044 | 18054 | #define OP_RowData 135 /* synopsis: r[P2]=data */ |
| 18045 | 18055 | #define OP_Rowid 136 /* synopsis: r[P2]=PX rowid of P1 */ |
| 18046 | | -#define OP_NullRow 137 |
| 18047 | | -#define OP_SeekEnd 138 |
| 18048 | | -#define OP_IdxInsert 139 /* synopsis: key=r[P2] */ |
| 18049 | | -#define OP_SorterInsert 140 /* synopsis: key=r[P2] */ |
| 18050 | | -#define OP_IdxDelete 141 /* synopsis: key=r[P2@P5] */ |
| 18051 | | -#define OP_DeferredSeek 142 /* synopsis: Move P3 to P1.rowid if needed */ |
| 18052 | | -#define OP_IdxRowid 143 /* synopsis: r[P2]=rowid */ |
| 18053 | | -#define OP_FinishSeek 144 |
| 18054 | | -#define OP_Destroy 145 |
| 18055 | | -#define OP_Clear 146 |
| 18056 | | -#define OP_ResetSorter 147 |
| 18057 | | -#define OP_CreateBtree 148 /* synopsis: r[P2]=root iDb=P1 flags=P3 */ |
| 18058 | | -#define OP_SqlExec 149 |
| 18059 | | -#define OP_ParseSchema 150 |
| 18060 | | -#define OP_LoadAnalysis 151 |
| 18061 | | -#define OP_DropTable 152 |
| 18062 | | -#define OP_DropIndex 153 |
| 18056 | +#define OP_Stale 137 |
| 18057 | +#define OP_NullRow 138 |
| 18058 | +#define OP_SeekEnd 139 |
| 18059 | +#define OP_IdxInsert 140 /* synopsis: key=r[P2] */ |
| 18060 | +#define OP_SorterInsert 141 /* synopsis: key=r[P2] */ |
| 18061 | +#define OP_IdxDelete 142 /* synopsis: key=r[P2@P5] */ |
| 18062 | +#define OP_DeferredSeek 143 /* synopsis: Move P3 to P1.rowid if needed */ |
| 18063 | +#define OP_IdxRowid 144 /* synopsis: r[P2]=rowid */ |
| 18064 | +#define OP_FinishSeek 145 |
| 18065 | +#define OP_Destroy 146 |
| 18066 | +#define OP_DbLock 147 |
| 18067 | +#define OP_Clear 148 |
| 18068 | +#define OP_ResetSorter 149 |
| 18069 | +#define OP_CreateBtree 150 /* synopsis: r[P2]=root iDb=P1 flags=P3 */ |
| 18070 | +#define OP_SqlExec 151 |
| 18071 | +#define OP_ParseSchema 152 |
| 18072 | +#define OP_LoadAnalysis 153 |
| 18063 | 18073 | #define OP_Real 154 /* same as TK_FLOAT, synopsis: r[P2]=PDBL13 */ |
| 18064 | | -#define OP_DropTrigger 155 |
| 18065 | | -#define OP_IntegrityCk 156 |
| 18066 | | -#define OP_RowSetAdd 157 /* synopsis: rowset(P1)=r[P2] */ |
| 18067 | | -#define OP_Param 158 |
| 18068 | | -#define OP_FkCounter 159 /* synopsis: fkctr[P1]+=P2 */ |
| 18069 | | -#define OP_MemMax 160 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 18070 | | -#define OP_OffsetLimit 161 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */ |
| 18071 | | -#define OP_AggInverse 162 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */ |
| 18072 | | -#define OP_AggStep 163 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 18073 | | -#define OP_AggStep1 164 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 18074 | | -#define OP_AggValue 165 /* synopsis: r[P3]=value N=P2 */ |
| 18075 | | -#define OP_AggFinal 166 /* synopsis: accum=r[P1] N=P2 */ |
| 18076 | | -#define OP_Expire 167 |
| 18077 | | -#define OP_CursorLock 168 |
| 18078 | | -#define OP_CursorUnlock 169 |
| 18079 | | -#define OP_TableLock 170 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 18080 | | -#define OP_VBegin 171 |
| 18081 | | -#define OP_VCreate 172 |
| 18082 | | -#define OP_VDestroy 173 |
| 18083 | | -#define OP_VOpen 174 |
| 18084 | | -#define OP_VCheck 175 |
| 18085 | | -#define OP_VInitIn 176 /* synopsis: r[P2]=ValueList(P1,P3) */ |
| 18086 | | -#define OP_VColumn 177 /* synopsis: r[P3]=vcolumn(P2) */ |
| 18087 | | -#define OP_VRename 178 |
| 18088 | | -#define OP_Pagecount 179 |
| 18089 | | -#define OP_MaxPgcnt 180 |
| 18090 | | -#define OP_ClrSubtype 181 /* synopsis: r[P1].subtype = 0 */ |
| 18091 | | -#define OP_GetSubtype 182 /* synopsis: r[P2] = r[P1].subtype */ |
| 18092 | | -#define OP_SetSubtype 183 /* synopsis: r[P2].subtype = r[P1] */ |
| 18093 | | -#define OP_FilterAdd 184 /* synopsis: filter(P1) += key(P3@P4) */ |
| 18094 | | -#define OP_Trace 185 |
| 18095 | | -#define OP_CursorHint 186 |
| 18096 | | -#define OP_ReleaseReg 187 /* synopsis: release r[P1@P2] mask P3 */ |
| 18097 | | -#define OP_Noop 188 |
| 18098 | | -#define OP_Explain 189 |
| 18099 | | -#define OP_Abortable 190 |
| 18074 | +#define OP_DropTable 155 |
| 18075 | +#define OP_DropIndex 156 |
| 18076 | +#define OP_DropTrigger 157 |
| 18077 | +#define OP_IntegrityCk 158 |
| 18078 | +#define OP_RowSetAdd 159 /* synopsis: rowset(P1)=r[P2] */ |
| 18079 | +#define OP_Param 160 |
| 18080 | +#define OP_FkCounter 161 /* synopsis: fkctr[P1]+=P2 */ |
| 18081 | +#define OP_MemMax 162 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 18082 | +#define OP_OffsetLimit 163 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */ |
| 18083 | +#define OP_AggInverse 164 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */ |
| 18084 | +#define OP_AggStep 165 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 18085 | +#define OP_AggStep1 166 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 18086 | +#define OP_AggValue 167 /* synopsis: r[P3]=value N=P2 */ |
| 18087 | +#define OP_AggFinal 168 /* synopsis: accum=r[P1] N=P2 */ |
| 18088 | +#define OP_Expire 169 |
| 18089 | +#define OP_CursorLock 170 |
| 18090 | +#define OP_CursorUnlock 171 |
| 18091 | +#define OP_TableLock 172 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 18092 | +#define OP_VBegin 173 |
| 18093 | +#define OP_VCreate 174 |
| 18094 | +#define OP_VDestroy 175 |
| 18095 | +#define OP_VOpen 176 |
| 18096 | +#define OP_VCheck 177 |
| 18097 | +#define OP_VInitIn 178 /* synopsis: r[P2]=ValueList(P1,P3) */ |
| 18098 | +#define OP_VColumn 179 /* synopsis: r[P3]=vcolumn(P2) */ |
| 18099 | +#define OP_VRename 180 |
| 18100 | +#define OP_Pagecount 181 |
| 18101 | +#define OP_MaxPgcnt 182 |
| 18102 | +#define OP_ClrSubtype 183 /* synopsis: r[P1].subtype = 0 */ |
| 18103 | +#define OP_GetSubtype 184 /* synopsis: r[P2] = r[P1].subtype */ |
| 18104 | +#define OP_SetSubtype 185 /* synopsis: r[P2].subtype = r[P1] */ |
| 18105 | +#define OP_FilterAdd 186 /* synopsis: filter(P1) += key(P3@P4) */ |
| 18106 | +#define OP_Trace 187 |
| 18107 | +#define OP_CursorHint 188 |
| 18108 | +#define OP_ReleaseReg 189 /* synopsis: release r[P1@P2] mask P3 */ |
| 18109 | +#define OP_Noop 190 |
| 18110 | +#define OP_Explain 191 |
| 18111 | +#define OP_Abortable 192 |
| 18100 | 18112 | |
| 18101 | 18113 | /* Properties such as "out2" or "jump" that are specified in |
| 18102 | 18114 | ** comments following the "case" for each opcode in the vdbe.c |
| 18103 | 18115 | ** are encoded into bitvectors as follows: |
| 18104 | 18116 | */ |
| | @@ -18126,17 +18138,18 @@ |
| 18126 | 18138 | /* 96 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x26,\ |
| 18127 | 18139 | /* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\ |
| 18128 | 18140 | /* 112 */ 0x26, 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40,\ |
| 18129 | 18141 | /* 120 */ 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10,\ |
| 18130 | 18142 | /* 128 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,\ |
| 18131 | | -/* 136 */ 0x50, 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50,\ |
| 18132 | | -/* 144 */ 0x40, 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,\ |
| 18133 | | -/* 152 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00,\ |
| 18134 | | -/* 160 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 18135 | | -/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10,\ |
| 18136 | | -/* 176 */ 0x50, 0x40, 0x00, 0x10, 0x10, 0x02, 0x12, 0x12,\ |
| 18137 | | -/* 184 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,} |
| 18143 | +/* 136 */ 0x50, 0x00, 0x00, 0x40, 0x04, 0x04, 0x00, 0x40,\ |
| 18144 | +/* 144 */ 0x50, 0x40, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00,\ |
| 18145 | +/* 152 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x06,\ |
| 18146 | +/* 160 */ 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00,\ |
| 18147 | +/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 18148 | +/* 176 */ 0x40, 0x10, 0x50, 0x40, 0x00, 0x10, 0x10, 0x02,\ |
| 18149 | +/* 184 */ 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 18150 | +/* 192 */ 0x00,} |
| 18138 | 18151 | |
| 18139 | 18152 | /* The resolve3P2Values() routine is able to run faster if it knows |
| 18140 | 18153 | ** the value of the largest JUMP opcode. The smaller the maximum |
| 18141 | 18154 | ** JUMP opcode the better, so the mkopcodeh.tcl script that |
| 18142 | 18155 | ** generated this include file strives to group all JUMP opcodes |
| | @@ -18930,14 +18943,15 @@ |
| 18930 | 18943 | u8 mTrace; /* zero or more SQLITE_TRACE flags */ |
| 18931 | 18944 | u8 noSharedCache; /* True if no shared-cache backends */ |
| 18932 | 18945 | u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */ |
| 18933 | 18946 | u8 eOpenState; /* Current condition of the connection */ |
| 18934 | 18947 | u8 nFpDigit; /* Significant digits to keep on double->text */ |
| 18948 | + u8 bPendingSchemaReset; /* 1 or more schema resets pending */ |
| 18935 | 18949 | int nextPagesize; /* Pagesize after VACUUM if >0 */ |
| 18936 | 18950 | i64 nChange; /* Value returned by sqlite3_changes() */ |
| 18937 | 18951 | i64 nTotalChange; /* Value returned by sqlite3_total_changes() */ |
| 18938 | | - u32 dbOptFlags; /* Flags to enable/disable optimizations */ |
| 18952 | + u64 dbOptFlags; /* Flags to enable/disable optimizations */ |
| 18939 | 18953 | int aLimit[SQLITE_N_LIMIT]; /* Limits */ |
| 18940 | 18954 | int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */ |
| 18941 | 18955 | struct sqlite3InitInfo { /* Information used during initialization */ |
| 18942 | 18956 | Pgno newTnum; /* Rootpage of table being initialized */ |
| 18943 | 18957 | u8 iDb; /* Which db file is being initialized */ |
| | @@ -19161,11 +19175,11 @@ |
| 19161 | 19175 | #define SQLITE_PropagateConst 16 /* The constant propagation opt */ |
| 19162 | 19176 | #define SQLITE_MinMaxOpt 17 /* The min/max optimization */ |
| 19163 | 19177 | #define SQLITE_SeekScan 18 /* The OP_SeekScan optimization */ |
| 19164 | 19178 | #define SQLITE_OmitOrderBy 19 /* Omit pointless ORDER BY */ |
| 19165 | 19179 | #define SQLITE_BloomFilter 20 /* Use a Bloom filters */ |
| 19166 | | - /* 21 -- Available for reuse */ |
| 19180 | +#define SQLITE_DevTest 21 /* Temporary development testing use */ |
| 19167 | 19181 | #define SQLITE_BalancedMerge 22 /* Balance multi-way merges */ |
| 19168 | 19182 | #define SQLITE_ReleaseReg 23 /* Use OP_ReleaseReg for testing */ |
| 19169 | 19183 | #define SQLITE_FlttnUnionAll 24 /* Disable the UNION ALL flattener */ |
| 19170 | 19184 | #define SQLITE_IndexedExpr 25 /* Pull exprs from index when able */ |
| 19171 | 19185 | #define SQLITE_Coroutines 26 /* Co-routines for subqueries */ |
| | @@ -19173,16 +19187,17 @@ |
| 19173 | 19187 | #define SQLITE_OnePass 28 /* Single-pass DELETE and UPDATE */ |
| 19174 | 19188 | #define SQLITE_OrderBySubq 29 /* ORDER BY in subquery helps outer */ |
| 19175 | 19189 | #define SQLITE_StarQuery 30 /* Heurists for star queries */ |
| 19176 | 19190 | #define SQLITE_ExistsToJoin 31 /* The EXISTS-to-JOIN optimization */ |
| 19177 | 19191 | #define SQLITE_UnionLimit 32 /* Optimizations for UNION + LIMIT */ |
| 19192 | +#define SQLITE_HavingToWhere 33 /* HAVING-to-WHERE optimization */ |
| 19178 | 19193 | |
| 19179 | 19194 | /* |
| 19180 | 19195 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 19181 | 19196 | */ |
| 19182 | | -#define OptimizationDisabled(D,M) (((D)->dbOptFlags&(1ul<<((M)-1)))!=0) |
| 19183 | | -#define OptimizationEnabled(D,M) (((D)->dbOptFlags&(1ul<<((M)-1)))==0) |
| 19197 | +#define OptimizationDisabled(D,M) (((D)->dbOptFlags&(1ull<<((M)-1)))!=0) |
| 19198 | +#define OptimizationEnabled(D,M) (((D)->dbOptFlags&(1ull<<((M)-1)))==0) |
| 19184 | 19199 | |
| 19185 | 19200 | /* |
| 19186 | 19201 | ** Return true if it OK to factor constant expressions into the initialization |
| 19187 | 19202 | ** code. The argument is a Parse object for the code generator. |
| 19188 | 19203 | */ |
| | @@ -20486,10 +20501,11 @@ |
| 20486 | 20501 | unsigned reusable :1; /* Constant expression is reusable */ |
| 20487 | 20502 | unsigned bSorterRef :1; /* Defer evaluation until after sorting */ |
| 20488 | 20503 | unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */ |
| 20489 | 20504 | unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */ |
| 20490 | 20505 | unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */ |
| 20506 | + unsigned bStarName:1; /* RENAME only: zEName from expanding a "*" */ |
| 20491 | 20507 | unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should |
| 20492 | 20508 | ** not be expanded by "*" in parent queries */ |
| 20493 | 20509 | } fg; |
| 20494 | 20510 | union { |
| 20495 | 20511 | struct { /* Used by any ExprList other than Parse.pConsExpr */ |
| | @@ -21053,20 +21069,26 @@ |
| 21053 | 21069 | #if SQLITE_MAX_ATTACHED>30 |
| 21054 | 21070 | typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8]; |
| 21055 | 21071 | # define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0) |
| 21056 | 21072 | # define DbMaskZero(M) memset((M),0,sizeof(M)) |
| 21057 | 21073 | # define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7)) |
| 21074 | +# define DbMaskUnion(M,X) sqlite3DbMaskUnion(M,X) |
| 21058 | 21075 | # define DbMaskAllZero(M) sqlite3DbMaskAllZero(M) |
| 21059 | 21076 | # define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0) |
| 21060 | 21077 | #else |
| 21061 | 21078 | typedef unsigned int yDbMask; |
| 21062 | 21079 | # define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0) |
| 21063 | 21080 | # define DbMaskZero(M) ((M)=0) |
| 21064 | 21081 | # define DbMaskSet(M,I) ((M)|=(((yDbMask)1)<<(I))) |
| 21082 | +# define DbMaskUnion(M,X) ((M)|=(X)) |
| 21065 | 21083 | # define DbMaskAllZero(M) ((M)==0) |
| 21066 | 21084 | # define DbMaskNonZero(M) ((M)!=0) |
| 21067 | 21085 | #endif |
| 21086 | + |
| 21087 | +/* Should be in vdbe.h, but we needed yDbMask definition first */ |
| 21088 | +SQLITE_PRIVATE void sqlite3VdbeSchemaInUse(sqlite3*,Vdbe*,yDbMask*); |
| 21089 | + |
| 21068 | 21090 | |
| 21069 | 21091 | /* |
| 21070 | 21092 | ** For each index X that has as one of its arguments either an expression |
| 21071 | 21093 | ** or the name of a virtual generated column, and if X is in scope such that |
| 21072 | 21094 | ** the value of the expression can simply be read from the index, then |
| | @@ -21194,11 +21216,10 @@ |
| 21194 | 21216 | ** determined by offsetof(Parse,aTempReg). |
| 21195 | 21217 | **************************************************************************/ |
| 21196 | 21218 | |
| 21197 | 21219 | int aTempReg[8]; /* Holding area for temporary registers */ |
| 21198 | 21220 | Parse *pOuterParse; /* Outer Parse object when nested */ |
| 21199 | | - Token sNameToken; /* Token with unqualified schema object name */ |
| 21200 | 21221 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 21201 | 21222 | u32 newmask; /* Mask of new.* columns referenced */ |
| 21202 | 21223 | union { |
| 21203 | 21224 | struct { /* These fields available when isCreate is true */ |
| 21204 | 21225 | int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */ |
| | @@ -21223,11 +21244,12 @@ |
| 21223 | 21244 | ** each recursion. The boundary between these two regions is determined |
| 21224 | 21245 | ** using offsetof(Parse,sLastToken) so the sLastToken field must be the |
| 21225 | 21246 | ** first field in the recursive region. |
| 21226 | 21247 | ************************************************************************/ |
| 21227 | 21248 | |
| 21228 | | - Token sLastToken; /* The last token parsed */ |
| 21249 | + Token sLastToken; /* The last token parsed */ |
| 21250 | + Token sNameToken; /* Token with unqualified schema object name */ |
| 21229 | 21251 | ynVar nVar; /* Number of '?' variables seen in the SQL so far */ |
| 21230 | 21252 | u64 aVnbmc[SQLITE_VNBMC]; /* Varible Number Bitmap Cache */ |
| 21231 | 21253 | u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */ |
| 21232 | 21254 | u8 explain; /* True if the EXPLAIN flag is found on the query */ |
| 21233 | 21255 | u8 eParseMode; /* PARSE_MODE_XXX constant */ |
| | @@ -21347,17 +21369,25 @@ |
| 21347 | 21369 | ** linked list is stored as the "pTrigger" member of the associated |
| 21348 | 21370 | ** struct Table. |
| 21349 | 21371 | ** |
| 21350 | 21372 | ** The "step_list" member points to the first element of a linked list |
| 21351 | 21373 | ** containing the SQL statements specified as the trigger program. |
| 21374 | +** |
| 21375 | +** Trigger.nSelect is a copy of the Parse.nSelect value from when the |
| 21376 | +** trigger and its steps were parsed. There are no Select.selId values |
| 21377 | +** equal to or greater than this anywhere in the trigger program. |
| 21378 | +** Initializing the Parse.nSelect field to this value before coding |
| 21379 | +** the trigger program based on these structures ensures that no duplicate |
| 21380 | +** selId values are assigned. |
| 21352 | 21381 | */ |
| 21353 | 21382 | struct Trigger { |
| 21354 | 21383 | char *zName; /* The name of the trigger */ |
| 21355 | 21384 | char *table; /* The table or view to which the trigger applies */ |
| 21356 | 21385 | u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */ |
| 21357 | 21386 | u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ |
| 21358 | 21387 | u8 bReturning; /* This trigger implements a RETURNING clause */ |
| 21388 | + int nSelect; /* Upper bound on Select.selId in this trigger */ |
| 21359 | 21389 | Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */ |
| 21360 | 21390 | IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger, |
| 21361 | 21391 | the <column-list> is stored here */ |
| 21362 | 21392 | Schema *pSchema; /* Schema containing the trigger */ |
| 21363 | 21393 | Schema *pTabSchema; /* Schema containing the table */ |
| | @@ -22276,11 +22306,11 @@ |
| 22276 | 22306 | SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int); |
| 22277 | 22307 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 22278 | 22308 | SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName); |
| 22279 | 22309 | #endif |
| 22280 | 22310 | SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*); |
| 22281 | | -SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int); |
| 22311 | +SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int,Vdbe*); |
| 22282 | 22312 | SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*); |
| 22283 | 22313 | SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*); |
| 22284 | 22314 | SQLITE_PRIVATE void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*); |
| 22285 | 22315 | SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table*,Column*); |
| 22286 | 22316 | SQLITE_PRIVATE Expr *sqlite3ColumnExprAuth(Table*,Column*,Parse*); |
| | @@ -22353,10 +22383,11 @@ |
| 22353 | 22383 | # define sqlite3ViewGetColumnNames(A,B) 0 |
| 22354 | 22384 | #endif |
| 22355 | 22385 | |
| 22356 | 22386 | #if SQLITE_MAX_ATTACHED>30 |
| 22357 | 22387 | SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask); |
| 22388 | +SQLITE_PRIVATE void sqlite3DbMaskUnion(yDbMask,yDbMask); |
| 22358 | 22389 | #endif |
| 22359 | 22390 | SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); |
| 22360 | 22391 | SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); |
| 22361 | 22392 | SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); |
| 22362 | 22393 | SQLITE_PRIVATE void sqlite3DeleteTableGeneric(sqlite3*, void*); |
| | @@ -22718,11 +22749,11 @@ |
| 22718 | 22749 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); |
| 22719 | 22750 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*); |
| 22720 | 22751 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*); |
| 22721 | 22752 | SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); |
| 22722 | 22753 | SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*); |
| 22723 | | -SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*); |
| 22754 | +SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*,const char*,const char*,const char*,const char*); |
| 22724 | 22755 | SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, i64); |
| 22725 | 22756 | SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64); |
| 22726 | 22757 | SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64); |
| 22727 | 22758 | SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64); |
| 22728 | 22759 | SQLITE_PRIVATE int sqlite3AbsInt32(int); |
| | @@ -22787,10 +22818,11 @@ |
| 22787 | 22818 | ); |
| 22788 | 22819 | SQLITE_PRIVATE void sqlite3AlterSetNotNull(Parse*, SrcList*, Token*, Token*); |
| 22789 | 22820 | SQLITE_PRIVATE i64 sqlite3GetToken(const unsigned char *, int *); |
| 22790 | 22821 | SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); |
| 22791 | 22822 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int); |
| 22823 | +SQLITE_PRIVATE void sqlite3ExpireStmtsUsingSchema(sqlite3*, int, Vdbe*); |
| 22792 | 22824 | SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int, int); |
| 22793 | 22825 | SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*); |
| 22794 | 22826 | SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); |
| 22795 | 22827 | SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, SrcItem*); |
| 22796 | 22828 | SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); |
| | @@ -22952,16 +22984,16 @@ |
| 22952 | 22984 | ); |
| 22953 | 22985 | # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) |
| 22954 | 22986 | #endif |
| 22955 | 22987 | SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db); |
| 22956 | 22988 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 22957 | | -SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName); |
| 22989 | +SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName, const char *zDb); |
| 22958 | 22990 | SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*); |
| 22959 | 22991 | SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*); |
| 22960 | 22992 | SQLITE_PRIVATE void sqlite3VtabEponymousTableClearAll(sqlite3*); |
| 22961 | 22993 | #else |
| 22962 | | -# define sqlite3ShadowTableName(A,B) 0 |
| 22994 | +# define sqlite3ShadowTableName(A,B,C) 0 |
| 22963 | 22995 | # define sqlite3IsShadowTableOf(A,B,C) 0 |
| 22964 | 22996 | # define sqlite3MarkAllShadowTablesOf(A,B) |
| 22965 | 22997 | # define sqlite3VtabEponymousTableClearAll(X) |
| 22966 | 22998 | #endif |
| 22967 | 22999 | SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*); |
| | @@ -25999,11 +26031,11 @@ |
| 25999 | 26031 | ** The HH, MM, and SS must each be exactly 2 digits. The |
| 26000 | 26032 | ** fractional seconds FFFF can be one or more digits. |
| 26001 | 26033 | ** |
| 26002 | 26034 | ** Return 1 if there is a parsing error and 0 on success. |
| 26003 | 26035 | */ |
| 26004 | | -static int parseHhMmSs(const char *zDate, DateTime *p){ |
| 26036 | +static int parseHhMmSs(const char *zDate, DateTime *p, int bAllowTZ){ |
| 26005 | 26037 | int h, m, s; |
| 26006 | 26038 | double ms = 0.0; |
| 26007 | 26039 | if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){ |
| 26008 | 26040 | return 1; |
| 26009 | 26041 | } |
| | @@ -26035,10 +26067,11 @@ |
| 26035 | 26067 | p->rawS = 0; |
| 26036 | 26068 | p->validHMS = 1; |
| 26037 | 26069 | p->h = h; |
| 26038 | 26070 | p->m = m; |
| 26039 | 26071 | p->s = s + ms; |
| 26072 | + if( !bAllowTZ && zDate[0] ) return 1; |
| 26040 | 26073 | if( parseTimezone(zDate, p) ) return 1; |
| 26041 | 26074 | return 0; |
| 26042 | 26075 | } |
| 26043 | 26076 | |
| 26044 | 26077 | /* |
| | @@ -26154,11 +26187,11 @@ |
| 26154 | 26187 | return 1; |
| 26155 | 26188 | } |
| 26156 | 26189 | if( neg ) Y = -Y; |
| 26157 | 26190 | zDate += 10; |
| 26158 | 26191 | while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; } |
| 26159 | | - if( parseHhMmSs(zDate, p)==0 ){ |
| 26192 | + if( parseHhMmSs(zDate, p, 1)==0 ){ |
| 26160 | 26193 | if( p->h==24 ){ |
| 26161 | 26194 | int mxD = daysOfMonth(Y,M); |
| 26162 | 26195 | p->h = 0; |
| 26163 | 26196 | D++; |
| 26164 | 26197 | if( D>mxD ){ |
| | @@ -26245,11 +26278,11 @@ |
| 26245 | 26278 | DateTime *p |
| 26246 | 26279 | ){ |
| 26247 | 26280 | double r; |
| 26248 | 26281 | if( parseYyyyMmDd(zDate,p)==0 ){ |
| 26249 | 26282 | return 0; |
| 26250 | | - }else if( parseHhMmSs(zDate, p)==0 ){ |
| 26283 | + }else if( parseHhMmSs(zDate, p, 1)==0 ){ |
| 26251 | 26284 | return 0; |
| 26252 | 26285 | }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){ |
| 26253 | 26286 | return setDateTimeToCurrent(context, p); |
| 26254 | 26287 | }else if( sqlite3AtoF(zDate, &r)>0 ){ |
| 26255 | 26288 | setRawDateNumber(p, r); |
| | @@ -26624,10 +26657,11 @@ |
| 26624 | 26657 | clearYMD_HMS_TZ(p); |
| 26625 | 26658 | rc = 0; |
| 26626 | 26659 | }else if( sqlite3_stricmp(z,"year")==0 ){ |
| 26627 | 26660 | p->M = 12; |
| 26628 | 26661 | p->D = 31; |
| 26662 | + p->nFloor = 0; |
| 26629 | 26663 | rc = 0; |
| 26630 | 26664 | }else if( sqlite3_stricmp(z,"day")==0 ){ |
| 26631 | 26665 | rc = 0; |
| 26632 | 26666 | } |
| 26633 | 26667 | break; |
| | @@ -26640,10 +26674,11 @@ |
| 26640 | 26674 | ** previous month. |
| 26641 | 26675 | */ |
| 26642 | 26676 | if( sqlite3_stricmp(z, "floor")==0 ){ |
| 26643 | 26677 | computeJD(p); |
| 26644 | 26678 | p->iJD -= p->nFloor*86400000; |
| 26679 | + p->nFloor = 0; |
| 26645 | 26680 | clearYMD_HMS_TZ(p); |
| 26646 | 26681 | rc = 0; |
| 26647 | 26682 | } |
| 26648 | 26683 | break; |
| 26649 | 26684 | } |
| | @@ -26703,11 +26738,13 @@ |
| 26703 | 26738 | if( p->isUtc==0 ){ |
| 26704 | 26739 | i64 iOrigJD; /* Original localtime */ |
| 26705 | 26740 | i64 iGuess; /* Guess at the corresponding utc time */ |
| 26706 | 26741 | int cnt = 0; /* Safety to prevent infinite loop */ |
| 26707 | 26742 | i64 iErr; /* Guess is off by this much */ |
| 26743 | + int useSubsec; /* Saved subseconds flag */ |
| 26708 | 26744 | |
| 26745 | + useSubsec = p->useSubsec; |
| 26709 | 26746 | computeJD(p); |
| 26710 | 26747 | iGuess = iOrigJD = p->iJD; |
| 26711 | 26748 | iErr = 0; |
| 26712 | 26749 | do{ |
| 26713 | 26750 | DateTime new; |
| | @@ -26723,10 +26760,11 @@ |
| 26723 | 26760 | memset(p, 0, sizeof(*p)); |
| 26724 | 26761 | p->iJD = iGuess; |
| 26725 | 26762 | p->validJD = 1; |
| 26726 | 26763 | p->isUtc = 1; |
| 26727 | 26764 | p->isLocal = 0; |
| 26765 | + p->useSubsec = useSubsec; |
| 26728 | 26766 | } |
| 26729 | 26767 | rc = SQLITE_OK; |
| 26730 | 26768 | } |
| 26731 | 26769 | #endif |
| 26732 | 26770 | break; |
| | @@ -26757,10 +26795,11 @@ |
| 26757 | 26795 | if( Z>n ) Z -= 7; |
| 26758 | 26796 | p->iJD += (n - Z)*86400000; |
| 26759 | 26797 | if( strchr(z+8,'-')!=0 ) p->iJD -= 7*86400000; |
| 26760 | 26798 | } |
| 26761 | 26799 | clearYMD_HMS_TZ(p); |
| 26800 | + p->nFloor = 0; |
| 26762 | 26801 | rc = 0; |
| 26763 | 26802 | } |
| 26764 | 26803 | break; |
| 26765 | 26804 | } |
| 26766 | 26805 | case 's': { |
| | @@ -26796,14 +26835,16 @@ |
| 26796 | 26835 | p->rawS = 0; |
| 26797 | 26836 | p->tz = 0; |
| 26798 | 26837 | p->validJD = 0; |
| 26799 | 26838 | if( sqlite3_stricmp(z,"month")==0 ){ |
| 26800 | 26839 | p->D = 1; |
| 26840 | + p->nFloor = 0; |
| 26801 | 26841 | rc = 0; |
| 26802 | 26842 | }else if( sqlite3_stricmp(z,"year")==0 ){ |
| 26803 | 26843 | p->M = 1; |
| 26804 | 26844 | p->D = 1; |
| 26845 | + p->nFloor = 0; |
| 26805 | 26846 | rc = 0; |
| 26806 | 26847 | }else if( sqlite3_stricmp(z,"day")==0 ){ |
| 26807 | 26848 | rc = 0; |
| 26808 | 26849 | } |
| 26809 | 26850 | break; |
| | @@ -26896,21 +26937,30 @@ |
| 26896 | 26937 | ** omitted. |
| 26897 | 26938 | */ |
| 26898 | 26939 | |
| 26899 | 26940 | DateTime tx; |
| 26900 | 26941 | sqlite3_int64 day; |
| 26942 | + int is24hr; /* 1 for a shift of exactly 24:00 */ |
| 26943 | + int bTimeOnly = z2==z; /* This is not the HH:MM tail of a |
| 26944 | + ** +/-YYYY-MM-DD HH:MM shift, whose own |
| 26945 | + ** nFloor must survive (e0fe679e7c) */ |
| 26901 | 26946 | if( !sqlite3Isdigit(*z2) ) z2++; |
| 26902 | 26947 | memset(&tx, 0, sizeof(tx)); |
| 26903 | | - if( parseHhMmSs(z2, &tx) ) break; |
| 26948 | + if( parseHhMmSs(z2, &tx, 0) ){ |
| 26949 | + break; /* HH:MM:SS.FFF failed to parse */ |
| 26950 | + } |
| 26951 | + is24hr = tx.h==24; |
| 26904 | 26952 | computeJD(&tx); |
| 26905 | 26953 | tx.iJD -= 43200000; |
| 26906 | 26954 | day = tx.iJD/86400000; |
| 26907 | 26955 | tx.iJD -= day*86400000; |
| 26956 | + if( is24hr ) tx.iJD += (sqlite3_int64)86400000; |
| 26908 | 26957 | if( z0=='-' ) tx.iJD = -tx.iJD; |
| 26909 | 26958 | computeJD(p); |
| 26910 | 26959 | clearYMD_HMS_TZ(p); |
| 26911 | 26960 | p->iJD += tx.iJD; |
| 26961 | + if( bTimeOnly ) p->nFloor = 0; |
| 26912 | 26962 | rc = 0; |
| 26913 | 26963 | break; |
| 26914 | 26964 | } |
| 26915 | 26965 | |
| 26916 | 26966 | /* If control reaches this point, it means the transformation is |
| | @@ -27007,13 +27057,14 @@ |
| 27007 | 27057 | n = sqlite3_value_bytes(argv[i]); |
| 27008 | 27058 | if( z==0 || parseModifier(context, (char*)z, n, p, i) ) return 1; |
| 27009 | 27059 | } |
| 27010 | 27060 | computeJD(p); |
| 27011 | 27061 | if( p->isError || !validJulianDay(p->iJD) ) return 1; |
| 27012 | | - if( argc==1 && p->validYMD && p->D>28 ){ |
| 27062 | + if( p->validYMD && p->D>28 ){ |
| 27013 | 27063 | /* Make sure a YYYY-MM-DD is normalized. |
| 27014 | | - ** Example: 2023-02-31 -> 2023-03-03 */ |
| 27064 | + ** Example: 2023-02-31 -> 2023-03-03 |
| 27065 | + */ |
| 27015 | 27066 | assert( p->validJD ); |
| 27016 | 27067 | p->validYMD = 0; |
| 27017 | 27068 | } |
| 27018 | 27069 | return 0; |
| 27019 | 27070 | } |
| | @@ -33722,11 +33773,11 @@ |
| 33722 | 33773 | if( (zz[i]&0xc0)==0x80 ) len--; |
| 33723 | 33774 | } |
| 33724 | 33775 | } |
| 33725 | 33776 | if( width>len ){ |
| 33726 | 33777 | sqlite3_int64 sp = width-len; |
| 33727 | | - assert( sp>0 && sp<0x7fffffff ); |
| 33778 | + assert( sp>0 && sp<=0x7fffffff ); |
| 33728 | 33779 | sqlite3StrAppendchar64(pAccum, (int)sp, ' '); |
| 33729 | 33780 | if( !flag_leftjustify |
| 33730 | 33781 | && n>0 |
| 33731 | 33782 | && sqlite3_str_errcode(pAccum)==0 |
| 33732 | 33783 | ){ |
| | @@ -37664,11 +37715,11 @@ |
| 37664 | 37715 | ** Return an IEEE754 floating point value that approximates d*pow(10,p). |
| 37665 | 37716 | ** |
| 37666 | 37717 | ** The (current) algorithm is adapted from the work of Ross Cox at |
| 37667 | 37718 | ** https://github.com/rsc/fpfmt |
| 37668 | 37719 | */ |
| 37669 | | -static double sqlite3Fp10Convert2(u64 d, int p){ |
| 37720 | +static double sqlite3Fp10Convert2(u64 d, i64 p){ |
| 37670 | 37721 | int b, lp, e, adj, s; |
| 37671 | 37722 | u32 pwr10l, mid1; |
| 37672 | 37723 | u64 pwr10h, x, hi, lo, sticky, u, m; |
| 37673 | 37724 | double r; |
| 37674 | 37725 | if( p<POWERSOF10_FIRST ) return 0.0; |
| | @@ -37723,15 +37774,17 @@ |
| 37723 | 37774 | ** |
| 37724 | 37775 | ** bit 0 => Set if any prefix of the input is valid. Clear if |
| 37725 | 37776 | ** there is no prefix of the input that can be seen as |
| 37726 | 37777 | ** a valid floating point number. |
| 37727 | 37778 | ** bit 1 => Set if the input contains a decimal point or eNNN |
| 37728 | | -** clause. Zero if the input is an integer. |
| 37779 | +** clause or other text that disqualifies it as an |
| 37780 | +** integer value. Zero if the input is an integer. |
| 37729 | 37781 | ** bit 2 => The input is exactly 0.0, not an underflow from |
| 37730 | 37782 | ** some value near zero. |
| 37731 | 37783 | ** bit 3 => Set if there are more than about 19 significant |
| 37732 | 37784 | ** digits in the input. |
| 37785 | +** bit 4 => The input is a special value like "Inf" or "NaN" |
| 37733 | 37786 | ** |
| 37734 | 37787 | ** If the input contains a syntax error but begins with text that might |
| 37735 | 37788 | ** be a valid number of some kind, then the result is negative. The |
| 37736 | 37789 | ** result is only zero if no prefix of the input could be interpreted as |
| 37737 | 37790 | ** a number. |
| | @@ -37760,11 +37813,11 @@ |
| 37760 | 37813 | SQLITE_PRIVATE int sqlite3AtoF(const char *zIn, double *pResult){ |
| 37761 | 37814 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 37762 | 37815 | const unsigned char *z = (const unsigned char*)zIn; |
| 37763 | 37816 | int neg = 0; /* True for a negative value */ |
| 37764 | 37817 | u64 s = 0; /* mantissa */ |
| 37765 | | - int d = 0; /* Value is s * pow(10,d) */ |
| 37818 | + i64 d = 0; /* Value is s * pow(10,d) */ |
| 37766 | 37819 | int mState = 0; /* 1: digit seen 2: fp 4: hard-zero */ |
| 37767 | 37820 | unsigned v; /* Value of a single digit */ |
| 37768 | 37821 | |
| 37769 | 37822 | start_of_text: |
| 37770 | 37823 | if( (v = (unsigned)z[0] - '0')<10 ){ |
| | @@ -37812,12 +37865,17 @@ |
| 37812 | 37865 | *pResult = 0.0; |
| 37813 | 37866 | return 0; |
| 37814 | 37867 | } |
| 37815 | 37868 | mState |= 2; |
| 37816 | 37869 | }else if( mState==0 ){ |
| 37817 | | - *pResult = 0.0; |
| 37818 | | - return 0; |
| 37870 | + if( sqlite3StrICmp((const char*)z,"Inf")==0 ){ |
| 37871 | + *pResult = neg ? -INFINITY : INFINITY; |
| 37872 | + return 0x13; |
| 37873 | + }else{ |
| 37874 | + *pResult = 0.0; |
| 37875 | + return 0; |
| 37876 | + } |
| 37819 | 37877 | } |
| 37820 | 37878 | |
| 37821 | 37879 | /* if exponent is present */ |
| 37822 | 37880 | if( *z=='e' || *z=='E' ){ |
| 37823 | 37881 | int esign; |
| | @@ -37833,18 +37891,18 @@ |
| 37833 | 37891 | z++; |
| 37834 | 37892 | } |
| 37835 | 37893 | } |
| 37836 | 37894 | /* copy digits to exponent */ |
| 37837 | 37895 | if( (v = (unsigned)z[0] - '0')<10 ){ |
| 37838 | | - int exp = v; |
| 37896 | + u64 exp = v; |
| 37839 | 37897 | z++; |
| 37840 | 37898 | mState |= 2; |
| 37841 | 37899 | while( (v = (unsigned)z[0] - '0')<10 ){ |
| 37842 | | - exp = exp<10000 ? (exp*10 + v) : 10000; |
| 37900 | + exp = exp*10 + v + (exp & (0xffULL<<32)); |
| 37843 | 37901 | z++; |
| 37844 | 37902 | } |
| 37845 | | - d += esign*exp; |
| 37903 | + d += esign*(exp & ((1ULL<<48)-1)); |
| 37846 | 37904 | }else{ |
| 37847 | 37905 | z--; /* Leave z[0] at 'e' or '+' or '-', |
| 37848 | 37906 | ** so that the return is 0 or -1 */ |
| 37849 | 37907 | } |
| 37850 | 37908 | } |
| | @@ -39387,11 +39445,10 @@ |
| 39387 | 39445 | } |
| 39388 | 39446 | insertElement(pH, pH->ht ? &pH->ht[new_elem->h % pH->htsize] : 0, new_elem); |
| 39389 | 39447 | return 0; |
| 39390 | 39448 | } |
| 39391 | 39449 | |
| 39392 | | - |
| 39393 | 39450 | /************** End of hash.c ************************************************/ |
| 39394 | 39451 | /************** Begin file opcodes.c *****************************************/ |
| 39395 | 39452 | /* Automatically generated. Do not edit */ |
| 39396 | 39453 | /* See the tool/mkopcodec.tcl script for details. */ |
| 39397 | 39454 | #if !defined(SQLITE_OMIT_EXPLAIN) \ |
| | @@ -39539,64 +39596,66 @@ |
| 39539 | 39596 | /* 132 */ "ResetCount" OpHelp(""), |
| 39540 | 39597 | /* 133 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), |
| 39541 | 39598 | /* 134 */ "SorterData" OpHelp("r[P2]=data"), |
| 39542 | 39599 | /* 135 */ "RowData" OpHelp("r[P2]=data"), |
| 39543 | 39600 | /* 136 */ "Rowid" OpHelp("r[P2]=PX rowid of P1"), |
| 39544 | | - /* 137 */ "NullRow" OpHelp(""), |
| 39545 | | - /* 138 */ "SeekEnd" OpHelp(""), |
| 39546 | | - /* 139 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 39547 | | - /* 140 */ "SorterInsert" OpHelp("key=r[P2]"), |
| 39548 | | - /* 141 */ "IdxDelete" OpHelp("key=r[P2@P5]"), |
| 39549 | | - /* 142 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"), |
| 39550 | | - /* 143 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 39551 | | - /* 144 */ "FinishSeek" OpHelp(""), |
| 39552 | | - /* 145 */ "Destroy" OpHelp(""), |
| 39553 | | - /* 146 */ "Clear" OpHelp(""), |
| 39554 | | - /* 147 */ "ResetSorter" OpHelp(""), |
| 39555 | | - /* 148 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"), |
| 39556 | | - /* 149 */ "SqlExec" OpHelp(""), |
| 39557 | | - /* 150 */ "ParseSchema" OpHelp(""), |
| 39558 | | - /* 151 */ "LoadAnalysis" OpHelp(""), |
| 39559 | | - /* 152 */ "DropTable" OpHelp(""), |
| 39560 | | - /* 153 */ "DropIndex" OpHelp(""), |
| 39601 | + /* 137 */ "Stale" OpHelp(""), |
| 39602 | + /* 138 */ "NullRow" OpHelp(""), |
| 39603 | + /* 139 */ "SeekEnd" OpHelp(""), |
| 39604 | + /* 140 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 39605 | + /* 141 */ "SorterInsert" OpHelp("key=r[P2]"), |
| 39606 | + /* 142 */ "IdxDelete" OpHelp("key=r[P2@P5]"), |
| 39607 | + /* 143 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"), |
| 39608 | + /* 144 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 39609 | + /* 145 */ "FinishSeek" OpHelp(""), |
| 39610 | + /* 146 */ "Destroy" OpHelp(""), |
| 39611 | + /* 147 */ "DbLock" OpHelp(""), |
| 39612 | + /* 148 */ "Clear" OpHelp(""), |
| 39613 | + /* 149 */ "ResetSorter" OpHelp(""), |
| 39614 | + /* 150 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"), |
| 39615 | + /* 151 */ "SqlExec" OpHelp(""), |
| 39616 | + /* 152 */ "ParseSchema" OpHelp(""), |
| 39617 | + /* 153 */ "LoadAnalysis" OpHelp(""), |
| 39561 | 39618 | /* 154 */ "Real" OpHelp("r[P2]=PDBL13"), |
| 39562 | | - /* 155 */ "DropTrigger" OpHelp(""), |
| 39563 | | - /* 156 */ "IntegrityCk" OpHelp(""), |
| 39564 | | - /* 157 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 39565 | | - /* 158 */ "Param" OpHelp(""), |
| 39566 | | - /* 159 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 39567 | | - /* 160 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 39568 | | - /* 161 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), |
| 39569 | | - /* 162 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"), |
| 39570 | | - /* 163 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 39571 | | - /* 164 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 39572 | | - /* 165 */ "AggValue" OpHelp("r[P3]=value N=P2"), |
| 39573 | | - /* 166 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 39574 | | - /* 167 */ "Expire" OpHelp(""), |
| 39575 | | - /* 168 */ "CursorLock" OpHelp(""), |
| 39576 | | - /* 169 */ "CursorUnlock" OpHelp(""), |
| 39577 | | - /* 170 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 39578 | | - /* 171 */ "VBegin" OpHelp(""), |
| 39579 | | - /* 172 */ "VCreate" OpHelp(""), |
| 39580 | | - /* 173 */ "VDestroy" OpHelp(""), |
| 39581 | | - /* 174 */ "VOpen" OpHelp(""), |
| 39582 | | - /* 175 */ "VCheck" OpHelp(""), |
| 39583 | | - /* 176 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"), |
| 39584 | | - /* 177 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 39585 | | - /* 178 */ "VRename" OpHelp(""), |
| 39586 | | - /* 179 */ "Pagecount" OpHelp(""), |
| 39587 | | - /* 180 */ "MaxPgcnt" OpHelp(""), |
| 39588 | | - /* 181 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"), |
| 39589 | | - /* 182 */ "GetSubtype" OpHelp("r[P2] = r[P1].subtype"), |
| 39590 | | - /* 183 */ "SetSubtype" OpHelp("r[P2].subtype = r[P1]"), |
| 39591 | | - /* 184 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 39592 | | - /* 185 */ "Trace" OpHelp(""), |
| 39593 | | - /* 186 */ "CursorHint" OpHelp(""), |
| 39594 | | - /* 187 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 39595 | | - /* 188 */ "Noop" OpHelp(""), |
| 39596 | | - /* 189 */ "Explain" OpHelp(""), |
| 39597 | | - /* 190 */ "Abortable" OpHelp(""), |
| 39619 | + /* 155 */ "DropTable" OpHelp(""), |
| 39620 | + /* 156 */ "DropIndex" OpHelp(""), |
| 39621 | + /* 157 */ "DropTrigger" OpHelp(""), |
| 39622 | + /* 158 */ "IntegrityCk" OpHelp(""), |
| 39623 | + /* 159 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 39624 | + /* 160 */ "Param" OpHelp(""), |
| 39625 | + /* 161 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 39626 | + /* 162 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 39627 | + /* 163 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), |
| 39628 | + /* 164 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"), |
| 39629 | + /* 165 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 39630 | + /* 166 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 39631 | + /* 167 */ "AggValue" OpHelp("r[P3]=value N=P2"), |
| 39632 | + /* 168 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 39633 | + /* 169 */ "Expire" OpHelp(""), |
| 39634 | + /* 170 */ "CursorLock" OpHelp(""), |
| 39635 | + /* 171 */ "CursorUnlock" OpHelp(""), |
| 39636 | + /* 172 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 39637 | + /* 173 */ "VBegin" OpHelp(""), |
| 39638 | + /* 174 */ "VCreate" OpHelp(""), |
| 39639 | + /* 175 */ "VDestroy" OpHelp(""), |
| 39640 | + /* 176 */ "VOpen" OpHelp(""), |
| 39641 | + /* 177 */ "VCheck" OpHelp(""), |
| 39642 | + /* 178 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"), |
| 39643 | + /* 179 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 39644 | + /* 180 */ "VRename" OpHelp(""), |
| 39645 | + /* 181 */ "Pagecount" OpHelp(""), |
| 39646 | + /* 182 */ "MaxPgcnt" OpHelp(""), |
| 39647 | + /* 183 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"), |
| 39648 | + /* 184 */ "GetSubtype" OpHelp("r[P2] = r[P1].subtype"), |
| 39649 | + /* 185 */ "SetSubtype" OpHelp("r[P2].subtype = r[P1]"), |
| 39650 | + /* 186 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 39651 | + /* 187 */ "Trace" OpHelp(""), |
| 39652 | + /* 188 */ "CursorHint" OpHelp(""), |
| 39653 | + /* 189 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 39654 | + /* 190 */ "Noop" OpHelp(""), |
| 39655 | + /* 191 */ "Explain" OpHelp(""), |
| 39656 | + /* 192 */ "Abortable" OpHelp(""), |
| 39598 | 39657 | }; |
| 39599 | 39658 | return azName[i]; |
| 39600 | 39659 | } |
| 39601 | 39660 | #endif |
| 39602 | 39661 | |
| | @@ -41832,11 +41891,11 @@ |
| 41832 | 41891 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 41833 | 41892 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 41834 | 41893 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 41835 | 41894 | int n; /* Length of zAbsoluteName string */ |
| 41836 | 41895 | |
| 41837 | | - assert( zAbsoluteName[0]=='/' ); |
| 41896 | + if( zAbsoluteName==0 || zAbsoluteName[0]=='/' ); |
| 41838 | 41897 | n = (int)strlen(zAbsoluteName); |
| 41839 | 41898 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
| 41840 | 41899 | if( pNew==0 ) return 0; |
| 41841 | 41900 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 41842 | 41901 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| | @@ -45261,23 +45320,23 @@ |
| 45261 | 45320 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
| 45262 | 45321 | sqlite3_mutex *pShmMutex; /* Mutex to access this object */ |
| 45263 | 45322 | char *zFilename; /* Name of the mmapped file */ |
| 45264 | 45323 | int hShm; /* Open file descriptor */ |
| 45265 | 45324 | int szRegion; /* Size of shared-memory regions */ |
| 45266 | | - u16 nRegion; /* Size of array apRegion */ |
| 45325 | + int nRegion; /* Size of array apRegion */ |
| 45326 | + int nRef; /* Number of unixShm objects pointing to this */ |
| 45267 | 45327 | u8 isReadonly; /* True if read-only */ |
| 45268 | 45328 | u8 isUnlocked; /* True if no DMS lock held */ |
| 45329 | +#ifdef SQLITE_DEBUG |
| 45330 | + u8 nextShmId; /* Next available unixShm.id value */ |
| 45331 | +#endif |
| 45269 | 45332 | char **apRegion; /* Array of mapped shared-memory regions */ |
| 45270 | | - int nRef; /* Number of unixShm objects pointing to this */ |
| 45271 | 45333 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
| 45272 | 45334 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 45273 | 45335 | sqlite3_mutex *aMutex[SQLITE_SHM_NLOCK]; |
| 45274 | 45336 | #endif |
| 45275 | 45337 | int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */ |
| 45276 | | -#ifdef SQLITE_DEBUG |
| 45277 | | - u8 nextShmId; /* Next available unixShm.id value */ |
| 45278 | | -#endif |
| 45279 | 45338 | }; |
| 45280 | 45339 | |
| 45281 | 45340 | /* |
| 45282 | 45341 | ** Structure used internally by this VFS to record the state of an |
| 45283 | 45342 | ** open shared memory connection. |
| | @@ -46809,14 +46868,18 @@ |
| 46809 | 46868 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
| 46810 | 46869 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
| 46811 | 46870 | } |
| 46812 | 46871 | |
| 46813 | 46872 | #if OS_VXWORKS |
| 46814 | | - pNew->pId = vxworksFindFileId(zFilename); |
| 46815 | | - if( pNew->pId==0 ){ |
| 46816 | | - ctrlFlags |= UNIXFILE_NOLOCK; |
| 46817 | | - rc = SQLITE_NOMEM_BKPT; |
| 46873 | + if( zFilename ){ |
| 46874 | + pNew->pId = vxworksFindFileId(zFilename); |
| 46875 | + if( pNew->pId==0 ){ |
| 46876 | + ctrlFlags |= UNIXFILE_NOLOCK; |
| 46877 | + rc = SQLITE_NOMEM_BKPT; |
| 46878 | + } |
| 46879 | + }else{ |
| 46880 | + pNew->pId = 0; |
| 46818 | 46881 | } |
| 46819 | 46882 | #endif |
| 46820 | 46883 | |
| 46821 | 46884 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
| 46822 | 46885 | pLockingStyle = &nolockIoMethods; |
| | @@ -46917,16 +46980,19 @@ |
| 46917 | 46980 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 46918 | 46981 | char *zSemName = pNew->pInode->aSemName; |
| 46919 | 46982 | int n; |
| 46920 | 46983 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
| 46921 | 46984 | pNew->pId->zCanonicalName); |
| 46922 | | - for( n=1; zSemName[n]; n++ ) |
| 46923 | | - if( zSemName[n]=='/' ) zSemName[n] = '_'; |
| 46985 | + for(n=1; zSemName[n]; n++){ |
| 46986 | + if( zSemName[n]=='/' && !sqlite3FaultSim(801) ) zSemName[n] = '_'; |
| 46987 | + } |
| 46924 | 46988 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 46925 | 46989 | if( pNew->pInode->pSem == SEM_FAILED ){ |
| 46926 | 46990 | rc = SQLITE_NOMEM_BKPT; |
| 46927 | 46991 | pNew->pInode->aSemName[0] = '\0'; |
| 46992 | + releaseInodeInfo(pNew); |
| 46993 | + pNew->pInode = 0; |
| 46928 | 46994 | } |
| 46929 | 46995 | } |
| 46930 | 46996 | unixLeaveMutex(); |
| 46931 | 46997 | } |
| 46932 | 46998 | #endif |
| | @@ -61074,15 +61140,22 @@ |
| 61074 | 61140 | ** |
| 61075 | 61141 | ** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing |
| 61076 | 61142 | ** file has been created already (via a spill on pagerStress()) and |
| 61077 | 61143 | ** when the number of dirty pages in memory exceeds 25% of the total |
| 61078 | 61144 | ** cache size. |
| 61145 | +** |
| 61146 | +** * Never sync a TEMP database if the current transaction has not |
| 61147 | +** modified the database (eState<PAGER_WRITER_CACHEMOD). In this case |
| 61148 | +** sqlite3PagerCommitPhaseOne() returns early without flushing any |
| 61149 | +** dirty pages left over from earlier transactions, so they must not |
| 61150 | +** be marked as clean by pager_end_transaction(). |
| 61079 | 61151 | */ |
| 61080 | 61152 | static int pagerFlushOnCommit(Pager *pPager, int bCommit){ |
| 61081 | 61153 | if( pPager->tempFile==0 ) return 1; |
| 61082 | 61154 | if( !bCommit ) return 0; |
| 61083 | 61155 | if( !isOpen(pPager->fd) ) return 0; |
| 61156 | + if( pPager->eState<PAGER_WRITER_CACHEMOD ) return 0; |
| 61084 | 61157 | return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); |
| 61085 | 61158 | } |
| 61086 | 61159 | |
| 61087 | 61160 | /* |
| 61088 | 61161 | ** This routine ends a transaction. A transaction is usually ended by |
| | @@ -65750,10 +65823,15 @@ |
| 65750 | 65823 | if( rc!=SQLITE_OK ){ |
| 65751 | 65824 | sqlite3OsClose(pPager->jfd); |
| 65752 | 65825 | goto commit_phase_one_exit; |
| 65753 | 65826 | } |
| 65754 | 65827 | bBatch = 0; |
| 65828 | + |
| 65829 | + /* The journal file was just created on disk. Sync it now before |
| 65830 | + ** any pages are written to the database file. */ |
| 65831 | + rc = syncJournal(pPager, 0); |
| 65832 | + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 65755 | 65833 | }else{ |
| 65756 | 65834 | sqlite3OsClose(pPager->jfd); |
| 65757 | 65835 | } |
| 65758 | 65836 | } |
| 65759 | 65837 | #endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
| | @@ -78225,32 +78303,38 @@ |
| 78225 | 78303 | |
| 78226 | 78304 | /* |
| 78227 | 78305 | ** This variant of sqlite3BtreePayload() works even if the cursor has not |
| 78228 | 78306 | ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() |
| 78229 | 78307 | ** interface. |
| 78308 | +** |
| 78309 | +** If the cursor has to be restored to its saved position, then SQLITE_MOVED |
| 78310 | +** is returned and no data is read. The content of the row might have changed |
| 78311 | +** while the cursor position was saved, so the caller needs to recompute the |
| 78312 | +** size and offset of the data it is reading before it tries again. If the |
| 78313 | +** row no longer exists, SQLITE_ABORT is returned. |
| 78230 | 78314 | */ |
| 78231 | 78315 | #ifndef SQLITE_OMIT_INCRBLOB |
| 78232 | | -static SQLITE_NOINLINE int accessPayloadChecked( |
| 78233 | | - BtCursor *pCur, |
| 78234 | | - u32 offset, |
| 78235 | | - u32 amt, |
| 78236 | | - void *pBuf |
| 78237 | | -){ |
| 78316 | +static SQLITE_NOINLINE int accessPayloadChecked(BtCursor *pCur){ |
| 78238 | 78317 | int rc; |
| 78239 | 78318 | if ( pCur->eState==CURSOR_INVALID ){ |
| 78240 | 78319 | return SQLITE_ABORT; |
| 78241 | 78320 | } |
| 78242 | 78321 | assert( cursorOwnsBtShared(pCur) ); |
| 78243 | 78322 | rc = btreeRestoreCursorPosition(pCur); |
| 78244 | | - return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0); |
| 78323 | + if( rc ) return rc; |
| 78324 | + if( pCur->eState!=CURSOR_VALID ){ |
| 78325 | + /* The row is gone, and the cursor was restored onto a neighbor. */ |
| 78326 | + return SQLITE_ABORT; |
| 78327 | + } |
| 78328 | + return SQLITE_MOVED; |
| 78245 | 78329 | } |
| 78246 | 78330 | SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
| 78247 | 78331 | if( pCur->eState==CURSOR_VALID ){ |
| 78248 | 78332 | assert( cursorOwnsBtShared(pCur) ); |
| 78249 | 78333 | return accessPayload(pCur, offset, amt, pBuf, 0); |
| 78250 | 78334 | }else{ |
| 78251 | | - return accessPayloadChecked(pCur, offset, amt, pBuf); |
| 78335 | + return accessPayloadChecked(pCur); |
| 78252 | 78336 | } |
| 78253 | 78337 | } |
| 78254 | 78338 | #endif /* SQLITE_OMIT_INCRBLOB */ |
| 78255 | 78339 | |
| 78256 | 78340 | /* |
| | @@ -79871,14 +79955,12 @@ |
| 79871 | 79955 | } |
| 79872 | 79956 | ovflPgno = get4byte(pCell + pInfo->nSize - 4); |
| 79873 | 79957 | pBt = pPage->pBt; |
| 79874 | 79958 | assert( pBt->usableSize > 4 ); |
| 79875 | 79959 | ovflPageSize = pBt->usableSize - 4; |
| 79876 | | - nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize; |
| 79877 | | - assert( nOvfl>0 || |
| 79878 | | - (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize) |
| 79879 | | - ); |
| 79960 | + nOvfl = (pInfo->nPayload - pInfo->nLocal + (u64)ovflPageSize-1)/ovflPageSize; |
| 79961 | + assert( nOvfl>0 ); |
| 79880 | 79962 | while( nOvfl-- ){ |
| 79881 | 79963 | Pgno iNext = 0; |
| 79882 | 79964 | MemPage *pOvfl = 0; |
| 79883 | 79965 | if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ |
| 79884 | 79966 | /* 0 is not a legal page number and page 1 cannot be an |
| | @@ -82331,10 +82413,11 @@ |
| 82331 | 82413 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 82332 | 82414 | testcase( pCur->eState==CURSOR_REQUIRESEEK ); |
| 82333 | 82415 | testcase( pCur->eState==CURSOR_FAULT ); |
| 82334 | 82416 | rc = moveToRoot(pCur); |
| 82335 | 82417 | if( rc && rc!=SQLITE_EMPTY ) return rc; |
| 82418 | + loc = 0; /* Indicate the the cursor is not in a known position */ |
| 82336 | 82419 | } |
| 82337 | 82420 | |
| 82338 | 82421 | assert( cursorOwnsBtShared(pCur) ); |
| 82339 | 82422 | assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 82340 | 82423 | && p->pBt->inTransaction==TRANS_WRITE |
| | @@ -82354,15 +82437,17 @@ |
| 82354 | 82437 | ** cursors open on the row being replaced */ |
| 82355 | 82438 | if( p->hasIncrblobCur ){ |
| 82356 | 82439 | invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0); |
| 82357 | 82440 | } |
| 82358 | 82441 | |
| 82359 | | - /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing |
| 82360 | | - ** to a row with the same key as the new entry being inserted. |
| 82442 | + /* If BTREE_SAVEPOSITION is set and seekResult is non-zero and |
| 82443 | + ** if we didn't have to reposition the cursor initially, then |
| 82444 | + ** the cursor must already be pointing to a row with the same |
| 82445 | + ** key as the new entry being inserted. Verify that this is so. |
| 82361 | 82446 | */ |
| 82362 | 82447 | #ifdef SQLITE_DEBUG |
| 82363 | | - if( flags & BTREE_SAVEPOSITION ){ |
| 82448 | + if( (flags & BTREE_SAVEPOSITION)!=0 && loc!=0 ){ |
| 82364 | 82449 | assert( pCur->curFlags & BTCF_ValidNKey ); |
| 82365 | 82450 | assert( pX->nKey==pCur->info.nKey ); |
| 82366 | 82451 | assert( loc==0 ); |
| 82367 | 82452 | } |
| 82368 | 82453 | #endif |
| | @@ -86019,11 +86104,15 @@ |
| 86019 | 86104 | } |
| 86020 | 86105 | } |
| 86021 | 86106 | assert( j<=n/2 ); |
| 86022 | 86107 | zCopy[j] = 0; |
| 86023 | 86108 | rc = sqlite3AtoF(zCopy, pValue); |
| 86024 | | - if( i<n ) rc = -100; |
| 86109 | + if( i<n && rc>0 ){ |
| 86110 | + /* Only a prefix was converted. Adjust rc to match would |
| 86111 | + ** sqlite3AtoF() would have done. */ |
| 86112 | + rc |= -16; |
| 86113 | + } |
| 86025 | 86114 | sqlite3DbFree(pMem->db, zCopy); |
| 86026 | 86115 | } |
| 86027 | 86116 | return rc; |
| 86028 | 86117 | } |
| 86029 | 86118 | } |
| | @@ -87162,11 +87251,11 @@ |
| 87162 | 87251 | |
| 87163 | 87252 | if( op==TK_CAST ){ |
| 87164 | 87253 | u8 aff; |
| 87165 | 87254 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 87166 | 87255 | aff = sqlite3AffinityType(pExpr->u.zToken,0); |
| 87167 | | - rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); |
| 87256 | + rc = valueFromExpr(db, pExpr->pLeft, enc, SQLITE_AFF_BLOB, ppVal, pCtx); |
| 87168 | 87257 | testcase( rc!=SQLITE_OK ); |
| 87169 | 87258 | if( *ppVal ){ |
| 87170 | 87259 | #ifdef SQLITE_ENABLE_STAT4 |
| 87171 | 87260 | rc = ExpandBlob(*ppVal); |
| 87172 | 87261 | #else |
| | @@ -88420,10 +88509,13 @@ |
| 88420 | 88509 | if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename |
| 88421 | 88510 | || opcode==OP_VDestroy |
| 88422 | 88511 | || opcode==OP_VCreate |
| 88423 | 88512 | || opcode==OP_ParseSchema |
| 88424 | 88513 | || opcode==OP_Function || opcode==OP_PureFunc |
| 88514 | + || opcode==OP_AggStep || opcode==OP_AggInverse |
| 88515 | + || opcode==OP_AggFinal || opcode==OP_AggValue |
| 88516 | + || opcode==OP_VFilter || opcode==OP_VColumn |
| 88425 | 88517 | || ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
| 88426 | 88518 | && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort)) |
| 88427 | 88519 | || (opcode==OP_MustBeInt && pOp->p2==0) |
| 88428 | 88520 | ){ |
| 88429 | 88521 | hasAbort = 1; |
| | @@ -89992,10 +90084,12 @@ |
| 89992 | 90084 | if( eMode==2 ){ |
| 89993 | 90085 | Op *pOp = aOp + i; |
| 89994 | 90086 | if( pOp->opcode==OP_OpenRead ) break; |
| 89995 | 90087 | if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break; |
| 89996 | 90088 | if( pOp->opcode==OP_ReopenIdx ) break; |
| 90089 | + if( pOp->opcode==OP_Clear ) break; |
| 90090 | + if( pOp->opcode==OP_Destroy ) break; |
| 89997 | 90091 | }else |
| 89998 | 90092 | #endif |
| 89999 | 90093 | { |
| 90000 | 90094 | assert( eMode==1 ); |
| 90001 | 90095 | if( aOp[i].opcode==OP_Explain ) break; |
| | @@ -90846,10 +90940,26 @@ |
| 90846 | 90940 | assert( nRead==db->nVdbeRead ); |
| 90847 | 90941 | } |
| 90848 | 90942 | #else |
| 90849 | 90943 | #define checkActiveVdbeCnt(x) |
| 90850 | 90944 | #endif |
| 90945 | + |
| 90946 | +/* |
| 90947 | +** Return a bitmask of all schemas referenced by active VDBEs, other |
| 90948 | +** than pExcept. |
| 90949 | +*/ |
| 90950 | +SQLITE_PRIVATE void sqlite3VdbeSchemaInUse(sqlite3 *db, Vdbe *pExcept, yDbMask *pM){ |
| 90951 | + Vdbe *p; |
| 90952 | + yDbMask m; |
| 90953 | + DbMaskZero(m); |
| 90954 | + for(p=db->pVdbe; p; p=p->pVNext){ |
| 90955 | + if( p==pExcept ) continue; |
| 90956 | + if( p->eVdbeState!=VDBE_RUN_STATE ) continue; |
| 90957 | + DbMaskUnion(m,p->btreeMask); |
| 90958 | + } |
| 90959 | + memcpy(pM, &m, sizeof(m)); |
| 90960 | +} |
| 90851 | 90961 | |
| 90852 | 90962 | /* |
| 90853 | 90963 | ** If the Vdbe passed as the first argument opened a statement-transaction, |
| 90854 | 90964 | ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
| 90855 | 90965 | ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
| | @@ -91152,10 +91262,17 @@ |
| 91152 | 91262 | ** to invoke any required unlock-notify callbacks. |
| 91153 | 91263 | */ |
| 91154 | 91264 | if( db->autoCommit ){ |
| 91155 | 91265 | sqlite3ConnectionUnlocked(db); |
| 91156 | 91266 | } |
| 91267 | + |
| 91268 | + /* Do any pending schema resets that were deferred because multiple |
| 91269 | + ** VDBEs where active at the same time. |
| 91270 | + */ |
| 91271 | + if( db->bPendingSchemaReset && db->nVdbeActive==0 ){ |
| 91272 | + sqlite3ResetOneSchema(db, -1, 0); |
| 91273 | + } |
| 91157 | 91274 | |
| 91158 | 91275 | assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 ); |
| 91159 | 91276 | return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); |
| 91160 | 91277 | } |
| 91161 | 91278 | |
| | @@ -92919,10 +93036,27 @@ |
| 92919 | 93036 | Vdbe *p; |
| 92920 | 93037 | for(p = db->pVdbe; p; p=p->pVNext){ |
| 92921 | 93038 | p->expired = iCode+1; |
| 92922 | 93039 | } |
| 92923 | 93040 | } |
| 93041 | + |
| 93042 | +/* |
| 93043 | +** Expire (urgently, with iCode 0) every prepared statement that is using |
| 93044 | +** a particular schema (iDb). |
| 93045 | +*/ |
| 93046 | +SQLITE_PRIVATE void sqlite3ExpireStmtsUsingSchema( |
| 93047 | + sqlite3 *db, /* The database connection statements belong to */ |
| 93048 | + int iDb, /* Index of the schema they are using */ |
| 93049 | + Vdbe *pExcept /* Except, don't expire this one */ |
| 93050 | +){ |
| 93051 | + Vdbe *p; |
| 93052 | + for(p = db->pVdbe; p; p=p->pVNext){ |
| 93053 | + if( p==pExcept ) continue; |
| 93054 | + if( !DbMaskTest(p->btreeMask, iDb) ) continue; |
| 93055 | + p->expired = 1; |
| 93056 | + } |
| 93057 | +} |
| 92924 | 93058 | |
| 92925 | 93059 | /* |
| 92926 | 93060 | ** Return the database associated with the Vdbe. |
| 92927 | 93061 | */ |
| 92928 | 93062 | SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){ |
| | @@ -93512,10 +93646,13 @@ |
| 93512 | 93646 | rc = SQLITE_OK; |
| 93513 | 93647 | }else{ |
| 93514 | 93648 | Vdbe *v = (Vdbe*)pStmt; |
| 93515 | 93649 | sqlite3 *db = v->db; |
| 93516 | 93650 | if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; |
| 93651 | +#if defined(SQLITE_DISPOSAL_NOTIFIER) |
| 93652 | + SQLITE_DISPOSAL_NOTIFIER(2,pStmt); |
| 93653 | +#endif |
| 93517 | 93654 | sqlite3_mutex_enter(db->mutex); |
| 93518 | 93655 | checkProfileCallback(db, v); |
| 93519 | 93656 | assert( v->eVdbeState>=VDBE_READY_STATE ); |
| 93520 | 93657 | rc = sqlite3VdbeReset(v); |
| 93521 | 93658 | sqlite3VdbeDelete(v); |
| | @@ -99426,11 +99563,11 @@ |
| 99426 | 99563 | case OP_IfNullRow: { /* jump */ |
| 99427 | 99564 | VdbeCursor *pC; |
| 99428 | 99565 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 99429 | 99566 | pC = p->apCsr[pOp->p1]; |
| 99430 | 99567 | if( pC && pC->nullRow ){ |
| 99431 | | - sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 99568 | + if( pOp->p3>0 ) sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 99432 | 99569 | goto jump_to_p2; |
| 99433 | 99570 | } |
| 99434 | 99571 | break; |
| 99435 | 99572 | } |
| 99436 | 99573 | |
| | @@ -99890,10 +100027,11 @@ |
| 99890 | 100027 | Column *aCol; |
| 99891 | 100028 | int i; |
| 99892 | 100029 | int nCol; |
| 99893 | 100030 | |
| 99894 | 100031 | assert( pOp->p4type==P4_TABLE ); |
| 100032 | + if( p->expired==1 ) goto abort_due_to_expired; |
| 99895 | 100033 | pTab = pOp->p4.pTab; |
| 99896 | 100034 | assert( pTab->tabFlags & TF_Strict ); |
| 99897 | 100035 | assert( pOp->p3>=0 && pOp->p3<pTab->nCol+2 ); |
| 99898 | 100036 | aCol = pTab->aCol; |
| 99899 | 100037 | pIn1 = &aMem[pOp->p1]; |
| | @@ -99942,15 +100080,15 @@ |
| 99942 | 100080 | testcase( pIn1->u.i==140737488355327LL ); |
| 99943 | 100081 | testcase( pIn1->u.i==-140737488355328LL ); |
| 99944 | 100082 | testcase( pIn1->u.i==-140737488355329LL ); |
| 99945 | 100083 | if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){ |
| 99946 | 100084 | pIn1->flags |= MEM_IntReal; |
| 99947 | | - pIn1->flags &= ~MEM_Int; |
| 100085 | + pIn1->flags &= ~(MEM_Int|MEM_Str); |
| 99948 | 100086 | }else{ |
| 99949 | 100087 | pIn1->u.r = (double)pIn1->u.i; |
| 99950 | 100088 | pIn1->flags |= MEM_Real; |
| 99951 | | - pIn1->flags &= ~MEM_Int; |
| 100089 | + pIn1->flags &= ~(MEM_Int|MEM_Str); |
| 99952 | 100090 | } |
| 99953 | 100091 | }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ |
| 99954 | 100092 | goto vdbe_type_error; |
| 99955 | 100093 | } |
| 99956 | 100094 | break; |
| | @@ -100089,11 +100227,11 @@ |
| 100089 | 100227 | pRec = pData0; |
| 100090 | 100228 | do{ |
| 100091 | 100229 | applyAffinity(pRec, zAffinity[0], encoding); |
| 100092 | 100230 | if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ |
| 100093 | 100231 | pRec->flags |= MEM_IntReal; |
| 100094 | | - pRec->flags &= ~(MEM_Int); |
| 100232 | + pRec->flags &= ~(MEM_Int|MEM_Str); |
| 100095 | 100233 | } |
| 100096 | 100234 | REGISTER_TRACE((int)(pRec-aMem), pRec); |
| 100097 | 100235 | zAffinity++; |
| 100098 | 100236 | pRec++; |
| 100099 | 100237 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| | @@ -100217,17 +100355,17 @@ |
| 100217 | 100355 | assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); |
| 100218 | 100356 | assert( pRec->n>=0 ); |
| 100219 | 100357 | len = (u32)pRec->n; |
| 100220 | 100358 | serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); |
| 100221 | 100359 | if( pRec->flags & MEM_Zero ){ |
| 100222 | | - serial_type += (u32)pRec->u.nZero*2; |
| 100223 | 100360 | if( nData ){ |
| 100224 | 100361 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
| 100225 | 100362 | len += pRec->u.nZero; |
| 100226 | 100363 | }else{ |
| 100227 | 100364 | nZero += pRec->u.nZero; |
| 100228 | 100365 | } |
| 100366 | + serial_type += 2*(u32)pRec->u.nZero; |
| 100229 | 100367 | } |
| 100230 | 100368 | nData += len; |
| 100231 | 100369 | nHdr += sqlite3VarintLen(serial_type); |
| 100232 | 100370 | pRec->uTemp = serial_type; |
| 100233 | 100371 | } |
| | @@ -100789,11 +100927,11 @@ |
| 100789 | 100927 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 100790 | 100928 | ** to be invalidated whenever sqlite3_step() is called from within |
| 100791 | 100929 | ** a v-table method. |
| 100792 | 100930 | */ |
| 100793 | 100931 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 100794 | | - sqlite3ResetOneSchema(db, pOp->p1); |
| 100932 | + sqlite3ResetOneSchema(db, pOp->p1, p); |
| 100795 | 100933 | } |
| 100796 | 100934 | p->expired = 1; |
| 100797 | 100935 | rc = SQLITE_SCHEMA; |
| 100798 | 100936 | |
| 100799 | 100937 | /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes() |
| | @@ -100936,11 +101074,14 @@ |
| 100936 | 101074 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
| 100937 | 101075 | ** Synopsis: root=P2 iDb=P3 |
| 100938 | 101076 | ** |
| 100939 | 101077 | ** Open a read/write cursor named P1 on the table or index whose root |
| 100940 | 101078 | ** page is P2 (or whose root page is held in register P2 if the |
| 100941 | | -** OPFLAG_P2ISREG bit is set in P5 - see below). |
| 101079 | +** OPFLAG_P2ISREG bit is set in P5 - see below). The database file is |
| 101080 | +** determined by P3. P3==0 means the main database, P3==1 means the |
| 101081 | +** database used for temporary tables, and P3>1 means used the |
| 101082 | +** corresponding attached database. |
| 100942 | 101083 | ** |
| 100943 | 101084 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 100944 | 101085 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 100945 | 101086 | ** object, then table being opened must be an [index b-tree] where the |
| 100946 | 101087 | ** KeyInfo object defines the content and collating |
| | @@ -102122,11 +102263,11 @@ |
| 102122 | 102263 | ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted |
| 102123 | 102264 | ** into an integer without loss of information. Take care to avoid |
| 102124 | 102265 | ** changing the datatype of pIn3, however, as it is used by other |
| 102125 | 102266 | ** parts of the prepared statement. */ |
| 102126 | 102267 | Mem x = pIn3[0]; |
| 102127 | | - if( x.flags & MEM_Str ){ |
| 102268 | + if( (x.flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ){ |
| 102128 | 102269 | applyNumericAffinity(&x, 1); |
| 102129 | 102270 | } |
| 102130 | 102271 | if( x.flags & MEM_Int ){ |
| 102131 | 102272 | iKey = x.u.i; |
| 102132 | 102273 | }else |
| | @@ -102399,10 +102540,11 @@ |
| 102399 | 102540 | REGISTER_TRACE(pOp->p3, pKey); |
| 102400 | 102541 | x.nKey = pKey->u.i; |
| 102401 | 102542 | |
| 102402 | 102543 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
| 102403 | 102544 | assert( pC->iDb>=0 ); |
| 102545 | + if( p->expired==1 ) goto abort_due_to_expired; |
| 102404 | 102546 | zDb = db->aDb[pC->iDb].zDbSName; |
| 102405 | 102547 | pTab = pOp->p4.pTab; |
| 102406 | 102548 | assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
| 102407 | 102549 | }else{ |
| 102408 | 102550 | pTab = 0; |
| | @@ -102541,10 +102683,11 @@ |
| 102541 | 102683 | assert( pC->deferredMoveto==0 ); |
| 102542 | 102684 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 102543 | 102685 | |
| 102544 | 102686 | #ifdef SQLITE_DEBUG |
| 102545 | 102687 | if( pOp->p4type==P4_TABLE |
| 102688 | + && !p->expired |
| 102546 | 102689 | && HasRowid(pOp->p4.pTab) |
| 102547 | 102690 | && pOp->p5==0 |
| 102548 | 102691 | && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) |
| 102549 | 102692 | ){ |
| 102550 | 102693 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| | @@ -102559,10 +102702,11 @@ |
| 102559 | 102702 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 102560 | 102703 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 102561 | 102704 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 102562 | 102705 | ** VdbeCursor.movetoTarget to the current rowid. */ |
| 102563 | 102706 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
| 102707 | + if( p->expired==1 ) goto abort_due_to_expired; |
| 102564 | 102708 | assert( pC->iDb>=0 ); |
| 102565 | 102709 | assert( pOp->p4.pTab!=0 ); |
| 102566 | 102710 | zDb = db->aDb[pC->iDb].zDbSName; |
| 102567 | 102711 | pTab = pOp->p4.pTab; |
| 102568 | 102712 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
| | @@ -102815,10 +102959,26 @@ |
| 102815 | 102959 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
| 102816 | 102960 | } |
| 102817 | 102961 | pOut->u.i = v; |
| 102818 | 102962 | break; |
| 102819 | 102963 | } |
| 102964 | + |
| 102965 | +/* Opcode: Stale P1 * * * * |
| 102966 | +** |
| 102967 | +** Mark the row cache for cursor P1 as stale. This opcode is used |
| 102968 | +** on pseudo-cursors right before the content of the register that |
| 102969 | +** holds their record is modified. |
| 102970 | +*/ |
| 102971 | +case OP_Stale: { |
| 102972 | + VdbeCursor *pC; |
| 102973 | + |
| 102974 | + pC = p->apCsr[pOp->p1]; |
| 102975 | + assert( pC!=0 ); |
| 102976 | + assert( pC->eCurType==CURTYPE_PSEUDO ); |
| 102977 | + pC->cacheStatus = CACHE_STALE; |
| 102978 | + break; |
| 102979 | +} |
| 102820 | 102980 | |
| 102821 | 102981 | /* Opcode: NullRow P1 P2 * * * |
| 102822 | 102982 | ** |
| 102823 | 102983 | ** Move the cursor P1 to a null row if P2 is 0. Any OP_Column operations |
| 102824 | 102984 | ** that occur while the cursor is on the null row will always |
| | @@ -102830,10 +102990,11 @@ |
| 102830 | 102990 | ** If P2 is not 0, then the nullRow flag on the pseudo-cursor is |
| 102831 | 102991 | ** turned off. |
| 102832 | 102992 | */ |
| 102833 | 102993 | case OP_NullRow: { |
| 102834 | 102994 | VdbeCursor *pC; |
| 102995 | + Mem *pMem; |
| 102835 | 102996 | |
| 102836 | 102997 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 102837 | 102998 | pC = p->apCsr[pOp->p1]; |
| 102838 | 102999 | if( pC==0 ){ |
| 102839 | 103000 | /* If the cursor is not already open, create a special kind of |
| | @@ -102848,10 +103009,18 @@ |
| 102848 | 103009 | pC->nullRow = (pOp->p2==0); |
| 102849 | 103010 | pC->cacheStatus = CACHE_STALE; |
| 102850 | 103011 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 102851 | 103012 | assert( pC->uc.pCursor!=0 ); |
| 102852 | 103013 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
| 103014 | + }else if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ |
| 103015 | + static char nullRecord[] = { 0x02, 0x00 }; |
| 103016 | + pMem = &aMem[pC->seekResult]; |
| 103017 | + assert( !VdbeMemDynamic(pMem) ); |
| 103018 | + pC->nullRow = 1; |
| 103019 | + pMem->z = nullRecord; |
| 103020 | + pMem->n = 2; |
| 103021 | + pMem->flags = MEM_Blob|MEM_Ephem; |
| 102853 | 103022 | } |
| 102854 | 103023 | #ifdef SQLITE_DEBUG |
| 102855 | 103024 | if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; |
| 102856 | 103025 | #endif |
| 102857 | 103026 | break; |
| | @@ -103288,10 +103457,11 @@ |
| 103288 | 103457 | r.default_rc = 0; |
| 103289 | 103458 | r.aMem = &aMem[pOp->p2]; |
| 103290 | 103459 | rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res); |
| 103291 | 103460 | if( rc ) goto abort_due_to_error; |
| 103292 | 103461 | if( res!=0 ){ |
| 103462 | + if( p->expired==1 ) goto abort_due_to_expired; |
| 103293 | 103463 | rc = sqlite3VdbeFindIndexKey(pCrsr, pOp->p4.pIdx, &r, &res, 0); |
| 103294 | 103464 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 103295 | 103465 | if( res!=0 ){ |
| 103296 | 103466 | if( !sqlite3WritableSchema(db) ){ |
| 103297 | 103467 | rc = sqlite3ReportError( |
| | @@ -103601,10 +103771,25 @@ |
| 103601 | 103771 | } |
| 103602 | 103772 | #endif |
| 103603 | 103773 | } |
| 103604 | 103774 | break; |
| 103605 | 103775 | } |
| 103776 | + |
| 103777 | +/* |
| 103778 | +** Opcode: DbLock |
| 103779 | +** |
| 103780 | +** Raise an SQLITE_LOCKED error if there are any other reader statements |
| 103781 | +** currently active. |
| 103782 | +*/ |
| 103783 | +case OP_DbLock: { |
| 103784 | + if( db->nVdbeRead>1 ){ |
| 103785 | + rc = SQLITE_LOCKED; |
| 103786 | + p->errorAction = OE_Abort; |
| 103787 | + goto abort_due_to_error; |
| 103788 | + } |
| 103789 | + break; |
| 103790 | +}; |
| 103606 | 103791 | |
| 103607 | 103792 | /* Opcode: Clear P1 P2 P3 |
| 103608 | 103793 | ** |
| 103609 | 103794 | ** Delete all contents of the database table or index whose root page |
| 103610 | 103795 | ** in the database file is given by P1. But, unlike Destroy, do not |
| | @@ -103784,13 +103969,17 @@ |
| 103784 | 103969 | assert( iDb>=0 && iDb<db->nDb ); |
| 103785 | 103970 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) |
| 103786 | 103971 | || db->mallocFailed |
| 103787 | 103972 | || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) ); |
| 103788 | 103973 | |
| 103974 | + /* ParseSchema is never invoked on a read-only database */ |
| 103975 | + assert( db->aDb[iDb].pBt==0 || sqlite3BtreeIsReadonly(db->aDb[iDb].pBt)==0 ); |
| 103976 | + |
| 103789 | 103977 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 103790 | 103978 | if( pOp->p4.z==0 ){ |
| 103791 | 103979 | assert( pOp->p5!=0 ); |
| 103980 | + sqlite3ExpireStmtsUsingSchema(db, iDb, p); |
| 103792 | 103981 | sqlite3SchemaClear(db->aDb[iDb].pSchema); |
| 103793 | 103982 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
| 103794 | 103983 | rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5); |
| 103795 | 103984 | db->mDbFlags |= DBFLAG_SchemaChange; |
| 103796 | 103985 | p->expired = 0; |
| | @@ -103827,10 +104016,11 @@ |
| 103827 | 104016 | db->init.busy = 0; |
| 103828 | 104017 | } |
| 103829 | 104018 | } |
| 103830 | 104019 | if( rc ){ |
| 103831 | 104020 | sqlite3ResetAllSchemasOfConnection(db); |
| 104021 | + sqlite3ExpirePreparedStatements(db, 0); |
| 103832 | 104022 | if( rc==SQLITE_NOMEM ){ |
| 103833 | 104023 | goto no_mem; |
| 103834 | 104024 | } |
| 103835 | 104025 | goto abort_due_to_error; |
| 103836 | 104026 | } |
| | @@ -103985,10 +104175,11 @@ |
| 103985 | 104175 | assert( pOp[-1].opcode==OP_Found ); |
| 103986 | 104176 | assert( pOp[-1].p1==pOp->p1 ); |
| 103987 | 104177 | assert( pOp[-1].p3==pOp->p3 ); |
| 103988 | 104178 | pC = p->apCsr[pOp->p1]; |
| 103989 | 104179 | assert( pOp->p4type==P4_INDEX ); |
| 104180 | + if( p->expired==1 ) goto abort_due_to_expired; |
| 103990 | 104181 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 103991 | 104182 | assert( pC->uc.pCursor!=0 ); |
| 103992 | 104183 | assert( pC->isTable==0 ); |
| 103993 | 104184 | |
| 103994 | 104185 | memset(&r, 0, sizeof(r)); |
| | @@ -105984,11 +106175,11 @@ |
| 105984 | 106175 | if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ |
| 105985 | 106176 | db->flags |= SQLITE_CorruptRdOnly; |
| 105986 | 106177 | } |
| 105987 | 106178 | rc = SQLITE_ERROR; |
| 105988 | 106179 | if( resetSchemaOnFault>0 ){ |
| 105989 | | - sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
| 106180 | + sqlite3ResetOneSchema(db, resetSchemaOnFault-1, p); |
| 105990 | 106181 | } |
| 105991 | 106182 | |
| 105992 | 106183 | /* This is the only way out of this procedure. We have to |
| 105993 | 106184 | ** release the mutexes on btrees that were acquired at the |
| 105994 | 106185 | ** top. */ |
| | @@ -106045,10 +106236,14 @@ |
| 106045 | 106236 | */ |
| 106046 | 106237 | abort_due_to_interrupt: |
| 106047 | 106238 | assert( AtomicLoad(&db->u1.isInterrupted) ); |
| 106048 | 106239 | rc = SQLITE_INTERRUPT; |
| 106049 | 106240 | goto abort_due_to_error; |
| 106241 | + |
| 106242 | +abort_due_to_expired: |
| 106243 | + rc = SQLITE_SCHEMA; |
| 106244 | + goto abort_due_to_error; |
| 106050 | 106245 | } |
| 106051 | 106246 | |
| 106052 | 106247 | |
| 106053 | 106248 | /************** End of vdbe.c ************************************************/ |
| 106054 | 106249 | /************** Begin file vdbeblob.c ****************************************/ |
| | @@ -106078,10 +106273,11 @@ |
| 106078 | 106273 | typedef struct Incrblob Incrblob; |
| 106079 | 106274 | struct Incrblob { |
| 106080 | 106275 | int nByte; /* Size of open blob, in bytes */ |
| 106081 | 106276 | int iOffset; /* Byte offset of blob in cursor data */ |
| 106082 | 106277 | u16 iCol; /* Table column this handle is open on */ |
| 106278 | + u8 bRetry; /* Doing a blobReadWrite() retry */ |
| 106083 | 106279 | BtCursor *pCsr; /* Cursor pointing at blob row */ |
| 106084 | 106280 | sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
| 106085 | 106281 | sqlite3 *db; /* The associated database */ |
| 106086 | 106282 | char *zDb; /* Database name */ |
| 106087 | 106283 | Table *pTab; /* Table object */ |
| | @@ -106426,10 +106622,71 @@ |
| 106426 | 106622 | }else{ |
| 106427 | 106623 | rc = SQLITE_OK; |
| 106428 | 106624 | } |
| 106429 | 106625 | return rc; |
| 106430 | 106626 | } |
| 106627 | + |
| 106628 | +/* Forward reference */ |
| 106629 | +static int blobReadWrite( |
| 106630 | + sqlite3_blob *pBlob, |
| 106631 | + void *z, |
| 106632 | + int n, |
| 106633 | + int iOffset, |
| 106634 | + int (*xCall)(BtCursor*, u32, u32, void*) |
| 106635 | +); |
| 106636 | + |
| 106637 | +/* |
| 106638 | +** The b-tree cursor for blob handle p was restored to its saved position by |
| 106639 | +** sqlite3BtreePayloadChecked(), which then returned SQLITE_MOVED. The |
| 106640 | +** position is saved, for example, by a ROLLBACK, and that might have changed |
| 106641 | +** the content of the row. So the iOffset and nByte values computed when the |
| 106642 | +** handle was opened or last moved cannot be trusted. A ROLLBACK can change |
| 106643 | +** the size of a column that precedes the blob, which moves the blob within |
| 106644 | +** the row. |
| 106645 | +** |
| 106646 | +** Recompute iOffset and nByte in the same way that sqlite3_blob_reopen() |
| 106647 | +** does. Then retry the blobReadWrite(). |
| 106648 | +*/ |
| 106649 | +static SQLITE_NOINLINE int blobReadWriteRetry( |
| 106650 | + Incrblob *p, |
| 106651 | + void *z, |
| 106652 | + int n, |
| 106653 | + int iOffset, |
| 106654 | + int (*xCall)(BtCursor*, u32, u32, void*) |
| 106655 | +){ |
| 106656 | + int rc; |
| 106657 | + sqlite3_int64 iRow; |
| 106658 | + char *zErr = 0; |
| 106659 | + sqlite3 *db = p->db; |
| 106660 | + |
| 106661 | + sqlite3BtreeEnterCursor(p->pCsr); |
| 106662 | + iRow = sqlite3BtreeIntegerKey(p->pCsr); |
| 106663 | + sqlite3BtreeLeaveCursor(p->pCsr); |
| 106664 | + ((Vdbe*)p->pStmt)->rc = SQLITE_OK; |
| 106665 | + if( NEVER(p->bRetry) || sqlite3FaultSim(210) ){ |
| 106666 | + /* It should not be possible for blobReadWrite() and blobReadWriteRetry() |
| 106667 | + ** to get into a loop, but we check anyway, for safety. */ |
| 106668 | + rc = SQLITE_ABORT; |
| 106669 | + sqlite3VdbeFinalize((Vdbe*)p->pStmt); |
| 106670 | + p->pStmt = 0; |
| 106671 | + }else{ |
| 106672 | + rc = blobSeekToRow(p, iRow, &zErr); |
| 106673 | + } |
| 106674 | + assert( rc==SQLITE_OK || p->pStmt==0 ); |
| 106675 | + if( rc==SQLITE_OK ){ |
| 106676 | + sqlite3DbFree(db, zErr); |
| 106677 | + p->bRetry = 1; |
| 106678 | + rc = blobReadWrite((sqlite3_blob*)p,z,n,iOffset,xCall); |
| 106679 | + p->bRetry = 0; |
| 106680 | + }else{ |
| 106681 | + sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr); |
| 106682 | + sqlite3DbFree(db, zErr); |
| 106683 | + rc = sqlite3ApiExit(db, rc); |
| 106684 | + } |
| 106685 | + sqlite3_mutex_leave(db->mutex); |
| 106686 | + return rc; |
| 106687 | +} |
| 106431 | 106688 | |
| 106432 | 106689 | /* |
| 106433 | 106690 | ** Perform a read or write operation on a blob |
| 106434 | 106691 | */ |
| 106435 | 106692 | static int blobReadWrite( |
| | @@ -106504,15 +106761,25 @@ |
| 106504 | 106761 | #else |
| 106505 | 106762 | rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
| 106506 | 106763 | #endif |
| 106507 | 106764 | |
| 106508 | 106765 | sqlite3BtreeLeaveCursor(p->pCsr); |
| 106509 | | - if( rc==SQLITE_ABORT ){ |
| 106510 | | - sqlite3VdbeFinalize(v); |
| 106511 | | - p->pStmt = 0; |
| 106766 | + if( rc==SQLITE_OK ){ |
| 106767 | + v->rc = SQLITE_OK; |
| 106512 | 106768 | }else{ |
| 106513 | | - v->rc = rc; |
| 106769 | + if( rc==SQLITE_MOVED ){ |
| 106770 | + /* The cursor was restored and no data was read. Refresh the |
| 106771 | + ** handle and try the read again. */ |
| 106772 | + return blobReadWriteRetry(p,z,n,iOffset,xCall); |
| 106773 | + } |
| 106774 | + if( rc==SQLITE_ABORT ){ |
| 106775 | + assert( p->pStmt ); |
| 106776 | + sqlite3VdbeFinalize(v); |
| 106777 | + p->pStmt = 0; |
| 106778 | + }else{ |
| 106779 | + v->rc = rc; |
| 106780 | + } |
| 106514 | 106781 | } |
| 106515 | 106782 | } |
| 106516 | 106783 | sqlite3Error(db, rc); |
| 106517 | 106784 | rc = sqlite3ApiExit(db, rc); |
| 106518 | 106785 | sqlite3_mutex_leave(db->mutex); |
| | @@ -109749,12 +110016,28 @@ |
| 109749 | 110016 | i = 8; |
| 109750 | 110017 | }else{ |
| 109751 | 110018 | if( i<=2 && pCur->zType==0 ){ |
| 109752 | 110019 | Schema *pSchema; |
| 109753 | 110020 | HashElem *k; |
| 109754 | | - int iDb = pOp->p3; |
| 109755 | | - Pgno iRoot = (Pgno)pOp->p2; |
| 110021 | + int iDb; |
| 110022 | + Pgno iRoot; |
| 110023 | + switch( pOp->opcode ){ |
| 110024 | + case OP_Clear: { |
| 110025 | + iRoot = (Pgno)pOp->p1; |
| 110026 | + iDb = pOp->p2; |
| 110027 | + break; |
| 110028 | + } |
| 110029 | + case OP_Destroy: { |
| 110030 | + iRoot = (Pgno)pOp->p1; |
| 110031 | + iDb = pOp->p3; |
| 110032 | + break; |
| 110033 | + } |
| 110034 | + default: { |
| 110035 | + iRoot = (Pgno)pOp->p2; |
| 110036 | + iDb = pOp->p3; |
| 110037 | + } |
| 110038 | + } |
| 109756 | 110039 | sqlite3 *db = pVTab->db; |
| 109757 | 110040 | pSchema = db->aDb[iDb].pSchema; |
| 109758 | 110041 | pCur->zSchema = db->aDb[iDb].zDbSName; |
| 109759 | 110042 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 109760 | 110043 | Table *pTab = (Table*)sqliteHashData(k); |
| | @@ -109847,11 +110130,14 @@ |
| 109847 | 110130 | break; |
| 109848 | 110131 | case 22: /* tables_used.name */ |
| 109849 | 110132 | sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC); |
| 109850 | 110133 | break; |
| 109851 | 110134 | case 23: /* tables_used.wr */ |
| 109852 | | - sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite); |
| 110135 | + sqlite3_result_int(ctx, |
| 110136 | + pOp->opcode==OP_OpenWrite || |
| 110137 | + pOp->opcode==OP_Clear || |
| 110138 | + pOp->opcode==OP_Destroy); |
| 109853 | 110139 | break; |
| 109854 | 110140 | } |
| 109855 | 110141 | return SQLITE_OK; |
| 109856 | 110142 | } |
| 109857 | 110143 | |
| | @@ -110690,10 +110976,23 @@ |
| 110690 | 110976 | */ |
| 110691 | 110977 | SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 110692 | 110978 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 110693 | 110979 | return WRC_Continue; |
| 110694 | 110980 | } |
| 110981 | + |
| 110982 | +/* |
| 110983 | +** This is a SELECT-node callback for the expression walker that |
| 110984 | +** always "fails". By "fail" in this case, we mean set |
| 110985 | +** pWalker->eCode to zero and abort. |
| 110986 | +** |
| 110987 | +** This callback is used by multiple expression walkers. |
| 110988 | +*/ |
| 110989 | +SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){ |
| 110990 | + UNUSED_PARAMETER(NotUsed); |
| 110991 | + pWalker->eCode = 0; |
| 110992 | + return WRC_Abort; |
| 110993 | +} |
| 110695 | 110994 | |
| 110696 | 110995 | /************** End of walker.c **********************************************/ |
| 110697 | 110996 | /************** Begin file resolve.c *****************************************/ |
| 110698 | 110997 | /* |
| 110699 | 110998 | ** 2008 August 18 |
| | @@ -111157,12 +111456,19 @@ |
| 111157 | 111456 | extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); |
| 111158 | 111457 | } |
| 111159 | 111458 | } |
| 111160 | 111459 | cnt++; |
| 111161 | 111460 | pMatch = pItem; |
| 111162 | | - /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 111163 | | - pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; |
| 111461 | + /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY. |
| 111462 | + ** Except when parsing for ALTER TABLE. In that case leave |
| 111463 | + ** Expr.iColumn set to iCol so that it can be distinguished from |
| 111464 | + ** literal "rowid" references. */ |
| 111465 | + if( j==pTab->iPKey && !IN_RENAME_OBJECT ){ |
| 111466 | + pExpr->iColumn = -1; |
| 111467 | + }else{ |
| 111468 | + pExpr->iColumn = (i16)j; |
| 111469 | + } |
| 111164 | 111470 | if( pItem->fg.isNestedFrom ){ |
| 111165 | 111471 | sqlite3SrcItemColumnUsed(pItem, j); |
| 111166 | 111472 | } |
| 111167 | 111473 | } |
| 111168 | 111474 | if( 0==cnt && VisibleRowid(pTab) ){ |
| | @@ -111257,11 +111563,11 @@ |
| 111257 | 111563 | int iCol; |
| 111258 | 111564 | pSchema = pTab->pSchema; |
| 111259 | 111565 | cntTab++; |
| 111260 | 111566 | iCol = sqlite3ColumnIndex(pTab, zCol); |
| 111261 | 111567 | if( iCol>=0 ){ |
| 111262 | | - if( pTab->iPKey==iCol ) iCol = -1; |
| 111568 | + if( pTab->iPKey==iCol && !IN_RENAME_OBJECT ) iCol = -1; |
| 111263 | 111569 | }else{ |
| 111264 | 111570 | if( sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ |
| 111265 | 111571 | iCol = -1; |
| 111266 | 111572 | }else{ |
| 111267 | 111573 | iCol = pTab->nCol; |
| | @@ -111672,10 +111978,14 @@ |
| 111672 | 111978 | break; |
| 111673 | 111979 | } |
| 111674 | 111980 | if( pExpr->op==TK_UPLUS ){ |
| 111675 | 111981 | pExpr = pExpr->pLeft; |
| 111676 | 111982 | assert( pExpr!=0 ); |
| 111983 | + }else if( pExpr->op==TK_CASE ){ |
| 111984 | + assert( ExprUseXList(pExpr) ); |
| 111985 | + resolveSetExprSubtypeArg(pExpr->x.pList); |
| 111986 | + break; |
| 111677 | 111987 | }else{ |
| 111678 | 111988 | break; |
| 111679 | 111989 | } |
| 111680 | 111990 | } |
| 111681 | 111991 | } |
| | @@ -112300,10 +112610,33 @@ |
| 112300 | 112610 | } |
| 112301 | 112611 | |
| 112302 | 112612 | /* If no match, return 0. */ |
| 112303 | 112613 | return 0; |
| 112304 | 112614 | } |
| 112615 | + |
| 112616 | +/* |
| 112617 | +** ORDER BY term pE, a simple identifier, has been matched by resolveAsName() |
| 112618 | +** against the iCol-th (1-based) entry of result-set pEList. If this is an |
| 112619 | +** ALTER TABLE rename, and the name of that entry was generated by expanding |
| 112620 | +** "*" or "tbl.*", then modify pE to refer to the table column directly. |
| 112621 | +*/ |
| 112622 | +static void resolveRenameStarTerm( |
| 112623 | + Parse *pParse, /* Parsing context */ |
| 112624 | + ExprList *pEList, /* Result-set that pE was matched against */ |
| 112625 | + int iCol, /* 1-based index of the matching pEList entry */ |
| 112626 | + Expr *pE /* ORDER BY term to convert */ |
| 112627 | +){ |
| 112628 | + if( IN_RENAME_OBJECT && pEList->a[iCol-1].fg.bStarName ){ |
| 112629 | + Expr *pX = pEList->a[iCol-1].pExpr; |
| 112630 | + if( ALWAYS(pX->op==TK_COLUMN) && ALWAYS(pE->op==TK_ID) ){ |
| 112631 | + pE->op = TK_COLUMN; |
| 112632 | + pE->iTable = pX->iTable; |
| 112633 | + pE->iColumn = pX->iColumn; |
| 112634 | + pE->y.pTab = pX->y.pTab; |
| 112635 | + } |
| 112636 | + } |
| 112637 | +} |
| 112305 | 112638 | |
| 112306 | 112639 | /* |
| 112307 | 112640 | ** Generate an ORDER BY or GROUP BY term out-of-range error. |
| 112308 | 112641 | */ |
| 112309 | 112642 | static void resolveOutOfRangeError( |
| | @@ -112375,11 +112708,13 @@ |
| 112375 | 112708 | resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE); |
| 112376 | 112709 | return 1; |
| 112377 | 112710 | } |
| 112378 | 112711 | }else{ |
| 112379 | 112712 | iCol = resolveAsName(pParse, pEList, pE); |
| 112380 | | - if( iCol==0 ){ |
| 112713 | + if( iCol>0 ){ |
| 112714 | + resolveRenameStarTerm(pParse, pEList, iCol, pE); |
| 112715 | + }else{ |
| 112381 | 112716 | /* Now test if expression pE matches one of the values returned |
| 112382 | 112717 | ** by pSelect. In the usual case this is done by duplicating the |
| 112383 | 112718 | ** expression, resolving any symbols in it, and then comparing |
| 112384 | 112719 | ** it against each expression returned by the SELECT statement. |
| 112385 | 112720 | ** Once the comparisons are finished, the duplicate expression |
| | @@ -112543,10 +112878,11 @@ |
| 112543 | 112878 | Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE); |
| 112544 | 112879 | if( NEVER(pE2==0) ) continue; |
| 112545 | 112880 | if( zType[0]!='G' ){ |
| 112546 | 112881 | iCol = resolveAsName(pParse, pSelect->pEList, pE2); |
| 112547 | 112882 | if( iCol>0 ){ |
| 112883 | + resolveRenameStarTerm(pParse, pSelect->pEList, iCol, pE2); |
| 112548 | 112884 | /* If an AS-name match is found, mark this ORDER BY column as being |
| 112549 | 112885 | ** a copy of the iCol-th result-set column. The subsequent call to |
| 112550 | 112886 | ** sqlite3ResolveOrderGroupBy() will convert the expression to a |
| 112551 | 112887 | ** copy of the iCol-th result-set expression. */ |
| 112552 | 112888 | pItem->u.x.iOrderByCol = (u16)iCol; |
| | @@ -113141,15 +113477,20 @@ |
| 113141 | 113477 | assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr ); |
| 113142 | 113478 | return sqlite3ExprAffinity( |
| 113143 | 113479 | pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr |
| 113144 | 113480 | ); |
| 113145 | 113481 | } |
| 113146 | | - if( op==TK_VECTOR |
| 113147 | | - || (op==TK_FUNCTION && pExpr->affExpr==SQLITE_AFF_DEFER) |
| 113148 | | - ){ |
| 113482 | + if( op==TK_VECTOR ){ |
| 113149 | 113483 | assert( ExprUseXList(pExpr) ); |
| 113150 | 113484 | return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr); |
| 113485 | + } |
| 113486 | + if( op==TK_FUNCTION && pExpr->affExpr==SQLITE_AFF_DEFER ){ |
| 113487 | + /* The USING column of a FULL JOIN: coalesce(L.x, R.x). Use the |
| 113488 | + ** affinity of L.x, except that REAL becomes NUMERIC, since the |
| 113489 | + ** value may come from R.x and so need not be REAL. */ |
| 113490 | + char aff = sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr); |
| 113491 | + return aff==SQLITE_AFF_REAL ? SQLITE_AFF_NUMERIC : aff; |
| 113151 | 113492 | } |
| 113152 | 113493 | if( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow) ){ |
| 113153 | 113494 | assert( pExpr->op==TK_COLLATE |
| 113154 | 113495 | || pExpr->op==TK_IF_NULL_ROW |
| 113155 | 113496 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) ); |
| | @@ -114665,12 +115006,12 @@ |
| 114665 | 115006 | ** |
| 114666 | 115007 | ** The return value includes space to duplicate all Expr nodes in the |
| 114667 | 115008 | ** tree formed by Expr.pLeft and Expr.pRight, but not any other |
| 114668 | 115009 | ** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin. |
| 114669 | 115010 | */ |
| 114670 | | -static int dupedExprSize(const Expr *p){ |
| 114671 | | - int nByte; |
| 115011 | +static u64 dupedExprSize(const Expr *p){ |
| 115012 | + u64 nByte; |
| 114672 | 115013 | assert( p!=0 ); |
| 114673 | 115014 | nByte = dupedExprNodeSize(p, EXPRDUP_REDUCE); |
| 114674 | 115015 | if( p->pLeft ) nByte += dupedExprSize(p->pLeft); |
| 114675 | 115016 | if( p->pRight ) nByte += dupedExprSize(p->pRight); |
| 114676 | 115017 | assert( nByte==ROUND8(nByte) ); |
| | @@ -114723,11 +115064,11 @@ |
| 114723 | 115064 | #endif |
| 114724 | 115065 | staticFlag = EP_Static; |
| 114725 | 115066 | assert( sEdupBuf.zAlloc!=0 ); |
| 114726 | 115067 | assert( dupFlags==EXPRDUP_REDUCE ); |
| 114727 | 115068 | }else{ |
| 114728 | | - int nAlloc; |
| 115069 | + u64 nAlloc; |
| 114729 | 115070 | if( dupFlags ){ |
| 114730 | 115071 | nAlloc = dupedExprSize(p); |
| 114731 | 115072 | }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ |
| 114732 | 115073 | nToken = sqlite3Strlen30NN(p->u.zToken)+1; |
| 114733 | 115074 | nAlloc = ROUND8(EXPR_FULLSIZE + nToken); |
| | @@ -115392,23 +115733,10 @@ |
| 115392 | 115733 | m |= pExpr->flags; |
| 115393 | 115734 | } |
| 115394 | 115735 | return m; |
| 115395 | 115736 | } |
| 115396 | 115737 | |
| 115397 | | -/* |
| 115398 | | -** This is a SELECT-node callback for the expression walker that |
| 115399 | | -** always "fails". By "fail" in this case, we mean set |
| 115400 | | -** pWalker->eCode to zero and abort. |
| 115401 | | -** |
| 115402 | | -** This callback is used by multiple expression walkers. |
| 115403 | | -*/ |
| 115404 | | -SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){ |
| 115405 | | - UNUSED_PARAMETER(NotUsed); |
| 115406 | | - pWalker->eCode = 0; |
| 115407 | | - return WRC_Abort; |
| 115408 | | -} |
| 115409 | | - |
| 115410 | 115738 | /* |
| 115411 | 115739 | ** Check the input string to see if it is "true" or "false" (in any case). |
| 115412 | 115740 | ** |
| 115413 | 115741 | ** If the string is.... Return |
| 115414 | 115742 | ** "true" EP_IsTrue |
| | @@ -115702,10 +116030,22 @@ |
| 115702 | 116030 | case TK_IS: |
| 115703 | 116031 | case TK_ISNOT: |
| 115704 | 116032 | if( pWalker->eCode==6 ){ |
| 115705 | 116033 | pWalker->eCode = 0; |
| 115706 | 116034 | return WRC_Abort; |
| 116035 | + } |
| 116036 | + if( pWalker->eCode==4 || pWalker->eCode==5 ){ |
| 116037 | + /* This is a DEFAULT expression. As it will never be name resolved, |
| 116038 | + ** attempt the the TK_IS -> TK_TRUTH transform here. */ |
| 116039 | + Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight); |
| 116040 | + if( ALWAYS(pRight) ){ |
| 116041 | + if( pRight->op==TK_ID ) sqlite3ExprIdToTrueFalse(pRight); |
| 116042 | + if( pRight->op==TK_TRUEFALSE ){ |
| 116043 | + pExpr->op2 = pExpr->op; |
| 116044 | + pExpr->op = TK_TRUTH; |
| 116045 | + } |
| 116046 | + } |
| 115707 | 116047 | } |
| 115708 | 116048 | return WRC_Continue; |
| 115709 | 116049 | case TK_VARIABLE: |
| 115710 | 116050 | if( pWalker->eCode==5 ){ |
| 115711 | 116051 | /* Silently convert bound parameters that appear inside of CREATE |
| | @@ -115907,35 +116247,157 @@ |
| 115907 | 116247 | } |
| 115908 | 116248 | /* Rules (1), (2a), and (2b) handled by the following: */ |
| 115909 | 116249 | return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq); |
| 115910 | 116250 | } |
| 115911 | 116251 | |
| 116252 | + |
| 116253 | +/* |
| 116254 | +** Return true if pExpr is a copy of a term of pGroupBy that sorts using |
| 116255 | +** the BINARY collating sequence. |
| 116256 | +*/ |
| 116257 | +static int exprIsBinaryGroupByTerm( |
| 116258 | + Parse *pParse, |
| 116259 | + Expr *pExpr, |
| 116260 | + ExprList *pGroupBy |
| 116261 | +){ |
| 116262 | + int i; |
| 116263 | + for(i=0; i<pGroupBy->nExpr; i++){ |
| 116264 | + Expr *p = pGroupBy->a[i].pExpr; |
| 116265 | + if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){ |
| 116266 | + CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, p); |
| 116267 | + if( sqlite3IsBinary(pColl) ) return 1; |
| 116268 | + } |
| 116269 | + } |
| 116270 | + return 0; |
| 116271 | +} |
| 116272 | + |
| 116273 | +/* |
| 116274 | +** Bits returned by exprScanOperand(). |
| 116275 | +*/ |
| 116276 | +#define GBOP_GROUPBY 0x01 /* The operand is a GROUP BY term */ |
| 116277 | +#define GBOP_INEXACT 0x02 /* That GROUP BY term might be a number */ |
| 116278 | +#define GBOP_NOAFF 0x04 /* That GROUP BY term has no affinity */ |
| 116279 | +#define GBOP_TEXT 0x08 /* The operand has TEXT affinity */ |
| 116280 | + |
| 116281 | +/* |
| 116282 | +** Inspect operand pOp of an operator and return a mask of GBOP_* bits that |
| 116283 | +** describe it. GBOP_GROUPBY is set if pOp is a GROUP BY term. Then |
| 116284 | +** GBOP_INEXACT is also set if that term might be a number, and GBOP_NOAFF |
| 116285 | +** if it has no affinity. GBOP_TEXT is set if pOp has TEXT affinity. |
| 116286 | +*/ |
| 116287 | +static int exprScanOperand(Walker *pWalker, Expr *pOp){ |
| 116288 | + int m = 0; |
| 116289 | + if( pOp ){ |
| 116290 | + char aff = sqlite3ExprAffinity(pOp); |
| 116291 | + if( exprIsBinaryGroupByTerm(pWalker->pParse, pOp, pWalker->u.pGroupBy) ){ |
| 116292 | + m |= GBOP_GROUPBY; |
| 116293 | + if( aff<=SQLITE_AFF_NONE ) m |= GBOP_NOAFF; |
| 116294 | + if( aff!=SQLITE_AFF_TEXT && pOp->op!=TK_CONCAT ) m |= GBOP_INEXACT; |
| 116295 | + } |
| 116296 | + if( aff==SQLITE_AFF_TEXT ) m |= GBOP_TEXT; |
| 116297 | + } |
| 116298 | + return m; |
| 116299 | +} |
| 116300 | + |
| 116301 | +/* |
| 116302 | +** pExpr is not itself a GROUP BY term. Return true if it has an operand |
| 116303 | +** that is a GROUP BY term, and the value of pExpr might depend on which |
| 116304 | +** member of a group that operand was taken from. |
| 116305 | +** |
| 116306 | +** The rows of a group are equal in the GROUP BY sense but are not always |
| 116307 | +** identical. With the BINARY collating sequence, two values in the same |
| 116308 | +** group can still differ in type: 1 and 1.0 (also -0.0 and 0.0) are in the |
| 116309 | +** same group. So typeof(x), length(x), x||'', x/2, x+1 and so forth can |
| 116310 | +** have a different value for different rows of one group. A GROUP BY |
| 116311 | +** term that might be a number may therefore only be an operand of a |
| 116312 | +** comparison, because comparisons treat values that are equal as |
| 116313 | +** interchangeable, or of a logical operator. A GROUP BY term that is |
| 116314 | +** always TEXT or BLOB (the result of the || operator, or a column or CAST |
| 116315 | +** with TEXT affinity) can be an operand of anything, since equal text |
| 116316 | +** values are identical. |
| 116317 | +** |
| 116318 | +** The exception for comparisons is a comparison of a GROUP BY term that |
| 116319 | +** has no affinity (an expression, not a column) against a TEXT operand. |
| 116320 | +** TEXT affinity is applied to the term and converts 1 to '1' but 1.0 to |
| 116321 | +** '1.0'. |
| 116322 | +*/ |
| 116323 | +static int exprGroupByOperandUnsafe(Walker *pWalker, Expr *pExpr){ |
| 116324 | + int bCmp = 0; /* True for a comparison operator */ |
| 116325 | + int bOther = 0; /* True for any operator or function that is not a |
| 116326 | + ** comparison or a logical operator */ |
| 116327 | + int m = 0; /* Union of the GBOP_* bits for all operands */ |
| 116328 | + int i; |
| 116329 | + |
| 116330 | + switch( pExpr->op ){ |
| 116331 | + case TK_EQ: case TK_NE: case TK_LT: case TK_LE: |
| 116332 | + case TK_GT: case TK_GE: case TK_IS: case TK_ISNOT: |
| 116333 | + case TK_IN: case TK_BETWEEN: { |
| 116334 | + testcase( pExpr->op==TK_EQ ); testcase( pExpr->op==TK_NE ); |
| 116335 | + testcase( pExpr->op==TK_LT ); testcase( pExpr->op==TK_LE ); |
| 116336 | + testcase( pExpr->op==TK_GT ); testcase( pExpr->op==TK_GE ); |
| 116337 | + testcase( pExpr->op==TK_IS ); testcase( pExpr->op==TK_ISNOT ); |
| 116338 | + testcase( pExpr->op==TK_IN ); testcase( pExpr->op==TK_BETWEEN ); |
| 116339 | + bCmp = 1; |
| 116340 | + break; |
| 116341 | + } |
| 116342 | + case TK_ISNULL: case TK_NOTNULL: case TK_AND: case TK_OR: case TK_NOT: { |
| 116343 | + testcase( pExpr->op==TK_ISNULL ); testcase( pExpr->op==TK_NOTNULL ); |
| 116344 | + testcase( pExpr->op==TK_AND ); testcase( pExpr->op==TK_OR ); |
| 116345 | + testcase( pExpr->op==TK_NOT ); |
| 116346 | + break; |
| 116347 | + } |
| 116348 | + default: { |
| 116349 | + bOther = 1; |
| 116350 | + break; |
| 116351 | + } |
| 116352 | + } |
| 116353 | + m |= exprScanOperand(pWalker, pExpr->pLeft); |
| 116354 | + m |= exprScanOperand(pWalker, pExpr->pRight); |
| 116355 | + assert( ExprUseXList(pExpr) ); /* Sub-selects were handled by the caller */ |
| 116356 | + if( pExpr->x.pList ){ |
| 116357 | + for(i=0; i<pExpr->x.pList->nExpr; i++){ |
| 116358 | + m |= exprScanOperand(pWalker, pExpr->x.pList->a[i].pExpr); |
| 116359 | + } |
| 116360 | + } |
| 116361 | + if( (m & GBOP_GROUPBY)==0 ) return 0; |
| 116362 | + |
| 116363 | + /* Any other operator or function is unsafe if it has an operand that |
| 116364 | + ** is a GROUP BY term that might be a number. */ |
| 116365 | + if( bOther ) return (m & GBOP_INEXACT)!=0; |
| 116366 | + |
| 116367 | + /* A comparison is unsafe if a GROUP BY operand without affinity is |
| 116368 | + ** compared against an operand with TEXT affinity. */ |
| 116369 | + testcase( (m & (GBOP_NOAFF|GBOP_TEXT))==0 ); |
| 116370 | + testcase( (m & (GBOP_NOAFF|GBOP_TEXT))==GBOP_NOAFF ); |
| 116371 | + testcase( (m & (GBOP_NOAFF|GBOP_TEXT))==GBOP_TEXT ); |
| 116372 | + return bCmp && (m & (GBOP_NOAFF|GBOP_TEXT))==(GBOP_NOAFF|GBOP_TEXT); |
| 116373 | +} |
| 115912 | 116374 | |
| 115913 | 116375 | /* |
| 115914 | 116376 | ** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy(). |
| 115915 | 116377 | */ |
| 115916 | 116378 | static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){ |
| 115917 | 116379 | ExprList *pGroupBy = pWalker->u.pGroupBy; |
| 115918 | | - int i; |
| 115919 | 116380 | |
| 115920 | 116381 | /* Check if pExpr is identical to any GROUP BY term. If so, consider |
| 115921 | 116382 | ** it constant. */ |
| 115922 | | - for(i=0; i<pGroupBy->nExpr; i++){ |
| 115923 | | - Expr *p = pGroupBy->a[i].pExpr; |
| 115924 | | - if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){ |
| 115925 | | - CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p); |
| 115926 | | - if( sqlite3IsBinary(pColl) ){ |
| 115927 | | - return WRC_Prune; |
| 115928 | | - } |
| 115929 | | - } |
| 116383 | + if( exprIsBinaryGroupByTerm(pWalker->pParse, pExpr, pGroupBy) ){ |
| 116384 | + return WRC_Prune; |
| 115930 | 116385 | } |
| 115931 | 116386 | |
| 115932 | 116387 | /* Check if pExpr is a sub-select. If so, consider it variable. */ |
| 115933 | 116388 | if( ExprUseXSelect(pExpr) ){ |
| 115934 | 116389 | pWalker->eCode = 0; |
| 115935 | 116390 | return WRC_Abort; |
| 115936 | 116391 | } |
| 116392 | + |
| 116393 | + /* An operator applied to a GROUP BY term is constant only if its value |
| 116394 | + ** is the same for every member of a group. */ |
| 116395 | + if( exprGroupByOperandUnsafe(pWalker, pExpr) ){ |
| 116396 | + pWalker->eCode = 0; |
| 116397 | + return WRC_Abort; |
| 116398 | + } |
| 115937 | 116399 | |
| 115938 | 116400 | return exprNodeIsConstant(pWalker, pExpr); |
| 115939 | 116401 | } |
| 115940 | 116402 | |
| 115941 | 116403 | /* |
| | @@ -115944,20 +116406,29 @@ |
| 115944 | 116406 | ** in pGroupBy that sort with the BINARY collation sequence. |
| 115945 | 116407 | ** |
| 115946 | 116408 | ** This routine is used to determine if a term of the HAVING clause can |
| 115947 | 116409 | ** be promoted into the WHERE clause. In order for such a promotion to work, |
| 115948 | 116410 | ** the value of the HAVING clause term must be the same for all members of |
| 115949 | | -** a "group". The requirement that the GROUP BY term must be BINARY |
| 115950 | | -** assumes that no other collating sequence will have a finer-grained |
| 115951 | | -** grouping than binary. In other words (A=B COLLATE binary) implies |
| 115952 | | -** A=B in every other collating sequence. The requirement that the |
| 115953 | | -** GROUP BY be BINARY is stricter than necessary. It would also work |
| 115954 | | -** to promote HAVING clauses that use the same alternative collating |
| 115955 | | -** sequence as the GROUP BY term, but that is much harder to check, |
| 115956 | | -** alternative collating sequences are uncommon, and this is only an |
| 115957 | | -** optimization, so we take the easy way out and simply require the |
| 115958 | | -** GROUP BY to use the BINARY collating sequence. |
| 116411 | +** a "group". There are two requirements for this. |
| 116412 | +** |
| 116413 | +** (1) Members of a group are equal in the sense of the GROUP BY collating |
| 116414 | +** sequence, but need not be identical. The requirement that the |
| 116415 | +** GROUP BY term be BINARY assumes that no other collating sequence |
| 116416 | +** will have a finer-grained grouping than binary. In other words |
| 116417 | +** (A=B COLLATE binary) implies A=B in every other collating sequence. |
| 116418 | +** The requirement that the GROUP BY be BINARY is stricter than |
| 116419 | +** necessary. It would also work to promote HAVING clauses that use the |
| 116420 | +** same alternative collating sequence as the GROUP BY term, but that |
| 116421 | +** is much harder to check, alternative collating sequences are |
| 116422 | +** uncommon, and this is only an optimization, so we take the easy way |
| 116423 | +** out and simply require the GROUP BY to use the BINARY collating |
| 116424 | +** sequence. |
| 116425 | +** |
| 116426 | +** (2) Even with BINARY, equal values can be distinguishable, as in the |
| 116427 | +** case of 1 and 1.0. A GROUP BY term may therefore only be used as |
| 116428 | +** an operand of a comparison or logical operator. See |
| 116429 | +** exprGroupByOperandUnsafe() for details. |
| 115959 | 116430 | */ |
| 115960 | 116431 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){ |
| 115961 | 116432 | Walker w; |
| 115962 | 116433 | w.eCode = 1; |
| 115963 | 116434 | w.xExprCallback = exprNodeIsConstantOrGroupBy; |
| | @@ -116617,10 +117088,15 @@ |
| 116617 | 117088 | #ifndef SQLITE_OMIT_SUBQUERY |
| 116618 | 117089 | /* |
| 116619 | 117090 | ** Argument pExpr is an (?, ?...) IN(...) expression. This |
| 116620 | 117091 | ** function allocates and returns a nul-terminated string containing |
| 116621 | 117092 | ** the affinities to be used for each column of the comparison. |
| 117093 | +** |
| 117094 | +** NUMERIC affinity is used in place of REAL. Within a comparison the |
| 117095 | +** two are the same, except that REAL affinity (as applied by OP_Affinity |
| 117096 | +** and OP_MakeRecord) converts integers into floating point, and that |
| 117097 | +** might change the value of a large integer. |
| 116622 | 117098 | ** |
| 116623 | 117099 | ** It is the responsibility of the caller to ensure that the returned |
| 116624 | 117100 | ** string is eventually freed using sqlite3DbFree(). |
| 116625 | 117101 | */ |
| 116626 | 117102 | static char *exprINAffinity(Parse *pParse, const Expr *pExpr){ |
| | @@ -116639,10 +117115,11 @@ |
| 116639 | 117115 | if( pSelect ){ |
| 116640 | 117116 | zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a); |
| 116641 | 117117 | }else{ |
| 116642 | 117118 | zRet[i] = a; |
| 116643 | 117119 | } |
| 117120 | + if( zRet[i]==SQLITE_AFF_REAL ) zRet[i] = SQLITE_AFF_NUMERIC; |
| 116644 | 117121 | } |
| 116645 | 117122 | zRet[nVal] = '\0'; |
| 116646 | 117123 | } |
| 116647 | 117124 | return zRet; |
| 116648 | 117125 | } |
| | @@ -117115,11 +117592,11 @@ |
| 117115 | 117592 | } |
| 117116 | 117593 | sqlite3ExprDeferredDelete(pParse, pLeft); |
| 117117 | 117594 | pSel->pLimit->pLeft = pLimit; |
| 117118 | 117595 | } |
| 117119 | 117596 | }else{ |
| 117120 | | - /* If there is no pre-existing limit add a limit of 1 */ |
| 117597 | + /* If there is no limit add a limit of 1. (tag-202611101908) */ |
| 117121 | 117598 | pLimit = sqlite3ExprInt32(pParse->db, 1); |
| 117122 | 117599 | pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); |
| 117123 | 117600 | } |
| 117124 | 117601 | pSel->iLimit = 0; |
| 117125 | 117602 | if( sqlite3Select(pParse, pSel, &dest) ){ |
| | @@ -117435,13 +117912,14 @@ |
| 117435 | 117912 | ** |
| 117436 | 117913 | ** For a scalar LHS, it is sufficient to check just the first row |
| 117437 | 117914 | ** of the RHS. |
| 117438 | 117915 | */ |
| 117439 | 117916 | if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); |
| 117440 | | - if( eType==IN_INDEX_INDEX_DESC ){ |
| 117917 | + if( eType==IN_INDEX_INDEX_DESC && nVector==1 ){ |
| 117441 | 117918 | addrTop = sqlite3VdbeAddOp2(v, OP_Last, iTab, destIfFalse); |
| 117442 | 117919 | }else{ |
| 117920 | + testcase( eType==IN_INDEX_INDEX_DESC ); |
| 117443 | 117921 | testcase( eType==IN_INDEX_EPH ); |
| 117444 | 117922 | testcase( eType==IN_INDEX_ROWID ); |
| 117445 | 117923 | addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse); |
| 117446 | 117924 | } |
| 117447 | 117925 | VdbeCoverage(v); |
| | @@ -117953,10 +118431,34 @@ |
| 117953 | 118431 | w.xExprCallback = exprNodeCanReturnSubtype; |
| 117954 | 118432 | sqlite3WalkExpr(&w, pExpr); |
| 117955 | 118433 | return w.eCode; |
| 117956 | 118434 | } |
| 117957 | 118435 | |
| 118436 | +/* |
| 118437 | +** Return 0 iff the expression in IndexedExpr object p is identical to |
| 118438 | +** expression pExpr, and the affinity is such that an entry read from |
| 118439 | +** the index may be substituted for pExpr in any context. Otherwise |
| 118440 | +** return non-zero. |
| 118441 | +*/ |
| 118442 | +static int indexedExprCompare( |
| 118443 | + IndexedExpr *p, /* Indexed expression */ |
| 118444 | + Expr *pExpr, /* Expression to compare to p */ |
| 118445 | + int iDataCur /* iTab argument for ExprCompare() */ |
| 118446 | +){ |
| 118447 | + u8 exprAff; |
| 118448 | + if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) return 1; |
| 118449 | + assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC ); |
| 118450 | + exprAff = sqlite3ExprAffinity(pExpr); |
| 118451 | + if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB) |
| 118452 | + || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT) |
| 118453 | + || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC) |
| 118454 | + ){ |
| 118455 | + /* Affinity mismatch on a generated column */ |
| 118456 | + return 1; |
| 118457 | + } |
| 118458 | + return 0; |
| 118459 | +} |
| 117958 | 118460 | |
| 117959 | 118461 | /* |
| 117960 | 118462 | ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr. |
| 117961 | 118463 | ** If it is, then resolve the expression by reading from the index and |
| 117962 | 118464 | ** return the register into which the value has been read. If pExpr is |
| | @@ -117967,29 +118469,19 @@ |
| 117967 | 118469 | Expr *pExpr, /* The expression to potentially bypass */ |
| 117968 | 118470 | int target /* Where to store the result of the expression */ |
| 117969 | 118471 | ){ |
| 117970 | 118472 | IndexedExpr *p; |
| 117971 | 118473 | Vdbe *v; |
| 118474 | + |
| 117972 | 118475 | for(p=pParse->pIdxEpr; p; p=p->pIENext){ |
| 117973 | | - u8 exprAff; |
| 117974 | 118476 | int iDataCur = p->iDataCur; |
| 117975 | 118477 | if( iDataCur<0 ) continue; |
| 117976 | 118478 | if( pParse->iSelfTab ){ |
| 117977 | 118479 | if( p->iDataCur!=pParse->iSelfTab-1 ) continue; |
| 117978 | 118480 | iDataCur = -1; |
| 117979 | 118481 | } |
| 117980 | | - if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue; |
| 117981 | | - assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC ); |
| 117982 | | - exprAff = sqlite3ExprAffinity(pExpr); |
| 117983 | | - if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB) |
| 117984 | | - || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT) |
| 117985 | | - || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC) |
| 117986 | | - ){ |
| 117987 | | - /* Affinity mismatch on a generated column */ |
| 117988 | | - continue; |
| 117989 | | - } |
| 117990 | | - |
| 118482 | + if( indexedExprCompare(p, pExpr, iDataCur)!=0 ) continue; |
| 117991 | 118483 | |
| 117992 | 118484 | /* Functions that might set a subtype should not be replaced by the |
| 117993 | 118485 | ** value taken from an expression index if they are themselves an |
| 117994 | 118486 | ** argument to another scalar function or aggregate. |
| 117995 | 118487 | ** https://sqlite.org/forum/forumpost/68d284c86b082c3e */ |
| | @@ -117996,23 +118488,24 @@ |
| 117996 | 118488 | if( ExprHasProperty(pExpr, EP_SubtArg) |
| 117997 | 118489 | && sqlite3ExprCanReturnSubtype(pParse, pExpr) |
| 117998 | 118490 | ){ |
| 117999 | 118491 | continue; |
| 118000 | 118492 | } |
| 118493 | + |
| 118494 | + /* Do not read the value from an index if the expression is a constant. |
| 118495 | + ** This prevents an attempt to use an index value outside of the loop that |
| 118496 | + ** uses the index. (Bugs 2026-10-11T19:26:40Z and 2026-11-03T02:12:43Z). */ |
| 118497 | + if( sqlite3ExprIsConstant(pParse, pExpr) ){ |
| 118498 | + return -1; |
| 118499 | + } |
| 118001 | 118500 | |
| 118002 | 118501 | v = pParse->pVdbe; |
| 118003 | 118502 | assert( v!=0 ); |
| 118004 | | - if( p->bMaybeNullRow || sqlite3ExprIsConstant(pParse, pExpr) ){ |
| 118503 | + if( p->bMaybeNullRow ){ |
| 118005 | 118504 | /* The value of pExpr should be computed from the original expression, |
| 118006 | | - ** not from the index, if either: |
| 118007 | | - ** |
| 118008 | | - ** 1. The index might be on a NULL row due to an outer join. |
| 118009 | | - ** |
| 118010 | | - ** 2. The expression is a constant. This prevents an attempt to |
| 118011 | | - ** use an index value outside of the loop that positions the |
| 118012 | | - ** index. (Bug 2026-10-11T19:26:40Z). |
| 118013 | | - */ |
| 118505 | + ** not from the index, if the index might be on a NULL row due to an |
| 118506 | + ** outer join. */ |
| 118014 | 118507 | int addr = sqlite3VdbeCurrentAddr(v); |
| 118015 | 118508 | sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target); |
| 118016 | 118509 | VdbeCoverage(v); |
| 118017 | 118510 | sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); |
| 118018 | 118511 | VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); |
| | @@ -118047,13 +118540,17 @@ |
| 118047 | 118540 | for(p=pParse->pIdxPartExpr; p; p=p->pIENext){ |
| 118048 | 118541 | if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){ |
| 118049 | 118542 | Vdbe *v = pParse->pVdbe; |
| 118050 | 118543 | int addr = 0; |
| 118051 | 118544 | int ret; |
| 118545 | + u8 okConstFactor = pParse->okConstFactor; |
| 118052 | 118546 | |
| 118053 | 118547 | if( p->bMaybeNullRow ){ |
| 118548 | + /* OP_IfNullRow can overwrite the result register with NULL, so the |
| 118549 | + ** value must be recomputed each time, not factored out. */ |
| 118054 | 118550 | addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur); |
| 118551 | + pParse->okConstFactor = 0; |
| 118055 | 118552 | } |
| 118056 | 118553 | ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget); |
| 118057 | 118554 | sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0, |
| 118058 | 118555 | (const char*)&p->aff, 1); |
| 118059 | 118556 | if( sqlite3ExprCanReturnSubtype(pParse, p->pExpr) ){ |
| | @@ -118060,10 +118557,11 @@ |
| 118060 | 118557 | /* If the expression value may have a sub-type, clear it. Values |
| 118061 | 118558 | ** read from columns do not have subtypes. */ |
| 118062 | 118559 | sqlite3VdbeAddOp1(pParse->pVdbe, OP_ClrSubtype, ret); |
| 118063 | 118560 | } |
| 118064 | 118561 | if( addr ){ |
| 118562 | + pParse->okConstFactor = okConstFactor; |
| 118065 | 118563 | sqlite3VdbeJumpHere(v, addr); |
| 118066 | 118564 | sqlite3VdbeChangeP3(v, addr, ret); |
| 118067 | 118565 | } |
| 118068 | 118566 | return ret; |
| 118069 | 118567 | } |
| | @@ -118187,11 +118685,11 @@ |
| 118187 | 118685 | case TK_AGG_COLUMN: { |
| 118188 | 118686 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 118189 | 118687 | struct AggInfo_col *pCol; |
| 118190 | 118688 | assert( pAggInfo!=0 ); |
| 118191 | 118689 | assert( pExpr->iAgg>=0 ); |
| 118192 | | - if( pExpr->iAgg>=pAggInfo->nColumn ){ |
| 118690 | + if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ){ |
| 118193 | 118691 | /* Happens when the left table of a RIGHT JOIN is null and |
| 118194 | 118692 | ** is using an expression index */ |
| 118195 | 118693 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
| 118196 | 118694 | #ifdef SQLITE_VDBE_COVERAGE |
| 118197 | 118695 | /* Verify that the OP_Null above is exercised by tests |
| | @@ -118963,11 +119461,11 @@ |
| 118963 | 119461 | assert( pExpr->affExpr==OE_Rollback |
| 118964 | 119462 | || pExpr->affExpr==OE_Abort |
| 118965 | 119463 | || pExpr->affExpr==OE_Fail |
| 118966 | 119464 | || pExpr->affExpr==OE_Ignore |
| 118967 | 119465 | ); |
| 118968 | | - if( !pParse->pTriggerTab && !pParse->nested ){ |
| 119466 | + if( !pParse->pTriggerTab ){ |
| 118969 | 119467 | sqlite3ErrorMsg(pParse, |
| 118970 | 119468 | "RAISE() may only be used within a trigger-program"); |
| 118971 | 119469 | return 0; |
| 118972 | 119470 | } |
| 118973 | 119471 | if( pExpr->affExpr==OE_Abort ){ |
| | @@ -118977,12 +119475,11 @@ |
| 118977 | 119475 | if( pExpr->affExpr==OE_Ignore ){ |
| 118978 | 119476 | sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, OE_Ignore); |
| 118979 | 119477 | VdbeCoverage(v); |
| 118980 | 119478 | }else{ |
| 118981 | 119479 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 118982 | | - sqlite3VdbeAddOp3(v, OP_Halt, |
| 118983 | | - pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR, |
| 119480 | + sqlite3VdbeAddOp3(v, OP_Halt, SQLITE_CONSTRAINT_TRIGGER, |
| 118984 | 119481 | pExpr->affExpr, r1); |
| 118985 | 119482 | } |
| 118986 | 119483 | break; |
| 118987 | 119484 | } |
| 118988 | 119485 | #endif |
| | @@ -119736,14 +120233,22 @@ |
| 119736 | 120233 | if( pR ){ |
| 119737 | 120234 | iVar = pVar->iColumn; |
| 119738 | 120235 | sqlite3VdbeReprepareOnBind(pParse->pVdbe, iVar, 0); |
| 119739 | 120236 | pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); |
| 119740 | 120237 | if( pL ){ |
| 120238 | + int eType; |
| 119741 | 120239 | if( sqlite3_value_type(pL)==SQLITE_TEXT ){ |
| 119742 | 120240 | sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ |
| 119743 | 120241 | } |
| 119744 | | - res = sqlite3MemCompare(pL, pR, 0) ? 2 : 0; |
| 120242 | + /* The bound value and the literal must be interchangeable, not |
| 120243 | + ** just compare equal. For example, integer 1 and real 1.0 are equal |
| 120244 | + ** according to MemCompare(), but are different if TEXT affinity |
| 120245 | + ** is applied to them. */ |
| 120246 | + eType = sqlite3_value_type(pL); |
| 120247 | + if( eType==sqlite3_value_type(pR) && sqlite3MemCompare(pL, pR, 0)==0 ){ |
| 120248 | + res = 0; |
| 120249 | + } |
| 119745 | 120250 | } |
| 119746 | 120251 | sqlite3ValueFree(pR); |
| 119747 | 120252 | sqlite3ValueFree(pL); |
| 119748 | 120253 | } |
| 119749 | 120254 | return res; |
| | @@ -120629,14 +121134,24 @@ |
| 120629 | 121134 | IndexedExpr *pIEpr; |
| 120630 | 121135 | Expr tmp; |
| 120631 | 121136 | assert( pParse->iSelfTab==0 ); |
| 120632 | 121137 | if( (pNC->ncFlags & NC_InAggFunc)==0 ) break; |
| 120633 | 121138 | if( pParse->pIdxEpr==0 ) break; |
| 121139 | + if( pWalker->walkerDepth>0 ){ |
| 121140 | + break; /* See bug report 2026-11-07T18:33:11Z */ |
| 121141 | + } |
| 120634 | 121142 | for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){ |
| 120635 | 121143 | int iDataCur = pIEpr->iDataCur; |
| 120636 | 121144 | if( iDataCur<0 ) continue; |
| 120637 | | - if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break; |
| 121145 | + /* If the index might be on the NULL row of an outer join, then the |
| 121146 | + ** expression must not be replaced by a plain read of the index |
| 121147 | + ** column, as the value of the expression on the NULL row is not |
| 121148 | + ** necessarily NULL. In many cases (when the GROUP BY is not |
| 121149 | + ** using temp b-tree to sort), sqlite3IndexedExprLookup() will make |
| 121150 | + ** the same substitution later on anyway. */ |
| 121151 | + if( pIEpr->bMaybeNullRow ) continue; |
| 121152 | + if( indexedExprCompare(pIEpr, pExpr, iDataCur)==0 ) break; |
| 120638 | 121153 | } |
| 120639 | 121154 | if( pIEpr==0 ) break; |
| 120640 | 121155 | if( NEVER(!ExprUseYTab(pExpr)) ) break; |
| 120641 | 121156 | for(i=0; i<pSrcList->nSrc; i++){ |
| 120642 | 121157 | if( pSrcList->a[i].iCursor==pIEpr->iDataCur ){ |
| | @@ -120644,10 +121159,15 @@ |
| 120644 | 121159 | break; |
| 120645 | 121160 | } |
| 120646 | 121161 | } |
| 120647 | 121162 | if( i>=pSrcList->nSrc ) break; |
| 120648 | 121163 | if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */ |
| 121164 | + if( ExprHasProperty(pExpr, EP_SubtArg) |
| 121165 | + && sqlite3ExprCanReturnSubtype(pParse, pExpr) |
| 121166 | + ){ |
| 121167 | + break; |
| 121168 | + } |
| 120649 | 121169 | if( pParse->nErr ){ return WRC_Abort; } |
| 120650 | 121170 | |
| 120651 | 121171 | /* If we reach this point, it means that expression pExpr can be |
| 120652 | 121172 | ** translated into a reference to an index column as described by |
| 120653 | 121173 | ** pIEpr. |
| | @@ -121112,11 +121632,11 @@ |
| 121112 | 121632 | ** that the table is being renamed to. |
| 121113 | 121633 | */ |
| 121114 | 121634 | if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){ |
| 121115 | 121635 | goto exit_rename_table; |
| 121116 | 121636 | } |
| 121117 | | - if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){ |
| 121637 | + if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName,zDb) ){ |
| 121118 | 121638 | goto exit_rename_table; |
| 121119 | 121639 | } |
| 121120 | 121640 | |
| 121121 | 121641 | #ifndef SQLITE_OMIT_VIEW |
| 121122 | 121642 | if( IsView(pTab) ){ |
| | @@ -121157,35 +121677,35 @@ |
| 121157 | 121677 | /* figure out how many UTF-8 characters are in zName */ |
| 121158 | 121678 | zTabName = pTab->zName; |
| 121159 | 121679 | nTabName = sqlite3Utf8CharLen(zTabName, -1); |
| 121160 | 121680 | |
| 121161 | 121681 | /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in |
| 121162 | | - ** the schema to use the new table name. */ |
| 121682 | + ** the schema to use the new table name, and update the tbl_name and |
| 121683 | + ** name columns as required. */ |
| 121163 | 121684 | sqlite3NestedParse(pParse, |
| 121164 | 121685 | "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 121165 | | - "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) " |
| 121166 | | - "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)" |
| 121167 | | - "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 121168 | | - , zDb, zDb, zTabName, zName, (iDb==1), zTabName |
| 121169 | | - ); |
| 121170 | | - |
| 121171 | | - /* Update the tbl_name and name columns of the sqlite_schema table |
| 121172 | | - ** as required. */ |
| 121173 | | - sqlite3NestedParse(pParse, |
| 121174 | | - "UPDATE %Q." LEGACY_SCHEMA_TABLE " SET " |
| 121175 | | - "tbl_name = %Q, " |
| 121176 | | - "name = CASE " |
| 121177 | | - "WHEN type='table' THEN %Q " |
| 121178 | | - "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' " |
| 121179 | | - " AND type='index' THEN " |
| 121180 | | - "'sqlite_autoindex_' || %Q || substr(name,%d+18) " |
| 121181 | | - "ELSE name END " |
| 121182 | | - "WHERE tbl_name=%Q COLLATE nocase AND " |
| 121183 | | - "(type='table' OR type='index' OR type='trigger');", |
| 121184 | | - zDb, |
| 121185 | | - zName, zName, zName, |
| 121186 | | - nTabName, zTabName |
| 121686 | + "sql = CASE WHEN type='index' AND " |
| 121687 | + "name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' THEN sql " |
| 121688 | + "ELSE sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) END, " |
| 121689 | + "tbl_name = CASE " |
| 121690 | + "WHEN tbl_name=%Q COLLATE nocase AND (type!='trigger' OR %d=0 OR " |
| 121691 | + " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0)) " |
| 121692 | + "THEN %Q ELSE tbl_name END, " |
| 121693 | + "name = CASE " |
| 121694 | + "WHEN type='table' AND tbl_name=%Q COLLATE nocase THEN %Q " |
| 121695 | + "WHEN type='index' AND name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' " |
| 121696 | + "THEN 'sqlite_autoindex_' || %Q || substr(name,%d+18) " |
| 121697 | + "ELSE name END " |
| 121698 | + "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase) " |
| 121699 | + "AND (name NOT LIKE 'sqliteX_%%' ESCAPE 'X' " |
| 121700 | + "OR (type='index' AND name LIKE 'sqliteX_autoindex%%' ESCAPE 'X'))" |
| 121701 | + , zDb, |
| 121702 | + zDb, zTabName, zName, (iDb==1), |
| 121703 | + zTabName, (iDb==1), zDb, zName, |
| 121704 | + zTabName, zName, |
| 121705 | + zName, nTabName, |
| 121706 | + zTabName |
| 121187 | 121707 | ); |
| 121188 | 121708 | |
| 121189 | 121709 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 121190 | 121710 | /* If the sqlite_sequence table exists in this database, then update |
| 121191 | 121711 | ** it with the new table name. |
| | @@ -121242,11 +121762,11 @@ |
| 121242 | 121762 | const char *zDb, /* Schema holding the table */ |
| 121243 | 121763 | const char *zTab, /* Table to check for empty */ |
| 121244 | 121764 | const char *zErr /* Error message text */ |
| 121245 | 121765 | ){ |
| 121246 | 121766 | sqlite3NestedParse(pParse, |
| 121247 | | - "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", |
| 121767 | + "SELECT sqlite_fail(%Q, 1) FROM \"%w\".\"%w\"", |
| 121248 | 121768 | zErr, zDb, zTab |
| 121249 | 121769 | ); |
| 121250 | 121770 | } |
| 121251 | 121771 | |
| 121252 | 121772 | /* |
| | @@ -121270,10 +121790,55 @@ |
| 121270 | 121790 | } |
| 121271 | 121791 | return SQLITE_OK; |
| 121272 | 121792 | } |
| 121273 | 121793 | |
| 121274 | 121794 | |
| 121795 | + |
| 121796 | +/* |
| 121797 | +** Buffer pFrag, which is nFrag bytes in size, contains a fragment of |
| 121798 | +** SQL that will be inserted into a CREATE TABLE statement. If successful, this |
| 121799 | +** function returns the size of the buffer in bytes not including any trailing |
| 121800 | +** whitespace, semicolons, "--" style comments or unterminated C-style |
| 121801 | +** comments. Or, if an OOM occurs, it returns 0 and sets db->mallocFailed to |
| 121802 | +** true. |
| 121803 | +** |
| 121804 | +** Terminated C-style comments at the end are preserved. "--" style |
| 121805 | +** comments and unterminated C-style comments are removed because otherwise |
| 121806 | +** inserting pFrag[] into the middle of a larger SQL statement without a |
| 121807 | +** comment terminator will mess up its syntax. |
| 121808 | +*/ |
| 121809 | +static int alterRtrimFragment( |
| 121810 | + sqlite3 *db, /* used to record OOM error */ |
| 121811 | + const char *pFrag, /* Buffer containing SQL fragment */ |
| 121812 | + int nFrag /* Size of pFrag in bytes */ |
| 121813 | +){ |
| 121814 | + u8 *zTmp = (u8*)sqlite3MPrintf(db, "%.*s", nFrag, pFrag); |
| 121815 | + int iOff = 0; |
| 121816 | + int iEnd = 0; |
| 121817 | + |
| 121818 | + if( zTmp==0 ) return 0; |
| 121819 | + |
| 121820 | + while( 1 ){ |
| 121821 | + int t = 0; |
| 121822 | + int nToken = sqlite3GetToken(&zTmp[iOff], &t); |
| 121823 | + if( t==TK_ILLEGAL ) break; |
| 121824 | + if( t==TK_COMMENT ){ |
| 121825 | + /* Ignore all but properly terminated C-style comments */ |
| 121826 | + if( zTmp[iOff]=='/' && nToken>=4 |
| 121827 | + && zTmp[iOff+nToken-2]=='*' && zTmp[iOff+nToken-1]=='/' |
| 121828 | + ){ |
| 121829 | + iEnd = iOff+nToken; |
| 121830 | + } |
| 121831 | + }else if( t!=TK_SPACE && t!=TK_SEMI ){ |
| 121832 | + iEnd = iOff+nToken; |
| 121833 | + } |
| 121834 | + iOff += nToken; |
| 121835 | + } |
| 121836 | + |
| 121837 | + sqlite3DbFree(db, zTmp); |
| 121838 | + return iEnd; |
| 121839 | +} |
| 121275 | 121840 | |
| 121276 | 121841 | /* |
| 121277 | 121842 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 121278 | 121843 | ** has been parsed. Argument pColDef contains the text of the new |
| 121279 | 121844 | ** column definition. |
| | @@ -121286,10 +121851,11 @@ |
| 121286 | 121851 | Table *pTab; /* Table being altered */ |
| 121287 | 121852 | int iDb; /* Database number */ |
| 121288 | 121853 | const char *zDb; /* Database name */ |
| 121289 | 121854 | const char *zTab; /* Table name */ |
| 121290 | 121855 | char *zCol; /* Null-terminated column definition */ |
| 121856 | + int nCol; /* Size of zCol in bytes */ |
| 121291 | 121857 | Column *pCol; /* The new column */ |
| 121292 | 121858 | Expr *pDflt; /* Default value for the new column */ |
| 121293 | 121859 | sqlite3 *db; /* The database connection; */ |
| 121294 | 121860 | Vdbe *v; /* The prepared statement under construction */ |
| 121295 | 121861 | int r1; /* Temporary registers */ |
| | @@ -121331,32 +121897,16 @@ |
| 121331 | 121897 | "Cannot add a UNIQUE column"); |
| 121332 | 121898 | return; |
| 121333 | 121899 | } |
| 121334 | 121900 | if( isRowidAlias(pParse, pTab, pCol->zCnName, "add") ) return; |
| 121335 | 121901 | if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ |
| 121336 | | - /* If the default value for the new column was specified with a |
| 121337 | | - ** literal NULL, then set pDflt to 0. This simplifies checking |
| 121338 | | - ** for an SQL NULL default below. |
| 121339 | | - */ |
| 121340 | 121902 | assert( pDflt==0 || pDflt->op==TK_SPAN ); |
| 121341 | | - if( pDflt && pDflt->pLeft->op==TK_NULL ){ |
| 121342 | | - pDflt = 0; |
| 121343 | | - } |
| 121344 | | - assert( IsOrdinaryTable(pNew) ); |
| 121345 | | - if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){ |
| 121346 | | - sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 121347 | | - "Cannot add a REFERENCES column with non-NULL default value"); |
| 121348 | | - } |
| 121349 | | - if( pCol->notNull && !pDflt ){ |
| 121350 | | - sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 121351 | | - "Cannot add a NOT NULL column with default value NULL"); |
| 121352 | | - } |
| 121353 | | - |
| 121354 | | - |
| 121355 | | - /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 121356 | | - ** can handle (i.e. not CURRENT_TIME etc.) |
| 121357 | | - */ |
| 121903 | + |
| 121904 | + /* Ensure the default expression, if any, is something that |
| 121905 | + ** sqlite3ValueFromExpr() can handle (i.e. not CURRENT_TIME etc.). |
| 121906 | + ** If the default expression evaluates to NULL, then set pDflt to |
| 121907 | + ** 0. This simplifies the checks below. */ |
| 121358 | 121908 | if( pDflt ){ |
| 121359 | 121909 | sqlite3_value *pVal = 0; |
| 121360 | 121910 | int rc; |
| 121361 | 121911 | rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); |
| 121362 | 121912 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| | @@ -121365,25 +121915,35 @@ |
| 121365 | 121915 | return; |
| 121366 | 121916 | } |
| 121367 | 121917 | if( !pVal ){ |
| 121368 | 121918 | sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 121369 | 121919 | "Cannot add a column with non-constant default"); |
| 121920 | + }else if( sqlite3_value_type(pVal)==SQLITE_NULL ){ |
| 121921 | + pDflt = 0; |
| 121370 | 121922 | } |
| 121371 | 121923 | sqlite3ValueFree(pVal); |
| 121372 | 121924 | } |
| 121925 | + |
| 121926 | + assert( IsOrdinaryTable(pNew) ); |
| 121927 | + if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){ |
| 121928 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 121929 | + "Cannot add a REFERENCES column with non-NULL default value"); |
| 121930 | + } |
| 121931 | + if( pCol->notNull && !pDflt ){ |
| 121932 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 121933 | + "Cannot add a NOT NULL column with default value NULL"); |
| 121934 | + } |
| 121935 | + |
| 121373 | 121936 | }else if( pCol->colFlags & COLFLAG_STORED ){ |
| 121374 | 121937 | sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column"); |
| 121375 | 121938 | } |
| 121376 | 121939 | |
| 121377 | 121940 | |
| 121378 | 121941 | /* Modify the CREATE TABLE statement. */ |
| 121379 | | - zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
| 121942 | + nCol = alterRtrimFragment(db, (char*)pColDef->z, pColDef->n); |
| 121943 | + zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, nCol); |
| 121380 | 121944 | if( zCol ){ |
| 121381 | | - char *zEnd = &zCol[pColDef->n-1]; |
| 121382 | | - while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ |
| 121383 | | - *zEnd-- = '\0'; |
| 121384 | | - } |
| 121385 | 121945 | /* substr() operations on characters, but addColOffset is in bytes. So we |
| 121386 | 121946 | ** have to use printf() to translate between these units: */ |
| 121387 | 121947 | assert( IsOrdinaryTable(pTab) ); |
| 121388 | 121948 | assert( IsOrdinaryTable(pNew) ); |
| 121389 | 121949 | sqlite3NestedParse(pParse, |
| | @@ -121397,10 +121957,13 @@ |
| 121397 | 121957 | sqlite3DbFree(db, zCol); |
| 121398 | 121958 | } |
| 121399 | 121959 | |
| 121400 | 121960 | v = sqlite3GetVdbe(pParse); |
| 121401 | 121961 | if( v ){ |
| 121962 | + int isStrict = pTab->tabFlags & TF_Strict; |
| 121963 | + int isGen = (pCol->colFlags & COLFLAG_GENERATED)!=0; |
| 121964 | + |
| 121402 | 121965 | /* Make sure the schema version is at least 3. But do not upgrade |
| 121403 | 121966 | ** from less than 3 to 4, as that will corrupt any preexisting DESC |
| 121404 | 121967 | ** index. |
| 121405 | 121968 | */ |
| 121406 | 121969 | r1 = sqlite3GetTempReg(pParse); |
| | @@ -121413,28 +121976,46 @@ |
| 121413 | 121976 | sqlite3ReleaseTempReg(pParse, r1); |
| 121414 | 121977 | |
| 121415 | 121978 | /* Reload the table definition */ |
| 121416 | 121979 | renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd); |
| 121417 | 121980 | |
| 121418 | | - /* Verify that constraints are still satisfied */ |
| 121981 | + /* Verify that constraints are still satisfied. This requires a scan |
| 121982 | + ** of the entire table if: |
| 121983 | + ** |
| 121984 | + ** 1) the new column has a CHECK constraint, or |
| 121985 | + ** 2) the new column is a NOT NULL generated column, or |
| 121986 | + ** 3) the table is STRICT and the new column is a generated column |
| 121987 | + ** with a type other than ANY. |
| 121988 | + */ |
| 121419 | 121989 | if( pNew->pCheck!=0 |
| 121420 | | - || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0) |
| 121421 | | - || (pTab->tabFlags & TF_Strict)!=0 |
| 121990 | + || (pCol->notNull && isGen) |
| 121991 | + || (isStrict && isGen && pCol->eCType!=COLTYPE_ANY) |
| 121422 | 121992 | ){ |
| 121423 | 121993 | pParse->colNamesSet = 1; |
| 121424 | 121994 | sqlite3NestedParse(pParse, |
| 121425 | 121995 | "SELECT CASE WHEN quick_check GLOB 'CHECK*'" |
| 121426 | | - " THEN raise(ABORT,'CHECK constraint failed')" |
| 121996 | + " THEN sqlite_fail('CHECK constraint failed', 1)" |
| 121427 | 121997 | " WHEN quick_check GLOB 'non-* value in*'" |
| 121428 | | - " THEN raise(ABORT,'type mismatch on DEFAULT')" |
| 121429 | | - " ELSE raise(ABORT,'NOT NULL constraint failed')" |
| 121998 | + " THEN sqlite_fail('type mismatch on DEFAULT', 1)" |
| 121999 | + " ELSE sqlite_fail('NOT NULL constraint failed', 1)" |
| 121430 | 122000 | " END" |
| 121431 | 122001 | " FROM pragma_quick_check(%Q,%Q)" |
| 121432 | 122002 | " WHERE quick_check GLOB 'CHECK*'" |
| 121433 | 122003 | " OR quick_check GLOB 'NULL*'" |
| 121434 | 122004 | " OR quick_check GLOB 'non-* value in*'", |
| 121435 | 122005 | zTab, zDb |
| 122006 | + ); |
| 122007 | + }else if( pDflt && isStrict && pCol->eCType>COLTYPE_ANY ){ |
| 122008 | + static const char *azTypeof[] = { |
| 122009 | + "any", "blob", "integer", "integer", "real", "text" |
| 122010 | + }; |
| 122011 | + assert( pCol->eCType<=ArraySize(azTypeof) ); |
| 122012 | + pParse->colNamesSet = 1; |
| 122013 | + sqlite3NestedParse(pParse, |
| 122014 | + "SELECT sqlite_fail('type mismatch on DEFAULT', 1)" |
| 122015 | + " WHERE sqlite_check_type(%Q, %Q, %Q, %Q)", |
| 122016 | + zDb, zTab, pCol->zCnName, azTypeof[pCol->eCType-1] |
| 121436 | 122017 | ); |
| 121437 | 122018 | } |
| 121438 | 122019 | } |
| 121439 | 122020 | } |
| 121440 | 122021 | |
| | @@ -122348,10 +122929,11 @@ |
| 122348 | 122929 | SrcList *pSrc = sqlite3SrcListDup(db, pStep->pSrc, 0); |
| 122349 | 122930 | if( pSrc ){ |
| 122350 | 122931 | Select *pSel = sqlite3SelectNew( |
| 122351 | 122932 | pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0 |
| 122352 | 122933 | ); |
| 122934 | + Upsert *pUpsert = 0; |
| 122353 | 122935 | if( pSel==0 ){ |
| 122354 | 122936 | pStep->pExprList = 0; |
| 122355 | 122937 | pSrc = 0; |
| 122356 | 122938 | rc = SQLITE_NOMEM; |
| 122357 | 122939 | }else{ |
| | @@ -122371,34 +122953,39 @@ |
| 122371 | 122953 | assert( pSrc==pSel->pSrc ); |
| 122372 | 122954 | if( pStep->pExprList ) pSel->pEList = 0; |
| 122373 | 122955 | pSel->pSrc = 0; |
| 122374 | 122956 | sqlite3SelectDelete(db, pSel); |
| 122375 | 122957 | } |
| 122958 | + |
| 122959 | + sNC.pSrcList = pSrc; |
| 122376 | 122960 | if( ALWAYS(pStep->pSrc) ){ |
| 122377 | 122961 | int i; |
| 122378 | 122962 | for(i=0; i<pStep->pSrc->nSrc && rc==SQLITE_OK; i++){ |
| 122379 | 122963 | SrcItem *p = &pStep->pSrc->a[i]; |
| 122380 | 122964 | if( p->fg.isSubquery ){ |
| 122381 | 122965 | assert( p->u4.pSubq!=0 ); |
| 122382 | 122966 | sqlite3SelectPrep(pParse, p->u4.pSubq->pSelect, 0); |
| 122383 | 122967 | } |
| 122968 | + if( p->fg.isUsing==0 ){ |
| 122969 | + rc = sqlite3ResolveExprNames(&sNC, p->u3.pOn); |
| 122970 | + } |
| 122384 | 122971 | } |
| 122385 | 122972 | } |
| 122386 | | - |
| 122387 | | - if( db->mallocFailed ){ |
| 122973 | + if( db->mallocFailed ){ |
| 122388 | 122974 | rc = SQLITE_NOMEM; |
| 122389 | 122975 | } |
| 122390 | | - sNC.pSrcList = pSrc; |
| 122391 | 122976 | if( rc==SQLITE_OK && pStep->pWhere ){ |
| 122392 | 122977 | rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere); |
| 122393 | 122978 | } |
| 122394 | 122979 | if( rc==SQLITE_OK ){ |
| 122395 | 122980 | rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList); |
| 122396 | 122981 | } |
| 122397 | 122982 | assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) ); |
| 122398 | | - if( pStep->pUpsert && rc==SQLITE_OK ){ |
| 122399 | | - Upsert *pUpsert = pStep->pUpsert; |
| 122983 | + for(pUpsert=pStep->pUpsert; |
| 122984 | + pUpsert && rc==SQLITE_OK; |
| 122985 | + pUpsert=pUpsert->pNextUpsert |
| 122986 | + ){ |
| 122400 | 122987 | pUpsert->pUpsertSrc = pSrc; |
| 122401 | 122988 | sNC.uNC.pUpsert = pUpsert; |
| 122402 | 122989 | sNC.ncFlags = NC_UUpsert; |
| 122403 | 122990 | rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); |
| 122404 | 122991 | if( rc==SQLITE_OK ){ |
| | @@ -122433,27 +123020,31 @@ |
| 122433 | 123020 | /* Find tokens to edit in WHEN clause */ |
| 122434 | 123021 | sqlite3WalkExpr(pWalker, pTrigger->pWhen); |
| 122435 | 123022 | |
| 122436 | 123023 | /* Find tokens to edit in trigger steps */ |
| 122437 | 123024 | for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){ |
| 123025 | + Upsert *pUpsert = 0; |
| 122438 | 123026 | sqlite3WalkSelect(pWalker, pStep->pSelect); |
| 122439 | 123027 | sqlite3WalkExpr(pWalker, pStep->pWhere); |
| 122440 | 123028 | sqlite3WalkExprList(pWalker, pStep->pExprList); |
| 122441 | | - if( pStep->pUpsert ){ |
| 122442 | | - Upsert *pUpsert = pStep->pUpsert; |
| 123029 | + for(pUpsert=pStep->pUpsert; pUpsert; pUpsert=pUpsert->pNextUpsert){ |
| 122443 | 123030 | sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget); |
| 122444 | 123031 | sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet); |
| 122445 | 123032 | sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere); |
| 122446 | 123033 | sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere); |
| 122447 | 123034 | } |
| 122448 | 123035 | if( pStep->pSrc ){ |
| 122449 | 123036 | int i; |
| 122450 | 123037 | SrcList *pSrc = pStep->pSrc; |
| 122451 | 123038 | for(i=0; i<pSrc->nSrc; i++){ |
| 122452 | | - if( pSrc->a[i].fg.isSubquery ){ |
| 122453 | | - assert( pSrc->a[i].u4.pSubq!=0 ); |
| 122454 | | - sqlite3WalkSelect(pWalker, pSrc->a[i].u4.pSubq->pSelect); |
| 123039 | + SrcItem *p = &pSrc->a[i]; |
| 123040 | + if( p->fg.isSubquery ){ |
| 123041 | + assert( p->u4.pSubq!=0 ); |
| 123042 | + sqlite3WalkSelect(pWalker, p->u4.pSubq->pSelect); |
| 123043 | + } |
| 123044 | + if( p->fg.isUsing==0 ){ |
| 123045 | + sqlite3WalkExpr(pWalker, p->u3.pOn); |
| 122455 | 123046 | } |
| 122456 | 123047 | } |
| 122457 | 123048 | } |
| 122458 | 123049 | } |
| 122459 | 123050 | } |
| | @@ -122539,11 +123130,11 @@ |
| 122539 | 123130 | sqlite3BtreeLeaveAll(db); |
| 122540 | 123131 | return; |
| 122541 | 123132 | } |
| 122542 | 123133 | zOld = pTab->aCol[iCol].zCnName; |
| 122543 | 123134 | memset(&sCtx, 0, sizeof(sCtx)); |
| 122544 | | - sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol); |
| 123135 | + sCtx.iCol = iCol; |
| 122545 | 123136 | |
| 122546 | 123137 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 122547 | 123138 | db->xAuth = 0; |
| 122548 | 123139 | #endif |
| 122549 | 123140 | rc = renameParseSql(&sParse, zDb, db, zSql, bTemp); |
| | @@ -122577,11 +123168,11 @@ |
| 122577 | 123168 | if( iCol<sParse.pNewTable->nCol ){ |
| 122578 | 123169 | renameTokenFind( |
| 122579 | 123170 | &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName |
| 122580 | 123171 | ); |
| 122581 | 123172 | } |
| 122582 | | - if( sCtx.iCol<0 ){ |
| 123173 | + if( iCol==sParse.pNewTable->iPKey ){ |
| 122583 | 123174 | renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey); |
| 122584 | 123175 | } |
| 122585 | 123176 | sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck); |
| 122586 | 123177 | for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 122587 | 123178 | sqlite3WalkExprList(&sWalker, pIdx->aColExpr); |
| | @@ -122623,13 +123214,13 @@ |
| 122623 | 123214 | |
| 122624 | 123215 | for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){ |
| 122625 | 123216 | if( pStep->pSrc ){ |
| 122626 | 123217 | Table *pTarget = sqlite3LocateTableItem(&sParse, 0, &pStep->pSrc->a[0]); |
| 122627 | 123218 | if( pTarget==pTab ){ |
| 122628 | | - if( pStep->pUpsert ){ |
| 122629 | | - ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet; |
| 122630 | | - renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld); |
| 123219 | + Upsert *pUpsert = 0; |
| 123220 | + for(pUpsert=pStep->pUpsert; pUpsert; pUpsert=pUpsert->pNextUpsert){ |
| 123221 | + renameColumnElistNames(&sParse,&sCtx,pUpsert->pUpsertSet,zOld); |
| 122631 | 123222 | } |
| 122632 | 123223 | renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld); |
| 122633 | 123224 | renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld); |
| 122634 | 123225 | } |
| 122635 | 123226 | } |
| | @@ -122705,10 +123296,30 @@ |
| 122705 | 123296 | renameWalkWith(pWalker, pSelect); |
| 122706 | 123297 | |
| 122707 | 123298 | return WRC_Continue; |
| 122708 | 123299 | } |
| 122709 | 123300 | |
| 123301 | + |
| 123302 | +#ifndef SQLITE_OMIT_TRIGGER |
| 123303 | +/* |
| 123304 | +** Return the table that SrcItem pItem, which is part of the pSrc list of |
| 123305 | +** an INSERT, UPDATE or DELETE trigger step (the target table, or a table |
| 123306 | +** from the FROM clause of an UPDATE ... FROM), refers to. Or NULL if it |
| 123307 | +** does not refer to any existing table. |
| 123308 | +*/ |
| 123309 | +static Table *renameTriggerSrcTab(sqlite3 *db, SrcItem *pItem){ |
| 123310 | + const char *zDb; |
| 123311 | + assert( pItem->fg.isSubquery==0 ); |
| 123312 | + if( pItem->fg.fixedSchema ){ |
| 123313 | + int iDb = sqlite3SchemaToIndex(db, pItem->u4.pSchema); |
| 123314 | + zDb = db->aDb[iDb].zDbSName; |
| 123315 | + }else{ |
| 123316 | + zDb = pItem->u4.zDatabase; |
| 123317 | + } |
| 123318 | + return sqlite3FindTable(db, pItem->zName, zDb); |
| 123319 | +} |
| 123320 | +#endif |
| 122710 | 123321 | |
| 122711 | 123322 | /* |
| 122712 | 123323 | ** This C function implements an SQL user function that is used by SQL code |
| 122713 | 123324 | ** generated by the ALTER TABLE ... RENAME command to modify the definition |
| 122714 | 123325 | ** of any foreign key constraints that use the table being renamed as the |
| | @@ -122824,12 +123435,12 @@ |
| 122824 | 123435 | #ifndef SQLITE_OMIT_TRIGGER |
| 122825 | 123436 | else{ |
| 122826 | 123437 | Trigger *pTrigger = sParse.pNewTrigger; |
| 122827 | 123438 | TriggerStep *pStep; |
| 122828 | 123439 | if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) |
| 122829 | | - && sCtx.pTab->pSchema==pTrigger->pTabSchema |
| 122830 | | - ){ |
| 123440 | + && sCtx.pTab->pSchema==pTrigger->pTabSchema |
| 123441 | + ){ |
| 122831 | 123442 | renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table); |
| 122832 | 123443 | } |
| 122833 | 123444 | |
| 122834 | 123445 | if( isLegacy==0 ){ |
| 122835 | 123446 | rc = renameResolveTrigger(&sParse); |
| | @@ -122837,13 +123448,13 @@ |
| 122837 | 123448 | renameWalkTrigger(&sWalker, pTrigger); |
| 122838 | 123449 | for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){ |
| 122839 | 123450 | if( pStep->pSrc ){ |
| 122840 | 123451 | int i; |
| 122841 | 123452 | for(i=0; i<pStep->pSrc->nSrc; i++){ |
| 122842 | | - SrcItem *pItem = &pStep->pSrc->a[i]; |
| 122843 | | - if( 0==sqlite3_stricmp(pItem->zName, zOld) ){ |
| 122844 | | - renameTokenFind(&sParse, &sCtx, pItem->zName); |
| 123453 | + SrcItem *p = &pStep->pSrc->a[i]; |
| 123454 | + if( p->zName && renameTriggerSrcTab(db, p)==sCtx.pTab ){ |
| 123455 | + renameTokenFind(&sParse, &sCtx, p->zName); |
| 122845 | 123456 | } |
| 122846 | 123457 | } |
| 122847 | 123458 | } |
| 122848 | 123459 | } |
| 122849 | 123460 | } |
| | @@ -123294,75 +123905,78 @@ |
| 123294 | 123905 | /* Drop and reload the database schema. */ |
| 123295 | 123906 | renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop); |
| 123296 | 123907 | renameTestSchema(pParse, zDb, iDb==1, "after drop column", 1); |
| 123297 | 123908 | |
| 123298 | 123909 | /* Edit rows of table on disk */ |
| 123299 | | - if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){ |
| 123300 | | - int i; |
| 123301 | | - int addr; |
| 123302 | | - int reg; |
| 123303 | | - int regRec; |
| 123304 | | - Index *pPk = 0; |
| 123305 | | - int nField = 0; /* Number of non-virtual columns after drop */ |
| 123306 | | - int iCur; |
| 123910 | + if( pParse->nErr==0 ){ |
| 123307 | 123911 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 123308 | | - iCur = pParse->nTab++; |
| 123309 | | - sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); |
| 123310 | | - addr = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
| 123311 | | - reg = ++pParse->nMem; |
| 123312 | | - if( HasRowid(pTab) ){ |
| 123313 | | - sqlite3VdbeAddOp2(v, OP_Rowid, iCur, reg); |
| 123314 | | - pParse->nMem += pTab->nCol; |
| 123315 | | - }else{ |
| 123316 | | - pPk = sqlite3PrimaryKeyIndex(pTab); |
| 123317 | | - pParse->nMem += pPk->nColumn; |
| 123318 | | - for(i=0; i<pPk->nKeyCol; i++){ |
| 123319 | | - sqlite3VdbeAddOp3(v, OP_Column, iCur, i, reg+i+1); |
| 123320 | | - } |
| 123321 | | - nField = pPk->nKeyCol; |
| 123322 | | - } |
| 123323 | | - regRec = ++pParse->nMem; |
| 123324 | | - for(i=0; i<pTab->nCol; i++){ |
| 123325 | | - if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ |
| 123326 | | - int regOut; |
| 123327 | | - if( pPk ){ |
| 123328 | | - int iPos = sqlite3TableColumnToIndex(pPk, i); |
| 123329 | | - int iColPos = sqlite3TableColumnToIndex(pPk, iCol); |
| 123330 | | - if( iPos<pPk->nKeyCol ) continue; |
| 123331 | | - regOut = reg+1+iPos-(iPos>iColPos); |
| 123332 | | - }else{ |
| 123333 | | - regOut = reg+1+nField; |
| 123334 | | - } |
| 123335 | | - if( i==pTab->iPKey ){ |
| 123336 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, regOut); |
| 123337 | | - }else{ |
| 123338 | | - sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut); |
| 123339 | | - assert( pParse->nErr |
| 123340 | | - || sqlite3VdbeGetOp(v,sqlite3VdbeCurrentAddr(v)-1)->opcode |
| 123341 | | - != OP_RealAffinity |
| 123342 | | - || pTab->aCol[i].affinity==SQLITE_AFF_REAL ); |
| 123343 | | - sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity); |
| 123344 | | - } |
| 123345 | | - nField++; |
| 123346 | | - } |
| 123347 | | - } |
| 123348 | | - if( nField==0 ){ |
| 123349 | | - /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */ |
| 123350 | | - pParse->nMem++; |
| 123351 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, reg+1); |
| 123352 | | - nField = 1; |
| 123353 | | - } |
| 123354 | | - sqlite3VdbeAddOp3(v, OP_MakeRecord, reg+1, nField, regRec); |
| 123355 | | - if( pPk ){ |
| 123356 | | - sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol); |
| 123357 | | - }else{ |
| 123358 | | - sqlite3VdbeAddOp3(v, OP_Insert, iCur, regRec, reg); |
| 123359 | | - } |
| 123360 | | - sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION); |
| 123361 | | - |
| 123362 | | - sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+1); VdbeCoverage(v); |
| 123363 | | - sqlite3VdbeJumpHere(v, addr); |
| 123912 | + sqlite3VdbeAddOp0(v, OP_DbLock); |
| 123913 | + if( (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){ |
| 123914 | + int i; |
| 123915 | + int addr; |
| 123916 | + int reg; |
| 123917 | + int regRec; |
| 123918 | + Index *pPk = 0; |
| 123919 | + int nField = 0; /* Number of non-virtual columns after drop */ |
| 123920 | + int iCur; |
| 123921 | + iCur = pParse->nTab++; |
| 123922 | + sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); |
| 123923 | + addr = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
| 123924 | + reg = ++pParse->nMem; |
| 123925 | + if( HasRowid(pTab) ){ |
| 123926 | + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, reg); |
| 123927 | + pParse->nMem += pTab->nCol; |
| 123928 | + }else{ |
| 123929 | + pPk = sqlite3PrimaryKeyIndex(pTab); |
| 123930 | + pParse->nMem += pPk->nColumn; |
| 123931 | + for(i=0; i<pPk->nKeyCol; i++){ |
| 123932 | + sqlite3VdbeAddOp3(v, OP_Column, iCur, i, reg+i+1); |
| 123933 | + } |
| 123934 | + nField = pPk->nKeyCol; |
| 123935 | + } |
| 123936 | + regRec = ++pParse->nMem; |
| 123937 | + for(i=0; i<pTab->nCol; i++){ |
| 123938 | + if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ |
| 123939 | + int regOut; |
| 123940 | + if( pPk ){ |
| 123941 | + int iPos = sqlite3TableColumnToIndex(pPk, i); |
| 123942 | + int iColPos = sqlite3TableColumnToIndex(pPk, iCol); |
| 123943 | + if( iPos<pPk->nKeyCol ) continue; |
| 123944 | + regOut = reg+1+iPos-(iPos>iColPos); |
| 123945 | + }else{ |
| 123946 | + regOut = reg+1+nField; |
| 123947 | + } |
| 123948 | + if( i==pTab->iPKey ){ |
| 123949 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regOut); |
| 123950 | + }else{ |
| 123951 | + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut); |
| 123952 | + assert( pParse->nErr |
| 123953 | + || sqlite3VdbeGetOp(v,sqlite3VdbeCurrentAddr(v)-1)->opcode |
| 123954 | + != OP_RealAffinity |
| 123955 | + || pTab->aCol[i].affinity==SQLITE_AFF_REAL ); |
| 123956 | + sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity); |
| 123957 | + } |
| 123958 | + nField++; |
| 123959 | + } |
| 123960 | + } |
| 123961 | + if( nField==0 ){ |
| 123962 | + /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */ |
| 123963 | + pParse->nMem++; |
| 123964 | + sqlite3VdbeAddOp2(v, OP_Null, 0, reg+1); |
| 123965 | + nField = 1; |
| 123966 | + } |
| 123967 | + sqlite3VdbeAddOp3(v, OP_MakeRecord, reg+1, nField, regRec); |
| 123968 | + if( pPk ){ |
| 123969 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1,pPk->nKeyCol); |
| 123970 | + }else{ |
| 123971 | + sqlite3VdbeAddOp3(v, OP_Insert, iCur, regRec, reg); |
| 123972 | + } |
| 123973 | + sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION); |
| 123974 | + |
| 123975 | + sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+1); VdbeCoverage(v); |
| 123976 | + sqlite3VdbeJumpHere(v, addr); |
| 123977 | + } |
| 123364 | 123978 | } |
| 123365 | 123979 | |
| 123366 | 123980 | exit_drop_column: |
| 123367 | 123981 | sqlite3DbFree(db, zCol); |
| 123368 | 123982 | sqlite3SrcListDelete(db, pSrc); |
| | @@ -123864,48 +124478,10 @@ |
| 123864 | 124478 | (void)NotUsed; |
| 123865 | 124479 | sqlite3_result_error(ctx, zText, -1); |
| 123866 | 124480 | sqlite3_result_error_code(ctx, err); |
| 123867 | 124481 | } |
| 123868 | 124482 | |
| 123869 | | -/* |
| 123870 | | -** Buffer pCons, which is nCons bytes in size, contains the text of a |
| 123871 | | -** NOT NULL or CHECK constraint that will be inserted into a CREATE TABLE |
| 123872 | | -** statement. If successful, this function returns the size of the buffer in |
| 123873 | | -** bytes not including any trailing whitespace or "--" style comments. Or, |
| 123874 | | -** if an OOM occurs, it returns 0 and sets db->mallocFailed to true. |
| 123875 | | -** |
| 123876 | | -** C-style comments at the end are preserved. "--" style comments are |
| 123877 | | -** removed because the comment terminator might be \000, and we are about |
| 123878 | | -** to insert the pCons[] text into the middle of a larger string, and that |
| 123879 | | -** will have the effect of removing the comment terminator and messing up |
| 123880 | | -** the syntax. |
| 123881 | | -*/ |
| 123882 | | -static int alterRtrimConstraint( |
| 123883 | | - sqlite3 *db, /* used to record OOM error */ |
| 123884 | | - const char *pCons, /* Buffer containing constraint */ |
| 123885 | | - int nCons /* Size of pCons in bytes */ |
| 123886 | | -){ |
| 123887 | | - u8 *zTmp = (u8*)sqlite3MPrintf(db, "%.*s", nCons, pCons); |
| 123888 | | - int iOff = 0; |
| 123889 | | - int iEnd = 0; |
| 123890 | | - |
| 123891 | | - if( zTmp==0 ) return 0; |
| 123892 | | - |
| 123893 | | - while( 1 ){ |
| 123894 | | - int t = 0; |
| 123895 | | - int nToken = sqlite3GetToken(&zTmp[iOff], &t); |
| 123896 | | - if( t==TK_ILLEGAL ) break; |
| 123897 | | - if( t!=TK_SPACE && (t!=TK_COMMENT || zTmp[iOff]!='-') ){ |
| 123898 | | - iEnd = iOff+nToken; |
| 123899 | | - } |
| 123900 | | - iOff += nToken; |
| 123901 | | - } |
| 123902 | | - |
| 123903 | | - sqlite3DbFree(db, zTmp); |
| 123904 | | - return iEnd; |
| 123905 | | -} |
| 123906 | | - |
| 123907 | 124483 | /* |
| 123908 | 124484 | ** Prepare a statement of the form: |
| 123909 | 124485 | ** |
| 123910 | 124486 | ** ALTER TABLE pSrc ALTER pCol SET NOT NULL |
| 123911 | 124487 | */ |
| | @@ -123932,11 +124508,11 @@ |
| 123932 | 124508 | return; |
| 123933 | 124509 | } |
| 123934 | 124510 | |
| 123935 | 124511 | /* Find the length in bytes of the constraint definition */ |
| 123936 | 124512 | pCons = pFirst->z; |
| 123937 | | - nCons = alterRtrimConstraint(pParse->db, pCons, pParse->sLastToken.z - pCons); |
| 124513 | + nCons = alterRtrimFragment(pParse->db, pCons, pParse->sLastToken.z - pCons); |
| 123938 | 124514 | |
| 123939 | 124515 | /* Search for a constraint violation. Throw an exception if one is found. */ |
| 123940 | 124516 | sqlite3NestedParse(pParse, |
| 123941 | 124517 | "SELECT sqlite_fail('constraint failed', %d) " |
| 123942 | 124518 | "FROM %Q.%Q AS x WHERE x.%.*s IS NULL", |
| | @@ -124064,11 +124640,11 @@ |
| 124064 | 124640 | SQLITE_CONSTRAINT, zDb, pTab->zName, nExpr, zExpr |
| 124065 | 124641 | ); |
| 124066 | 124642 | |
| 124067 | 124643 | /* Edit the SQL for the named table. */ |
| 124068 | 124644 | pCons = pFirst->z; |
| 124069 | | - nCons = alterRtrimConstraint(pParse->db, pCons, pParse->sLastToken.z - pCons); |
| 124645 | + nCons = alterRtrimFragment(pParse->db, pCons, pParse->sLastToken.z - pCons); |
| 124070 | 124646 | |
| 124071 | 124647 | sqlite3NestedParse(pParse, |
| 124072 | 124648 | "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 124073 | 124649 | "sql = sqlite_add_constraint(sql, %.*Q, -1) " |
| 124074 | 124650 | "WHERE type='table' AND tbl_name=%Q COLLATE nocase" |
| | @@ -124076,25 +124652,83 @@ |
| 124076 | 124652 | ); |
| 124077 | 124653 | |
| 124078 | 124654 | /* Finally, reload the database schema. */ |
| 124079 | 124655 | renameReloadSchema(pParse, iDb, INITFLAG_AlterDropCons); |
| 124080 | 124656 | } |
| 124657 | + |
| 124658 | +/* |
| 124659 | +** The implementation of internal SQL function: |
| 124660 | +** |
| 124661 | +** sqlite_check_type(DB, TAB, COL, TYPE) |
| 124662 | +** |
| 124663 | +** Read the value of column COL from the first row of table DB.TAB. Return |
| 124664 | +** 1 if the table contains at least one row and typeof() of the value read |
| 124665 | +** is not TYPE. Otherwise, return NULL. |
| 124666 | +*/ |
| 124667 | +static void checkTypeFunc( |
| 124668 | + sqlite3_context *ctx, |
| 124669 | + int NotUsed, |
| 124670 | + sqlite3_value **argv |
| 124671 | +){ |
| 124672 | + sqlite3 *db = sqlite3_context_db_handle(ctx); |
| 124673 | + const char *zDb = (const char*)sqlite3_value_text(argv[0]); |
| 124674 | + const char *zTab = (const char*)sqlite3_value_text(argv[1]); |
| 124675 | + const char *zCol = (const char*)sqlite3_value_text(argv[2]); |
| 124676 | + const char *zType = (const char*)sqlite3_value_text(argv[3]); |
| 124677 | + sqlite3_stmt *pStmt = 0; |
| 124678 | + char *zSql; |
| 124679 | + int rc; |
| 124680 | + |
| 124681 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 124682 | + sqlite3_xauth xAuth = db->xAuth; |
| 124683 | + db->xAuth = 0; |
| 124684 | +#endif |
| 124685 | + |
| 124686 | + (void)NotUsed; |
| 124687 | + if( zDb==0 || zTab==0 || zCol==0 || zType==0 ) return; |
| 124688 | + zSql = sqlite3MPrintf(db, |
| 124689 | + "SELECT typeof(\"%w\") FROM \"%w\".\"%w\" LIMIT 1", zCol, zDb, zTab |
| 124690 | + ); |
| 124691 | + if( zSql==0 ){ |
| 124692 | + sqlite3_result_error_nomem(ctx); |
| 124693 | + return; |
| 124694 | + } |
| 124695 | + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 124696 | + sqlite3DbFree(db, zSql); |
| 124697 | + if( rc==SQLITE_OK ){ |
| 124698 | + if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 124699 | + const char *zVal = (const char*)sqlite3_column_text(pStmt, 0); |
| 124700 | + if( sqlite3_stricmp(zVal, zType)!=0 ){ |
| 124701 | + sqlite3_result_int(ctx, 1); |
| 124702 | + } |
| 124703 | + } |
| 124704 | + rc = sqlite3_finalize(pStmt); |
| 124705 | + } |
| 124706 | + if( rc!=SQLITE_OK ){ |
| 124707 | + sqlite3_result_error(ctx, sqlite3_errmsg(db), -1); |
| 124708 | + sqlite3_result_error_code(ctx, rc); |
| 124709 | + } |
| 124710 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 124711 | + db->xAuth = xAuth; |
| 124712 | +#endif |
| 124713 | +} |
| 124081 | 124714 | |
| 124082 | 124715 | /* |
| 124083 | 124716 | ** Register built-in functions used to help implement ALTER TABLE |
| 124084 | 124717 | */ |
| 124085 | 124718 | SQLITE_PRIVATE void sqlite3AlterFunctions(void){ |
| 124086 | 124719 | static FuncDef aAlterTableFuncs[] = { |
| 124720 | + INTERNAL_FUNCTION(sqlite_check_type, 4, checkTypeFunc), |
| 124087 | 124721 | INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc), |
| 124088 | 124722 | INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc), |
| 124089 | 124723 | INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest), |
| 124090 | 124724 | INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc), |
| 124091 | 124725 | INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc), |
| 124092 | 124726 | INTERNAL_FUNCTION(sqlite_drop_constraint,2, dropConstraintFunc), |
| 124093 | 124727 | INTERNAL_FUNCTION(sqlite_fail, 2, failConstraintFunc), |
| 124094 | 124728 | INTERNAL_FUNCTION(sqlite_add_constraint, 3, addConstraintFunc), |
| 124095 | | - INTERNAL_FUNCTION(sqlite_find_constraint,2, findConstraintFunc), |
| 124729 | + INTERNAL_FUNCTION(sqlite_find_constraint,2, findConstraintFunc) |
| 124096 | 124730 | }; |
| 124097 | 124731 | sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); |
| 124098 | 124732 | } |
| 124099 | 124733 | #endif /* SQLITE_ALTER_TABLE */ |
| 124100 | 124734 | |
| | @@ -127172,10 +127806,20 @@ |
| 127172 | 127806 | for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; |
| 127173 | 127807 | return 1; |
| 127174 | 127808 | } |
| 127175 | 127809 | #endif |
| 127176 | 127810 | |
| 127811 | +/* |
| 127812 | +** Do the union all two yDbMask objects into the first. |
| 127813 | +*/ |
| 127814 | +#if SQLITE_MAX_ATTACHED>30 |
| 127815 | +SQLITE_PRIVATE void sqlite3DbMaskUnion(yDbMask m, yDbMask x){ |
| 127816 | + int i; |
| 127817 | + for(i=0; i<sizeof(yDbMask); i++){ m[i] |= x[i]; } |
| 127818 | +} |
| 127819 | +#endif |
| 127820 | + |
| 127177 | 127821 | /* |
| 127178 | 127822 | ** This routine is called after a single SQL statement has been |
| 127179 | 127823 | ** parsed and a VDBE program to execute that statement has been |
| 127180 | 127824 | ** prepared. This routine puts the finishing touches on the |
| 127181 | 127825 | ** VDBE program and resets the pParse structure for the next |
| | @@ -127664,27 +128308,37 @@ |
| 127664 | 128308 | } |
| 127665 | 128309 | } |
| 127666 | 128310 | |
| 127667 | 128311 | /* |
| 127668 | 128312 | ** Reset the schema for the database at index iDb. Also reset the |
| 127669 | | -** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. |
| 127670 | | -** Deferred resets may be run by calling with iDb<0. |
| 128313 | +** TEMP schema. The reset is deferred if db->nSchemaLock is not zero |
| 128314 | +** or if an active VDBE other than pSafe is using iDb |
| 128315 | +** |
| 128316 | +** Deferred schema resets may be retried by calling with iDb<0. |
| 127671 | 128317 | */ |
| 127672 | | -SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ |
| 128318 | +SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb, Vdbe *pSafe){ |
| 127673 | 128319 | int i; |
| 127674 | 128320 | assert( iDb<db->nDb ); |
| 127675 | 128321 | |
| 127676 | 128322 | if( iDb>=0 ){ |
| 127677 | 128323 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 127678 | 128324 | DbSetProperty(db, iDb, DB_ResetWanted); |
| 127679 | 128325 | DbSetProperty(db, 1, DB_ResetWanted); |
| 127680 | 128326 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
| 128327 | + db->bPendingSchemaReset = 1; |
| 127681 | 128328 | } |
| 127682 | 128329 | |
| 127683 | 128330 | if( db->nSchemaLock==0 ){ |
| 128331 | + yDbMask m; |
| 128332 | + sqlite3VdbeSchemaInUse(db, pSafe, &m); |
| 128333 | + db->bPendingSchemaReset = 0; |
| 127684 | 128334 | for(i=0; i<db->nDb; i++){ |
| 127685 | | - if( DbHasProperty(db, i, DB_ResetWanted) ){ |
| 128335 | + if( !DbHasProperty(db, i, DB_ResetWanted) ){ |
| 128336 | + /* no-op */ |
| 128337 | + }else if( DbMaskTest(m,i) ){ |
| 128338 | + db->bPendingSchemaReset = 1; |
| 128339 | + }else{ |
| 127686 | 128340 | sqlite3SchemaClear(db->aDb[i].pSchema); |
| 127687 | 128341 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
| 127688 | 128342 | } |
| 127689 | 128343 | } |
| 127690 | 128344 | } |
| | @@ -127705,10 +128359,11 @@ |
| 127705 | 128359 | }else{ |
| 127706 | 128360 | DbSetProperty(db, i, DB_ResetWanted); |
| 127707 | 128361 | } |
| 127708 | 128362 | } |
| 127709 | 128363 | } |
| 128364 | + sqlite3ExpirePreparedStatements(db, 0); |
| 127710 | 128365 | db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); |
| 127711 | 128366 | sqlite3VtabUnlockList(db); |
| 127712 | 128367 | sqlite3BtreeLeaveAll(db); |
| 127713 | 128368 | if( db->nSchemaLock==0 ){ |
| 127714 | 128369 | sqlite3CollapseDatabaseArray(db); |
| | @@ -128130,16 +128785,20 @@ |
| 128130 | 128785 | ** is reserved for internal use. |
| 128131 | 128786 | ** |
| 128132 | 128787 | ** When parsing the sqlite_schema table, this routine also checks to |
| 128133 | 128788 | ** make sure the "type", "name", and "tbl_name" columns are consistent |
| 128134 | 128789 | ** with the SQL. |
| 128790 | +** |
| 128791 | +** If shadow tables are read-only, names that are shadow table names |
| 128792 | +** of a virtual table in schema zDb are also reserved. |
| 128135 | 128793 | */ |
| 128136 | 128794 | SQLITE_PRIVATE int sqlite3CheckObjectName( |
| 128137 | 128795 | Parse *pParse, /* Parsing context */ |
| 128138 | 128796 | const char *zName, /* Name of the object to check */ |
| 128139 | 128797 | const char *zType, /* Type of this object */ |
| 128140 | | - const char *zTblName /* Parent table name for triggers and indexes */ |
| 128798 | + const char *zTblName, /* Parent table name for triggers and indexes */ |
| 128799 | + const char *zDb /* Schema in which the object will be created */ |
| 128141 | 128800 | ){ |
| 128142 | 128801 | sqlite3 *db = pParse->db; |
| 128143 | 128802 | if( sqlite3WritableSchema(db) |
| 128144 | 128803 | || db->init.imposterTable |
| 128145 | 128804 | || !sqlite3Config.bExtraSchemaChecks |
| | @@ -128155,11 +128814,12 @@ |
| 128155 | 128814 | sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ |
| 128156 | 128815 | return SQLITE_ERROR; |
| 128157 | 128816 | } |
| 128158 | 128817 | }else{ |
| 128159 | 128818 | if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) |
| 128160 | | - || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) |
| 128819 | + || (sqlite3ReadOnlyShadowTables(db) |
| 128820 | + && sqlite3ShadowTableName(db, zName, zDb)) |
| 128161 | 128821 | ){ |
| 128162 | 128822 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", |
| 128163 | 128823 | zName); |
| 128164 | 128824 | return SQLITE_ERROR; |
| 128165 | 128825 | } |
| | @@ -128342,14 +129002,18 @@ |
| 128342 | 129002 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 128343 | 129003 | zName = sqlite3NameFromToken(db, pName); |
| 128344 | 129004 | if( IN_RENAME_OBJECT ){ |
| 128345 | 129005 | sqlite3RenameTokenMap(pParse, (void*)zName, pName); |
| 128346 | 129006 | } |
| 129007 | + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 129008 | + goto begin_table_error; |
| 129009 | + } |
| 128347 | 129010 | } |
| 128348 | 129011 | pParse->sNameToken = *pName; |
| 128349 | 129012 | if( zName==0 ) return; |
| 128350 | | - if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ |
| 129013 | + if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName, |
| 129014 | + db->aDb[iDb].zDbSName) ){ |
| 128351 | 129015 | goto begin_table_error; |
| 128352 | 129016 | } |
| 128353 | 129017 | if( db->init.iDb==1 ) isTemp = 1; |
| 128354 | 129018 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 128355 | 129019 | assert( isTemp==0 || isTemp==1 ); |
| | @@ -128379,13 +129043,10 @@ |
| 128379 | 129043 | ** and types will be used, so there is no need to test for namespace |
| 128380 | 129044 | ** collisions. |
| 128381 | 129045 | */ |
| 128382 | 129046 | if( !IN_SPECIAL_PARSE ){ |
| 128383 | 129047 | char *zDb = db->aDb[iDb].zDbSName; |
| 128384 | | - if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 128385 | | - goto begin_table_error; |
| 128386 | | - } |
| 128387 | 129048 | pTable = sqlite3FindTable(db, zName, zDb); |
| 128388 | 129049 | if( pTable ){ |
| 128389 | 129050 | if( !noErr ){ |
| 128390 | 129051 | sqlite3ErrorMsg(pParse, "%s %T already exists", |
| 128391 | 129052 | (IsView(pTable)? "view" : "table"), pName); |
| | @@ -128613,19 +129274,18 @@ |
| 128613 | 129274 | |
| 128614 | 129275 | /* Because keywords GENERATE ALWAYS can be converted into identifiers |
| 128615 | 129276 | ** by the parser, we can sometimes end up with a typename that ends |
| 128616 | 129277 | ** with "generated always". Check for this case and omit the surplus |
| 128617 | 129278 | ** text. */ |
| 128618 | | - if( sType.n>=16 |
| 128619 | | - && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0 |
| 128620 | | - ){ |
| 128621 | | - sType.n -= 6; |
| 128622 | | - while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 128623 | | - if( sType.n>=9 |
| 128624 | | - && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0 |
| 128625 | | - ){ |
| 128626 | | - sType.n -= 9; |
| 129279 | + if( sType.n>=16 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0 ){ |
| 129280 | + u32 nType = sType.n - 6; |
| 129281 | + while( ALWAYS(nType>0) && sqlite3Isspace(sType.z[nType-1]) ) nType--; |
| 129282 | + if( nType>=9 && nType<(sType.n - 6) |
| 129283 | + && sqlite3_strnicmp(sType.z+(nType-9),"generated",9)==0 |
| 129284 | + && (nType==9 || sqlite3Isspace(sType.z[nType-10])) |
| 129285 | + ){ |
| 129286 | + sType.n = nType - 9; |
| 128627 | 129287 | while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 128628 | 129288 | } |
| 128629 | 129289 | } |
| 128630 | 129290 | |
| 128631 | 129291 | /* Check for standard typenames. For standard typenames we will |
| | @@ -129692,25 +130352,38 @@ |
| 129692 | 130352 | } |
| 129693 | 130353 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 129694 | 130354 | |
| 129695 | 130355 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 129696 | 130356 | /* |
| 129697 | | -** Return true if zName is a shadow table name in the current database |
| 129698 | | -** connection. |
| 130357 | +** Return true if zName is the name of a shadow table of a virtual table |
| 130358 | +** in schema zDb. If zDb is NULL, return true if zName is a shadow table |
| 130359 | +** name of a virtual table in any schema of the database connection. |
| 130360 | +** |
| 130361 | +** Each schema is checked separately. An ordinary table in one schema |
| 130362 | +** (for example, a TEMP table) must not hide a same-named virtual table |
| 130363 | +** in another schema. |
| 129699 | 130364 | */ |
| 129700 | | -SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
| 130365 | +SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName, const char *zDb){ |
| 129701 | 130366 | const char *zTail; /* Pointer to the last "_" in zName */ |
| 129702 | 130367 | Table *pTab; /* Table that zName is a shadow of */ |
| 129703 | 130368 | char *zCopy; /* Transient copy of zName after last "_" */ |
| 130369 | + int i; /* Loop counter over schemas */ |
| 130370 | + int bRet = 0; /* Return value */ |
| 129704 | 130371 | zTail = strrchr(zName, '_'); |
| 129705 | 130372 | if( zTail==0 ) return 0; |
| 129706 | 130373 | zCopy = sqlite3DbStrNDup(db, zName, (int)(zTail-zName)); |
| 129707 | | - pTab = zCopy ? sqlite3FindTable(db, zCopy, 0) : 0; |
| 130374 | + if( zCopy==0 ) return 0; |
| 130375 | + for(i=0; i<db->nDb && bRet==0; i++){ |
| 130376 | + const char *zSchema = db->aDb[i].zDbSName; |
| 130377 | + if( zDb && sqlite3DbIsNamed(db, i, zDb)==0 ) continue; |
| 130378 | + pTab = sqlite3FindTable(db, zCopy, zSchema); |
| 130379 | + if( pTab && IsVirtual(pTab) ){ |
| 130380 | + bRet = sqlite3IsShadowTableOf(db, pTab, zName); |
| 130381 | + } |
| 130382 | + } |
| 129708 | 130383 | sqlite3DbFree(db, zCopy); |
| 129709 | | - if( pTab==0 ) return 0; |
| 129710 | | - if( !IsVirtual(pTab) ) return 0; |
| 129711 | | - return sqlite3IsShadowTableOf(db, pTab, zName); |
| 130384 | + return bRet; |
| 129712 | 130385 | } |
| 129713 | 130386 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 129714 | 130387 | |
| 129715 | 130388 | |
| 129716 | 130389 | #ifdef SQLITE_DEBUG |
| | @@ -129775,11 +130448,14 @@ |
| 129775 | 130448 | return; |
| 129776 | 130449 | } |
| 129777 | 130450 | p = pParse->pNewTable; |
| 129778 | 130451 | if( p==0 ) return; |
| 129779 | 130452 | |
| 129780 | | - if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ |
| 130453 | + if( pSelect==0 |
| 130454 | + && sqlite3ShadowTableName(db, p->zName, |
| 130455 | + db->aDb[sqlite3SchemaToIndex(db, p->pSchema)].zDbSName) |
| 130456 | + ){ |
| 129781 | 130457 | p->tabFlags |= TF_Shadow; |
| 129782 | 130458 | } |
| 129783 | 130459 | |
| 129784 | 130460 | /* If the db->init.busy is 1 it means we are reading the SQL off the |
| 129785 | 130461 | ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. |
| | @@ -131123,13 +131799,17 @@ |
| 131123 | 131799 | assert( pName && pName->z ); |
| 131124 | 131800 | |
| 131125 | 131801 | #ifndef SQLITE_OMIT_TEMPDB |
| 131126 | 131802 | /* If the index name was unqualified, check if the table |
| 131127 | 131803 | ** is a temp table. If so, set the database to 1. Do not do this |
| 131128 | | - ** if initializing a database schema. |
| 131129 | | - */ |
| 131130 | | - if( !db->init.busy ){ |
| 131804 | + ** if initializing a database schema, if ALTER TABLE is reparsing an |
| 131805 | + ** existing index, or if VACUUM is recreating the indexes of a |
| 131806 | + ** database. */ |
| 131807 | + if( !db->init.busy |
| 131808 | + && !IN_RENAME_OBJECT |
| 131809 | + && (db->mDbFlags & DBFLAG_Vacuum)==0 |
| 131810 | + ){ |
| 131131 | 131811 | pTab = sqlite3SrcListLookup(pParse, pTblName); |
| 131132 | 131812 | if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
| 131133 | 131813 | iDb = 1; |
| 131134 | 131814 | } |
| 131135 | 131815 | } |
| | @@ -131196,11 +131876,13 @@ |
| 131196 | 131876 | */ |
| 131197 | 131877 | if( pName ){ |
| 131198 | 131878 | zName = sqlite3NameFromToken(db, pName); |
| 131199 | 131879 | if( zName==0 ) goto exit_create_index; |
| 131200 | 131880 | assert( pName->z!=0 ); |
| 131201 | | - if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ |
| 131881 | + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName, "index", |
| 131882 | + pTab->zName, db->aDb[iDb].zDbSName) |
| 131883 | + ){ |
| 131202 | 131884 | goto exit_create_index; |
| 131203 | 131885 | } |
| 131204 | 131886 | if( !IN_RENAME_OBJECT ){ |
| 131205 | 131887 | if( !db->init.busy ){ |
| 131206 | 131888 | if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){ |
| | @@ -131775,12 +132457,12 @@ |
| 131775 | 132457 | sqlite3NestedParse(pParse, |
| 131776 | 132458 | "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
| 131777 | 132459 | db->aDb[iDb].zDbSName, pIndex->zName |
| 131778 | 132460 | ); |
| 131779 | 132461 | sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
| 131780 | | - sqlite3ChangeCookie(pParse, iDb); |
| 131781 | 132462 | destroyRootPage(pParse, pIndex->tnum, iDb); |
| 132463 | + sqlite3ChangeCookie(pParse, iDb); |
| 131782 | 132464 | sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
| 131783 | 132465 | } |
| 131784 | 132466 | |
| 131785 | 132467 | exit_drop_index: |
| 131786 | 132468 | sqlite3SrcListDelete(db, pName); |
| | @@ -134754,11 +135436,11 @@ |
| 134754 | 135436 | ** something that can be converted into a number, we have: |
| 134755 | 135437 | ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob |
| 134756 | 135438 | ** that cannot be converted to a numeric value. |
| 134757 | 135439 | */ |
| 134758 | 135440 | double rVal = sqlite3_value_double(argv[0]); |
| 134759 | | - if( rVal<0 ) rVal = -rVal; |
| 135441 | + if( signbit(rVal) ) rVal = -rVal; |
| 134760 | 135442 | sqlite3_result_double(context, rVal); |
| 134761 | 135443 | break; |
| 134762 | 135444 | } |
| 134763 | 135445 | } |
| 134764 | 135446 | } |
| | @@ -134897,12 +135579,19 @@ |
| 134897 | 135579 | assert( argc==3 || argc==2 ); |
| 134898 | 135580 | p0type = sqlite3_value_type(argv[0]); |
| 134899 | 135581 | p1 = sqlite3_value_int64(argv[1]); |
| 134900 | 135582 | if( p0type==SQLITE_BLOB ){ |
| 134901 | 135583 | len = sqlite3_value_bytes(argv[0]); |
| 134902 | | - z = sqlite3_value_blob(argv[0]); |
| 134903 | | - if( z==0 ) return; |
| 135584 | + if( len==0){ |
| 135585 | + /* sqlite3_value_blob() returns a NULL pointer for a zero-length blob. |
| 135586 | + ** But the call to sqlite3_result_blob() below interprets a NULL pointer |
| 135587 | + ** as an SQL NULL. So set z to point to some non-NULL location here. */ |
| 135588 | + z = (const unsigned char*)""; |
| 135589 | + }else{ |
| 135590 | + z = sqlite3_value_blob(argv[0]); |
| 135591 | + if( z==0 ) return; |
| 135592 | + } |
| 134904 | 135593 | assert( len==sqlite3_value_bytes(argv[0]) ); |
| 134905 | 135594 | }else{ |
| 134906 | 135595 | z = sqlite3_value_text(argv[0]); |
| 134907 | 135596 | if( z==0 ) return; |
| 134908 | 135597 | len = 0; |
| | @@ -134932,11 +135621,11 @@ |
| 134932 | 135621 | if( p1<0 ){ |
| 134933 | 135622 | p1 += len; |
| 134934 | 135623 | if( p1<0 ){ |
| 134935 | 135624 | if( p2<0 ){ |
| 134936 | 135625 | p2 = 0; |
| 134937 | | - }else{ |
| 135626 | + }else if( argc==3 ){ |
| 134938 | 135627 | p2 += p1; |
| 134939 | 135628 | } |
| 134940 | 135629 | p1 = 0; |
| 134941 | 135630 | } |
| 134942 | 135631 | }else if( p1>0 ){ |
| | @@ -135022,14 +135711,12 @@ |
| 135022 | 135711 | ** handle the rounding directly, |
| 135023 | 135712 | ** otherwise use printf. |
| 135024 | 135713 | */ |
| 135025 | 135714 | if( r<-4503599627370496.0 || r>+4503599627370496.0 ){ |
| 135026 | 135715 | /* The value has no fractional part so there is nothing to round */ |
| 135027 | | - }else if( n==0 ){ |
| 135028 | | - r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); |
| 135029 | 135716 | }else{ |
| 135030 | | - zBuf = sqlite3_mprintf("%!.*f",(int)n,r); |
| 135717 | + zBuf = sqlite3_mprintf("%#!.*f",(int)n,r); |
| 135031 | 135718 | if( zBuf==0 ){ |
| 135032 | 135719 | sqlite3_result_error_nomem(context); |
| 135033 | 135720 | return; |
| 135034 | 135721 | } |
| 135035 | 135722 | sqlite3AtoF(zBuf, &r); |
| | @@ -137109,11 +137796,11 @@ |
| 137109 | 137796 | if( argc==2 ){ |
| 137110 | 137797 | switch( sqlite3_value_numeric_type(argv[1]) ){ |
| 137111 | 137798 | case SQLITE_INTEGER: |
| 137112 | 137799 | case SQLITE_FLOAT: |
| 137113 | 137800 | b = log(x); |
| 137114 | | - if( b<=0.0 ) return; |
| 137801 | + if( b==0.0 ) return; |
| 137115 | 137802 | x = sqlite3_value_double(argv[1]); |
| 137116 | 137803 | if( x<=0.0 ) return; |
| 137117 | 137804 | break; |
| 137118 | 137805 | default: |
| 137119 | 137806 | return; |
| | @@ -138544,10 +139231,12 @@ |
| 138544 | 139231 | for(i=0; i<nCol; i++){ |
| 138545 | 139232 | sqlite3VdbeAddOp2(v, OP_Copy, |
| 138546 | 139233 | sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData, |
| 138547 | 139234 | regTemp+i); |
| 138548 | 139235 | } |
| 139236 | + sqlite3VdbeAddOp4(v, OP_Affinity, regTemp, nCol, 0, |
| 139237 | + sqlite3IndexAffinityStr(pParse->db,pIdx), nCol); |
| 138549 | 139238 | |
| 138550 | 139239 | /* If the parent table is the same as the child table, and we are about |
| 138551 | 139240 | ** to increment the constraint-counter (i.e. this is an INSERT operation), |
| 138552 | 139241 | ** then check if the row being inserted matches itself. If so, do not |
| 138553 | 139242 | ** increment the constraint-counter. |
| | @@ -138558,29 +139247,29 @@ |
| 138558 | 139247 | ** none of the child key values are). |
| 138559 | 139248 | */ |
| 138560 | 139249 | if( pTab==pFKey->pFrom && nIncr==1 ){ |
| 138561 | 139250 | int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1; |
| 138562 | 139251 | for(i=0; i<nCol; i++){ |
| 138563 | | - int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) |
| 138564 | | - +1+regData; |
| 139252 | + CollSeq *pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 139253 | + int iChild = regTemp+i; |
| 138565 | 139254 | int iParent = 1+regData; |
| 138566 | 139255 | iParent += sqlite3TableColumnToStorage(pIdx->pTable, |
| 138567 | 139256 | pIdx->aiColumn[i]); |
| 138568 | 139257 | assert( pIdx->aiColumn[i]>=0 ); |
| 138569 | 139258 | assert( aiCol[i]!=pTab->iPKey ); |
| 138570 | 139259 | if( pIdx->aiColumn[i]==pTab->iPKey ){ |
| 138571 | 139260 | /* The parent key is a composite key that includes the IPK column */ |
| 138572 | 139261 | iParent = regData; |
| 138573 | 139262 | } |
| 138574 | | - sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v); |
| 139263 | + sqlite3VdbeAddOp4(v, OP_Ne, iChild, iJump, iParent, |
| 139264 | + (char*)pColl, P4_COLLSEQ); |
| 139265 | + VdbeCoverage(v); |
| 138575 | 139266 | sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); |
| 138576 | 139267 | } |
| 138577 | 139268 | sqlite3VdbeGoto(v, iOk); |
| 138578 | 139269 | } |
| 138579 | 139270 | |
| 138580 | | - sqlite3VdbeAddOp4(v, OP_Affinity, regTemp, nCol, 0, |
| 138581 | | - sqlite3IndexAffinityStr(pParse->db,pIdx), nCol); |
| 138582 | 139271 | sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regTemp, nCol); |
| 138583 | 139272 | VdbeCoverage(v); |
| 138584 | 139273 | sqlite3ReleaseTempRange(pParse, regTemp, nCol); |
| 138585 | 139274 | } |
| 138586 | 139275 | } |
| | @@ -139150,10 +139839,27 @@ |
| 139150 | 139839 | return 1; |
| 139151 | 139840 | } |
| 139152 | 139841 | } |
| 139153 | 139842 | return 0; |
| 139154 | 139843 | } |
| 139844 | + |
| 139845 | +/* |
| 139846 | +** Return true if the child table of FK constraint pFKey has one or |
| 139847 | +** more BEFORE triggers that could interfere with the action (either |
| 139848 | +** a CASCADE or SET NULL) caused by either a DELETE (if bUpdate==0) |
| 139849 | +** or UPDATE (if bUpdate==1) of the parent table. Or false otherwise. |
| 139850 | +*/ |
| 139851 | +static int fkBeforeTriggersExist( |
| 139852 | + Parse *pParse, /* Parse context */ |
| 139853 | + FKey *pFKey, /* Foreign key constraint */ |
| 139854 | + int bUpdate /* True for ON UPDATE, false ON DELETE */ |
| 139855 | +){ |
| 139856 | + int mask = 0; |
| 139857 | + int op = (!bUpdate && pFKey->aAction[0]==OE_Cascade) ? TK_DELETE : TK_UPDATE; |
| 139858 | + sqlite3TriggersExist(pParse, pFKey->pFrom, op, 0, &mask); |
| 139859 | + return (mask & TRIGGER_BEFORE)!=0; |
| 139860 | +} |
| 139155 | 139861 | |
| 139156 | 139862 | /* |
| 139157 | 139863 | ** This function is called when inserting, deleting or updating a row of |
| 139158 | 139864 | ** table pTab to generate VDBE code to perform foreign key constraint |
| 139159 | 139865 | ** processing for the operation. |
| | @@ -139184,10 +139890,11 @@ |
| 139184 | 139890 | sqlite3 *db = pParse->db; /* Database handle */ |
| 139185 | 139891 | FKey *pFKey; /* Used to iterate through FKs */ |
| 139186 | 139892 | int iDb; /* Index of database containing pTab */ |
| 139187 | 139893 | const char *zDb; /* Name of database containing pTab */ |
| 139188 | 139894 | int isIgnoreErrors = pParse->disableTriggers; |
| 139895 | + int iPass; /* 0: immediate FKs, 1: deferred FKs */ |
| 139189 | 139896 | |
| 139190 | 139897 | /* Exactly one of regOld and regNew should be non-zero. */ |
| 139191 | 139898 | assert( (regOld==0)!=(regNew==0) ); |
| 139192 | 139899 | |
| 139193 | 139900 | /* If foreign-keys are disabled, this function is a no-op. */ |
| | @@ -139197,110 +139904,120 @@ |
| 139197 | 139904 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 139198 | 139905 | assert( iDb>=00 && iDb<db->nDb ); |
| 139199 | 139906 | zDb = db->aDb[iDb].zDbSName; |
| 139200 | 139907 | |
| 139201 | 139908 | /* Loop through all the foreign key constraints for which pTab is the |
| 139202 | | - ** child table (the table that the foreign key definition is part of). */ |
| 139203 | | - for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 139204 | | - Table *pTo; /* Parent table of foreign key pFKey */ |
| 139205 | | - Index *pIdx = 0; /* Index on key columns in pTo */ |
| 139206 | | - int *aiFree = 0; |
| 139207 | | - int *aiCol; |
| 139208 | | - int iCol; |
| 139209 | | - int i; |
| 139210 | | - int bIgnore = 0; |
| 139211 | | - |
| 139212 | | - if( aChange |
| 139213 | | - && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0 |
| 139214 | | - && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0 |
| 139215 | | - ){ |
| 139216 | | - continue; |
| 139217 | | - } |
| 139218 | | - |
| 139219 | | - /* Find the parent table of this foreign key. Also find a unique index |
| 139220 | | - ** on the parent key columns in the parent table. If either of these |
| 139221 | | - ** schema items cannot be located, set an error in pParse and return |
| 139222 | | - ** early. */ |
| 139223 | | - if( pParse->disableTriggers ){ |
| 139224 | | - pTo = sqlite3FindTable(db, pFKey->zTo, zDb); |
| 139225 | | - }else{ |
| 139226 | | - pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb); |
| 139227 | | - } |
| 139228 | | - if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){ |
| 139229 | | - assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) ); |
| 139230 | | - if( !isIgnoreErrors || db->mallocFailed ) return; |
| 139231 | | - if( pTo==0 ){ |
| 139232 | | - /* If isIgnoreErrors is true, then a table is being dropped. In this |
| 139233 | | - ** case SQLite runs a "DELETE FROM xxx" on the table being dropped |
| 139234 | | - ** before actually dropping it in order to check FK constraints. |
| 139235 | | - ** If the parent table of an FK constraint on the current table is |
| 139236 | | - ** missing, behave as if it is empty. i.e. decrement the relevant |
| 139237 | | - ** FK counter for each row of the current table with non-NULL keys. |
| 139238 | | - */ |
| 139239 | | - Vdbe *v = sqlite3GetVdbe(pParse); |
| 139240 | | - int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1; |
| 139241 | | - for(i=0; i<pFKey->nCol; i++){ |
| 139242 | | - int iFromCol, iReg; |
| 139243 | | - iFromCol = pFKey->aCol[i].iFrom; |
| 139244 | | - iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1; |
| 139245 | | - sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v); |
| 139246 | | - } |
| 139247 | | - sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1); |
| 139248 | | - } |
| 139249 | | - continue; |
| 139250 | | - } |
| 139251 | | - assert( pFKey->nCol==1 || (aiFree && pIdx) ); |
| 139252 | | - |
| 139253 | | - if( aiFree ){ |
| 139254 | | - aiCol = aiFree; |
| 139255 | | - }else{ |
| 139256 | | - iCol = pFKey->aCol[0].iFrom; |
| 139257 | | - aiCol = &iCol; |
| 139258 | | - } |
| 139259 | | - for(i=0; i<pFKey->nCol; i++){ |
| 139260 | | - if( aiCol[i]==pTab->iPKey ){ |
| 139261 | | - aiCol[i] = -1; |
| 139262 | | - } |
| 139263 | | - assert( pIdx==0 || pIdx->aiColumn[i]>=0 ); |
| 139264 | | -#ifndef SQLITE_OMIT_AUTHORIZATION |
| 139265 | | - /* Request permission to read the parent key columns. If the |
| 139266 | | - ** authorization callback returns SQLITE_IGNORE, behave as if any |
| 139267 | | - ** values read from the parent table are NULL. */ |
| 139268 | | - if( db->xAuth ){ |
| 139269 | | - int rcauth; |
| 139270 | | - char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName; |
| 139271 | | - rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb); |
| 139272 | | - bIgnore = (rcauth==SQLITE_IGNORE); |
| 139273 | | - } |
| 139274 | | -#endif |
| 139275 | | - } |
| 139276 | | - |
| 139277 | | - /* Take a shared-cache advisory read-lock on the parent table. Allocate |
| 139278 | | - ** a cursor to use to search the unique index on the parent key columns |
| 139279 | | - ** in the parent table. */ |
| 139280 | | - sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName); |
| 139281 | | - pParse->nTab++; |
| 139282 | | - |
| 139283 | | - if( regOld!=0 ){ |
| 139284 | | - /* A row is being removed from the child table. Search for the parent. |
| 139285 | | - ** If the parent does not exist, removing the child row resolves an |
| 139286 | | - ** outstanding foreign key constraint violation. */ |
| 139287 | | - fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore); |
| 139288 | | - } |
| 139289 | | - if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){ |
| 139290 | | - /* A row is being added to the child table. If a parent row cannot |
| 139291 | | - ** be found, adding the child row has violated the FK constraint. |
| 139292 | | - ** |
| 139293 | | - ** If this operation is being performed as part of a trigger program |
| 139294 | | - ** that is actually a "SET NULL" action belonging to this very |
| 139295 | | - ** foreign key, then omit this scan altogether. As all child key |
| 139296 | | - ** values are guaranteed to be NULL, it is not possible for adding |
| 139297 | | - ** this row to cause an FK violation. */ |
| 139298 | | - fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore); |
| 139299 | | - } |
| 139300 | | - |
| 139301 | | - sqlite3DbFree(db, aiFree); |
| 139909 | + ** child table (the table that the foreign key definition is part of). |
| 139910 | + ** |
| 139911 | + ** Immediate constraints are processed first, then deferred constraints |
| 139912 | + ** in a second pass. This is because immediate FK processing may halt |
| 139913 | + ** the VM, and in cases where there is no statement journal this needs |
| 139914 | + ** to happen before any deferred FKs are processed. */ |
| 139915 | + for(iPass=0; iPass<2; iPass++){ |
| 139916 | + for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 139917 | + Table *pTo; /* Parent table of foreign key pFKey */ |
| 139918 | + Index *pIdx = 0; /* Index on key columns in pTo */ |
| 139919 | + int *aiFree = 0; |
| 139920 | + int *aiCol; |
| 139921 | + int iCol; |
| 139922 | + int i; |
| 139923 | + int bIgnore = 0; |
| 139924 | + |
| 139925 | + assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 ); |
| 139926 | + if( pFKey->isDeferred!=iPass ) continue; |
| 139927 | + |
| 139928 | + if( aChange |
| 139929 | + && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0 |
| 139930 | + && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0 |
| 139931 | + ){ |
| 139932 | + continue; |
| 139933 | + } |
| 139934 | + |
| 139935 | + /* Find the parent table of this foreign key. Also find a unique index |
| 139936 | + ** on the parent key columns in the parent table. If either of these |
| 139937 | + ** schema items cannot be located, set an error in pParse and return |
| 139938 | + ** early. */ |
| 139939 | + if( pParse->disableTriggers ){ |
| 139940 | + pTo = sqlite3FindTable(db, pFKey->zTo, zDb); |
| 139941 | + }else{ |
| 139942 | + pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb); |
| 139943 | + } |
| 139944 | + if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){ |
| 139945 | + assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) ); |
| 139946 | + if( !isIgnoreErrors || db->mallocFailed ) return; |
| 139947 | + if( pTo==0 ){ |
| 139948 | + /* If isIgnoreErrors is true, then a table is being dropped. In this |
| 139949 | + ** case SQLite runs a "DELETE FROM xxx" on the table being dropped |
| 139950 | + ** before actually dropping it in order to check FK constraints. |
| 139951 | + ** If the parent table of an FK constraint on the current table is |
| 139952 | + ** missing, behave as if it is empty. i.e. decrement the relevant |
| 139953 | + ** FK counter for each row of the current table with non-NULL keys. |
| 139954 | + */ |
| 139955 | + Vdbe *v = sqlite3GetVdbe(pParse); |
| 139956 | + int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1; |
| 139957 | + for(i=0; i<pFKey->nCol; i++){ |
| 139958 | + int iFromCol, iReg; |
| 139959 | + iFromCol = pFKey->aCol[i].iFrom; |
| 139960 | + iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol)+regOld+1; |
| 139961 | + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v); |
| 139962 | + } |
| 139963 | + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1); |
| 139964 | + } |
| 139965 | + continue; |
| 139966 | + } |
| 139967 | + assert( pFKey->nCol==1 || (aiFree && pIdx) ); |
| 139968 | + |
| 139969 | + if( aiFree ){ |
| 139970 | + aiCol = aiFree; |
| 139971 | + }else{ |
| 139972 | + iCol = pFKey->aCol[0].iFrom; |
| 139973 | + aiCol = &iCol; |
| 139974 | + } |
| 139975 | + for(i=0; i<pFKey->nCol; i++){ |
| 139976 | + if( aiCol[i]==pTab->iPKey ){ |
| 139977 | + aiCol[i] = -1; |
| 139978 | + } |
| 139979 | + assert( pIdx==0 || pIdx->aiColumn[i]>=0 ); |
| 139980 | + #ifndef SQLITE_OMIT_AUTHORIZATION |
| 139981 | + /* Request permission to read the parent key columns. If the |
| 139982 | + ** authorization callback returns SQLITE_IGNORE, behave as if any |
| 139983 | + ** values read from the parent table are NULL. */ |
| 139984 | + if( db->xAuth ){ |
| 139985 | + int rcauth; |
| 139986 | + char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName; |
| 139987 | + rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb); |
| 139988 | + bIgnore = (rcauth==SQLITE_IGNORE); |
| 139989 | + } |
| 139990 | + #endif |
| 139991 | + } |
| 139992 | + |
| 139993 | + /* Take a shared-cache advisory read-lock on the parent table. Allocate |
| 139994 | + ** a cursor to use to search the unique index on the parent key columns |
| 139995 | + ** in the parent table. */ |
| 139996 | + sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName); |
| 139997 | + pParse->nTab++; |
| 139998 | + |
| 139999 | + if( regOld!=0 ){ |
| 140000 | + /* A row is being removed from the child table. Search for the parent. |
| 140001 | + ** If the parent does not exist, removing the child row resolves an |
| 140002 | + ** outstanding foreign key constraint violation. */ |
| 140003 | + fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld,-1,bIgnore); |
| 140004 | + } |
| 140005 | + if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){ |
| 140006 | + /* A row is being added to the child table. If a parent row cannot |
| 140007 | + ** be found, adding the child row has violated the FK constraint. |
| 140008 | + ** |
| 140009 | + ** If this operation is being performed as part of a trigger program |
| 140010 | + ** that is actually a "SET NULL" action belonging to this very |
| 140011 | + ** foreign key, then omit this scan altogether. As all child key |
| 140012 | + ** values are guaranteed to be NULL, it is not possible for adding |
| 140013 | + ** this row to cause an FK violation. */ |
| 140014 | + fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew,+1,bIgnore); |
| 140015 | + } |
| 140016 | + |
| 140017 | + sqlite3DbFree(db, aiFree); |
| 140018 | + } |
| 139302 | 140019 | } |
| 139303 | 140020 | |
| 139304 | 140021 | /* Loop through all the foreign key constraints that refer to this table. |
| 139305 | 140022 | ** (the "child" constraints) */ |
| 139306 | 140023 | for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){ |
| | @@ -139343,14 +140060,20 @@ |
| 139343 | 140060 | if( regOld!=0 ){ |
| 139344 | 140061 | int eAction = pFKey->aAction[aChange!=0]; |
| 139345 | 140062 | if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None; |
| 139346 | 140063 | |
| 139347 | 140064 | fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1); |
| 139348 | | - /* If this is a deferred FK constraint, or a CASCADE or SET NULL |
| 139349 | | - ** action applies, then any foreign key violations caused by |
| 139350 | | - ** removing the parent key will be rectified by the action trigger. |
| 139351 | | - ** So do not set the "may-abort" flag in this case. |
| 140065 | + /* If this is an immediate FK constraint, then foreign key violations |
| 140066 | + ** caused by removing the parent key may cause an abort. |
| 140067 | + ** |
| 140068 | + ** Except - if a CASCADE or SET NULL action applies, then any such |
| 140069 | + ** foreign key violations will be rectified by the action trigger. So |
| 140070 | + ** do not set the "may-abort" flag in this case. |
| 140071 | + ** |
| 140072 | + ** Except - if there are BEFORE triggers on the child table, then |
| 140073 | + ** one of them may use RAISE(IGNORE) to prevent the SET NULL or |
| 140074 | + ** CASCADE action from taking place. Do set the "may-abort" flag then. |
| 139352 | 140075 | ** |
| 139353 | 140076 | ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the |
| 139354 | 140077 | ** may-abort flag will eventually be set on this statement anyway |
| 139355 | 140078 | ** (when this function is called as part of processing the UPDATE |
| 139356 | 140079 | ** within the action trigger). |
| | @@ -139360,11 +140083,14 @@ |
| 139360 | 140083 | ** applies. The trouble starts if the CASCADE or SET NULL action |
| 139361 | 140084 | ** trigger causes other triggers or action rules attached to the |
| 139362 | 140085 | ** child table to fire. In these cases the fk constraint counters |
| 139363 | 140086 | ** might be set incorrectly if any OP_FkCounter related scans are |
| 139364 | 140087 | ** omitted. */ |
| 139365 | | - if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){ |
| 140088 | + if( !pFKey->isDeferred && ( |
| 140089 | + (eAction!=OE_Cascade && eAction!=OE_SetNull) |
| 140090 | + || fkBeforeTriggersExist(pParse, pFKey, aChange!=0) |
| 140091 | + )){ |
| 139366 | 140092 | sqlite3MayAbort(pParse); |
| 139367 | 140093 | } |
| 139368 | 140094 | } |
| 139369 | 140095 | pItem->zName = 0; |
| 139370 | 140096 | sqlite3SrcListDelete(db, pSrc); |
| | @@ -139423,11 +140149,16 @@ |
| 139423 | 140149 | ** |
| 139424 | 140150 | ** * There are any FKs for which pTab is the child and the parent table |
| 139425 | 140151 | ** and any FK processing at all is required (even of a different FK), or |
| 139426 | 140152 | ** |
| 139427 | 140153 | ** * the UPDATE modifies one or more parent keys for which the action is |
| 139428 | | -** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL). |
| 140154 | +** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL), or |
| 140155 | +** |
| 140156 | +** * the UPDATE modifies one or more parent keys of an FK for which pTab |
| 140157 | +** is also the child table. The old version of the row must be removed |
| 140158 | +** from the table before the FK checks run, as otherwise the scan for |
| 140159 | +** rows that refer to the new parent key may find it. |
| 139429 | 140160 | ** |
| 139430 | 140161 | ** Or, assuming some other foreign key processing is required, 1. |
| 139431 | 140162 | */ |
| 139432 | 140163 | SQLITE_PRIVATE int sqlite3FkRequired( |
| 139433 | 140164 | Parse *pParse, /* Parse context */ |
| | @@ -139462,10 +140193,11 @@ |
| 139462 | 140193 | if( (pParse->db->flags & SQLITE_FkNoAction)==0 |
| 139463 | 140194 | && p->aAction[1]!=OE_None |
| 139464 | 140195 | ){ |
| 139465 | 140196 | return 2; |
| 139466 | 140197 | } |
| 140198 | + if( p->pFrom==pTab ) eRet = 2; |
| 139467 | 140199 | bHaveFK = 1; |
| 139468 | 140200 | } |
| 139469 | 140201 | } |
| 139470 | 140202 | } |
| 139471 | 140203 | } |
| | @@ -140403,10 +141135,22 @@ |
| 140403 | 141135 | if( 0!=sqlite3ExprAffinity(pExpr) ) return 0; |
| 140404 | 141136 | } |
| 140405 | 141137 | return 1; |
| 140406 | 141138 | |
| 140407 | 141139 | } |
| 141140 | + |
| 141141 | +/* |
| 141142 | +** Return true if any expression in the expression-list passed as the |
| 141143 | +** only argument contains a COLLATE operator. |
| 141144 | +*/ |
| 141145 | +static int exprListHasCollate(ExprList *pRow){ |
| 141146 | + int ii; |
| 141147 | + for(ii=0; ii<pRow->nExpr; ii++){ |
| 141148 | + if( ExprHasProperty(pRow->a[ii].pExpr, EP_Collate) ) return 1; |
| 141149 | + } |
| 141150 | + return 0; |
| 141151 | +} |
| 140408 | 141152 | |
| 140409 | 141153 | /* |
| 140410 | 141154 | ** This function is called by the parser for the second and subsequent |
| 140411 | 141155 | ** rows of a multi-row VALUES clause. Argument pLeft is the part of |
| 140412 | 141156 | ** the VALUES clause already parsed, argument pRow is the vector of values |
| | @@ -140454,18 +141198,28 @@ |
| 140454 | 141198 | ** has an affinity (i.e. is a CAST expression). This causes problems |
| 140455 | 141199 | ** because the complex rules SQLite uses (see function |
| 140456 | 141200 | ** sqlite3SubqueryColumnTypes() in select.c) to determine the effective |
| 140457 | 141201 | ** affinity of such a column for all rows require access to all values in |
| 140458 | 141202 | ** the column simultaneously. |
| 141203 | +** |
| 141204 | +** e) The new row, or the row that would become the first row of the |
| 141205 | +** co-routine, contains a COLLATE operator. This ensures that the |
| 141206 | +** columns of a co-routine never have a collation sequence (see |
| 141207 | +** multiSelectCollSeq() in select.c). Each explicit COLLATE in a VALUES |
| 141208 | +** clause is instead part of an ordinary SELECT in a UNION ALL |
| 141209 | +** compound, where it is available when the collation sequence for a |
| 141210 | +** compound SELECT column is determined. |
| 140459 | 141211 | */ |
| 140460 | 141212 | SQLITE_PRIVATE Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){ |
| 140461 | 141213 | |
| 140462 | 141214 | if( pParse->bHasWith /* condition (a) above */ |
| 140463 | 141215 | || pParse->db->init.busy /* condition (b) above */ |
| 140464 | 141216 | || sqlite3ExprListIsConstant(pParse, pRow, 1)==0 /* condition (c) above */ |
| 140465 | 141217 | || (pLeft->pSrc->nSrc==0 && |
| 140466 | 141218 | exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */ |
| 141219 | + || exprListHasCollate(pRow) /* condition (e) above */ |
| 141220 | + || (pLeft->pSrc->nSrc==0 && exprListHasCollate(pLeft->pEList)) |
| 140467 | 141221 | || IN_SPECIAL_PARSE |
| 140468 | 141222 | ){ |
| 140469 | 141223 | /* The co-routine method cannot be used. Fall back to UNION ALL. */ |
| 140470 | 141224 | Select *pSelect = 0; |
| 140471 | 141225 | int f = SF_Values | SF_MultiValue; |
| | @@ -140503,11 +141257,11 @@ |
| 140503 | 141257 | SelectDest dest; |
| 140504 | 141258 | Subquery *pSubq; |
| 140505 | 141259 | pRet->pSrc->nSrc = 1; |
| 140506 | 141260 | pRet->pPrior = pLeft->pPrior; |
| 140507 | 141261 | pRet->op = pLeft->op; |
| 140508 | | - if( pRet->pPrior ) pRet->selFlags |= SF_Values; |
| 141262 | + pRet->selFlags |= SF_Values; |
| 140509 | 141263 | pLeft->pPrior = 0; |
| 140510 | 141264 | pLeft->op = TK_SELECT; |
| 140511 | 141265 | assert( pLeft->pNext==0 ); |
| 140512 | 141266 | assert( pRet->pNext==0 ); |
| 140513 | 141267 | p = &pRet->pSrc->a[0]; |
| | @@ -140729,11 +141483,15 @@ |
| 140729 | 141483 | |
| 140730 | 141484 | /* If the Select object is really just a simple VALUES() list with a |
| 140731 | 141485 | ** single row (the common case) then keep that one row of values |
| 140732 | 141486 | ** and discard the other (unused) parts of the pSelect object |
| 140733 | 141487 | */ |
| 140734 | | - if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ |
| 141488 | + if( pSelect |
| 141489 | + && (pSelect->selFlags & SF_Values)!=0 |
| 141490 | + && pSelect->pPrior==0 |
| 141491 | + && pSelect->pSrc->nSrc==0 |
| 141492 | + ){ |
| 140735 | 141493 | pList = pSelect->pEList; |
| 140736 | 141494 | pSelect->pEList = 0; |
| 140737 | 141495 | sqlite3SelectDelete(db, pSelect); |
| 140738 | 141496 | pSelect = 0; |
| 140739 | 141497 | } |
| | @@ -142054,10 +142812,16 @@ |
| 142054 | 142812 | if( pUpsertClause!=0 ){ |
| 142055 | 142813 | if( pUpsertClause->isDoUpdate==0 ){ |
| 142056 | 142814 | onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ |
| 142057 | 142815 | }else{ |
| 142058 | 142816 | onError = OE_Update; /* DO UPDATE */ |
| 142817 | + /* The excluded.* values used by the DO UPDATE must have |
| 142818 | + ** column affinity applied before the rowid conflict check. */ |
| 142819 | + if( bAffinityDone==0 ){ |
| 142820 | + sqlite3TableAffinity(v, pTab, regNewData+1); |
| 142821 | + bAffinityDone = 1; |
| 142822 | + } |
| 142059 | 142823 | } |
| 142060 | 142824 | } |
| 142061 | 142825 | if( pUpsertClause!=pUpsert ){ |
| 142062 | 142826 | /* The first ON CONFLICT clause has a conflict target other than |
| 142063 | 142827 | ** the IPK. We have to jump ahead to that first ON CONFLICT clause |
| | @@ -142213,10 +142977,18 @@ |
| 142213 | 142977 | bAffinityDone = 1; |
| 142214 | 142978 | } |
| 142215 | 142979 | VdbeNoopComment((v, "prep index %s", pIdx->zName)); |
| 142216 | 142980 | iThisCur = iIdxCur+ix; |
| 142217 | 142981 | |
| 142982 | + /* If an OE_Replace resolution earlier in the constraint checks may |
| 142983 | + ** have already deleted a conflicting row, and evaluating the partial |
| 142984 | + ** index WHERE clause or an indexed expression for this index might |
| 142985 | + ** raise an error, then a statement journal may be required to undo |
| 142986 | + ** the deletion. */ |
| 142987 | + if( seenReplace && (pIdx->pPartIdxWhere || pIdx->aColExpr) ){ |
| 142988 | + sqlite3MultiWrite(pParse); |
| 142989 | + } |
| 142218 | 142990 | |
| 142219 | 142991 | /* Skip partial indices for which the WHERE clause is not true */ |
| 142220 | 142992 | if( pIdx->pPartIdxWhere ){ |
| 142221 | 142993 | sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); |
| 142222 | 142994 | pParse->iSelfTab = -(regNewData+1); |
| | @@ -142897,10 +143669,13 @@ |
| 142897 | 143669 | return 0; /* FROM clause must have exactly one term */ |
| 142898 | 143670 | } |
| 142899 | 143671 | if( pSelect->pSrc->a[0].fg.isSubquery ){ |
| 142900 | 143672 | return 0; /* FROM clause cannot contain a subquery */ |
| 142901 | 143673 | } |
| 143674 | + if( pSelect->pSrc->a[0].fg.isIndexedBy ){ |
| 143675 | + return 0; /* FROM clause cannot have an INDEXED BY clause */ |
| 143676 | + } |
| 142902 | 143677 | if( pSelect->pWhere ){ |
| 142903 | 143678 | return 0; /* SELECT may not have a WHERE clause */ |
| 142904 | 143679 | } |
| 142905 | 143680 | if( pSelect->pOrderBy ){ |
| 142906 | 143681 | return 0; /* SELECT may not have an ORDER BY clause */ |
| | @@ -143064,19 +143839,21 @@ |
| 143064 | 143839 | ){ |
| 143065 | 143840 | return 0; /* Tables have different CHECK constraints. Ticket #2252 */ |
| 143066 | 143841 | } |
| 143067 | 143842 | #endif |
| 143068 | 143843 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 143069 | | - /* Disallow the transfer optimization if the destination table contains |
| 143070 | | - ** any foreign key constraints. This is more restrictive than necessary. |
| 143071 | | - ** But the main beneficiary of the transfer optimization is the VACUUM |
| 143072 | | - ** command, and the VACUUM command disables foreign key constraints. So |
| 143073 | | - ** the extra complication to make this rule less restrictive is probably |
| 143074 | | - ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] |
| 143075 | | - */ |
| 143844 | + /* Disallow the transfer optimization if foreign key processing is |
| 143845 | + ** enabled and the destination table is either the child or the parent |
| 143846 | + ** of any foreign key constraint. This is more restrictive than |
| 143847 | + ** necessary. But the main beneficiary of the transfer optimization is |
| 143848 | + ** the VACUUM command, and the VACUUM command disables foreign key |
| 143849 | + ** constraints. So the extra complication to make this rule less |
| 143850 | + ** restrictive is probably not worth the effort. Ticket |
| 143851 | + ** [6284df89debdfa61db8073e062908af0c9b6118e] and bugs forum post |
| 143852 | + ** [2026-11-04T20:24:15Z]. */ |
| 143076 | 143853 | assert( IsOrdinaryTable(pDest) ); |
| 143077 | | - if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){ |
| 143854 | + if( sqlite3FkRequired(pParse, pDest, 0, 0) ){ |
| 143078 | 143855 | return 0; |
| 143079 | 143856 | } |
| 143080 | 143857 | #endif |
| 143081 | 143858 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
| 143082 | 143859 | return 0; /* xfer opt does not play well with PRAGMA count_changes */ |
| | @@ -146157,17 +146934,26 @@ |
| 146157 | 146934 | ** Helper subroutine for PRAGMA integrity_check: |
| 146158 | 146935 | ** |
| 146159 | 146936 | ** Generate code to output a single-column result row with a value of the |
| 146160 | 146937 | ** string held in register 3. Decrement the result count in register 1 |
| 146161 | 146938 | ** and halt if the maximum number of result rows have been issued. |
| 146939 | +** |
| 146940 | +** If iCol is not negative, then after the result row has been output, |
| 146941 | +** also load column iCol of cursor iCur into register 3. This is to ensure |
| 146942 | +** the cursor's column cache is valid after it is invalidated by the |
| 146943 | +** OP_ResultRow. |
| 146162 | 146944 | */ |
| 146163 | | -static int integrityCheckResultRow(Vdbe *v){ |
| 146945 | +static int integrityCheckResultRow(Vdbe *v, int iCur, int iCol){ |
| 146164 | 146946 | int addr; |
| 146165 | 146947 | sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); |
| 146166 | 146948 | addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1); |
| 146167 | 146949 | VdbeCoverage(v); |
| 146168 | 146950 | sqlite3VdbeAddOp0(v, OP_Halt); |
| 146951 | + if( iCol>=0 ){ |
| 146952 | + sqlite3VdbeAddOp3(v, OP_Column, iCur, iCol, 3); |
| 146953 | + sqlite3VdbeTypeofColumn(v, 3); |
| 146954 | + } |
| 146169 | 146955 | return addr; |
| 146170 | 146956 | } |
| 146171 | 146957 | |
| 146172 | 146958 | /* |
| 146173 | 146959 | ** Should table pTab be skipped when doing an integrity_check? |
| | @@ -147567,11 +148353,11 @@ |
| 147567 | 148353 | addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); |
| 147568 | 148354 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
| 147569 | 148355 | sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName), |
| 147570 | 148356 | P4_DYNAMIC); |
| 147571 | 148357 | sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3); |
| 147572 | | - integrityCheckResultRow(v); |
| 148358 | + integrityCheckResultRow(v, 0, -1); |
| 147573 | 148359 | sqlite3VdbeJumpHere(v, addr); |
| 147574 | 148360 | |
| 147575 | 148361 | /* Check that the indexes all have the right number of rows */ |
| 147576 | 148362 | cnt = pObjTab ? 1 : 0; |
| 147577 | 148363 | sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); |
| | @@ -147593,11 +148379,11 @@ |
| 147593 | 148379 | if( pIdx->pPartIdxWhere==0 ){ |
| 147594 | 148380 | addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+cnt, 0, 8+iTab); |
| 147595 | 148381 | VdbeCoverageNeverNull(v); |
| 147596 | 148382 | sqlite3VdbeLoadString(v, 4, pIdx->zName); |
| 147597 | 148383 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); |
| 147598 | | - integrityCheckResultRow(v); |
| 148384 | + integrityCheckResultRow(v, 0, -1); |
| 147599 | 148385 | sqlite3VdbeJumpHere(v, addr); |
| 147600 | 148386 | } |
| 147601 | 148387 | cnt++; |
| 147602 | 148388 | } |
| 147603 | 148389 | } |
| | @@ -147639,25 +148425,20 @@ |
| 147639 | 148425 | ** will also prepopulate the cursor column cache that is used |
| 147640 | 148426 | ** by the OP_IsType code, so it is a required step. |
| 147641 | 148427 | */ |
| 147642 | 148428 | assert( !IsVirtual(pTab) ); |
| 147643 | 148429 | if( HasRowid(pTab) ){ |
| 147644 | | - mxCol = -1; |
| 147645 | | - for(j=0; j<pTab->nCol; j++){ |
| 147646 | | - if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++; |
| 147647 | | - } |
| 147648 | | - if( mxCol==pTab->iPKey ) mxCol--; |
| 148430 | + mxCol = pTab->nNVCol-1; |
| 147649 | 148431 | }else{ |
| 147650 | 148432 | /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID |
| 147651 | 148433 | ** PK index column-count, so there is no need to account for them |
| 147652 | 148434 | ** in this case. */ |
| 147653 | 148435 | mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1; |
| 147654 | 148436 | } |
| 147655 | | - if( mxCol>=0 ){ |
| 147656 | | - sqlite3VdbeAddOp3(v, OP_Column, iDataCur, mxCol, 3); |
| 147657 | | - sqlite3VdbeTypeofColumn(v, 3); |
| 147658 | | - } |
| 148437 | + assert( mxCol>=0 ); |
| 148438 | + sqlite3VdbeAddOp3(v, OP_Column, iDataCur, mxCol, 3); |
| 148439 | + sqlite3VdbeTypeofColumn(v, 3); |
| 147659 | 148440 | |
| 147660 | 148441 | if( !isQuick ){ |
| 147661 | 148442 | if( pPk ){ |
| 147662 | 148443 | /* Verify WITHOUT ROWID keys are in ascending order */ |
| 147663 | 148444 | int a1; |
| | @@ -147667,11 +148448,11 @@ |
| 147667 | 148448 | sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v); |
| 147668 | 148449 | zErr = sqlite3MPrintf(db, |
| 147669 | 148450 | "row not in PRIMARY KEY order for %s", |
| 147670 | 148451 | pTab->zName); |
| 147671 | 148452 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 147672 | | - integrityCheckResultRow(v); |
| 148453 | + integrityCheckResultRow(v, iDataCur, mxCol); |
| 147673 | 148454 | sqlite3VdbeJumpHere(v, a1); |
| 147674 | 148455 | sqlite3VdbeJumpHere(v, a1+1); |
| 147675 | 148456 | for(j=0; j<pPk->nKeyCol; j++){ |
| 147676 | 148457 | sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j); |
| 147677 | 148458 | } |
| | @@ -147804,11 +148585,11 @@ |
| 147804 | 148585 | zErr = sqlite3MPrintf(db, "TEXT value in %s.%s", |
| 147805 | 148586 | pTab->zName, pTab->aCol[j].zCnName); |
| 147806 | 148587 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 147807 | 148588 | } |
| 147808 | 148589 | sqlite3VdbeResolveLabel(v, labelError); |
| 147809 | | - integrityCheckResultRow(v); |
| 148590 | + integrityCheckResultRow(v, iDataCur, mxCol); |
| 147810 | 148591 | sqlite3VdbeResolveLabel(v, labelOk); |
| 147811 | 148592 | } |
| 147812 | 148593 | /* Verify CHECK constraints */ |
| 147813 | 148594 | if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
| 147814 | 148595 | ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0); |
| | @@ -147826,11 +148607,11 @@ |
| 147826 | 148607 | sqlite3VdbeResolveLabel(v, addrCkFault); |
| 147827 | 148608 | pParse->iSelfTab = 0; |
| 147828 | 148609 | zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s", |
| 147829 | 148610 | pTab->zName); |
| 147830 | 148611 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 147831 | | - integrityCheckResultRow(v); |
| 148612 | + integrityCheckResultRow(v, 0, -1); |
| 147832 | 148613 | sqlite3VdbeResolveLabel(v, addrCkOk); |
| 147833 | 148614 | } |
| 147834 | 148615 | sqlite3ExprListDelete(db, pCheck); |
| 147835 | 148616 | } |
| 147836 | 148617 | if( !isQuick ){ /* Omit the remaining tests for quick_check */ |
| | @@ -147852,21 +148633,21 @@ |
| 147852 | 148633 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
| 147853 | 148634 | sqlite3MPrintf(db, "index %s stores an imprecise floating-point " |
| 147854 | 148635 | "value for row ", pIdx->zName), |
| 147855 | 148636 | P4_DYNAMIC); |
| 147856 | 148637 | sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); |
| 147857 | | - integrityCheckResultRow(v); |
| 148638 | + integrityCheckResultRow(v, 0, -1); |
| 147858 | 148639 | sqlite3VdbeAddOp2(v, OP_Goto, 0, ckUniq); |
| 147859 | 148640 | |
| 147860 | 148641 | sqlite3VdbeJumpHere(v, jmp2); |
| 147861 | 148642 | sqlite3VdbeLoadString(v, 3, "row "); |
| 147862 | 148643 | sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); |
| 147863 | 148644 | sqlite3VdbeLoadString(v, 4, " missing from index "); |
| 147864 | 148645 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); |
| 147865 | 148646 | jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); |
| 147866 | 148647 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); |
| 147867 | | - jmp4 = integrityCheckResultRow(v); |
| 148648 | + jmp4 = integrityCheckResultRow(v, 0, -1); |
| 147868 | 148649 | sqlite3VdbeResolveLabel(v, ckUniq); |
| 147869 | 148650 | |
| 147870 | 148651 | /* The OP_IdxRowid opcode is an optimized version of OP_Column |
| 147871 | 148652 | ** that extracts the rowid off the end of the index record. |
| 147872 | 148653 | ** But it only works correctly if index record does not have |
| | @@ -147961,11 +148742,11 @@ |
| 147961 | 148742 | if( pVTab->pModule->xIntegrity==0 ) continue; |
| 147962 | 148743 | sqlite3VdbeAddOp3(v, OP_VCheck, i, 3, isQuick); |
| 147963 | 148744 | pTab->nTabRef++; |
| 147964 | 148745 | sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF); |
| 147965 | 148746 | a1 = sqlite3VdbeAddOp1(v, OP_IsNull, 3); VdbeCoverage(v); |
| 147966 | | - integrityCheckResultRow(v); |
| 148747 | + integrityCheckResultRow(v, 0, -1); |
| 147967 | 148748 | sqlite3VdbeJumpHere(v, a1); |
| 147968 | 148749 | continue; |
| 147969 | 148750 | } |
| 147970 | 148751 | #endif |
| 147971 | 148752 | } |
| | @@ -148761,15 +149542,17 @@ |
| 148761 | 149542 | PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); |
| 148762 | 149543 | int rc; |
| 148763 | 149544 | int i, j; |
| 148764 | 149545 | StrAccum acc; |
| 148765 | 149546 | char *zSql; |
| 149547 | + int type0 = SQLITE_NULL; |
| 148766 | 149548 | |
| 148767 | 149549 | UNUSED_PARAMETER(idxNum); |
| 148768 | 149550 | UNUSED_PARAMETER(idxStr); |
| 148769 | 149551 | pragmaVtabCursorClear(pCsr); |
| 148770 | 149552 | j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1; |
| 149553 | + if( argc>0 ) type0 = sqlite3_value_type(argv[0]); |
| 148771 | 149554 | for(i=0; i<argc; i++, j++){ |
| 148772 | 149555 | const char *zText = (const char*)sqlite3_value_text(argv[i]); |
| 148773 | 149556 | assert( j<ArraySize(pCsr->azArg) ); |
| 148774 | 149557 | assert( pCsr->azArg[j]==0 ); |
| 148775 | 149558 | if( zText ){ |
| | @@ -148784,11 +149567,15 @@ |
| 148784 | 149567 | if( pCsr->azArg[1] ){ |
| 148785 | 149568 | sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]); |
| 148786 | 149569 | } |
| 148787 | 149570 | sqlite3_str_appendall(&acc, pTab->pName->zName); |
| 148788 | 149571 | if( pCsr->azArg[0] ){ |
| 148789 | | - sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]); |
| 149572 | + if( type0<=SQLITE_FLOAT ){ |
| 149573 | + sqlite3_str_appendf(&acc, "=%s", pCsr->azArg[0]); |
| 149574 | + }else{ |
| 149575 | + sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]); |
| 149576 | + } |
| 148790 | 149577 | } |
| 148791 | 149578 | zSql = sqlite3StrAccumFinish(&acc); |
| 148792 | 149579 | if( zSql==0 ) return SQLITE_NOMEM; |
| 148793 | 149580 | rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0); |
| 148794 | 149581 | sqlite3_free(zSql); |
| | @@ -149018,10 +149805,17 @@ |
| 149018 | 149805 | const char *zEnd; |
| 149019 | 149806 | TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ |
| 149020 | 149807 | |
| 149021 | 149808 | assert( db->init.busy ); |
| 149022 | 149809 | db->init.iDb = iDb; |
| 149810 | + |
| 149811 | + /* We never try to parse a trigger on a read-only database */ |
| 149812 | + assert( db->aDb[iDb].pBt==0 |
| 149813 | + || sqlite3BtreeIsReadonly(db->aDb[iDb].pBt)==0 |
| 149814 | + || sqlite3_stricmp(argv[0],"trigger")!=0 |
| 149815 | + || CORRUPT_DB ); |
| 149816 | + |
| 149023 | 149817 | if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0 |
| 149024 | 149818 | || (db->init.newTnum>pData->mxPage && pData->mxPage>0) |
| 149025 | 149819 | ){ |
| 149026 | 149820 | if( sqlite3Config.bExtraSchemaChecks ){ |
| 149027 | 149821 | corruptSchema(pData, argv, "invalid rootpage"); |
| | @@ -149267,12 +150061,13 @@ |
| 149267 | 150061 | assert( db->init.busy ); |
| 149268 | 150062 | initData.mxPage = sqlite3BtreeLastPage(pDb->pBt); |
| 149269 | 150063 | { |
| 149270 | 150064 | char *zSql; |
| 149271 | 150065 | zSql = sqlite3MPrintf(db, |
| 149272 | | - "SELECT*FROM\"%w\".%s ORDER BY rowid", |
| 149273 | | - db->aDb[iDb].zDbSName, zSchemaTabName); |
| 150066 | + "SELECT*FROM\"%w\".%s %sORDER BY rowid", |
| 150067 | + db->aDb[iDb].zDbSName, zSchemaTabName, |
| 150068 | + sqlite3BtreeIsReadonly(db->aDb[iDb].pBt)?"WHERE type<>'trigger'":""); |
| 149274 | 150069 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 149275 | 150070 | { |
| 149276 | 150071 | sqlite3_xauth xAuth; |
| 149277 | 150072 | xAuth = db->xAuth; |
| 149278 | 150073 | db->xAuth = 0; |
| | @@ -149289,10 +150084,16 @@ |
| 149289 | 150084 | sqlite3AnalysisLoad(db, iDb); |
| 149290 | 150085 | } |
| 149291 | 150086 | #endif |
| 149292 | 150087 | } |
| 149293 | 150088 | assert( pDb == &(db->aDb[iDb]) ); |
| 150089 | + |
| 150090 | + /* Confirm that TRIGGERs are not parsed on a read-only database */ |
| 150091 | + assert( sqlite3BtreeIsReadonly(pDb->pBt)==0 |
| 150092 | + || sqliteHashCount(&pDb->pSchema->trigHash)==0 |
| 150093 | + || CORRUPT_DB ); |
| 150094 | + |
| 149294 | 150095 | if( db->mallocFailed ){ |
| 149295 | 150096 | rc = SQLITE_NOMEM_BKPT; |
| 149296 | 150097 | sqlite3ResetAllSchemasOfConnection(db); |
| 149297 | 150098 | pDb = &db->aDb[iDb]; |
| 149298 | 150099 | }else |
| | @@ -149324,11 +150125,11 @@ |
| 149324 | 150125 | error_out: |
| 149325 | 150126 | if( rc ){ |
| 149326 | 150127 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
| 149327 | 150128 | sqlite3OomFault(db); |
| 149328 | 150129 | } |
| 149329 | | - sqlite3ResetOneSchema(db, iDb); |
| 150130 | + sqlite3ResetOneSchema(db, iDb, 0); |
| 149330 | 150131 | } |
| 149331 | 150132 | db->init.busy = 0; |
| 149332 | 150133 | return rc; |
| 149333 | 150134 | } |
| 149334 | 150135 | |
| | @@ -149426,11 +150227,11 @@ |
| 149426 | 150227 | ** set Parse.rc to SQLITE_SCHEMA. */ |
| 149427 | 150228 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); |
| 149428 | 150229 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 149429 | 150230 | if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
| 149430 | 150231 | if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA; |
| 149431 | | - sqlite3ResetOneSchema(db, iDb); |
| 150232 | + sqlite3ResetOneSchema(db, iDb, 0); |
| 149432 | 150233 | } |
| 149433 | 150234 | |
| 149434 | 150235 | /* Close the transaction, if one was opened. */ |
| 149435 | 150236 | if( openedTransaction ){ |
| 149436 | 150237 | sqlite3BtreeCommit(pBt); |
| | @@ -149768,11 +150569,11 @@ |
| 149768 | 150569 | rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail); |
| 149769 | 150570 | assert( rc==SQLITE_OK || *ppStmt==0 ); |
| 149770 | 150571 | if( rc==SQLITE_OK || db->mallocFailed ) break; |
| 149771 | 150572 | cnt++; |
| 149772 | 150573 | }while( (rc==SQLITE_ERROR_RETRY && ALWAYS(cnt<=SQLITE_MAX_PREPARE_RETRY)) |
| 149773 | | - || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt)==1) ); |
| 150574 | + || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1,0), cnt)==1) ); |
| 149774 | 150575 | sqlite3BtreeLeaveAll(db); |
| 149775 | 150576 | assert( rc!=SQLITE_ERROR_RETRY ); |
| 149776 | 150577 | rc = sqlite3ApiExit(db, rc); |
| 149777 | 150578 | assert( (rc&db->errMask)==rc ); |
| 149778 | 150579 | db->busyHandler.nBusy = 0; |
| | @@ -150492,10 +151293,41 @@ |
| 150492 | 151293 | } |
| 150493 | 151294 | unsetJoinExpr(p->pLeft, iTable, nullable); |
| 150494 | 151295 | p = p->pRight; |
| 150495 | 151296 | } |
| 150496 | 151297 | } |
| 151298 | + |
| 151299 | +/* |
| 151300 | +** Tree walker expression callback for clearOuterOnExpr(). |
| 151301 | +** |
| 151302 | +** If the Expr node is marked as coming from an outer join for |
| 151303 | +** table pWalker->u.iCur, then clear its join markers completely. |
| 151304 | +*/ |
| 151305 | +static int clearOuterOnExprCb(Walker *pWalker, Expr *p){ |
| 151306 | + if( ExprHasProperty(p, EP_OuterON) && p->w.iJoin==pWalker->u.iCur ){ |
| 151307 | + ExprClearProperty(p, EP_OuterON|EP_InnerON); |
| 151308 | + } |
| 151309 | + return WRC_Continue; |
| 151310 | +} |
| 151311 | + |
| 151312 | +/* |
| 151313 | +** Remove the EP_OuterON mark, without substituting EP_InnerON, from every |
| 151314 | +** term of the expression tree p that is marked EP_OuterON with w.iJoin==iTable. |
| 151315 | +** The resulting terms are ordinary WHERE-clause terms. |
| 151316 | +** |
| 151317 | +** This is used when table iTable was marked as an outer-join table only |
| 151318 | +** because it was the left operand of a RIGHT JOIN (JT_LTORJ) and that |
| 151319 | +** status has been removed by OUTER JOIN strength reduction. |
| 151320 | +*/ |
| 151321 | +static void clearOuterOnExpr(Expr *p, int iTable){ |
| 151322 | + Walker w; |
| 151323 | + memset(&w, 0, sizeof(w)); |
| 151324 | + w.u.iCur = iTable; |
| 151325 | + w.xExprCallback = clearOuterOnExprCb; |
| 151326 | + w.xSelectCallback = 0; |
| 151327 | + sqlite3WalkExpr(&w,p); |
| 151328 | +} |
| 150497 | 151329 | |
| 150498 | 151330 | /* |
| 150499 | 151331 | ** This routine processes the join information for a SELECT statement. |
| 150500 | 151332 | ** |
| 150501 | 151333 | ** * A NATURAL join is converted into a USING join. After that, we |
| | @@ -150522,10 +151354,11 @@ |
| 150522 | 151354 | SrcItem *pLeft; /* Left table being joined */ |
| 150523 | 151355 | SrcItem *pRight; /* Right table being joined */ |
| 150524 | 151356 | int bSeenTFunc = 0; /* True if have seen table-valued function */ |
| 150525 | 151357 | |
| 150526 | 151358 | pSrc = p->pSrc; |
| 151359 | + if( pSrc->nSrc<=1 ) return 0; |
| 150527 | 151360 | pLeft = &pSrc->a[0]; |
| 150528 | 151361 | pRight = &pLeft[1]; |
| 150529 | 151362 | for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){ |
| 150530 | 151363 | Table *pRightTab = pRight->pSTab; |
| 150531 | 151364 | u32 joinType; |
| | @@ -151254,17 +152087,17 @@ |
| 151254 | 152087 | if( srcTab>=0 ){ |
| 151255 | 152088 | for(i=0; i<nResultCol; i++){ |
| 151256 | 152089 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i); |
| 151257 | 152090 | VdbeComment((v, "%s", p->pEList->a[i].zEName)); |
| 151258 | 152091 | } |
| 151259 | | - }else if( eDest!=SRT_Exists ){ |
| 152092 | + }else if( eDest!=SRT_Exists || hasDistinct ){ |
| 152093 | + /* If the destination is an EXISTS(...) expression, the actual |
| 152094 | + ** values returned by the SELECT are only required if the query is |
| 152095 | + ** DISTINCT. */ |
| 151260 | 152096 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 151261 | 152097 | ExprList *pExtra = 0; |
| 151262 | 152098 | #endif |
| 151263 | | - /* If the destination is an EXISTS(...) expression, the actual |
| 151264 | | - ** values returned by the SELECT are not required. |
| 151265 | | - */ |
| 151266 | 152099 | u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */ |
| 151267 | 152100 | ExprList *pEList; |
| 151268 | 152101 | if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ |
| 151269 | 152102 | ecelFlags = SQLITE_ECEL_DUP; |
| 151270 | 152103 | }else{ |
| | @@ -152647,16 +153480,20 @@ |
| 152647 | 153480 | pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); |
| 152648 | 153481 | }else{ |
| 152649 | 153482 | pRet = 0; |
| 152650 | 153483 | } |
| 152651 | 153484 | assert( iCol>=0 ); |
| 152652 | | - /* iCol must be less than p->pEList->nExpr. Otherwise an error would |
| 153485 | + /* iCol must be less than p->pEList->nExpr. Otherwise an error would |
| 152653 | 153486 | ** have been thrown during name resolution and we would not have gotten |
| 152654 | | - ** this far */ |
| 152655 | | - if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){ |
| 153487 | + ** this far. If p is really a multi-row VALUES statement, it is guaranteed |
| 153488 | + ** not to have any collation sequences. So leave pRet set to 0 in this case */ |
| 153489 | + if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) |
| 153490 | + && ((p->selFlags & SF_Values)==0 || p->pSrc->nSrc==0) /* multi-row VALUES */ |
| 153491 | + ){ |
| 152656 | 153492 | pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); |
| 152657 | 153493 | } |
| 153494 | + |
| 152658 | 153495 | return pRet; |
| 152659 | 153496 | } |
| 152660 | 153497 | |
| 152661 | 153498 | /* |
| 152662 | 153499 | ** The select statement passed as the second parameter is a compound SELECT |
| | @@ -152864,11 +153701,11 @@ |
| 152864 | 153701 | |
| 152865 | 153702 | /* Find the next row in the Queue and output that row */ |
| 152866 | 153703 | addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v); |
| 152867 | 153704 | |
| 152868 | 153705 | /* Transfer the next row in Queue over to Current */ |
| 152869 | | - sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */ |
| 153706 | + sqlite3VdbeAddOp1(v, OP_Stale, iCurrent); /* To reset column cache */ |
| 152870 | 153707 | if( pOrderBy ){ |
| 152871 | 153708 | sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent); |
| 152872 | 153709 | }else{ |
| 152873 | 153710 | sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent); |
| 152874 | 153711 | } |
| | @@ -152968,10 +153805,21 @@ |
| 152968 | 153805 | */ |
| 152969 | 153806 | static int hasAnchor(Select *p){ |
| 152970 | 153807 | while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; } |
| 152971 | 153808 | return p!=0; |
| 152972 | 153809 | } |
| 153810 | + |
| 153811 | +/* |
| 153812 | +** Return TRUE if every compound operator in the compound SELECT that |
| 153813 | +** ends with p is UNION ALL. |
| 153814 | +*/ |
| 153815 | +static int selectIsUnionAll(Select *p){ |
| 153816 | + for(; p->pPrior; p=p->pPrior){ |
| 153817 | + if( p->op!=TK_ALL ) return 0; |
| 153818 | + } |
| 153819 | + return 1; |
| 153820 | +} |
| 152973 | 153821 | |
| 152974 | 153822 | /* |
| 152975 | 153823 | ** Return TRUE if p is a UNION with a LIMIT of exactly 1 and no OFFSET |
| 152976 | 153824 | ** clause. This is a precondition for a couple of related optimizations. |
| 152977 | 153825 | ** |
| | @@ -153080,11 +153928,11 @@ |
| 153080 | 153928 | #endif |
| 153081 | 153929 | if( p->pOrderBy ){ |
| 153082 | 153930 | /* If the compound has an ORDER BY clause, then always use the merge |
| 153083 | 153931 | ** algorithm. */ |
| 153084 | 153932 | return multiSelectByMerge(pParse, p, pDest); |
| 153085 | | - }else if( p->op!=TK_ALL && !unionWithLimitOne(db,p) ){ |
| 153933 | + }else if( !selectIsUnionAll(p) && !unionWithLimitOne(db,p) ){ |
| 153086 | 153934 | /* If the compound is EXCEPT, INTERSECT, or UNION (anything other than |
| 153087 | 153935 | ** UNION ALL) then also always use the merge algorithm. However, the |
| 153088 | 153936 | ** multiSelectByMerge() routine requires that the compound have an |
| 153089 | 153937 | ** ORDER BY clause, and it doesn't right now. So invent one first. */ |
| 153090 | 153938 | Expr *pOne = sqlite3ExprInt32(db, 1); |
| | @@ -153236,30 +154084,34 @@ |
| 153236 | 154084 | switch( pDest->eDest ){ |
| 153237 | 154085 | /* Store the result as data using a unique key. |
| 153238 | 154086 | */ |
| 153239 | 154087 | case SRT_Fifo: |
| 153240 | 154088 | case SRT_DistFifo: |
| 154089 | +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 153241 | 154090 | case SRT_Table: |
| 154091 | +#endif |
| 153242 | 154092 | case SRT_EphemTab: { |
| 153243 | 154093 | int r1 = sqlite3GetTempReg(pParse); |
| 153244 | 154094 | int r2 = sqlite3GetTempReg(pParse); |
| 153245 | 154095 | int iParm = pDest->iSDParm; |
| 153246 | | - testcase( pDest->eDest==SRT_Table ); |
| 153247 | 154096 | testcase( pDest->eDest==SRT_EphemTab ); |
| 153248 | 154097 | testcase( pDest->eDest==SRT_Fifo ); |
| 153249 | 154098 | testcase( pDest->eDest==SRT_DistFifo ); |
| 153250 | 154099 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1); |
| 154100 | +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 154101 | + testcase( pDest->eDest==SRT_Table ); |
| 153251 | 154102 | #if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG) |
| 153252 | 154103 | /* A destination of SRT_Table and a non-zero iSDParm2 parameter means |
| 153253 | 154104 | ** that this is an "UPDATE ... FROM" on a virtual table or view. In this |
| 153254 | 154105 | ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC. |
| 153255 | 154106 | ** This does not affect operation in any way - it just allows MakeRecord |
| 153256 | 154107 | ** to process OPFLAG_NOCHANGE values without an assert() failing. */ |
| 153257 | 154108 | if( pDest->eDest==SRT_Table && pDest->iSDParm2 ){ |
| 153258 | 154109 | sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC); |
| 153259 | 154110 | } |
| 153260 | | -#endif |
| 154111 | +#endif /* !ENABLE_NULL_TRIM && DEBUG */ |
| 154112 | +#endif /* UPDATE_DELETE_LIMIT */ |
| 153261 | 154113 | #ifndef SQLITE_OMIT_CTE |
| 153262 | 154114 | if( pDest->eDest==SRT_DistFifo ){ |
| 153263 | 154115 | /* If the destination is DistFifo, then cursor (iParm+1) is open |
| 153264 | 154116 | ** on an ephemeral index that is used to enforce uniqueness on the |
| 153265 | 154117 | ** total result. At this point, we are processing the setup portion |
| | @@ -153863,10 +154715,19 @@ |
| 153863 | 154715 | ** |
| 153864 | 154716 | ** When the subquery on the right side of the LEFT JOIN is flattened, we |
| 153865 | 154717 | ** have to add OP_IfNullRow in front of the OP_Integer that implements the |
| 153866 | 154718 | ** "m" value of the subquery so that a NULL will be loaded instead of 59 |
| 153867 | 154719 | ** when processing a non-matched row of the left. |
| 154720 | +** |
| 154721 | +** ## Unwrap mode: |
| 154722 | +** |
| 154723 | +** If pEList is NULL, then the SubstContext is being used by |
| 154724 | +** unwrapIfNullRow() to remove TK_IF_NULL_ROW wrappers instead of to |
| 154725 | +** substitute column references. In that mode, only the TK_IF_NULL_ROW |
| 154726 | +** nodes with iTable==iTable are touched, each one being replaced by its |
| 154727 | +** operand. Column references are left alone and iNewTable must equal |
| 154728 | +** iTable. |
| 153868 | 154729 | */ |
| 153869 | 154730 | typedef struct SubstContext { |
| 153870 | 154731 | Parse *pParse; /* The parsing context */ |
| 153871 | 154732 | int iTable; /* Replace references to this table */ |
| 153872 | 154733 | int iNewTable; /* New table number */ |
| | @@ -153904,10 +154765,11 @@ |
| 153904 | 154765 | testcase( ExprHasProperty(pExpr, EP_InnerON) ); |
| 153905 | 154766 | pExpr->w.iJoin = pSubst->iNewTable; |
| 153906 | 154767 | } |
| 153907 | 154768 | if( pExpr->op==TK_COLUMN |
| 153908 | 154769 | && pExpr->iTable==pSubst->iTable |
| 154770 | + && pSubst->pEList!=0 |
| 153909 | 154771 | && !ExprHasProperty(pExpr, EP_FixedCol) |
| 153910 | 154772 | ){ |
| 153911 | 154773 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 153912 | 154774 | if( pExpr->iColumn<0 ){ |
| 153913 | 154775 | pExpr->op = TK_NULL; |
| | @@ -153975,13 +154837,23 @@ |
| 153975 | 154837 | pExpr = pNew; |
| 153976 | 154838 | } |
| 153977 | 154839 | } |
| 153978 | 154840 | }else{ |
| 153979 | 154841 | if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){ |
| 154842 | + if( pSubst->pEList==0 ){ |
| 154843 | + /* Unwrap mode. See unwrapIfNullRow(). */ |
| 154844 | + Expr *pInner = pExpr->pLeft; |
| 154845 | + pExpr->pLeft = 0; |
| 154846 | + sqlite3ExprDelete(pSubst->pParse->db, pExpr); |
| 154847 | + return substExpr(pSubst, pInner); |
| 154848 | + } |
| 153980 | 154849 | pExpr->iTable = pSubst->iNewTable; |
| 153981 | 154850 | } |
| 153982 | | - if( pExpr->op==TK_AGG_FUNCTION && pExpr->op2>=pSubst->nSelDepth ){ |
| 154851 | + if( pExpr->op==TK_AGG_FUNCTION |
| 154852 | + && pSubst->pEList!=0 |
| 154853 | + && pExpr->op2>=pSubst->nSelDepth |
| 154854 | + ){ |
| 153983 | 154855 | pExpr->op2--; |
| 153984 | 154856 | } |
| 153985 | 154857 | pExpr->pLeft = substExpr(pSubst, pExpr->pLeft); |
| 153986 | 154858 | pExpr->pRight = substExpr(pSubst, pExpr->pRight); |
| 153987 | 154859 | if( ExprUseXSelect(pExpr) ){ |
| | @@ -154037,10 +154909,38 @@ |
| 154037 | 154909 | } |
| 154038 | 154910 | } |
| 154039 | 154911 | }while( doPrior && (p = p->pPrior)!=0 ); |
| 154040 | 154912 | pSubst->nSelDepth--; |
| 154041 | 154913 | } |
| 154914 | + |
| 154915 | +/* |
| 154916 | +** The flattener wraps each substituted expression in a TK_IF_NULL_ROW |
| 154917 | +** operator when the subquery being flattened is on the NULL-able side of |
| 154918 | +** an outer join. The wrapper is keyed to the cursor iCur of the |
| 154919 | +** (single-table) subquery. |
| 154920 | +** |
| 154921 | +** If OUTER JOIN strength reduction later determines that the table with |
| 154922 | +** cursor iCur can no longer be a NULL row, then those wrappers are |
| 154923 | +** unnecessary. Worse, they are harmful if the FROM-clause term for iCur |
| 154924 | +** is itself a subquery that is flattened later. The flattener would |
| 154925 | +** then renumber the wrappers to refer to the first table of that |
| 154926 | +** subquery, and that table might legitimately be a NULL row (for example, |
| 154927 | +** if it is the left operand of a FULL JOIN inside the subquery). The |
| 154928 | +** substituted expressions would then be incorrectly forced to NULL. |
| 154929 | +** |
| 154930 | +** So when strength reduction removes the NULL-able status from the |
| 154931 | +** table at iCur, remove all of the TK_IF_NULL_ROW wrappers for iCur from |
| 154932 | +** the entire SELECT statement p. |
| 154933 | +*/ |
| 154934 | +static void unwrapIfNullRow(Parse *pParse, Select *p, int iCur){ |
| 154935 | + SubstContext x; |
| 154936 | + memset(&x, 0, sizeof(x)); |
| 154937 | + x.pParse = pParse; |
| 154938 | + x.iTable = iCur; |
| 154939 | + x.iNewTable = iCur; |
| 154940 | + substSelect(&x, p, 0); |
| 154941 | +} |
| 154042 | 154942 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 154043 | 154943 | |
| 154044 | 154944 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 154045 | 154945 | /* |
| 154046 | 154946 | ** pSelect is a SELECT statement and pSrcItem is one item in the FROM |
| | @@ -154213,10 +155113,34 @@ |
| 154213 | 155113 | } |
| 154214 | 155114 | return 0; |
| 154215 | 155115 | } |
| 154216 | 155116 | |
| 154217 | 155117 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 155118 | +/* |
| 155119 | +** Return true if the FROM clause pSrc, other than item iExcept, contains |
| 155120 | +** a subquery implemented using a co-routine. Such co-routines may have |
| 155121 | +** been coded while the SQL text was being parsed, before query planning. |
| 155122 | +*/ |
| 155123 | +static int srcListHasCoroutine(const SrcList *pSrc, int iExcept){ |
| 155124 | + int i; |
| 155125 | + for(i=0; i<pSrc->nSrc; i++){ |
| 155126 | + const SrcItem *pItem = &pSrc->a[i]; |
| 155127 | + if( i==iExcept ) continue; |
| 155128 | + if( pItem->fg.viaCoroutine ) return 1; |
| 155129 | + if( pItem->fg.isSubquery ){ |
| 155130 | + Select *pSelect; |
| 155131 | + for(pSelect=pItem->u4.pSubq->pSelect; |
| 155132 | + pSelect; |
| 155133 | + pSelect=pSelect->pPrior |
| 155134 | + ){ |
| 155135 | + if( srcListHasCoroutine(pSelect->pSrc, -1) ) return 1; |
| 155136 | + } |
| 155137 | + } |
| 155138 | + } |
| 155139 | + return 0; |
| 155140 | +} |
| 155141 | + |
| 154218 | 155142 | /* |
| 154219 | 155143 | ** This routine attempts to flatten subqueries as a performance optimization. |
| 154220 | 155144 | ** This routine returns 1 if it makes changes and 0 if no flattening occurs. |
| 154221 | 155145 | ** |
| 154222 | 155146 | ** To understand the concept of flattening, consider the following |
| | @@ -154541,10 +155465,11 @@ |
| 154541 | 155465 | if( compoundHasDifferentAffinities(pSub) ) return 0; |
| 154542 | 155466 | |
| 154543 | 155467 | if( pSrc->nSrc>1 ){ |
| 154544 | 155468 | if( pParse->nSelect>500 ) return 0; |
| 154545 | 155469 | if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0; |
| 155470 | + if( p->pOrderBy && srcListHasCoroutine(pSrc, iFrom) ) return 0; |
| 154546 | 155471 | aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int)); |
| 154547 | 155472 | if( aCsrMap ) aCsrMap[0] = pParse->nTab; |
| 154548 | 155473 | } |
| 154549 | 155474 | } |
| 154550 | 155475 | |
| | @@ -154829,11 +155754,11 @@ |
| 154829 | 155754 | struct WhereConst { |
| 154830 | 155755 | Parse *pParse; /* Parsing context */ |
| 154831 | 155756 | u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */ |
| 154832 | 155757 | int nConst; /* Number for COLUMN=CONSTANT terms */ |
| 154833 | 155758 | int nChng; /* Number of times a constant is propagated */ |
| 154834 | | - int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */ |
| 155759 | + int bHasAffBlob; /* A column in apExpr[] has affinity NONE or BLOB */ |
| 154835 | 155760 | u32 mExcludeOn; /* Which ON expressions to exclude from considertion. |
| 154836 | 155761 | ** Either EP_OuterON or EP_InnerON|EP_OuterON */ |
| 154837 | 155762 | Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */ |
| 154838 | 155763 | }; |
| 154839 | 155764 | |
| | @@ -154997,12 +155922,13 @@ |
| 154997 | 155922 | assert( TK_GE==TK_EQ+4 ); |
| 154998 | 155923 | if( pConst->bHasAffBlob ){ |
| 154999 | 155924 | if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE) |
| 155000 | 155925 | || pExpr->op==TK_IS |
| 155001 | 155926 | ){ |
| 155002 | | - propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0); |
| 155003 | | - if( pConst->pOomFault[0] ) return WRC_Prune; |
| 155927 | + if( sqlite3ExprAffinity(pExpr->pRight)!=SQLITE_AFF_TEXT ){ |
| 155928 | + propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0); |
| 155929 | + } |
| 155004 | 155930 | if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){ |
| 155005 | 155931 | propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0); |
| 155006 | 155932 | } |
| 155007 | 155933 | } |
| 155008 | 155934 | } |
| | @@ -155466,14 +156392,19 @@ |
| 155466 | 156392 | for(pX=pSub; pX; pX=pX->pPrior) { |
| 155467 | 156393 | Expr *pY = pX->pEList->a[j].pExpr; |
| 155468 | 156394 | if( pY->op==TK_NULL ) continue; |
| 155469 | 156395 | pY->op = TK_NULL; |
| 155470 | 156396 | ExprClearProperty(pY, EP_Skip|EP_Unlikely); |
| 155471 | | - pX->selFlags |= SF_PushDown; |
| 155472 | 156397 | nChng++; |
| 155473 | 156398 | } |
| 155474 | 156399 | } |
| 156400 | + if( nChng ){ |
| 156401 | + /* If there were any changes, mark the SELECT as modified. This is |
| 156402 | + ** to let isSelfJoinView() know not to allow another reference to this |
| 156403 | + ** view to use the existing materialization. */ |
| 156404 | + pSub->selFlags |= SF_PushDown; |
| 156405 | + } |
| 155475 | 156406 | return nChng; |
| 155476 | 156407 | } |
| 155477 | 156408 | |
| 155478 | 156409 | |
| 155479 | 156410 | /* |
| | @@ -156447,10 +157378,11 @@ |
| 156447 | 157378 | pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName); |
| 156448 | 157379 | pX->fg.eEName = ENAME_NAME; |
| 156449 | 157380 | }else{ |
| 156450 | 157381 | pX->zEName = sqlite3DbStrDup(db, zName); |
| 156451 | 157382 | pX->fg.eEName = ENAME_NAME; |
| 157383 | + if( IN_RENAME_OBJECT ) pX->fg.bStarName = 1; |
| 156452 | 157384 | } |
| 156453 | 157385 | } |
| 156454 | 157386 | } |
| 156455 | 157387 | if( !tableSeen ){ |
| 156456 | 157388 | if( zTName ){ |
| | @@ -156816,10 +157748,13 @@ |
| 156816 | 157748 | assert( pAggInfo->iFirstReg>0 ); |
| 156817 | 157749 | assert( pParse->db->pParse==pParse ); |
| 156818 | 157750 | assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
| 156819 | 157751 | if( nReg==0 ) return; |
| 156820 | 157752 | if( pParse->nErr ) return; |
| 157753 | + if( pAggInfo->nFunc ){ |
| 157754 | + sqlite3MayAbort(pParse); |
| 157755 | + } |
| 156821 | 157756 | sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg, |
| 156822 | 157757 | pAggInfo->iFirstReg+nReg-1); |
| 156823 | 157758 | for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){ |
| 156824 | 157759 | if( pFunc->iDistinct>=0 ){ |
| 156825 | 157760 | Expr *pE = pFunc->pFExpr; |
| | @@ -157826,11 +158761,19 @@ |
| 157826 | 158761 | sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, |
| 157827 | 158762 | p->pOrderBy); |
| 157828 | 158763 | testcase( pParse->earlyCleanup ); |
| 157829 | 158764 | p->pOrderBy = 0; |
| 157830 | 158765 | } |
| 157831 | | - p->selFlags &= ~(u32)SF_Distinct; |
| 158766 | + /* Do not ignore DISTINCT for an EXISTS subquery with an OFFSET clause. |
| 158767 | + ** In this case the OFFSET must be applied after duplicates are discarded, |
| 158768 | + ** so the DISTINCT is required. */ |
| 158769 | + if( pDest->eDest!=SRT_Exists |
| 158770 | + || (ALWAYS(p->pLimit!=0) && p->pLimit->pRight==0) |
| 158771 | + /* ^^^^^^-- because tag-202611101908 and SRT_Exists set nowhere else */ |
| 158772 | + ){ |
| 158773 | + p->selFlags &= ~(u32)SF_Distinct; |
| 158774 | + } |
| 157832 | 158775 | } |
| 157833 | 158776 | sqlite3SelectPrep(pParse, p, 0); |
| 157834 | 158777 | if( pParse->nErr ){ |
| 157835 | 158778 | goto select_end; |
| 157836 | 158779 | } |
| | @@ -157949,12 +158892,17 @@ |
| 157949 | 158892 | unsetJoinExpr(p->pWhere, pI2->iCursor, 1); |
| 157950 | 158893 | } |
| 157951 | 158894 | } |
| 157952 | 158895 | } |
| 157953 | 158896 | for(j=pTabList->nSrc-1; j>=0; j--){ |
| 157954 | | - pTabList->a[j].fg.jointype &= ~JT_LTORJ; |
| 157955 | | - if( pTabList->a[j].fg.jointype & JT_RIGHT ) break; |
| 158897 | + SrcItem *pI2 = &pTabList->a[j]; |
| 158898 | + pI2->fg.jointype &= ~JT_LTORJ; |
| 158899 | + if( (pI2->fg.jointype & (JT_LEFT|JT_RIGHT))==0 ){ |
| 158900 | + clearOuterOnExpr(p->pWhere, pI2->iCursor); |
| 158901 | + unwrapIfNullRow(pParse, p, pI2->iCursor); |
| 158902 | + } |
| 158903 | + if( pI2->fg.jointype & JT_RIGHT ) break; |
| 157956 | 158904 | } |
| 157957 | 158905 | } |
| 157958 | 158906 | } |
| 157959 | 158907 | |
| 157960 | 158908 | /* No further action if this term of the FROM clause is not a subquery */ |
| | @@ -158618,11 +159566,11 @@ |
| 158618 | 159566 | pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0; |
| 158619 | 159567 | pAggInfo->pGroupBy = pGroupBy; |
| 158620 | 159568 | sqlite3ExprAnalyzeAggList(&sNC, pEList); |
| 158621 | 159569 | sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy); |
| 158622 | 159570 | if( pHaving ){ |
| 158623 | | - if( pGroupBy ){ |
| 159571 | + if( pGroupBy && OptimizationEnabled(db, SQLITE_HavingToWhere) ){ |
| 158624 | 159572 | assert( pWhere==p->pWhere ); |
| 158625 | 159573 | assert( pHaving==p->pHaving ); |
| 158626 | 159574 | assert( pGroupBy==p->pGroupBy ); |
| 158627 | 159575 | havingToWhere(pParse, p); |
| 158628 | 159576 | pWhere = p->pWhere; |
| | @@ -159529,15 +160477,19 @@ |
| 159529 | 160477 | |
| 159530 | 160478 | /* If the trigger name was unqualified, and the table is a temp table, |
| 159531 | 160479 | ** then set iDb to 1 to create the trigger in the temporary database. |
| 159532 | 160480 | ** If sqlite3SrcListLookup() returns 0, indicating the table does not |
| 159533 | 160481 | ** exist, the error is caught by the block below. |
| 159534 | | - */ |
| 159535 | | - pTab = sqlite3SrcListLookup(pParse, pTableName); |
| 159536 | | - if( db->init.busy==0 && pName2->n==0 && pTab |
| 159537 | | - && pTab->pSchema==db->aDb[1].pSchema ){ |
| 159538 | | - iDb = 1; |
| 160482 | + ** |
| 160483 | + ** Do not do this if ALTER TABLE is reparsing an existing trigger. In |
| 160484 | + ** that case iDb is already set correctly. */ |
| 160485 | + if( !IN_RENAME_OBJECT ){ |
| 160486 | + pTab = sqlite3SrcListLookup(pParse, pTableName); |
| 160487 | + if( db->init.busy==0 && pName2->n==0 && pTab |
| 160488 | + && pTab->pSchema==db->aDb[1].pSchema ){ |
| 160489 | + iDb = 1; |
| 160490 | + } |
| 159539 | 160491 | } |
| 159540 | 160492 | |
| 159541 | 160493 | /* Ensure the table name matches database name and that the table exists */ |
| 159542 | 160494 | if( db->mallocFailed ) goto trigger_cleanup; |
| 159543 | 160495 | assert( pTableName->nSrc==1 ); |
| | @@ -159564,11 +160516,12 @@ |
| 159564 | 160516 | zName = sqlite3NameFromToken(db, pName); |
| 159565 | 160517 | if( zName==0 ){ |
| 159566 | 160518 | assert( db->mallocFailed ); |
| 159567 | 160519 | goto trigger_cleanup; |
| 159568 | 160520 | } |
| 159569 | | - if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){ |
| 160521 | + if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName, |
| 160522 | + db->aDb[iDb].zDbSName) ){ |
| 159570 | 160523 | goto trigger_cleanup; |
| 159571 | 160524 | } |
| 159572 | 160525 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 159573 | 160526 | if( !IN_RENAME_OBJECT ){ |
| 159574 | 160527 | if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ |
| | @@ -159706,10 +160659,11 @@ |
| 159706 | 160659 | if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; |
| 159707 | 160660 | zName = pTrig->zName; |
| 159708 | 160661 | iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 159709 | 160662 | assert( iDb>=00 && iDb<db->nDb ); |
| 159710 | 160663 | pTrig->step_list = pStepList; |
| 160664 | + pTrig->nSelect = pParse->nSelect; |
| 159711 | 160665 | for(n=0; pStepList; n++){ |
| 159712 | 160666 | pStepList->pTrig = pTrig; |
| 159713 | 160667 | pStepList = pStepList->pNext; |
| 159714 | 160668 | } |
| 159715 | 160669 | if( n>pParse->db->aLimit[SQLITE_LIMIT_TRIGGER_STEPS] ){ |
| | @@ -159739,16 +160693,19 @@ |
| 159739 | 160693 | Vdbe *v; |
| 159740 | 160694 | char *z; |
| 159741 | 160695 | |
| 159742 | 160696 | /* If this is a new CREATE TABLE statement, and if shadow tables |
| 159743 | 160697 | ** are read-only, and the trigger makes a change to a shadow table, |
| 159744 | | - ** then raise an error - do not allow the trigger to be created. */ |
| 160698 | + ** then raise an error - do not allow the trigger to be created. |
| 160699 | + ** The targets of a TEMP trigger may be in any schema, so check all |
| 160700 | + ** of them in that case. */ |
| 159745 | 160701 | if( sqlite3ReadOnlyShadowTables(db) ){ |
| 159746 | 160702 | TriggerStep *pStep; |
| 160703 | + const char *zDb = iDb==1 ? 0 : db->aDb[iDb].zDbSName; |
| 159747 | 160704 | for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){ |
| 159748 | 160705 | if( pStep->pSrc!=0 |
| 159749 | | - && sqlite3ShadowTableName(db, pStep->pSrc->a[0].zName) |
| 160706 | + && sqlite3ShadowTableName(db, pStep->pSrc->a[0].zName, zDb) |
| 159750 | 160707 | ){ |
| 159751 | 160708 | sqlite3ErrorMsg(pParse, |
| 159752 | 160709 | "trigger \"%s\" may not write to shadow table \"%s\"", |
| 159753 | 160710 | pTrig->zName, pStep->pSrc->a[0].zName); |
| 159754 | 160711 | goto triggerfinish_cleanup; |
| | @@ -160656,10 +161613,11 @@ |
| 160656 | 161613 | memset(&sNC, 0, sizeof(sNC)); |
| 160657 | 161614 | sNC.pParse = &sSubParse; |
| 160658 | 161615 | sSubParse.pTriggerTab = pTab; |
| 160659 | 161616 | sSubParse.pToplevel = pTop; |
| 160660 | 161617 | sSubParse.pTrigCoded = pTrigger; |
| 161618 | + sSubParse.nSelect = pTrigger->nSelect; |
| 160661 | 161619 | sSubParse.zAuthContext = pTrigger->zName; |
| 160662 | 161620 | sSubParse.eTriggerOp = pTrigger->op; |
| 160663 | 161621 | sSubParse.nQueryLoop = pParse->nQueryLoop; |
| 160664 | 161622 | sSubParse.prepFlags = pParse->prepFlags; |
| 160665 | 161623 | sSubParse.oldmask = 0; |
| | @@ -164628,10 +165586,11 @@ |
| 164628 | 165586 | struct InLoop { |
| 164629 | 165587 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 164630 | 165588 | int addrInTop; /* Top of the IN loop */ |
| 164631 | 165589 | int iBase; /* Base register of multi-key index record */ |
| 164632 | 165590 | int nPrefix; /* Number of prior entries in the key */ |
| 165591 | + int lblNull; /* Jump here if RHS value is NULL */ |
| 164633 | 165592 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 164634 | 165593 | } *aInLoop; /* Information about each nested IN operator */ |
| 164635 | 165594 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 164636 | 165595 | Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */ |
| 164637 | 165596 | } u; |
| | @@ -165937,10 +166896,11 @@ |
| 165937 | 166896 | sqlite3WhereRealloc(pTerm->pWC->pWInfo, |
| 165938 | 166897 | pLevel->u.in.aInLoop, |
| 165939 | 166898 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 165940 | 166899 | pIn = pLevel->u.in.aInLoop; |
| 165941 | 166900 | if( pIn ){ |
| 166901 | + int lblNull = sqlite3VdbeMakeLabel(pParse); |
| 165942 | 166902 | int iMap = 0; /* Index in aiMap[] */ |
| 165943 | 166903 | pIn += i; |
| 165944 | 166904 | for(i=iEq; i<pLoop->nLTerm; i++){ |
| 165945 | 166905 | if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 165946 | 166906 | int iOut = iTarget + i - iEq; |
| | @@ -165948,13 +166908,14 @@ |
| 165948 | 166908 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut); |
| 165949 | 166909 | }else{ |
| 165950 | 166910 | int iCol = aiMap ? aiMap[iMap++] : 0; |
| 165951 | 166911 | pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut); |
| 165952 | 166912 | } |
| 165953 | | - sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v); |
| 166913 | + sqlite3VdbeAddOp2(v, OP_IsNull, iOut, lblNull); VdbeCoverage(v); |
| 165954 | 166914 | if( i==iEq ){ |
| 165955 | 166915 | pIn->iCur = iTab; |
| 166916 | + pIn->lblNull = lblNull; |
| 165956 | 166917 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
| 165957 | 166918 | if( iEq>0 ){ |
| 165958 | 166919 | pIn->iBase = iTarget - i; |
| 165959 | 166920 | pIn->nPrefix = i; |
| 165960 | 166921 | }else{ |
| | @@ -166924,10 +167885,11 @@ |
| 166924 | 167885 | */ |
| 166925 | 167886 | int iReg; /* P3 Value for OP_VFilter */ |
| 166926 | 167887 | int addrNotFound; |
| 166927 | 167888 | int nConstraint = pLoop->nLTerm; |
| 166928 | 167889 | |
| 167890 | + sqlite3MayAbort(pParse); |
| 166929 | 167891 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 166930 | 167892 | addrNotFound = pLevel->addrBrk; |
| 166931 | 167893 | for(j=0; j<nConstraint; j++){ |
| 166932 | 167894 | int iTarget = iReg+j+2; |
| 166933 | 167895 | pTerm = pLoop->aLTerm[j]; |
| | @@ -167297,10 +168259,11 @@ |
| 167297 | 168259 | ** index entries for which the column is null and for those for which |
| 167298 | 168260 | ** it is not. For an ASC sort, the non-NULL entries are scanned first. |
| 167299 | 168261 | ** For DESC, NULL entries are scanned first. |
| 167300 | 168262 | */ |
| 167301 | 168263 | if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0 |
| 168264 | + && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 |
| 167302 | 168265 | && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0 |
| 167303 | 168266 | ){ |
| 167304 | 168267 | assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 ); |
| 167305 | 168268 | assert( pRangeEnd==0 && pRangeStart==0 ); |
| 167306 | 168269 | testcase( pLoop->nSkip>0 ); |
| | @@ -168512,10 +169475,47 @@ |
| 168512 | 169475 | assert( op!=TK_GE || c==WO_GE ); |
| 168513 | 169476 | assert( op!=TK_IS || c==WO_IS ); |
| 168514 | 169477 | return c; |
| 168515 | 169478 | } |
| 168516 | 169479 | |
| 169480 | +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 169481 | +/* |
| 169482 | +** The input is an N-byte GLOB or LIKE pattern prefix |
| 169483 | +** with the following guarantees: |
| 169484 | +** |
| 169485 | +** (1) It contains no wildcards. |
| 169486 | +** (2) z[N]==0 |
| 169487 | +** (3) N>0 |
| 169488 | +** (4) bHasSpace is true if and only if the original complete LIKE/GLOB |
| 169489 | +** pattern contained wildcards and/or escapes. (It is usually true, |
| 169490 | +** therefore). bHasSpace also means that there is at least one byte |
| 169491 | +** of overrun space at z[N+1] that this routine may use. |
| 169492 | +** |
| 169493 | +** This routine checks to see if the input will be a prefix to |
| 169494 | +** any number (integer or floating-point) that SQLite might generate |
| 169495 | +** from a MEM_Int or MEM_Real, including "Inf" and "-Inf". |
| 169496 | +** |
| 169497 | +** Return true if the input is a possible numeric prefix and |
| 169498 | +** false if the input cannot possibly be a numeric prefix. |
| 169499 | +*/ |
| 169500 | +static SQLITE_NOINLINE int isNumericPrefix(char *z, int N, int bHasSpace){ |
| 169501 | + int x; |
| 169502 | + double rDummy; |
| 169503 | + assert( N>0 ); |
| 169504 | + if( sqlite3AtoF(z, &rDummy)>0 ) return 1; /* Boundary is itself a number */ |
| 169505 | + if( bHasSpace ){ |
| 169506 | + z[N] = '0'; |
| 169507 | + z[N+1] = 0; |
| 169508 | + x = sqlite3AtoF(z, &rDummy); /* Prefix like "1.0e" or "-" */ |
| 169509 | + z[N] = 0; |
| 169510 | + if( x>0 ) return 1; |
| 169511 | + } |
| 169512 | + if( z[0]=='-' ){ z++; N--; } |
| 169513 | + if( N>0 && N<=3 && sqlite3StrNICmp(z,"inf",N)==0 ) return 1; |
| 169514 | + return 0; |
| 169515 | +} |
| 169516 | +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 168517 | 169517 | |
| 168518 | 169518 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 168519 | 169519 | /* |
| 168520 | 169520 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 168521 | 169521 | ** can be optimized using inequality constraints. Return TRUE if it is |
| | @@ -168567,11 +169567,11 @@ |
| 168567 | 169567 | } |
| 168568 | 169568 | sqlite3VdbeReprepareOnBind(pParse->pVdbe, iCol, 0); |
| 168569 | 169569 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 168570 | 169570 | }else if( op==TK_STRING ){ |
| 168571 | 169571 | assert( !ExprHasProperty(pRight, EP_IntValue) ); |
| 168572 | | - z = (u8*)pRight->u.zToken; |
| 169572 | + z = (u8*)pRight->u.zToken; |
| 168573 | 169573 | } |
| 168574 | 169574 | if( z ){ |
| 168575 | 169575 | /* Count the number of prefix bytes prior to the first wildcard, |
| 168576 | 169576 | ** U+fffd character, or malformed utf-8. If the underlying database |
| 168577 | 169577 | ** has a UTF16LE encoding, then only consider ASCII characters. Note that |
| | @@ -168583,27 +169583,40 @@ |
| 168583 | 169583 | cnt++; |
| 168584 | 169584 | if( c==wc[3] && z[cnt]>0 && z[cnt]<0x80 ){ |
| 168585 | 169585 | cnt++; |
| 168586 | 169586 | }else if( c>=0x80 ){ |
| 168587 | 169587 | const u8 *z2 = z+cnt-1; |
| 168588 | | - if( c==0xff || sqlite3Utf8Read(&z2)==0xfffd /* bad utf-8 */ |
| 168589 | | - || ENC(db)==SQLITE_UTF16LE |
| 169588 | + u32 u; |
| 169589 | + if( c==0xff /* (1) */ |
| 169590 | + || (u = sqlite3Utf8Read(&z2))>=0xfffd /* (2) */ |
| 169591 | + || u==0x7ff /* (3) */ |
| 169592 | + || ENC(db)==SQLITE_UTF16LE /* (4) */ |
| 168590 | 169593 | ){ |
| 169594 | + /* Do not accept this character as part of the prefix because |
| 169595 | + ** (1) 0xff is not part of any valid UTF-8 character and the |
| 169596 | + ** 0xff causes difficult when incrementing to the upper bound |
| 169597 | + ** (2) 0xfffd is not a valid UTF-8 character and values greater |
| 169598 | + ** start getting into the surrogate pairs in UTF16BE. |
| 169599 | + ** (3) When incrementing this code point to the upper bound, |
| 169600 | + ** that would require adding an extra byte to the UTF-8 |
| 169601 | + ** (4) UTF16LE does not compare in sorted order. Only |
| 169602 | + ** UTF16BE and UTF8 have that property. |
| 169603 | + */ |
| 168591 | 169604 | cnt--; |
| 168592 | 169605 | break; |
| 168593 | 169606 | }else{ |
| 168594 | 169607 | cnt = (int)(z2-z); |
| 168595 | 169608 | } |
| 168596 | 169609 | } |
| 168597 | 169610 | } |
| 168598 | 169611 | |
| 168599 | | - /* The optimization is possible only if (1) the pattern does not begin |
| 168600 | | - ** with a wildcard and if (2) the non-wildcard prefix does not end with |
| 168601 | | - ** an (illegal 0xff) character, or (3) the pattern does not consist of |
| 168602 | | - ** a single escape character. The second condition is necessary so |
| 169612 | + /* The optimization is possible only if either (1a) the pattern does not |
| 169613 | + ** begin with a wildcard and if (1b) the non-wildcard prefix does not end |
| 169614 | + ** with an (illegal) 0xff character, or (2) the pattern does not consist of |
| 169615 | + ** a single escape character. The (1b) condition is necessary so |
| 168603 | 169616 | ** that we can increment the prefix key to find an upper bound for the |
| 168604 | | - ** range search. The third is because the caller assumes that the pattern |
| 169617 | + ** range search. The (2) is because the caller assumes that the pattern |
| 168605 | 169618 | ** consists of at least one character after all escapes have been |
| 168606 | 169619 | ** removed. */ |
| 168607 | 169620 | if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && ALWAYS(255!=(u8)z[cnt-1]) ){ |
| 168608 | 169621 | Expr *pPrefix; |
| 168609 | 169622 | |
| | @@ -168625,41 +169638,41 @@ |
| 168625 | 169638 | zNew[iTo] = 0; |
| 168626 | 169639 | assert( iTo>0 ); |
| 168627 | 169640 | |
| 168628 | 169641 | /* If the LHS is not an ordinary column with TEXT affinity, then the |
| 168629 | 169642 | ** pattern prefix boundaries (both the start and end boundaries) must |
| 168630 | | - ** not look like a number. Otherwise the pattern might be treated as |
| 168631 | | - ** a number, which will invalidate the LIKE optimization. |
| 169643 | + ** not look like a number or the prefix of a number. Otherwise the |
| 169644 | + ** pattern might might match against a number, which means this |
| 169645 | + ** optimization will not work. If either the start or end boundary |
| 169646 | + ** strings has any possibility of matching against a text rendering |
| 169647 | + ** of a MEM_Int or MEM_Real, then we must return 0. |
| 168632 | 169648 | ** |
| 168633 | 169649 | ** Getting this right has been a persistent source of bugs in the |
| 168634 | 169650 | ** LIKE optimization. See, for example: |
| 168635 | 169651 | ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1 |
| 168636 | 169652 | ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28 |
| 168637 | 169653 | ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07 |
| 168638 | 169654 | ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975 |
| 168639 | 169655 | ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a |
| 169656 | + ** 2026-10-28 https://sqlite.org/bugs/info/e85ab73aa1483325 |
| 168640 | 169657 | */ |
| 168641 | 169658 | if( pLeft->op!=TK_COLUMN |
| 168642 | 169659 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 168643 | 169660 | || (ALWAYS( ExprUseYTab(pLeft) ) |
| 168644 | 169661 | && ALWAYS(pLeft->y.pTab) |
| 168645 | 169662 | && IsVirtual(pLeft->y.pTab)) /* Might be numeric */ |
| 168646 | 169663 | ){ |
| 168647 | | - int isNum; |
| 168648 | | - double rDummy; |
| 169664 | + int bHasSpace = iTo < (int)strlen((const char*)z); |
| 169665 | + int x; |
| 168649 | 169666 | assert( zNew[iTo]==0 ); |
| 168650 | | - isNum = sqlite3AtoF(zNew, &rDummy); |
| 168651 | | - if( isNum<=0 ){ |
| 168652 | | - if( iTo==1 && zNew[0]=='-' ){ |
| 168653 | | - isNum = +1; |
| 168654 | | - }else{ |
| 168655 | | - zNew[iTo-1]++; |
| 168656 | | - isNum = sqlite3AtoF(zNew, &rDummy); |
| 168657 | | - zNew[iTo-1]--; |
| 168658 | | - } |
| 168659 | | - } |
| 168660 | | - if( isNum>0 ){ |
| 169667 | + x = isNumericPrefix(zNew, iTo, bHasSpace); /* start boundary */ |
| 169668 | + if( x==0 ){ |
| 169669 | + zNew[iTo-1]++; |
| 169670 | + x = isNumericPrefix(zNew, iTo, bHasSpace); /* end boundary */ |
| 169671 | + zNew[iTo-1]--; |
| 169672 | + } |
| 169673 | + if( x!=0 ){ |
| 168661 | 169674 | sqlite3ExprDelete(db, pPrefix); |
| 168662 | 169675 | sqlite3ValueFree(pVal); |
| 168663 | 169676 | return 0; |
| 168664 | 169677 | } |
| 168665 | 169678 | } |
| | @@ -168920,11 +169933,17 @@ |
| 168920 | 169933 | Expr *pNew; /* New virtual expression */ |
| 168921 | 169934 | int op; /* Operator for the combined expression */ |
| 168922 | 169935 | int idxNew; /* Index in pWC of the next virtual term */ |
| 168923 | 169936 | Expr *pA, *pB; /* Expressions associated with pOne and pTwo */ |
| 168924 | 169937 | |
| 168925 | | - if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return; |
| 169938 | + if( (pOne->wtFlags | pTwo->wtFlags) & (TERM_VNULL|TERM_LIKEOPT) ){ |
| 169939 | + testcase( pOne->wtFlags & TERM_VNULL ); |
| 169940 | + testcase( pOne->wtFlags & TERM_LIKEOPT ); |
| 169941 | + testcase( pTwo->wtFlags & TERM_VNULL ); |
| 169942 | + testcase( pTwo->wtFlags & TERM_LIKEOPT ); |
| 169943 | + return; |
| 169944 | + } |
| 168926 | 169945 | if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 168927 | 169946 | if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 168928 | 169947 | if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp |
| 168929 | 169948 | && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; |
| 168930 | 169949 | pA = pOne->pExpr; |
| | @@ -169881,11 +170900,11 @@ |
| 169881 | 170900 | ** used by each such virtual term is pExpr (the full vector IN(...) |
| 169882 | 170901 | ** expression). The WhereTerm.u.x.iField variable identifies the index within |
| 169883 | 170902 | ** the vector on the LHS that the virtual term represents. |
| 169884 | 170903 | ** |
| 169885 | 170904 | ** This only works if the RHS is a simple SELECT (not a compound) that does |
| 169886 | | - ** not use window functions. |
| 170905 | + ** not use window functions or a LIMIT clause. |
| 169887 | 170906 | */ |
| 169888 | 170907 | else if( pExpr->op==TK_IN |
| 169889 | 170908 | && pTerm->u.x.iField==0 |
| 169890 | 170909 | && pExpr->pLeft->op==TK_VECTOR |
| 169891 | 170910 | && ALWAYS( ExprUseXSelect(pExpr) ) |
| | @@ -169892,10 +170911,12 @@ |
| 169892 | 170911 | && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values)) |
| 169893 | 170912 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 169894 | 170913 | && pExpr->x.pSelect->pWin==0 |
| 169895 | 170914 | #endif |
| 169896 | 170915 | && (pExpr->x.pSelect->selFlags & SF_MinMaxAgg)==0 |
| 170916 | + && pExpr->x.pSelect->pLimit==0 |
| 170917 | + /* ^-- See bug 2026-11-04T00:59:22Z */ |
| 169897 | 170918 | && pWC->op==TK_AND |
| 169898 | 170919 | && pExpr->x.pSelect->pEList->nExpr <= UMXV(pTerm->nChild) |
| 169899 | 170920 | /* ^-- See bug 2026-06-04T10:00:49Z */ |
| 169900 | 170921 | ){ |
| 169901 | 170922 | int i; |
| | @@ -170574,18 +171595,20 @@ |
| 170574 | 171595 | return 0; |
| 170575 | 171596 | } |
| 170576 | 171597 | } |
| 170577 | 171598 | if( pSet->n<N_OR_COST ){ |
| 170578 | 171599 | p = &pSet->a[pSet->n++]; |
| 170579 | | - p->nOut = nOut; |
| 170580 | 171600 | }else{ |
| 171601 | + /* The set is full. Replace the most expensive entry, but only if the |
| 171602 | + ** new entry is less expensive than it. */ |
| 170581 | 171603 | p = pSet->a; |
| 170582 | 171604 | for(i=1; i<pSet->n; i++){ |
| 170583 | | - if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 171605 | + if( p->rRun<pSet->a[i].rRun ) p = pSet->a + i; |
| 170584 | 171606 | } |
| 170585 | 171607 | if( p->rRun<=rRun ) return 0; |
| 170586 | 171608 | } |
| 171609 | + p->nOut = nOut; |
| 170587 | 171610 | whereOrInsert_done: |
| 170588 | 171611 | p->prereq = prereq; |
| 170589 | 171612 | p->rRun = rRun; |
| 170590 | 171613 | if( p->nOut>nOut ) p->nOut = nOut; |
| 170591 | 171614 | return 1; |
| | @@ -170697,10 +171720,33 @@ |
| 170697 | 171720 | CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX); |
| 170698 | 171721 | return pRet ? pRet->zName : sqlite3StrBINARY; |
| 170699 | 171722 | } |
| 170700 | 171723 | return 0; |
| 170701 | 171724 | } |
| 171725 | + |
| 171726 | +/* |
| 171727 | +** pTerm is a WO_EQUIV term "X=Y" that whereScanNext() might use to add |
| 171728 | +** Y to the equivalence class of X. Return true if any of the following |
| 171729 | +** are true: |
| 171730 | +** |
| 171731 | +** * Parameter zCollName is NULL, |
| 171732 | +** * The collation sequence of pTerm is BINARY, or |
| 171733 | +** * The collation sequence of pTerm is zCollName. |
| 171734 | +** |
| 171735 | +** Otherwise return false. |
| 171736 | +*/ |
| 171737 | +static int whereScanEquivCollOk( |
| 171738 | + Parse *pParse, |
| 171739 | + WhereTerm *pTerm, |
| 171740 | + const char *zCollName |
| 171741 | +){ |
| 171742 | + CollSeq *pColl; |
| 171743 | + if( zCollName==0 ) return 1; |
| 171744 | + pColl = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr); |
| 171745 | + if( sqlite3IsBinary(pColl) ) return 1; |
| 171746 | + return sqlite3StrICmp(pColl->zName, zCollName)==0; |
| 171747 | +} |
| 170702 | 171748 | |
| 170703 | 171749 | /* |
| 170704 | 171750 | ** Advance to the next WhereTerm that matches according to the criteria |
| 170705 | 171751 | ** established when the pScan object was initialized by whereScanInit(). |
| 170706 | 171752 | ** Return NULL if there are no more matching WhereTerms. |
| | @@ -170731,10 +171777,11 @@ |
| 170731 | 171777 | && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON)) |
| 170732 | 171778 | ){ |
| 170733 | 171779 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 170734 | 171780 | && pScan->nEquiv<ArraySize(pScan->aiCur) |
| 170735 | 171781 | && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0 |
| 171782 | + && whereScanEquivCollOk(pWC->pWInfo->pParse, pTerm, pScan->zCollName) |
| 170736 | 171783 | ){ |
| 170737 | 171784 | int j; |
| 170738 | 171785 | for(j=0; j<pScan->nEquiv; j++){ |
| 170739 | 171786 | if( pScan->aiCur[j]==pX->iTable |
| 170740 | 171787 | && pScan->aiColumn[j]==pX->iColumn ){ |
| | @@ -170761,11 +171808,23 @@ |
| 170761 | 171808 | CollSeq *pColl; |
| 170762 | 171809 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 170763 | 171810 | continue; |
| 170764 | 171811 | } |
| 170765 | 171812 | assert(pX->pLeft); |
| 170766 | | - pColl = sqlite3ExprCompareCollSeq(pParse, pX); |
| 171813 | + if( pX->pLeft->op==TK_VECTOR ){ |
| 171814 | + /* A vector inequality such as (a,b)>(?,?). This term |
| 171815 | + ** constrains only the first field, so the collation is |
| 171816 | + ** that of the first pair of fields only. */ |
| 171817 | + Expr *pL = sqlite3VectorFieldSubexpr(pX->pLeft, 0); |
| 171818 | + Expr *pR = sqlite3VectorFieldSubexpr(pX->pRight, 0); |
| 171819 | + assert( pTerm->u.x.iField==0 ); |
| 171820 | + assert( pX->op>=TK_GT && pX->op<=TK_GE ); |
| 171821 | + if( ExprHasProperty(pX, EP_Commuted) ) SWAP(Expr*, pL, pR); |
| 171822 | + pColl = sqlite3BinaryCompareCollSeq(pParse, pL, pR); |
| 171823 | + }else{ |
| 171824 | + pColl = sqlite3ExprCompareCollSeq(pParse, pX); |
| 171825 | + } |
| 170767 | 171826 | zCollName = pColl ? pColl->zName : sqlite3StrBINARY; |
| 170768 | 171827 | } |
| 170769 | 171828 | |
| 170770 | 171829 | if( sqlite3StrICmp(zCollName, pScan->zCollName) ){ |
| 170771 | 171830 | continue; |
| | @@ -171360,11 +172419,13 @@ |
| 171360 | 172419 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 171361 | 172420 | CollSeq *pColl; /* Collating sequence to on a column */ |
| 171362 | 172421 | WhereLoop *pLoop; /* The Loop object */ |
| 171363 | 172422 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 171364 | 172423 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 172424 | + Bitmask pkCols; /* PK cols whose key collation keeps them unique */ |
| 171365 | 172425 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 172426 | + int bHighCols; /* True to add all columns >= BMS-1 */ |
| 171366 | 172427 | u8 sentWarning = 0; /* True if a warning has been issued */ |
| 171367 | 172428 | u8 useBloomFilter = 1; /* True to also add a Bloom filter */ |
| 171368 | 172429 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 171369 | 172430 | int iContinue = 0; /* Jump here to skip excluded rows */ |
| 171370 | 172431 | SrcList *pTabList; /* The complete FROM clause */ |
| | @@ -171388,10 +172449,11 @@ |
| 171388 | 172449 | pSrc = &pTabList->a[pLevel->iFrom]; |
| 171389 | 172450 | pTable = pSrc->pSTab; |
| 171390 | 172451 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 171391 | 172452 | pLoop = pLevel->pWLoop; |
| 171392 | 172453 | idxCols = 0; |
| 172454 | + pkCols = 0; |
| 171393 | 172455 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 171394 | 172456 | Expr *pExpr = pTerm->pExpr; |
| 171395 | 172457 | /* Make the automatic index a partial index if there are terms in the |
| 171396 | 172458 | ** WHERE clause (or the ON clause of a LEFT join) that constrain which |
| 171397 | 172459 | ** rows of the target table (pSrc) that can be used. */ |
| | @@ -171419,10 +172481,29 @@ |
| 171419 | 172481 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 171420 | 172482 | goto end_auto_index_create; |
| 171421 | 172483 | } |
| 171422 | 172484 | pLoop->aLTerm[nKeyCol++] = pTerm; |
| 171423 | 172485 | idxCols |= cMask; |
| 172486 | + |
| 172487 | + /* For a WITHOUT ROWID table, all PRIMARY KEY columns must be |
| 172488 | + ** present in the automatic index, sorted with either BINARY or |
| 172489 | + ** the same collation used by the PK index itself, to guarantee each |
| 172490 | + ** entry is unique. If the current field matches that criteria, |
| 172491 | + ** set the corresponding bit in pkCols. */ |
| 172492 | + if( !HasRowid(pTable) && iCol<BMS-1 |
| 172493 | + && (pTable->aCol[iCol].colFlags & COLFLAG_PRIMKEY) |
| 172494 | + ){ |
| 172495 | + Index *pPk = sqlite3PrimaryKeyIndex(pTable); |
| 172496 | + CollSeq *pX = sqlite3ExprCompareCollSeq(pParse, pExpr); |
| 172497 | + int iPk = sqlite3TableColumnToIndex(pPk, iCol); |
| 172498 | + assert( iPk>=0 && iPk<pPk->nKeyCol ); |
| 172499 | + if( sqlite3IsBinary(pX) |
| 172500 | + || sqlite3StrICmp(pPk->azColl[iPk], pX->zName)==0 |
| 172501 | + ){ |
| 172502 | + pkCols |= cMask; |
| 172503 | + } |
| 172504 | + } |
| 171424 | 172505 | } |
| 171425 | 172506 | } |
| 171426 | 172507 | } |
| 171427 | 172508 | assert( nKeyCol>0 || pParse->db->mallocFailed ); |
| 171428 | 172509 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
| | @@ -171440,35 +172521,38 @@ |
| 171440 | 172521 | if( IsView(pTable) ){ |
| 171441 | 172522 | extraCols = ALLBITS & ~idxCols; |
| 171442 | 172523 | }else{ |
| 171443 | 172524 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 171444 | 172525 | } |
| 172526 | + bHighCols = (pSrc->colUsed & MASKBIT(BMS-1))!=0; |
| 171445 | 172527 | if( !HasRowid(pTable) ){ |
| 171446 | 172528 | /* For WITHOUT ROWID tables, ensure that all PRIMARY KEY columns are |
| 171447 | | - ** either in the idxCols mask or in the extraCols mask */ |
| 172529 | + ** either in the pkCols mask or in the extraCols mask. A column that |
| 172530 | + ** is already an index key but is not in pkCols is added a second |
| 172531 | + ** time, using the BINARY collating sequence. */ |
| 171448 | 172532 | for(i=0; i<pTable->nCol; i++){ |
| 171449 | 172533 | if( (pTable->aCol[i].colFlags & COLFLAG_PRIMKEY)==0 ) continue; |
| 171450 | 172534 | if( i>=BMS-1 ){ |
| 171451 | | - extraCols |= MASKBIT(BMS-1); |
| 172535 | + bHighCols = 1; |
| 171452 | 172536 | break; |
| 171453 | 172537 | } |
| 171454 | | - if( idxCols & MASKBIT(i) ) continue; |
| 172538 | + if( pkCols & MASKBIT(i) ) continue; |
| 171455 | 172539 | extraCols |= MASKBIT(i); |
| 171456 | 172540 | } |
| 171457 | 172541 | } |
| 171458 | 172542 | mxBitCol = MIN(BMS-1,pTable->nCol); |
| 171459 | 172543 | testcase( pTable->nCol==BMS-1 ); |
| 171460 | 172544 | testcase( pTable->nCol==BMS-2 ); |
| 171461 | 172545 | for(i=0; i<mxBitCol; i++){ |
| 171462 | 172546 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 171463 | 172547 | } |
| 171464 | | - if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 172548 | + if( bHighCols ){ |
| 171465 | 172549 | nKeyCol += pTable->nCol - BMS + 1; |
| 171466 | 172550 | } |
| 171467 | 172551 | |
| 171468 | 172552 | /* Construct the Index object to describe this index */ |
| 171469 | | - assert( nKeyCol <= pTable->nCol + MAX(0, pTable->nCol - BMS + 1) ); |
| 172553 | + assert( nKeyCol <= pTable->nCol + MIN(pTable->nCol, BMS) ); |
| 171470 | 172554 | /* ^-- This guarantees that the number of index columns will fit in the u16 */ |
| 171471 | 172555 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable), |
| 171472 | 172556 | 0, &zNotUsed); |
| 171473 | 172557 | if( pIdx==0 ) goto end_auto_index_create; |
| 171474 | 172558 | pLoop->u.btree.pIndex = pIdx; |
| | @@ -171488,11 +172572,11 @@ |
| 171488 | 172572 | if( (idxCols & cMask)==0 ){ |
| 171489 | 172573 | Expr *pX = pTerm->pExpr; |
| 171490 | 172574 | idxCols |= cMask; |
| 171491 | 172575 | pIdx->aiColumn[n] = pTerm->u.x.leftColumn; |
| 171492 | 172576 | pColl = sqlite3ExprCompareCollSeq(pParse, pX); |
| 171493 | | - assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */ |
| 172577 | + testcase( pColl==0 && pParse->nErr==0 ); /* TH3 collate01.810 */ |
| 171494 | 172578 | if( !sqlite3IsBinary(pColl) ){ |
| 171495 | 172579 | /* Disallow the use of a Bloom filter if any non-BINARY collating |
| 171496 | 172580 | ** sequence is involved. tag-202607231411 */ |
| 171497 | 172581 | useBloomFilter = 0; |
| 171498 | 172582 | } |
| | @@ -171510,11 +172594,11 @@ |
| 171510 | 172594 | pIdx->aiColumn[n] = i; |
| 171511 | 172595 | pIdx->azColl[n] = sqlite3StrBINARY; |
| 171512 | 172596 | n++; |
| 171513 | 172597 | } |
| 171514 | 172598 | } |
| 171515 | | - if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 172599 | + if( bHighCols ){ |
| 171516 | 172600 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 171517 | 172601 | pIdx->aiColumn[n] = i; |
| 171518 | 172602 | pIdx->azColl[n] = sqlite3StrBINARY; |
| 171519 | 172603 | n++; |
| 171520 | 172604 | } |
| | @@ -174113,13 +175197,43 @@ |
| 174113 | 175197 | */ |
| 174114 | 175198 | typedef struct CoveringIndexCheck CoveringIndexCheck; |
| 174115 | 175199 | struct CoveringIndexCheck { |
| 174116 | 175200 | Index *pIdx; /* The index */ |
| 174117 | 175201 | int iTabCur; /* Cursor number for the corresponding table */ |
| 175202 | + WhereClause *pWC; /* Ignore terms of this WHERE implied by pIdx, or NULL */ |
| 174118 | 175203 | u8 bExpr; /* Uses an indexed expression */ |
| 174119 | 175204 | u8 bUnidx; /* Uses an unindexed column not within an indexed expr */ |
| 174120 | 175205 | }; |
| 175206 | + |
| 175207 | +/* |
| 175208 | +** Return true if pExpr is a term of the WHERE clause pCk->pWC (not merely |
| 175209 | +** a copy of such a term) and is identical to one of the AND-connected |
| 175210 | +** terms of the WHERE clause of partial index pCk->pIdx. Such a term is |
| 175211 | +** never evaluated when pCk->pIdx drives the loop, because the |
| 175212 | +** whereApplyPartialIndexConstraints() routine marks it as already coded. |
| 175213 | +** Hence any columns used by the term do not need to be in the index. |
| 175214 | +*/ |
| 175215 | +static int whereTermImpliedByPartialIndex( |
| 175216 | + CoveringIndexCheck *pCk, |
| 175217 | + const Expr *pExpr |
| 175218 | +){ |
| 175219 | + WhereClause *pWC = pCk->pWC; |
| 175220 | + Expr *pPart; |
| 175221 | + int i; |
| 175222 | + for(i=0; i<pWC->nTerm; i++){ |
| 175223 | + if( pWC->a[i].pExpr==pExpr ) break; |
| 175224 | + } |
| 175225 | + if( i>=pWC->nTerm ) return 0; |
| 175226 | + pPart = pCk->pIdx->pPartIdxWhere; |
| 175227 | + while( pPart->op==TK_AND ){ |
| 175228 | + if( sqlite3ExprCompare(0, pExpr, pPart->pRight, pCk->iTabCur)==0 ){ |
| 175229 | + return 1; |
| 175230 | + } |
| 175231 | + pPart = pPart->pLeft; |
| 175232 | + } |
| 175233 | + return sqlite3ExprCompare(0, pExpr, pPart, pCk->iTabCur)==0; |
| 175234 | +} |
| 174121 | 175235 | |
| 174122 | 175236 | /* |
| 174123 | 175237 | ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk. |
| 174124 | 175238 | ** |
| 174125 | 175239 | ** If the Expr node references the table with cursor pCk->iTabCur, then |
| | @@ -174143,10 +175257,13 @@ |
| 174143 | 175257 | u16 nColumn; /* Number of columns in the index */ |
| 174144 | 175258 | CoveringIndexCheck *pCk; /* Info about this search */ |
| 174145 | 175259 | |
| 174146 | 175260 | pCk = pWalk->u.pCovIdxCk; |
| 174147 | 175261 | pIdx = pCk->pIdx; |
| 175262 | + if( pCk->pWC && whereTermImpliedByPartialIndex(pCk, pExpr) ){ |
| 175263 | + return WRC_Prune; |
| 175264 | + } |
| 174148 | 175265 | if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){ |
| 174149 | 175266 | /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/ |
| 174150 | 175267 | if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue; |
| 174151 | 175268 | pIdx = pWalk->u.pCovIdxCk->pIdx; |
| 174152 | 175269 | aiColumn = pIdx->aiColumn; |
| | @@ -174169,10 +175286,16 @@ |
| 174169 | 175286 | ** pIdx is an index that covers all of the low-number columns used by |
| 174170 | 175287 | ** pWInfo->pSelect (columns from 0 through 62) or an index that has |
| 174171 | 175288 | ** expressions terms. Hence, we cannot determine whether or not it is |
| 174172 | 175289 | ** a covering index by using the colUsed bitmasks. We have to do a search |
| 174173 | 175290 | ** to see if the index is covering. This routine does that search. |
| 175291 | +** |
| 175292 | +** If pWC is not NULL, then pIdx is a partial index and terms of the |
| 175293 | +** WHERE clause pWC that are implied by the WHERE clause of pIdx are |
| 175294 | +** ignored, as they will never be evaluated when pIdx drives the loop. |
| 175295 | +** In this case pIdx might not cover all low-numbered columns used by |
| 175296 | +** pWInfo->pSelect. |
| 174174 | 175297 | ** |
| 174175 | 175298 | ** The return value is one of these: |
| 174176 | 175299 | ** |
| 174177 | 175300 | ** 0 The index is definitely not a covering index |
| 174178 | 175301 | ** |
| | @@ -174189,21 +175312,22 @@ |
| 174189 | 175312 | ** returned can lead to incorrect bytecode and assertion faults. |
| 174190 | 175313 | */ |
| 174191 | 175314 | static SQLITE_NOINLINE u32 whereIsCoveringIndex( |
| 174192 | 175315 | WhereInfo *pWInfo, /* The WHERE clause context */ |
| 174193 | 175316 | Index *pIdx, /* Index that is being tested */ |
| 174194 | | - int iTabCur /* Cursor for the table being indexed */ |
| 175317 | + int iTabCur, /* Cursor for the table being indexed */ |
| 175318 | + WhereClause *pWC /* Ignore terms implied by pIdx, or NULL */ |
| 174195 | 175319 | ){ |
| 174196 | 175320 | int i, rc; |
| 174197 | 175321 | struct CoveringIndexCheck ck; |
| 174198 | 175322 | Walker w; |
| 174199 | 175323 | if( pWInfo->pSelect==0 ){ |
| 174200 | 175324 | /* We don't have access to the full query, so we cannot check to see |
| 174201 | 175325 | ** if pIdx is covering. Assume it is not. */ |
| 174202 | 175326 | return 0; |
| 174203 | 175327 | } |
| 174204 | | - if( pIdx->bHasExpr==0 ){ |
| 175328 | + if( pIdx->bHasExpr==0 && pWC==0 ){ |
| 174205 | 175329 | for(i=0; i<pIdx->nColumn; i++){ |
| 174206 | 175330 | if( pIdx->aiColumn[i]>=BMS-1 ) break; |
| 174207 | 175331 | } |
| 174208 | 175332 | if( i>=pIdx->nColumn ){ |
| 174209 | 175333 | /* pIdx does not index any columns greater than 62, but we know from |
| | @@ -174212,10 +175336,11 @@ |
| 174212 | 175336 | return 0; |
| 174213 | 175337 | } |
| 174214 | 175338 | } |
| 174215 | 175339 | ck.pIdx = pIdx; |
| 174216 | 175340 | ck.iTabCur = iTabCur; |
| 175341 | + ck.pWC = pWC; |
| 174217 | 175342 | ck.bExpr = 0; |
| 174218 | 175343 | ck.bUnidx = 0; |
| 174219 | 175344 | memset(&w, 0, sizeof(w)); |
| 174220 | 175345 | w.xExprCallback = whereIsCoveringIndexWalkCallback; |
| 174221 | 175346 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| | @@ -174563,10 +175688,11 @@ |
| 174563 | 175688 | rc = whereLoopInsert(pBuilder, pNew); |
| 174564 | 175689 | pNew->nOut = rSize; |
| 174565 | 175690 | if( rc ) break; |
| 174566 | 175691 | }else{ |
| 174567 | 175692 | Bitmask m; |
| 175693 | + WhereClause *pPartWC = 0; /* WHERE terms that pProbe might imply */ |
| 174568 | 175694 | if( pProbe->isCovering ){ |
| 174569 | 175695 | m = 0; |
| 174570 | 175696 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 174571 | 175697 | }else{ |
| 174572 | 175698 | m = pSrc->colUsed & pProbe->colNotIdxed; |
| | @@ -174574,14 +175700,34 @@ |
| 174574 | 175700 | && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 174575 | 175701 | ){ |
| 174576 | 175702 | wherePartIdxExpr( |
| 174577 | 175703 | pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0 |
| 174578 | 175704 | ); |
| 175705 | + /* WHERE clause terms implied by the partial index WHERE clause |
| 175706 | + ** are not evaluated, so the columns they use need not be in the |
| 175707 | + ** index. But this does not apply to outer joins. See the |
| 175708 | + ** call to whereApplyPartialIndexConstraints() in wherecode.c. */ |
| 175709 | + if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==0 |
| 175710 | + && pWC==&pWInfo->sWC |
| 175711 | + ){ |
| 175712 | + pPartWC = pWC; |
| 175713 | + } |
| 174579 | 175714 | } |
| 174580 | 175715 | pNew->wsFlags = WHERE_INDEXED; |
| 174581 | | - if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){ |
| 174582 | | - u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor); |
| 175716 | + if( m==0 ){ |
| 175717 | + if( HasRowid(pTab) || pWInfo->pSelect!=0 || sqlite3FaultSim(700) ){ |
| 175718 | + WHERETRACE(0x200, |
| 175719 | + ("-> %s is a covering index according to bitmasks\n", |
| 175720 | + pProbe->zName, m==0 ? "is" : "is not")); |
| 175721 | + pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 175722 | + } |
| 175723 | + }else if( m==TOPBIT |
| 175724 | + || (pProbe->bHasExpr && !pProbe->bHasVCol) |
| 175725 | + || pPartWC |
| 175726 | + ){ |
| 175727 | + u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor, |
| 175728 | + pPartWC); |
| 174583 | 175729 | if( isCov==0 ){ |
| 174584 | 175730 | WHERETRACE(0x200, |
| 174585 | 175731 | ("-> %s is not a covering index" |
| 174586 | 175732 | " according to whereIsCoveringIndex()\n", pProbe->zName)); |
| 174587 | 175733 | assert( m!=0 ); |
| | @@ -174597,17 +175743,10 @@ |
| 174597 | 175743 | WHERETRACE(0x200, |
| 174598 | 175744 | ("-> %s might be a covering expression index" |
| 174599 | 175745 | " according to whereIsCoveringIndex()\n", pProbe->zName)); |
| 174600 | 175746 | } |
| 174601 | 175747 | } |
| 174602 | | - }else if( m==0 |
| 174603 | | - && (HasRowid(pTab) || pWInfo->pSelect!=0 || sqlite3FaultSim(700)) |
| 174604 | | - ){ |
| 174605 | | - WHERETRACE(0x200, |
| 174606 | | - ("-> %s is a covering index according to bitmasks\n", |
| 174607 | | - pProbe->zName, m==0 ? "is" : "is not")); |
| 174608 | | - pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 174609 | 175748 | } |
| 174610 | 175749 | } |
| 174611 | 175750 | |
| 174612 | 175751 | /* Full scan via index */ |
| 174613 | 175752 | if( b |
| | @@ -175445,21 +176584,19 @@ |
| 175445 | 176584 | ** long as the subquery ORDER BY does the same. But if the |
| 175446 | 176585 | ** outer ORDER BY uses COLLATE, even a redundant COLLATE, the |
| 175447 | 176586 | ** optimization is bypassed. |
| 175448 | 176587 | ** |
| 175449 | 176588 | ** (2) The subquery ORDER BY terms must exactly match subquery result |
| 175450 | | -** columns, including any COLLATE annotations. This routine relies |
| 175451 | | -** on iOrderByCol to do matching between order by terms and result |
| 175452 | | -** columns, and iOrderByCol will not be set if the result column |
| 175453 | | -** and ORDER BY collations differ. |
| 176589 | +** columns, and the collations used must match as well. |
| 175454 | 176590 | ** |
| 175455 | 176591 | ** (3) The subquery and outer ORDER BY can be in opposite directions as |
| 175456 | 176592 | ** long as the subquery is materialized. If the subquery is |
| 175457 | 176593 | ** implemented as a co-routine, the sort orders must be in the same |
| 175458 | 176594 | ** direction because there is no way to run a co-routine backwards. |
| 175459 | 176595 | */ |
| 175460 | 176596 | static SQLITE_NOINLINE int wherePathMatchSubqueryOB( |
| 176597 | + Parse *pParse, /* The parsing context */ |
| 175461 | 176598 | WhereLoop *pLoop, /* The nested loop term that is a subquery */ |
| 175462 | 176599 | int iLoop, /* Which level of the nested loop. 0==outermost */ |
| 175463 | 176600 | int iCur, /* Cursor used by the this loop */ |
| 175464 | 176601 | ExprList *pOrderBy, /* The ORDER BY clause on the whole query */ |
| 175465 | 176602 | Bitmask *pRevMask, /* When loops need to go in reverse order */ |
| | @@ -175475,18 +176612,20 @@ |
| 175475 | 176612 | |
| 175476 | 176613 | pSubOB = pLoop->u.btree.pOrderBy; |
| 175477 | 176614 | assert( pSubOB!=0 ); |
| 175478 | 176615 | for(iOB=0; (MASKBIT(iOB) & *pOBSat)!=0; iOB++){} |
| 175479 | 176616 | for(jSub=0; jSub<pSubOB->nExpr && iOB<pOrderBy->nExpr; jSub++, iOB++){ |
| 175480 | | - if( pSubOB->a[jSub].u.x.iOrderByCol==0 ) break; |
| 176617 | + struct ExprList_item *pItem = &pSubOB->a[jSub]; |
| 176618 | + if( pItem->u.x.iOrderByCol==0 ) break; |
| 175481 | 176619 | pOBExpr = pOrderBy->a[iOB].pExpr; |
| 175482 | 176620 | if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) break; |
| 175483 | 176621 | if( pOBExpr->iTable!=iCur ) break; |
| 175484 | | - if( pOBExpr->iColumn!=pSubOB->a[jSub].u.x.iOrderByCol-1 ) break; |
| 176622 | + if( pOBExpr->iColumn!=pItem->u.x.iOrderByCol-1 ) break; |
| 176623 | + if( 0==sqlite3ExprCollSeqMatch(pParse, pOBExpr, pItem->pExpr) ) break; |
| 175485 | 176624 | if( (wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 175486 | 176625 | u8 sfOB = pOrderBy->a[iOB].fg.sortFlags; /* sortFlags for iOB */ |
| 175487 | | - u8 sfSub = pSubOB->a[jSub].fg.sortFlags; /* sortFlags for jSub */ |
| 176626 | + u8 sfSub = pItem->fg.sortFlags; /* sortFlags for jSub */ |
| 175488 | 176627 | if( (sfSub & KEYINFO_ORDER_BIGNULL) != (sfOB & KEYINFO_ORDER_BIGNULL) ){ |
| 175489 | 176628 | break; |
| 175490 | 176629 | } |
| 175491 | 176630 | revIdx = sfSub & KEYINFO_ORDER_DESC; |
| 175492 | 176631 | if( jSub>0 ){ |
| | @@ -175662,11 +176801,11 @@ |
| 175662 | 176801 | |
| 175663 | 176802 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 175664 | 176803 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 175665 | 176804 | if( pLoop->u.btree.pOrderBy |
| 175666 | 176805 | && OptimizationEnabled(db, SQLITE_OrderBySubq) |
| 175667 | | - && wherePathMatchSubqueryOB(pLoop,iLoop,iCur, |
| 176806 | + && wherePathMatchSubqueryOB(pWInfo->pParse, pLoop,iLoop,iCur, |
| 175668 | 176807 | pOrderBy,pRevMask, &obSat, wctrlFlags) |
| 175669 | 176808 | ){ |
| 175670 | 176809 | nColumn = 0; |
| 175671 | 176810 | isOrderDistinct = 0; |
| 175672 | 176811 | }else{ |
| | @@ -176158,11 +177297,11 @@ |
| 176158 | 177297 | for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 176159 | 177298 | if( pWLoop->iTab<iFromIdx ) continue; |
| 176160 | 177299 | if( pWLoop->iTab>iFromIdx ) break; |
| 176161 | 177300 | if( pWLoop->rRun>mxRun ) mxRun = pWLoop->rRun; |
| 176162 | 177301 | } |
| 176163 | | - if( ALWAYS(mxRun<LOGEST_MAX) ) mxRun++; |
| 177302 | + if( mxRun<LOGEST_MAX ) mxRun++; |
| 176164 | 177303 | |
| 176165 | 177304 | /* Increase the cost of table scans for dimension tables to be |
| 176166 | 177305 | ** slightly more than the maximum cost of the fact table */ |
| 176167 | 177306 | for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 176168 | 177307 | if( (pWLoop->maskSelf & mSeen)==0 ) continue; |
| | @@ -178053,46 +179192,46 @@ |
| 178053 | 179192 | #endif |
| 178054 | 179193 | if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){ |
| 178055 | 179194 | struct InLoop *pIn; |
| 178056 | 179195 | int j; |
| 178057 | 179196 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 178058 | | - assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull |
| 178059 | | - || pParse->db->mallocFailed ); |
| 178060 | | - sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| 178061 | | - if( pIn->eEndLoopOp!=OP_Noop ){ |
| 178062 | | - if( pIn->nPrefix ){ |
| 178063 | | - int bEarlyOut = |
| 178064 | | - (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 178065 | | - && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0; |
| 178066 | | - if( pLevel->iLeftJoin ){ |
| 178067 | | - /* For LEFT JOIN queries, cursor pIn->iCur might have been |
| 178068 | | - ** disabled by OP_NullRow. This occurs for WHERE clauses such |
| 178069 | | - ** as "a = ? AND b IN (...)", where the index is on (a, b). |
| 178070 | | - ** When the OP_IfNullRow jump is taken, the pIn->iBase register |
| 178071 | | - ** is NULLed out as a side effect. But as that register is not |
| 178072 | | - ** used when the jump is taken, the side-effect is harmless. */ |
| 178073 | | - sqlite3VdbeAddOp3(v, OP_IfNullRow, pIn->iCur, |
| 178074 | | - sqlite3VdbeCurrentAddr(v) + 2 + bEarlyOut, pIn->iBase); |
| 178075 | | - VdbeCoverage(v); |
| 178076 | | - } |
| 178077 | | - if( bEarlyOut ){ |
| 178078 | | - sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur, |
| 178079 | | - sqlite3VdbeCurrentAddr(v)+2, |
| 178080 | | - pIn->iBase, pIn->nPrefix); |
| 178081 | | - VdbeCoverage(v); |
| 178082 | | - /* Retarget the OP_IsNull against the left operand of IN so |
| 178083 | | - ** it jumps past the OP_IfNoHope. This is because the |
| 178084 | | - ** OP_IsNull also bypasses the OP_Affinity opcode that is |
| 178085 | | - ** required by OP_IfNoHope. */ |
| 178086 | | - sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| 178087 | | - } |
| 178088 | | - } |
| 178089 | | - sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
| 178090 | | - VdbeCoverage(v); |
| 178091 | | - VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev); |
| 178092 | | - VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next); |
| 178093 | | - } |
| 179197 | + /* The second and subsequent components of a row-value IN operator |
| 179198 | + ** have eEndLoopOp==OP_Noop. Nothing to do for them. */ |
| 179199 | + if( pIn->eEndLoopOp==OP_Noop ) continue; |
| 179200 | + |
| 179201 | + if( pIn->nPrefix ){ |
| 179202 | + int bEarlyOut = (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 179203 | + && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0; |
| 179204 | + if( pLevel->iLeftJoin ){ |
| 179205 | + /* For LEFT JOIN queries, cursor pIn->iCur might have been |
| 179206 | + ** disabled by OP_NullRow. This occurs for WHERE clauses such |
| 179207 | + ** as "a = ? AND b IN (...)", where the index is on (a, b). |
| 179208 | + ** When the OP_IfNullRow jump is taken, the pIn->iBase register |
| 179209 | + ** is NULLed out as a side effect. But as that register is not |
| 179210 | + ** used when the jump is taken, the side-effect is harmless. */ |
| 179211 | + sqlite3VdbeAddOp3(v, OP_IfNullRow, pIn->iCur, |
| 179212 | + sqlite3VdbeCurrentAddr(v) + 2 + bEarlyOut, pIn->iBase); |
| 179213 | + VdbeCoverage(v); |
| 179214 | + } |
| 179215 | + if( bEarlyOut ){ |
| 179216 | + sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur, |
| 179217 | + sqlite3VdbeCurrentAddr(v)+2, |
| 179218 | + pIn->iBase, pIn->nPrefix); |
| 179219 | + VdbeCoverage(v); |
| 179220 | + } |
| 179221 | + } |
| 179222 | + |
| 179223 | + /* The OP_IsNull opcodes that test the components of the RHS of the |
| 179224 | + ** IN operator jump directly here to the end-of-loop opcode. They |
| 179225 | + ** must jump past the OP_IfNoHope above, because they may also jump |
| 179226 | + ** past the OP_Affinity codes that prepare the key used by IfNoHope. */ |
| 179227 | + sqlite3VdbeResolveLabel(v, pIn->lblNull); |
| 179228 | + |
| 179229 | + sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
| 179230 | + VdbeCoverage(v); |
| 179231 | + VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev); |
| 179232 | + VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next); |
| 178094 | 179233 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
| 178095 | 179234 | } |
| 178096 | 179235 | } |
| 178097 | 179236 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 178098 | 179237 | if( pLevel->pRJ ){ |
| | @@ -178140,10 +179279,20 @@ |
| 178140 | 179279 | if( pIn->eEndLoopOp!=OP_Noop ){ |
| 178141 | 179280 | sqlite3VdbeAddOp1(v, OP_NullRow, pIn->iCur); |
| 178142 | 179281 | } |
| 178143 | 179282 | } |
| 178144 | 179283 | } |
| 179284 | +#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 179285 | + if( pLevel->addrLikeRep ){ |
| 179286 | + /* The LIKE loop counter is initialized after the == and IN terms |
| 179287 | + ** are coded, so a NULL == term or an empty IN list can reach this |
| 179288 | + ** point without ever setting it. Set it to 0 so that the |
| 179289 | + ** OP_DecrJumpZero at the bottom of the loop falls through after |
| 179290 | + ** the NULL row instead of reading an undefined register. */ |
| 179291 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, (int)(pLevel->iLikeRepCntr>>1)); |
| 179292 | + } |
| 179293 | +#endif |
| 178145 | 179294 | if( pLevel->op==OP_Return ){ |
| 178146 | 179295 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 178147 | 179296 | }else{ |
| 178148 | 179297 | sqlite3VdbeGoto(v, pLevel->addrFirst); |
| 178149 | 179298 | } |
| | @@ -179744,10 +180893,11 @@ |
| 179744 | 180893 | assert( pSelect->pSrc->a[0].fg.isSubquery ); |
| 179745 | 180894 | nEphExpr = pSelect->pSrc->a[0].u4.pSubq->pSelect->pEList->nExpr; |
| 179746 | 180895 | pMWin = pSelect->pWin; |
| 179747 | 180896 | v = sqlite3GetVdbe(pParse); |
| 179748 | 180897 | |
| 180898 | + sqlite3MayAbort(pParse); |
| 179749 | 180899 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr); |
| 179750 | 180900 | sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr); |
| 179751 | 180901 | sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr); |
| 179752 | 180902 | sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr); |
| 179753 | 180903 | |
| | @@ -180438,17 +181588,21 @@ |
| 180438 | 181588 | ** |
| 180439 | 181589 | ** If the sort-order for the ORDER BY term in the window is DESC, then the |
| 180440 | 181590 | ** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is |
| 180441 | 181591 | ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=", |
| 180442 | 181592 | ** ">" becomes "<", and so on. So, with DESC sort order, if the argument op |
| 180443 | | -** is OP_Ge, the generated code is equivalent to: |
| 181593 | +** is OP_Ge, the test is logically: |
| 180444 | 181594 | ** |
| 180445 | 181595 | ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl; |
| 180446 | 181596 | ** |
| 180447 | | -** A special type of arithmetic is used such that if csr1.peerVal is not |
| 180448 | | -** a numeric type (real or integer), then the result of the addition |
| 180449 | | -** or subtraction is a a copy of csr1.peerVal. |
| 181597 | +** But in the DESC case, to avoid integer overflow in some cases, in practice: |
| 181598 | +** |
| 181599 | +** if( csr1.peerVal <= csr2.peerVal + regVal ) goto lbl; |
| 181600 | +** |
| 181601 | +** A special type of arithmetic is used such that if the peer-value that |
| 181602 | +** regVal is added to is not a numeric type (real or integer), then the |
| 181603 | +** result of the addition is a copy of that peer-value. |
| 180450 | 181604 | */ |
| 180451 | 181605 | static void windowCodeRangeTest( |
| 180452 | 181606 | WindowCodeArg *p, |
| 180453 | 181607 | int op, /* OP_Ge, OP_Gt, or OP_Le */ |
| 180454 | 181608 | int csr1, /* Cursor number for cursor 1 */ |
| | @@ -180460,11 +181614,12 @@ |
| 180460 | 181614 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 180461 | 181615 | ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */ |
| 180462 | 181616 | int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */ |
| 180463 | 181617 | int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */ |
| 180464 | 181618 | int regString = ++pParse->nMem; /* Reg. for constant value '' */ |
| 180465 | | - int arith = OP_Add; /* OP_Add or OP_Subtract */ |
| 181619 | + int regArith = reg1; /* Register to add regVal to */ |
| 181620 | + int bDesc = 0; /* True for DESC ORDER BY */ |
| 180466 | 181621 | int addrGe; /* Jump destination */ |
| 180467 | 181622 | int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */ |
| 180468 | 181623 | CollSeq *pColl; |
| 180469 | 181624 | |
| 180470 | 181625 | /* Read the peer-value from each cursor into a register */ |
| | @@ -180477,16 +181632,18 @@ |
| 180477 | 181632 | switch( op ){ |
| 180478 | 181633 | case OP_Ge: op = OP_Le; break; |
| 180479 | 181634 | case OP_Gt: op = OP_Lt; break; |
| 180480 | 181635 | default: assert( op==OP_Le ); op = OP_Ge; break; |
| 180481 | 181636 | } |
| 180482 | | - arith = OP_Subtract; |
| 181637 | + bDesc = 1; |
| 181638 | + regArith = reg2; |
| 180483 | 181639 | } |
| 180484 | 181640 | |
| 180485 | | - VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl", |
| 180486 | | - reg1, (arith==OP_Add ? "+" : "-"), regVal, |
| 180487 | | - ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2 |
| 181641 | + VdbeModuleComment((v, "CodeRangeTest: if( R%d%s%s %s R%d%s%s ) goto lbl", |
| 181642 | + reg1, (bDesc ? "" : " + R"), (bDesc ? "" : "regVal"), |
| 181643 | + ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2, |
| 181644 | + (bDesc ? " + R" : ""), (bDesc ? "regVal" : "") |
| 180488 | 181645 | )); |
| 180489 | 181646 | |
| 180490 | 181647 | /* If the BIGNULL flag is set for the ORDER BY, then it is required to |
| 180491 | 181648 | ** consider NULL values to be larger than all other values, instead of |
| 180492 | 181649 | ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this |
| | @@ -180530,30 +181687,31 @@ |
| 180530 | 181687 | sqlite3VdbeAddOp2(v, OP_IsNull, reg2, |
| 180531 | 181688 | (op==OP_Gt || op==OP_Ge) ? addrDone : lbl); |
| 180532 | 181689 | VdbeCoverage(v); |
| 180533 | 181690 | } |
| 180534 | 181691 | |
| 180535 | | - /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1). |
| 180536 | | - ** This block adds (or subtracts for DESC) the numeric value in regVal |
| 180537 | | - ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob), |
| 180538 | | - ** then leave reg1 as it is. In pseudo-code, this is implemented as: |
| 181692 | + /* Register regArith currently contains a peer-value - csr1.peerVal for |
| 181693 | + ** ASC, or csr2.peerVal for DESC. This block adds the numeric value in |
| 181694 | + ** regVal to it. Or, if regArith is not numeric (it is a NULL, a text value |
| 181695 | + ** or a blob), then leave it as it is. In pseudo-code, this is implemented |
| 181696 | + ** as: |
| 180539 | 181697 | ** |
| 180540 | | - ** if( reg1>='' ) goto addrGe; |
| 180541 | | - ** reg1 = reg1 +/- regVal |
| 181698 | + ** if( regArith>='' ) goto addrGe; |
| 181699 | + ** regArith = regArith + regVal |
| 180542 | 181700 | ** addrGe: |
| 180543 | 181701 | ** |
| 180544 | 181702 | ** Since all strings and blobs are greater-than-or-equal-to an empty string, |
| 180545 | | - ** the add/subtract is skipped for these, as required. If reg1 is a NULL, |
| 180546 | | - ** then the arithmetic is performed, but since adding or subtracting from |
| 180547 | | - ** NULL is always NULL anyway, this case is handled as required too. */ |
| 181703 | + ** the addition is skipped for these, as required. If regArith is a NULL, |
| 181704 | + ** then the arithmetic is performed, but since adding to NULL is always |
| 181705 | + ** NULL anyway, this case is handled as required too. */ |
| 180548 | 181706 | sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC); |
| 180549 | | - addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1); |
| 181707 | + addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, regArith); |
| 180550 | 181708 | VdbeCoverage(v); |
| 180551 | | - if( (op==OP_Ge && arith==OP_Add) || (op==OP_Le && arith==OP_Subtract) ){ |
| 181709 | + if( (op==OP_Ge && !bDesc) || (op==OP_Le && bDesc) ){ |
| 180552 | 181710 | sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v); |
| 180553 | 181711 | } |
| 180554 | | - sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1); |
| 181712 | + sqlite3VdbeAddOp3(v, OP_Add, regVal, regArith, regArith); |
| 180555 | 181713 | sqlite3VdbeJumpHere(v, addrGe); |
| 180556 | 181714 | |
| 180557 | 181715 | /* Compare registers reg2 and reg1, taking the jump if required. Note that |
| 180558 | 181716 | ** control skips over this test if the BIGNULL flag is set and either |
| 180559 | 181717 | ** reg1 or reg2 contain a NULL value. */ |
| | @@ -185801,13 +186959,12 @@ |
| 185801 | 186959 | { |
| 185802 | 186960 | yylhsminor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); |
| 185803 | 186961 | if( yylhsminor.yy203 ){ |
| 185804 | 186962 | if( IN_RENAME_OBJECT ){ |
| 185805 | 186963 | sqlite3RenameTokenMap(pParse, yylhsminor.yy203->a[0].zName, &yymsp[-2].minor.yy0); |
| 185806 | | - }else{ |
| 185807 | | - yylhsminor.yy203->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0); |
| 185808 | 186964 | } |
| 186965 | + yylhsminor.yy203->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0); |
| 185809 | 186966 | } |
| 185810 | 186967 | } |
| 185811 | 186968 | yymsp[-2].minor.yy203 = yylhsminor.yy203; |
| 185812 | 186969 | break; |
| 185813 | 186970 | case 123: /* xfullname ::= nm DOT nm AS nm */ |
| | @@ -185814,13 +186971,12 @@ |
| 185814 | 186971 | { |
| 185815 | 186972 | yylhsminor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); |
| 185816 | 186973 | if( yylhsminor.yy203 ){ |
| 185817 | 186974 | if( IN_RENAME_OBJECT ){ |
| 185818 | 186975 | sqlite3RenameTokenMap(pParse, yylhsminor.yy203->a[0].zName, &yymsp[-2].minor.yy0); |
| 185819 | | - }else{ |
| 185820 | | - yylhsminor.yy203->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0); |
| 185821 | 186976 | } |
| 186977 | + yylhsminor.yy203->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0); |
| 185822 | 186978 | } |
| 185823 | 186979 | } |
| 185824 | 186980 | yymsp[-4].minor.yy203 = yylhsminor.yy203; |
| 185825 | 186981 | break; |
| 185826 | 186982 | case 124: /* joinop ::= COMMA|JOIN */ |
| | @@ -186267,23 +187423,23 @@ |
| 186267 | 187423 | ** Except, do not apply this optimization if expr1 contains a function |
| 186268 | 187424 | ** because that function might be an aggregate (we don't know yet whether |
| 186269 | 187425 | ** it is or not) and if it is an aggregate, that could change the meaning |
| 186270 | 187426 | ** of the whole query. |
| 186271 | 187427 | */ |
| 186272 | | - Expr *pB = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy144 ? "true" : "false"); |
| 186273 | | - if( pB ) sqlite3ExprIdToTrueFalse(pB); |
| 187428 | + Expr *pB = sqlite3ExprInt32(pParse->db, yymsp[-3].minor.yy144!=0); |
| 186274 | 187429 | if( !ExprHasProperty(yymsp[-4].minor.yy454, EP_HasFunc) ){ |
| 187430 | + if( pB ) pB->op2 = 1; |
| 186275 | 187431 | sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy454); |
| 186276 | 187432 | yymsp[-4].minor.yy454 = pB; |
| 186277 | 187433 | }else{ |
| 186278 | 187434 | yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, yymsp[-3].minor.yy144 ? TK_OR : TK_AND, pB, yymsp[-4].minor.yy454); |
| 186279 | 187435 | } |
| 186280 | 187436 | }else{ |
| 186281 | 187437 | Expr *pRHS = yymsp[-1].minor.yy14->a[0].pExpr; |
| 186282 | 187438 | if( yymsp[-1].minor.yy14->nExpr==1 |
| 186283 | 187439 | && sqlite3ExprIsConstant(pParse,pRHS) |
| 186284 | | - && pRHS->op!=TK_COLLATE |
| 187440 | + && ExprHasProperty(pRHS, EP_Collate)==0 |
| 186285 | 187441 | && yymsp[-4].minor.yy454->op!=TK_VECTOR |
| 186286 | 187442 | ){ |
| 186287 | 187443 | yymsp[-1].minor.yy14->a[0].pExpr = 0; |
| 186288 | 187444 | sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14); |
| 186289 | 187445 | pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0); |
| | @@ -186328,10 +187484,13 @@ |
| 186328 | 187484 | break; |
| 186329 | 187485 | case 226: /* expr ::= expr in_op nm dbnm paren_exprlist */ |
| 186330 | 187486 | { |
| 186331 | 187487 | SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); |
| 186332 | 187488 | Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0); |
| 187489 | + if( pSelect && IN_RENAME_OBJECT ){ |
| 187490 | + sqlite3RenameTokenMap(pParse, pSrc->a[0].zName, yymsp[-1].minor.yy0.z ? &yymsp[-1].minor.yy0 : &yymsp[-2].minor.yy0); |
| 187491 | + } |
| 186333 | 187492 | if( yymsp[0].minor.yy14 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy14); |
| 186334 | 187493 | yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy454, 0); |
| 186335 | 187494 | sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy454, pSelect); |
| 186336 | 187495 | if( yymsp[-3].minor.yy144 ) yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy454, 0); |
| 186337 | 187496 | } |
| | @@ -190096,10 +191255,13 @@ |
| 190096 | 191255 | { SQLITE_DBCONFIG_STMT_SCANSTATUS, SQLITE_StmtScanStatus }, |
| 190097 | 191256 | { SQLITE_DBCONFIG_REVERSE_SCANORDER, SQLITE_ReverseOrder }, |
| 190098 | 191257 | { SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE, SQLITE_AttachCreate }, |
| 190099 | 191258 | { SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE, SQLITE_AttachWrite }, |
| 190100 | 191259 | { SQLITE_DBCONFIG_ENABLE_COMMENTS, SQLITE_Comments }, |
| 191260 | +#ifdef SQLITE_DEBUG |
| 191261 | + { SQLITE_DBCONFIG_VDBE_TRACE, SQLITE_VdbeTrace }, |
| 191262 | +#endif |
| 190101 | 191263 | }; |
| 190102 | 191264 | unsigned int i; |
| 190103 | 191265 | rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ |
| 190104 | 191266 | for(i=0; i<ArraySize(aFlagOp); i++){ |
| 190105 | 191267 | if( aFlagOp[i].op==op ){ |
| | @@ -190366,10 +191528,13 @@ |
| 190366 | 191528 | return SQLITE_OK; |
| 190367 | 191529 | } |
| 190368 | 191530 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
| 190369 | 191531 | return SQLITE_MISUSE_BKPT; |
| 190370 | 191532 | } |
| 191533 | +#if defined(SQLITE_DISPOSAL_NOTIFIER) |
| 191534 | + SQLITE_DISPOSAL_NOTIFIER(1,db); |
| 191535 | +#endif |
| 190371 | 191536 | sqlite3_mutex_enter(db->mutex); |
| 190372 | 191537 | if( db->mTrace & SQLITE_TRACE_CLOSE ){ |
| 190373 | 191538 | db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0); |
| 190374 | 191539 | } |
| 190375 | 191540 | |
| | @@ -193607,11 +194772,11 @@ |
| 193607 | 194772 | case SQLITE_TESTCTRL_BYTEORDER: { |
| 193608 | 194773 | rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN; |
| 193609 | 194774 | break; |
| 193610 | 194775 | } |
| 193611 | 194776 | |
| 193612 | | - /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) |
| 194777 | + /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, u64 N) |
| 193613 | 194778 | ** |
| 193614 | 194779 | ** Enable or disable various optimizations for testing purposes. The |
| 193615 | 194780 | ** argument N is a bitmask of optimizations to be disabled. For normal |
| 193616 | 194781 | ** operation N should be 0. The idea is that a test program (like the |
| 193617 | 194782 | ** SQL Logic Test or SLT test module) can run the same SQL multiple times |
| | @@ -193618,22 +194783,22 @@ |
| 193618 | 194783 | ** with various optimizations disabled to verify that the same answer |
| 193619 | 194784 | ** is obtained in every case. |
| 193620 | 194785 | */ |
| 193621 | 194786 | case SQLITE_TESTCTRL_OPTIMIZATIONS: { |
| 193622 | 194787 | sqlite3 *db = va_arg(ap, sqlite3*); |
| 193623 | | - db->dbOptFlags = va_arg(ap, u32); |
| 194788 | + db->dbOptFlags = va_arg(ap, u64); |
| 193624 | 194789 | break; |
| 193625 | 194790 | } |
| 193626 | 194791 | |
| 193627 | | - /* sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, int *N) |
| 194792 | + /* sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, u64 *N) |
| 193628 | 194793 | ** |
| 193629 | 194794 | ** Write the current optimization settings into *N. A zero bit means that |
| 193630 | 194795 | ** the optimization is on, and a 1 bit means that the optimization is off. |
| 193631 | 194796 | */ |
| 193632 | 194797 | case SQLITE_TESTCTRL_GETOPT: { |
| 193633 | 194798 | sqlite3 *db = va_arg(ap, sqlite3*); |
| 193634 | | - int *pN = va_arg(ap, int*); |
| 194799 | + u64 *pN = va_arg(ap, u64*); |
| 193635 | 194800 | *pN = db->dbOptFlags; |
| 193636 | 194801 | break; |
| 193637 | 194802 | } |
| 193638 | 194803 | |
| 193639 | 194804 | /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt); |
| | @@ -193877,10 +195042,32 @@ |
| 193877 | 195042 | const char *z = va_arg(ap,const char*); |
| 193878 | 195043 | double *pR = va_arg(ap,double*); |
| 193879 | 195044 | rc = sqlite3AtoF(z,pR); |
| 193880 | 195045 | break; |
| 193881 | 195046 | } |
| 195047 | + |
| 195048 | + /* sqlite3_test_control(SQLITE_TESTCTRL_ATOF, sqlite3 *db, int iDb) |
| 195049 | + ** |
| 195050 | + ** Test access to the sqlite3ExpireStmtsUsingSchema() routine. All |
| 195051 | + ** prepared statements associated with database connection db and that |
| 195052 | + ** use schema iDb (0==main, 1==temp, etc.) are marked as expired. |
| 195053 | + ** Or if iDb is exactly -1, then expire all prepared statements. |
| 195054 | + ** |
| 195055 | + ** If iDb is out of range, this test-control is a harmless no-op. |
| 195056 | + */ |
| 195057 | + case SQLITE_TESTCTRL_EXPIRE_STMTS: { |
| 195058 | + sqlite3 *db = va_arg(ap,sqlite3*); |
| 195059 | + int iDb = va_arg(ap,int); |
| 195060 | + assert( db!=0 ); |
| 195061 | + if( iDb>=0 && iDb<db->nDb ){ |
| 195062 | + sqlite3ExpireStmtsUsingSchema(db, iDb, 0); |
| 195063 | + }else if( iDb==(-1) ){ |
| 195064 | + sqlite3ExpirePreparedStatements(db, 0); |
| 195065 | + } |
| 195066 | + rc = SQLITE_OK; |
| 195067 | + break; |
| 195068 | + } |
| 193882 | 195069 | |
| 193883 | 195070 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) |
| 193884 | 195071 | /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue) |
| 193885 | 195072 | ** |
| 193886 | 195073 | ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value |
| | @@ -195581,12 +196768,12 @@ |
| 195581 | 196768 | int nIndex; /* Size of aIndex[] */ |
| 195582 | 196769 | struct Fts3Index { |
| 195583 | 196770 | int nPrefix; /* Prefix length (0 for main terms index) */ |
| 195584 | 196771 | Fts3Hash hPending; /* Pending terms table for this index */ |
| 195585 | 196772 | } *aIndex; |
| 195586 | | - int nMaxPendingData; /* Max pending data before flush to disk */ |
| 195587 | | - int nPendingData; /* Current bytes of pending data */ |
| 196773 | + sqlite_int64 nMaxPendingData; /* Max pending data before flush to disk */ |
| 196774 | + sqlite_int64 nPendingData; /* Current bytes of pending data */ |
| 195588 | 196775 | sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */ |
| 195589 | 196776 | int iPrevLangid; /* Langid of recently inserted document */ |
| 195590 | 196777 | int bPrevDelete; /* True if last operation was a delete */ |
| 195591 | 196778 | |
| 195592 | 196779 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| | @@ -195901,10 +197088,13 @@ |
| 195901 | 197088 | SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); |
| 195902 | 197089 | SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); |
| 195903 | 197090 | SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*); |
| 195904 | 197091 | SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc); |
| 195905 | 197092 | SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut); |
| 197093 | +SQLITE_PRIVATE int sqlite3Fts3CreateHashFunction(sqlite3*, const char*, int, int, Fts3Hash*, |
| 197094 | + void (*)(sqlite3_context*,int,sqlite3_value**) |
| 197095 | +); |
| 195906 | 197096 | |
| 195907 | 197097 | /* fts3_tokenizer.c */ |
| 195908 | 197098 | SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); |
| 195909 | 197099 | SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); |
| 195910 | 197100 | SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, |
| | @@ -196573,10 +197763,11 @@ |
| 196573 | 197763 | if( p->zContentTbl==0 ){ |
| 196574 | 197764 | if( !zFunc ){ |
| 196575 | 197765 | zFunction = ""; |
| 196576 | 197766 | }else{ |
| 196577 | 197767 | zFree = zFunction = fts3QuoteId(zFunc); |
| 197768 | + if( zFunction==0 ) *pRc = SQLITE_NOMEM; |
| 196578 | 197769 | } |
| 196579 | 197770 | fts3Appendf(pRc, &zRet, "docid"); |
| 196580 | 197771 | for(i=0; i<p->nColumn; i++){ |
| 196581 | 197772 | fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); |
| 196582 | 197773 | } |
| | @@ -196629,10 +197820,11 @@ |
| 196629 | 197820 | |
| 196630 | 197821 | if( !zFunc ){ |
| 196631 | 197822 | zFunction = ""; |
| 196632 | 197823 | }else{ |
| 196633 | 197824 | zFree = zFunction = fts3QuoteId(zFunc); |
| 197825 | + if( zFunction==0 ) *pRc = SQLITE_NOMEM; |
| 196634 | 197826 | } |
| 196635 | 197827 | fts3Appendf(pRc, &zRet, "?"); |
| 196636 | 197828 | for(i=0; i<p->nColumn; i++){ |
| 196637 | 197829 | fts3Appendf(pRc, &zRet, ",%s(?)", zFunction); |
| 196638 | 197830 | } |
| | @@ -199774,10 +200966,35 @@ |
| 199774 | 200966 | if( pHash->nRef<=0 ){ |
| 199775 | 200967 | sqlite3Fts3HashClear(&pHash->hash); |
| 199776 | 200968 | sqlite3_free(pHash); |
| 199777 | 200969 | } |
| 199778 | 200970 | } |
| 200971 | + |
| 200972 | +/* |
| 200973 | +** Register an SQL function that uses the tokenizer hash table pHash as its |
| 200974 | +** user-data pointer. The new function holds a reference to the hash table, |
| 200975 | +** which is released by hashDestroy() when the function is deleted or if |
| 200976 | +** this call fails. |
| 200977 | +** |
| 200978 | +** The Fts3Hash object passed as the fifth argument must be the "hash" member |
| 200979 | +** of an Fts3HashWrapper object. Since it is the first member of that |
| 200980 | +** structure, the pointer may be cast to (Fts3HashWrapper*). |
| 200981 | +*/ |
| 200982 | +SQLITE_PRIVATE int sqlite3Fts3CreateHashFunction( |
| 200983 | + sqlite3 *db, /* Database connection */ |
| 200984 | + const char *zName, /* Name of SQL function */ |
| 200985 | + int nArg, /* Number of arguments */ |
| 200986 | + int eTextRep, /* Text encoding and function flags */ |
| 200987 | + Fts3Hash *pHash, /* Tokenizer hash table */ |
| 200988 | + void (*xFunc)(sqlite3_context*,int,sqlite3_value**) /* Implementation */ |
| 200989 | +){ |
| 200990 | + Fts3HashWrapper *pWrapper = (Fts3HashWrapper*)pHash; |
| 200991 | + pWrapper->nRef++; |
| 200992 | + return sqlite3_create_function_v2( |
| 200993 | + db, zName, nArg, eTextRep, (void*)pWrapper, xFunc, 0, 0, hashDestroy |
| 200994 | + ); |
| 200995 | +} |
| 199779 | 200996 | |
| 199780 | 200997 | /* |
| 199781 | 200998 | ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are |
| 199782 | 200999 | ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c |
| 199783 | 201000 | ** respectively. The following three forward declarations are for functions |
| | @@ -199829,17 +201046,18 @@ |
| 199829 | 201046 | if( rc!=SQLITE_OK ) return rc; |
| 199830 | 201047 | |
| 199831 | 201048 | sqlite3Fts3SimpleTokenizerModule(&pSimple); |
| 199832 | 201049 | sqlite3Fts3PorterTokenizerModule(&pPorter); |
| 199833 | 201050 | |
| 199834 | | - /* Allocate and initialize the hash-table used to store tokenizers. */ |
| 201051 | + /* Allocate and initialize the hash-table used to store tokenizers. The |
| 201052 | + ** reference held by this function is released before it returns. */ |
| 199835 | 201053 | pHash = sqlite3_malloc(sizeof(Fts3HashWrapper)); |
| 199836 | 201054 | if( !pHash ){ |
| 199837 | 201055 | rc = SQLITE_NOMEM; |
| 199838 | 201056 | }else{ |
| 199839 | 201057 | sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING, 1); |
| 199840 | | - pHash->nRef = 0; |
| 201058 | + pHash->nRef = 1; |
| 199841 | 201059 | } |
| 199842 | 201060 | |
| 199843 | 201061 | /* Load the built-in tokenizers into the hash table */ |
| 199844 | 201062 | if( rc==SQLITE_OK ){ |
| 199845 | 201063 | if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple) |
| | @@ -199886,20 +201104,27 @@ |
| 199886 | 201104 | } |
| 199887 | 201105 | if( rc==SQLITE_OK ){ |
| 199888 | 201106 | pHash->nRef++; |
| 199889 | 201107 | rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy); |
| 199890 | 201108 | } |
| 199891 | | - return rc; |
| 199892 | 201109 | } |
| 199893 | 201110 | |
| 201111 | + /* If an error has occurred, remove any modules and functions registered |
| 201112 | + ** above. If this is a loadable extension, the shared library may be unloaded |
| 201113 | + ** as soon as this function returns, so they must not be left in place. */ |
| 201114 | + if( rc!=SQLITE_OK ){ |
| 201115 | + sqlite3_create_module(db, "fts3", 0, 0); |
| 201116 | + sqlite3_create_module(db, "fts4", 0, 0); |
| 201117 | + sqlite3_create_module(db, "fts3tokenize", 0, 0); |
| 201118 | + sqlite3_create_module(db, "fts4aux", 0, 0); |
| 201119 | + sqlite3_create_function(db, "fts3_tokenizer", 1, SQLITE_UTF8, 0, 0, 0, 0); |
| 201120 | + sqlite3_create_function(db, "fts3_tokenizer", 2, SQLITE_UTF8, 0, 0, 0, 0); |
| 201121 | + } |
| 199894 | 201122 | |
| 199895 | | - /* An error has occurred. Delete the hash table and return the error code. */ |
| 199896 | | - assert( rc!=SQLITE_OK ); |
| 199897 | | - if( pHash ){ |
| 199898 | | - sqlite3Fts3HashClear(&pHash->hash); |
| 199899 | | - sqlite3_free(pHash); |
| 199900 | | - } |
| 201123 | + /* Release the reference held by this function. If nothing else still holds |
| 201124 | + ** a reference, this deletes the hash table. */ |
| 201125 | + if( pHash ) hashDestroy(pHash); |
| 199901 | 201126 | return rc; |
| 199902 | 201127 | } |
| 199903 | 201128 | |
| 199904 | 201129 | /* |
| 199905 | 201130 | ** Allocate an Fts3MultiSegReader for each token in the expression headed |
| | @@ -201955,11 +203180,12 @@ |
| 201955 | 203180 | |
| 201956 | 203181 | /* |
| 201957 | 203182 | ** Schema of the terms table. |
| 201958 | 203183 | */ |
| 201959 | 203184 | #define FTS3_AUX_SCHEMA \ |
| 201960 | | - "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)" |
| 203185 | + "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN, " \ |
| 203186 | + "PRIMARY KEY(term, col, languageid)) WITHOUT ROWID" |
| 201961 | 203187 | |
| 201962 | 203188 | /* |
| 201963 | 203189 | ** This function does all the work for both the xConnect and xCreate methods. |
| 201964 | 203190 | ** These tables have no persistent representation of their own, so xConnect |
| 201965 | 203191 | ** and xCreate are identical operations. |
| | @@ -202342,11 +203568,11 @@ |
| 202342 | 203568 | if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM; |
| 202343 | 203569 | pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm); |
| 202344 | 203570 | } |
| 202345 | 203571 | } |
| 202346 | 203572 | |
| 202347 | | - if( iLe>=0 && sqlite3_value_type(apVal[0])==SQLITE_TEXT ){ |
| 203573 | + if( iLe>=0 && sqlite3_value_type(apVal[iLe])==SQLITE_TEXT ){ |
| 202348 | 203574 | pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe])); |
| 202349 | 203575 | if( pCsr->zStop==0 ) return SQLITE_NOMEM; |
| 202350 | 203576 | pCsr->nStop = (int)strlen(pCsr->zStop); |
| 202351 | 203577 | } |
| 202352 | 203578 | |
| | @@ -203780,16 +205006,17 @@ |
| 203780 | 205006 | /* |
| 203781 | 205007 | ** Register the query expression parser test function fts3_exprtest() |
| 203782 | 205008 | ** with database connection db. |
| 203783 | 205009 | */ |
| 203784 | 205010 | SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){ |
| 203785 | | - int rc = sqlite3_create_function( |
| 203786 | | - db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0 |
| 205011 | + int rc = sqlite3Fts3CreateHashFunction( |
| 205012 | + db, "fts3_exprtest", -1, SQLITE_UTF8, pHash, fts3ExprTest |
| 203787 | 205013 | ); |
| 203788 | 205014 | if( rc==SQLITE_OK ){ |
| 203789 | | - rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", |
| 203790 | | - -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0 |
| 205015 | + rc = sqlite3Fts3CreateHashFunction( |
| 205016 | + db, "fts3_exprtest_rebalance", -1, SQLITE_UTF8, pHash, |
| 205017 | + fts3ExprTestRebalance |
| 203791 | 205018 | ); |
| 203792 | 205019 | } |
| 203793 | 205020 | return rc; |
| 203794 | 205021 | } |
| 203795 | 205022 | |
| | @@ -205326,11 +206553,10 @@ |
| 205326 | 206553 | sqlite3 *db, |
| 205327 | 206554 | Fts3Hash *pHash, |
| 205328 | 206555 | const char *zName |
| 205329 | 206556 | ){ |
| 205330 | 206557 | int rc = SQLITE_OK; |
| 205331 | | - void *p = (void *)pHash; |
| 205332 | 206558 | const int any = SQLITE_UTF8|SQLITE_DIRECTONLY; |
| 205333 | 206559 | |
| 205334 | 206560 | #ifdef SQLITE_TEST |
| 205335 | 206561 | char *zTest = 0; |
| 205336 | 206562 | char *zTest2 = 0; |
| | @@ -205341,18 +206567,22 @@ |
| 205341 | 206567 | rc = SQLITE_NOMEM; |
| 205342 | 206568 | } |
| 205343 | 206569 | #endif |
| 205344 | 206570 | |
| 205345 | 206571 | if( SQLITE_OK==rc ){ |
| 205346 | | - rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0); |
| 206572 | + rc = sqlite3Fts3CreateHashFunction( |
| 206573 | + db, zName, 1, any, pHash, fts3TokenizerFunc |
| 206574 | + ); |
| 205347 | 206575 | } |
| 205348 | 206576 | if( SQLITE_OK==rc ){ |
| 205349 | | - rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0); |
| 206577 | + rc = sqlite3Fts3CreateHashFunction( |
| 206578 | + db, zName, 2, any, pHash, fts3TokenizerFunc |
| 206579 | + ); |
| 205350 | 206580 | } |
| 205351 | 206581 | #ifdef SQLITE_TEST |
| 205352 | 206582 | if( SQLITE_OK==rc ){ |
| 205353 | | - rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0); |
| 206583 | + rc = sqlite3Fts3CreateHashFunction(db, zTest, -1, any, pHash, testFunc); |
| 205354 | 206584 | } |
| 205355 | 206585 | if( SQLITE_OK==rc ){ |
| 205356 | 206586 | rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0); |
| 205357 | 206587 | } |
| 205358 | 206588 | #endif |
| | @@ -206847,12 +208077,12 @@ |
| 206847 | 208077 | int rc = SQLITE_OK; |
| 206848 | 208078 | |
| 206849 | 208079 | pList = (PendingList *)fts3HashFind(pHash, zToken, nToken); |
| 206850 | 208080 | if( pList ){ |
| 206851 | 208081 | assert( (i64)pList->nData+(i64)nToken+(i64)sizeof(Fts3HashElem) |
| 206852 | | - <= (i64)p->nPendingData ); |
| 206853 | | - p->nPendingData -= (int)(pList->nData + nToken + sizeof(Fts3HashElem)); |
| 208082 | + <= p->nPendingData ); |
| 208083 | + p->nPendingData -= pList->nData + nToken + sizeof(Fts3HashElem); |
| 206854 | 208084 | } |
| 206855 | 208085 | if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){ |
| 206856 | 208086 | if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){ |
| 206857 | 208087 | /* Malloc failed while inserting the new entry. This can only |
| 206858 | 208088 | ** happen if there was no previous entry for this token. |
| | @@ -206861,13 +208091,13 @@ |
| 206861 | 208091 | sqlite3_free(pList); |
| 206862 | 208092 | rc = SQLITE_NOMEM; |
| 206863 | 208093 | } |
| 206864 | 208094 | } |
| 206865 | 208095 | if( rc==SQLITE_OK ){ |
| 206866 | | - assert( (i64)p->nPendingData + pList->nData + nToken |
| 208096 | + assert( p->nPendingData + pList->nData + nToken |
| 206867 | 208097 | + sizeof(Fts3HashElem) <= 0x3fffffff ); |
| 206868 | | - p->nPendingData += (int)(pList->nData + nToken + sizeof(Fts3HashElem)); |
| 208098 | + p->nPendingData += pList->nData + nToken + sizeof(Fts3HashElem); |
| 206869 | 208099 | } |
| 206870 | 208100 | return rc; |
| 206871 | 208101 | } |
| 206872 | 208102 | |
| 206873 | 208103 | /* |
| | @@ -209848,13 +211078,13 @@ |
| 209848 | 211078 | ** |
| 209849 | 211079 | ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a |
| 209850 | 211080 | ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc |
| 209851 | 211081 | ** to reflect the new size of the pBlob->a[] buffer. |
| 209852 | 211082 | */ |
| 209853 | | -static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){ |
| 211083 | +static void blobGrowBuffer(Blob *pBlob, i64 nMin, int *pRc){ |
| 209854 | 211084 | if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){ |
| 209855 | | - int nAlloc = nMin; |
| 211085 | + i64 nAlloc = nMin; |
| 209856 | 211086 | char *a = (char *)sqlite3_realloc64(pBlob->a, nAlloc); |
| 209857 | 211087 | if( a ){ |
| 209858 | 211088 | pBlob->nAlloc = nAlloc; |
| 209859 | 211089 | pBlob->a = a; |
| 209860 | 211090 | }else{ |
| | @@ -210118,23 +211348,23 @@ |
| 210118 | 211348 | const char *zTerm = pCsr->zTerm; |
| 210119 | 211349 | int nTerm = pCsr->nTerm; |
| 210120 | 211350 | const char *aDoclist = pCsr->aDoclist; |
| 210121 | 211351 | int nDoclist = pCsr->nDoclist; |
| 210122 | 211352 | int rc = SQLITE_OK; /* Return code */ |
| 210123 | | - int nSpace; /* Total space in bytes required on leaf */ |
| 211353 | + i64 nSpace; /* Total space in bytes required on leaf */ |
| 210124 | 211354 | int nPrefix; /* Size of prefix shared with previous term */ |
| 210125 | 211355 | int nSuffix; /* Size of suffix (nTerm - nPrefix) */ |
| 210126 | 211356 | NodeWriter *pLeaf; /* Object used to write leaf nodes */ |
| 210127 | 211357 | |
| 210128 | 211358 | pLeaf = &pWriter->aNodeWriter[0]; |
| 210129 | 211359 | nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm); |
| 210130 | 211360 | nSuffix = nTerm - nPrefix; |
| 210131 | 211361 | if(nSuffix<=0 ) return FTS_CORRUPT_VTAB; |
| 210132 | 211362 | |
| 210133 | | - nSpace = sqlite3Fts3VarintLen(nPrefix); |
| 210134 | | - nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix; |
| 210135 | | - nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist; |
| 211363 | + nSpace = (i64)sqlite3Fts3VarintLen(nPrefix); |
| 211364 | + nSpace += (i64)sqlite3Fts3VarintLen(nSuffix) + nSuffix; |
| 211365 | + nSpace += (i64)sqlite3Fts3VarintLen(nDoclist) + nDoclist; |
| 210136 | 211366 | |
| 210137 | 211367 | /* If the current block is not empty, and if adding this term/doclist |
| 210138 | 211368 | ** to the current block would make it larger than Fts3Table.nNodeSize bytes, |
| 210139 | 211369 | ** and if there is still room for another leaf page, write this block out to |
| 210140 | 211370 | ** the database. */ |
| | @@ -211110,11 +212340,11 @@ |
| 211110 | 212340 | pFilter->flags = FTS3_SEGMENT_REQUIRE_POS; |
| 211111 | 212341 | |
| 211112 | 212342 | if( rc==SQLITE_OK ){ |
| 211113 | 212343 | rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx); |
| 211114 | 212344 | assert( bUseHint==1 || bUseHint==0 ); |
| 211115 | | - if( iIdx==0 || (bUseHint && iIdx==1) ){ |
| 212345 | + if( rc==SQLITE_OK && (iIdx==0 || (bUseHint && iIdx==1)) ){ |
| 211116 | 212346 | int bIgnore = 0; |
| 211117 | 212347 | rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore); |
| 211118 | 212348 | if( bIgnore ){ |
| 211119 | 212349 | pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY; |
| 211120 | 212350 | } |
| | @@ -214882,11 +216112,15 @@ |
| 214882 | 216112 | #define JEDIT_INS 3 /* Insert if not exists */ |
| 214883 | 216113 | #define JEDIT_SET 4 /* Insert or overwrite */ |
| 214884 | 216114 | #define JEDIT_AINS 5 /* array_insert() */ |
| 214885 | 216115 | |
| 214886 | 216116 | /* |
| 214887 | | -** Maximum nesting depth of JSON for this implementation. |
| 216117 | +** The approximate maximum nesting depth of JSON for this implementation. |
| 216118 | +** |
| 216119 | +** Applications should say well clear of this maximum. Depending on |
| 216120 | +** implementation quirks, the limit might not reached one or two before |
| 216121 | +** or one or two after the published limit. |
| 214888 | 216122 | ** |
| 214889 | 216123 | ** This limit is needed to avoid a stack overflow in the recursive |
| 214890 | 216124 | ** descent parser. A depth of 1000 is far deeper than any sane JSON |
| 214891 | 216125 | ** should go. Historical note: This limit was 2000 prior to version 3.42.0 |
| 214892 | 216126 | */ |
| | @@ -215901,10 +217135,11 @@ |
| 215901 | 217135 | if( z[j]=='-' ){ |
| 215902 | 217136 | j++; |
| 215903 | 217137 | if( sz<2 ) return i+1; |
| 215904 | 217138 | } |
| 215905 | 217139 | k = i+n+sz; |
| 217140 | + if( z[j]=='0' && j<k-1 ) return j+1; |
| 215906 | 217141 | while( j<k ){ |
| 215907 | 217142 | if( sqlite3Isdigit(z[j]) ){ |
| 215908 | 217143 | j++; |
| 215909 | 217144 | }else{ |
| 215910 | 217145 | return j+1; |
| | @@ -215961,11 +217196,11 @@ |
| 215961 | 217196 | } |
| 215962 | 217197 | seen = 1; |
| 215963 | 217198 | continue; |
| 215964 | 217199 | } |
| 215965 | 217200 | if( z[j]=='e' || z[j]=='E' ){ |
| 215966 | | - if( seen==2 ) return j+1; |
| 217201 | + if( seen==2 || j==i+n ) return j+1; |
| 215967 | 217202 | if( j==k-1 ) return j+1; |
| 215968 | 217203 | if( z[j+1]=='+' || z[j+1]=='-' ){ |
| 215969 | 217204 | j++; |
| 215970 | 217205 | if( j==k-1 ) return j+1; |
| 215971 | 217206 | } |
| | @@ -216375,10 +217610,11 @@ |
| 216375 | 217610 | pParse->iErr = i+1; |
| 216376 | 217611 | return -1; |
| 216377 | 217612 | } |
| 216378 | 217613 | }else{ |
| 216379 | 217614 | if( !sqlite3Isdigit(z[i+1]) ){ |
| 217615 | + assert( z[i]=='-' || z[i]=='+' ); |
| 216380 | 217616 | /* JSON5 allows for "+Infinity" and "-Infinity" using exactly |
| 216381 | 217617 | ** that case. SQLite also allows these in any case and it allows |
| 216382 | 217618 | ** "+inf" and "-inf". */ |
| 216383 | 217619 | if( (z[i+1]=='I' || z[i+1]=='i') |
| 216384 | 217620 | && sqlite3StrNICmp(&z[i+1], "inf",3)==0 |
| | @@ -216394,10 +217630,22 @@ |
| 216394 | 217630 | if( z[i+1]=='.' ){ |
| 216395 | 217631 | pParse->hasNonstd = 1; |
| 216396 | 217632 | t |= 0x01; |
| 216397 | 217633 | goto parse_number_2; |
| 216398 | 217634 | } |
| 217635 | +#if 0 |
| 217636 | + /* Full JSON5 compliance requires support for +NaN and -NaN, even |
| 217637 | + ** though the extra prefix is not used for anything. Enable this |
| 217638 | + ** block of code to get SQLite to do the same thing. It never |
| 217639 | + ** has in an official release, and the only accomplishment of |
| 217640 | + ** enabling it would be to take up a few bytes more code space. */ |
| 217641 | + if( sqlite3StrNICmp(&z[i+1],"NaN",3)==0 ){ |
| 217642 | + pParse->hasNonstd = 1; |
| 217643 | + jsonBlobAppendOneByte(pParse, JSONB_NULL); |
| 217644 | + return i+4; |
| 217645 | + } |
| 217646 | +#endif |
| 216399 | 217647 | pParse->iErr = i; |
| 216400 | 217648 | return -1; |
| 216401 | 217649 | } |
| 216402 | 217650 | if( z[i+1]=='0' ){ |
| 216403 | 217651 | if( sqlite3Isdigit(z[i+2]) ){ |
| | @@ -216573,10 +217821,11 @@ |
| 216573 | 217821 | #endif |
| 216574 | 217822 | while( jsonIsspace(zJson[i]) ) i++; |
| 216575 | 217823 | if( zJson[i] ){ |
| 216576 | 217824 | i += json5Whitespace(&zJson[i]); |
| 216577 | 217825 | if( zJson[i] ){ |
| 217826 | + pParse->iErr = i; |
| 216578 | 217827 | if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1); |
| 216579 | 217828 | jsonParseReset(pParse); |
| 216580 | 217829 | return 1; |
| 216581 | 217830 | } |
| 216582 | 217831 | pParse->hasNonstd = 1; |
| | @@ -216845,10 +218094,98 @@ |
| 216845 | 218094 | #else /* if !SQLITE_USES_INLINE */ |
| 216846 | 218095 | /* On compilers that do not support in-lining, just call the original |
| 216847 | 218096 | ** jsonbPayloadSize(). It's slower, but it gets the right answer. */ |
| 216848 | 218097 | # define jsonbPayloadSizeSemiInline(a,b,c) jsonbPayloadSize(a,b,c) |
| 216849 | 218098 | #endif |
| 218099 | + |
| 218100 | +/* |
| 218101 | +** Convert a JSONB integer literal into either an integer (if it is small |
| 218102 | +** enough) or into a double. Include the sign. Return: |
| 218103 | +** |
| 218104 | +** 0 Value is an integer, results written into *pI. |
| 218105 | +** 1 Value is a double, results written into *pR. |
| 218106 | +** 2 Syntax error |
| 218107 | +*/ |
| 218108 | +static int jsonConvertInteger(const char *zIn, u32 sz, i64 *pI, double *pR){ |
| 218109 | + u32 k = 0; |
| 218110 | + int bNeg = 0; |
| 218111 | + sqlite3_uint64 u = 0; |
| 218112 | + double rOverflow = 1.0; |
| 218113 | + if( sz==0 ) return 2; |
| 218114 | + if( zIn[0]=='-' ){ |
| 218115 | + bNeg = 1; |
| 218116 | + k = 1; |
| 218117 | + }else if( zIn[0]=='+' ){ |
| 218118 | + k = 1; |
| 218119 | + } |
| 218120 | + if( k+2<sz && zIn[k]=='0' && (zIn[k+1]=='x' || zIn[k+1]=='X') ){ |
| 218121 | + k += 2; |
| 218122 | + do{ |
| 218123 | + if( !sqlite3Isxdigit(zIn[k]) ){ |
| 218124 | + return 2; |
| 218125 | + }else if( (u>>60)!=0 ){ |
| 218126 | + rOverflow *= 16.0; |
| 218127 | + }else{ |
| 218128 | + u = u*16 + sqlite3HexToInt(zIn[k]); |
| 218129 | + } |
| 218130 | + k++; |
| 218131 | + }while( k<sz ); |
| 218132 | + }else if( k>=sz ){ |
| 218133 | + return 2; |
| 218134 | + }else{ |
| 218135 | + do{ |
| 218136 | + if( zIn[k]<'0' || zIn[k]>'9' ){ |
| 218137 | + return 2; |
| 218138 | + }else if( (u>>60)!=0 ){ |
| 218139 | + rOverflow *= 10.0; |
| 218140 | + }else{ |
| 218141 | + u = u*10 + zIn[k] - '0'; |
| 218142 | + } |
| 218143 | + k++; |
| 218144 | + }while( k<sz ); |
| 218145 | + } |
| 218146 | + if( rOverflow>1.0 |
| 218147 | + || (u>0x7fffffffffffffffULL && (!bNeg || u>0x8000000000000000ULL)) |
| 218148 | + ){ |
| 218149 | + *pR = rOverflow * (double)u; |
| 218150 | + if( bNeg ) *pR = -*pR; |
| 218151 | + return 1; |
| 218152 | + }else{ |
| 218153 | + memcpy(pI, &u, sizeof(u)); |
| 218154 | + if( bNeg && u!=0x8000000000000000ULL ) *pI = -*pI; |
| 218155 | + return 0; |
| 218156 | + } |
| 218157 | +} |
| 218158 | + |
| 218159 | +/* |
| 218160 | +** Tail recursive code to implement the JSON_INT5 case of of |
| 218161 | +** jsonTranslateBlobInt5ToText(). This is split out because |
| 218162 | +** the resulting reduction in the stack frame size helps the |
| 218163 | +** caller to run faster. |
| 218164 | +*/ |
| 218165 | +static SQLITE_NOINLINE u32 jsonTranslateBlobInt5ToText( |
| 218166 | + JsonParse *pParse, /* the complete parse of the JSON */ |
| 218167 | + u32 i, /* Start rendering at this index */ |
| 218168 | + JsonString *pOut, /* Write JSON here */ |
| 218169 | + u32 sz, /* Payload size */ |
| 218170 | + u32 n /* Header size */ |
| 218171 | +){ |
| 218172 | + int k; |
| 218173 | + const char *zIn = (const char*)&pParse->aBlob[i+n]; |
| 218174 | + i64 ival; |
| 218175 | + double rval; |
| 218176 | + k = jsonConvertInteger(zIn, sz, &ival, &rval); |
| 218177 | + if( k==0 ){ |
| 218178 | + jsonPrintf(100, pOut, "%lld", ival); |
| 218179 | + }else if( k==1 ){ |
| 218180 | + jsonPrintf(100, pOut, "%!0.17g", rval); |
| 218181 | + }else{ |
| 218182 | + pOut->eErr |= JSTRING_MALFORMED; |
| 218183 | + } |
| 218184 | + return i+n+sz; |
| 218185 | +} |
| 218186 | + |
| 216850 | 218187 | |
| 216851 | 218188 | /* |
| 216852 | 218189 | ** Translate the binary JSONB representation of JSON beginning at |
| 216853 | 218190 | ** pParse->aBlob[i] into a JSON text string. Append the JSON |
| 216854 | 218191 | ** text onto the end of pOut. Return the index in pParse->aBlob[] |
| | @@ -216891,33 +218228,11 @@ |
| 216891 | 218228 | if( sz==0 ) goto malformed_jsonb; |
| 216892 | 218229 | jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz); |
| 216893 | 218230 | break; |
| 216894 | 218231 | } |
| 216895 | 218232 | case JSONB_INT5: { /* Integer literal in hexadecimal notation */ |
| 216896 | | - u32 k = 2; |
| 216897 | | - sqlite3_uint64 u = 0; |
| 216898 | | - const char *zIn = (const char*)&pParse->aBlob[i+n]; |
| 216899 | | - int bOverflow = 0; |
| 216900 | | - if( sz==0 ) goto malformed_jsonb; |
| 216901 | | - if( zIn[0]=='-' ){ |
| 216902 | | - jsonAppendChar(pOut, '-'); |
| 216903 | | - k++; |
| 216904 | | - }else if( zIn[0]=='+' ){ |
| 216905 | | - k++; |
| 216906 | | - } |
| 216907 | | - for(; k<sz; k++){ |
| 216908 | | - if( !sqlite3Isxdigit(zIn[k]) ){ |
| 216909 | | - pOut->eErr |= JSTRING_MALFORMED; |
| 216910 | | - break; |
| 216911 | | - }else if( (u>>60)!=0 ){ |
| 216912 | | - bOverflow = 1; |
| 216913 | | - }else{ |
| 216914 | | - u = u*16 + sqlite3HexToInt(zIn[k]); |
| 216915 | | - } |
| 216916 | | - } |
| 216917 | | - jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u); |
| 216918 | | - break; |
| 218233 | + return jsonTranslateBlobInt5ToText(pParse, i, pOut, sz, n); |
| 216919 | 218234 | } |
| 216920 | 218235 | case JSONB_FLOAT5: { /* Float literal missing digits beside "." */ |
| 216921 | 218236 | u32 k = 0; |
| 216922 | 218237 | const char *zIn = (const char*)&pParse->aBlob[i+n]; |
| 216923 | 218238 | if( sz==0 ) goto malformed_jsonb; |
| | @@ -217170,11 +218485,10 @@ |
| 217170 | 218485 | jsonAppendChar(pOut, '\n'); |
| 217171 | 218486 | pPretty->nIndent++; |
| 217172 | 218487 | if( pPretty->nIndent >= JSON_MAX_DEPTH ){ |
| 217173 | 218488 | jsonStringTooDeep(pOut); |
| 217174 | 218489 | } |
| 217175 | | - pParse->iDepth = pPretty->nIndent; |
| 217176 | 218490 | while( pOut->eErr==0 ){ |
| 217177 | 218491 | jsonPrettyIndent(pPretty); |
| 217178 | 218492 | j = jsonTranslateBlobToText(pParse, j, pOut); |
| 217179 | 218493 | if( j>iEnd ){ |
| 217180 | 218494 | pOut->eErr |= JSTRING_MALFORMED; |
| | @@ -217672,11 +218986,11 @@ |
| 217672 | 218986 | jsonBlobEdit(pParse, iRoot, sz, 0, 0); |
| 217673 | 218987 | }else if( pParse->eEdit==JEDIT_INS ){ |
| 217674 | 218988 | /* Already exists, so json_insert() is a no-op */ |
| 217675 | 218989 | }else if( pParse->eEdit==JEDIT_AINS ){ |
| 217676 | 218990 | /* json_array_insert() */ |
| 217677 | | - if( zPath[-1]!=']' ){ |
| 218991 | + if( zPath[-1]!=']' || iLabel ){ |
| 217678 | 218992 | return JSON_LOOKUP_NOTARRAY; |
| 217679 | 218993 | }else{ |
| 217680 | 218994 | jsonBlobEdit(pParse, iRoot, 0, pParse->aIns, pParse->nIns); |
| 217681 | 218995 | } |
| 217682 | 218996 | }else{ |
| | @@ -217929,56 +219243,28 @@ |
| 217929 | 219243 | sqlite3_result_int(pCtx, 0); |
| 217930 | 219244 | break; |
| 217931 | 219245 | } |
| 217932 | 219246 | case JSONB_INT5: |
| 217933 | 219247 | case JSONB_INT: { |
| 217934 | | - sqlite3_int64 iRes = 0; |
| 217935 | | - char *z; |
| 217936 | | - int bNeg = 0; |
| 217937 | | - char x; |
| 217938 | | - if( sz==0 ) goto returnfromblob_malformed; |
| 217939 | | - x = (char)pParse->aBlob[i+n]; |
| 217940 | | - if( x=='-' ){ |
| 217941 | | - if( sz<2 ) goto returnfromblob_malformed; |
| 217942 | | - n++; |
| 217943 | | - sz--; |
| 217944 | | - bNeg = 1; |
| 217945 | | - } |
| 217946 | | - z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); |
| 217947 | | - if( z==0 ) goto returnfromblob_oom; |
| 217948 | | - rc = sqlite3DecOrHexToI64(z, &iRes); |
| 217949 | | - sqlite3DbFree(db, z); |
| 217950 | | - if( rc==0 ){ |
| 217951 | | - if( iRes<0 ){ |
| 217952 | | - /* A hexadecimal literal with 16 significant digits and with the |
| 217953 | | - ** high-order bit set is a negative integer in SQLite (and hence |
| 217954 | | - ** iRes comes back as negative) but should be interpreted as a |
| 217955 | | - ** positive value if it occurs within JSON. The value is too |
| 217956 | | - ** large to appear as an SQLite integer so it must be converted |
| 217957 | | - ** into floating point. */ |
| 217958 | | - double r; |
| 217959 | | - r = (double)*(sqlite3_uint64*)&iRes; |
| 217960 | | - sqlite3_result_double(pCtx, bNeg ? -r : r); |
| 217961 | | - }else{ |
| 217962 | | - sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes); |
| 217963 | | - } |
| 217964 | | - }else if( rc==3 && bNeg ){ |
| 217965 | | - sqlite3_result_int64(pCtx, SMALLEST_INT64); |
| 217966 | | - }else if( rc==1 ){ |
| 219248 | + sqlite3_int64 ival; |
| 219249 | + double rval; |
| 219250 | + int k; |
| 219251 | + k = jsonConvertInteger((const char*)&pParse->aBlob[i+n], sz,&ival,&rval); |
| 219252 | + if( k==0 ){ |
| 219253 | + sqlite3_result_int64(pCtx, ival); |
| 219254 | + }else if( k==1 ){ |
| 219255 | + sqlite3_result_double(pCtx, rval); |
| 219256 | + }else{ |
| 217967 | 219257 | goto returnfromblob_malformed; |
| 217968 | | - }else{ |
| 217969 | | - if( bNeg ){ n--; sz++; } |
| 217970 | | - goto to_double; |
| 217971 | 219258 | } |
| 217972 | 219259 | break; |
| 217973 | 219260 | } |
| 217974 | 219261 | case JSONB_FLOAT5: |
| 217975 | 219262 | case JSONB_FLOAT: { |
| 217976 | 219263 | double r; |
| 217977 | 219264 | char *z; |
| 217978 | 219265 | if( sz==0 ) goto returnfromblob_malformed; |
| 217979 | | - to_double: |
| 217980 | 219266 | z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); |
| 217981 | 219267 | if( z==0 ) goto returnfromblob_oom; |
| 217982 | 219268 | rc = sqlite3AtoF(z, &r); |
| 217983 | 219269 | sqlite3DbFree(db, z); |
| 217984 | 219270 | if( rc<=0 ) goto returnfromblob_malformed; |
| | @@ -218242,12 +219528,15 @@ |
| 218242 | 219528 | return; |
| 218243 | 219529 | } |
| 218244 | 219530 | if( zPath[1]==0 ){ |
| 218245 | 219531 | if( eEdit==JEDIT_REPL || eEdit==JEDIT_SET ){ |
| 218246 | 219532 | jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob); |
| 219533 | + }else if( eEdit==JEDIT_AINS ){ |
| 219534 | + rc = JSON_LOOKUP_NOTARRAY; |
| 219535 | + }else{ |
| 219536 | + rc = 0; |
| 218247 | 219537 | } |
| 218248 | | - rc = 0; |
| 218249 | 219538 | }else{ |
| 218250 | 219539 | p->eEdit = eEdit; |
| 218251 | 219540 | p->nIns = ax.nBlob; |
| 218252 | 219541 | p->aIns = ax.aBlob; |
| 218253 | 219542 | p->delta = 0; |
| | @@ -220063,13 +221352,14 @@ |
| 220063 | 221352 | } |
| 220064 | 221353 | break; |
| 220065 | 221354 | } |
| 220066 | 221355 | if( p->eType==JSONB_OBJECT ){ |
| 220067 | 221356 | jsonReturnFromBlob(&p->sParse, p->i, ctx, 1); |
| 220068 | | - }else{ |
| 220069 | | - assert( p->eType==JSONB_ARRAY ); |
| 221357 | + }else if( p->eType==JSONB_ARRAY ){ |
| 220070 | 221358 | sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey); |
| 221359 | + }else{ |
| 221360 | + sqlite3_result_error(ctx, "malformed JSON", -1); |
| 220071 | 221361 | } |
| 220072 | 221362 | break; |
| 220073 | 221363 | } |
| 220074 | 221364 | case JEACH_VALUE: { |
| 220075 | 221365 | u32 i = jsonSkipLabel(p); |
| | @@ -221333,11 +222623,13 @@ |
| 221333 | 222623 | if( pNode->iNode ){ |
| 221334 | 222624 | sqlite3_bind_int64(p, 1, pNode->iNode); |
| 221335 | 222625 | }else{ |
| 221336 | 222626 | sqlite3_bind_null(p, 1); |
| 221337 | 222627 | } |
| 221338 | | - sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC); |
| 222628 | + rc = sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, |
| 222629 | + SQLITE_STATIC); |
| 222630 | + if( rc!=SQLITE_OK ) return rc; |
| 221339 | 222631 | sqlite3_step(p); |
| 221340 | 222632 | pNode->isDirty = 0; |
| 221341 | 222633 | rc = sqlite3_reset(p); |
| 221342 | 222634 | sqlite3_bind_null(p, 2); |
| 221343 | 222635 | if( pNode->iNode==0 && rc==SQLITE_OK ){ |
| | @@ -225214,11 +226506,11 @@ |
| 225214 | 226506 | sqlite3_str_append(x, "[", 1); |
| 225215 | 226507 | for(i=0; i<p->nVertex; i++){ |
| 225216 | 226508 | sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i)); |
| 225217 | 226509 | } |
| 225218 | 226510 | sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0)); |
| 225219 | | - sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free); |
| 226511 | + sqlite3_result_str(context, x, SQLITE_FINISH); |
| 225220 | 226512 | sqlite3_free(p); |
| 225221 | 226513 | } |
| 225222 | 226514 | } |
| 225223 | 226515 | |
| 225224 | 226516 | /* |
| | @@ -225251,11 +226543,11 @@ |
| 225251 | 226543 | if( z && z[0] ){ |
| 225252 | 226544 | sqlite3_str_appendf(x, " %s", z); |
| 225253 | 226545 | } |
| 225254 | 226546 | } |
| 225255 | 226547 | sqlite3_str_appendf(x, "></polyline>"); |
| 225256 | | - sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free); |
| 226548 | + sqlite3_result_str(context, x, SQLITE_FINISH); |
| 225257 | 226549 | sqlite3_free(p); |
| 225258 | 226550 | } |
| 225259 | 226551 | } |
| 225260 | 226552 | |
| 225261 | 226553 | /* |
| | @@ -226570,11 +227862,12 @@ |
| 226570 | 227862 | GeoPoly *p = 0; |
| 226571 | 227863 | if( sqlite3_value_type(pShape)==SQLITE_TEXT |
| 226572 | 227864 | && (p = geopolyFuncParam(0, pShape, &rc))!=0 |
| 226573 | 227865 | && rc==SQLITE_OK |
| 226574 | 227866 | ){ |
| 226575 | | - sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT); |
| 227867 | + rc = sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, |
| 227868 | + SQLITE_TRANSIENT); |
| 226576 | 227869 | }else{ |
| 226577 | 227870 | sqlite3_bind_value(pUp, 2, pShape); |
| 226578 | 227871 | } |
| 226579 | 227872 | sqlite3_free(p); |
| 226580 | 227873 | nChange = 1; |
| | @@ -226581,11 +227874,11 @@ |
| 226581 | 227874 | } |
| 226582 | 227875 | for(jj=1; jj<nData-2; jj++){ |
| 226583 | 227876 | nChange++; |
| 226584 | 227877 | sqlite3_bind_value(pUp, jj+2, aData[jj+2]); |
| 226585 | 227878 | } |
| 226586 | | - if( nChange ){ |
| 227879 | + if( rc==SQLITE_OK && nChange ){ |
| 226587 | 227880 | sqlite3_step(pUp); |
| 226588 | 227881 | rc = sqlite3_reset(pUp); |
| 226589 | 227882 | } |
| 226590 | 227883 | } |
| 226591 | 227884 | |
| | @@ -228654,10 +229947,11 @@ |
| 228654 | 229947 | sqlite3_stmt *pTblIter; /* Iterate through tables */ |
| 228655 | 229948 | sqlite3_stmt *pIdxIter; /* Index iterator */ |
| 228656 | 229949 | int nTblCol; /* Size of azTblCol[] array */ |
| 228657 | 229950 | char **azTblCol; /* Array of unquoted target column names */ |
| 228658 | 229951 | char **azTblType; /* Array of target column types */ |
| 229952 | + char **azTblDflt; /* Array of target column DEFAULT exprs */ |
| 228659 | 229953 | int *aiSrcOrder; /* src table col -> target table col */ |
| 228660 | 229954 | u8 *abTblPk; /* Array of flags, set on target PK columns */ |
| 228661 | 229955 | u8 *abNotNull; /* Array of flags, set on NOT NULL columns */ |
| 228662 | 229956 | u8 *abIndexed; /* Array of flags, set on indexed & PK cols */ |
| 228663 | 229957 | int eType; /* Table type - an RBU_PK_XXX value */ |
| | @@ -229219,14 +230513,16 @@ |
| 229219 | 230513 | static void rbuObjIterFreeCols(RbuObjIter *pIter){ |
| 229220 | 230514 | int i; |
| 229221 | 230515 | for(i=0; i<pIter->nTblCol; i++){ |
| 229222 | 230516 | sqlite3_free(pIter->azTblCol[i]); |
| 229223 | 230517 | sqlite3_free(pIter->azTblType[i]); |
| 230518 | + sqlite3_free(pIter->azTblDflt[i]); |
| 229224 | 230519 | } |
| 229225 | 230520 | sqlite3_free(pIter->azTblCol); |
| 229226 | 230521 | pIter->azTblCol = 0; |
| 229227 | 230522 | pIter->azTblType = 0; |
| 230523 | + pIter->azTblDflt = 0; |
| 229228 | 230524 | pIter->aiSrcOrder = 0; |
| 229229 | 230525 | pIter->abTblPk = 0; |
| 229230 | 230526 | pIter->abNotNull = 0; |
| 229231 | 230527 | pIter->nTblCol = 0; |
| 229232 | 230528 | pIter->eType = 0; /* Invalid value */ |
| | @@ -229475,10 +230771,47 @@ |
| 229475 | 230771 | } |
| 229476 | 230772 | sqlite3_free(zSql); |
| 229477 | 230773 | va_end(ap); |
| 229478 | 230774 | return p->rc; |
| 229479 | 230775 | } |
| 230776 | + |
| 230777 | +/* |
| 230778 | +** Like rbuMPrintfExec() except that it is guaranteed to only run a |
| 230779 | +** single statement. If the generated SQL text includes two or more |
| 230780 | +** statements, all but the first is ignored. |
| 230781 | +** |
| 230782 | +** If an error occurs, an error code and error message is stored in the |
| 230783 | +** RBU handle. If an error has already occurred when this function is |
| 230784 | +** called, it is a no-op. |
| 230785 | +*/ |
| 230786 | +static int rbuMPrintfOneStmt(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){ |
| 230787 | + va_list ap; |
| 230788 | + char *zSql; |
| 230789 | + va_start(ap, zFmt); |
| 230790 | + zSql = sqlite3_vmprintf(zFmt, ap); |
| 230791 | + if( p->rc==SQLITE_OK ){ |
| 230792 | + if( zSql==0 ){ |
| 230793 | + p->rc = SQLITE_NOMEM; |
| 230794 | + }else{ |
| 230795 | + sqlite3_stmt *pStmt; |
| 230796 | + p->rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 230797 | + if( pStmt ){ |
| 230798 | + if( p->rc==SQLITE_OK ){ |
| 230799 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){} |
| 230800 | + } |
| 230801 | + p->rc = sqlite3_finalize(pStmt); |
| 230802 | + } |
| 230803 | + if( p->rc!=SQLITE_OK ){ |
| 230804 | + sqlite3_free(p->zErrmsg); |
| 230805 | + p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 230806 | + } |
| 230807 | + } |
| 230808 | + } |
| 230809 | + sqlite3_free(zSql); |
| 230810 | + va_end(ap); |
| 230811 | + return p->rc; |
| 230812 | +} |
| 229480 | 230813 | |
| 229481 | 230814 | /* |
| 229482 | 230815 | ** Attempt to allocate and return a pointer to a zeroed block of nByte |
| 229483 | 230816 | ** bytes. |
| 229484 | 230817 | ** |
| | @@ -229507,18 +230840,19 @@ |
| 229507 | 230840 | ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that |
| 229508 | 230841 | ** there is room for at least nCol elements. If an OOM occurs, store an |
| 229509 | 230842 | ** error code in the RBU handle passed as the first argument. |
| 229510 | 230843 | */ |
| 229511 | 230844 | static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){ |
| 229512 | | - sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol; |
| 230845 | + sqlite3_int64 nByte = (3*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol; |
| 229513 | 230846 | char **azNew; |
| 229514 | 230847 | |
| 229515 | 230848 | azNew = (char**)rbuMalloc(p, nByte); |
| 229516 | 230849 | if( azNew ){ |
| 229517 | 230850 | pIter->azTblCol = azNew; |
| 229518 | 230851 | pIter->azTblType = &azNew[nCol]; |
| 229519 | | - pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol]; |
| 230852 | + pIter->azTblDflt = &azNew[nCol*2]; |
| 230853 | + pIter->aiSrcOrder = (int*)&pIter->azTblDflt[nCol]; |
| 229520 | 230854 | pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol]; |
| 229521 | 230855 | pIter->abNotNull = (u8*)&pIter->abTblPk[nCol]; |
| 229522 | 230856 | pIter->abIndexed = (u8*)&pIter->abNotNull[nCol]; |
| 229523 | 230857 | } |
| 229524 | 230858 | } |
| | @@ -229548,10 +230882,35 @@ |
| 229548 | 230882 | } |
| 229549 | 230883 | } |
| 229550 | 230884 | |
| 229551 | 230885 | return zRet; |
| 229552 | 230886 | } |
| 230887 | + |
| 230888 | +/* |
| 230889 | +** Return a copy of the declared column type zType that is safe to paste |
| 230890 | +** into a CREATE TABLE statement. |
| 230891 | +** |
| 230892 | +** zType comes from the schema of a database that RBU does not control. |
| 230893 | +** A type name may legally be a quoted string, so after the quotes are |
| 230894 | +** removed (as PRAGMA table_xinfo does) it can contain any text at all, |
| 230895 | +** including ")", ";" and CHECK or other column constraints. So it is |
| 230896 | +** returned as a single double-quoted token, which the parser turns back |
| 230897 | +** into the identical type name. An empty type is returned as an empty |
| 230898 | +** string. The result must be freed using sqlite3_free(). |
| 230899 | +*/ |
| 230900 | +static char *rbuQuoteType(const char *zType, int *pRc){ |
| 230901 | + char *zRet = 0; |
| 230902 | + if( *pRc==SQLITE_OK ){ |
| 230903 | + if( zType==0 || zType[0]==0 ){ |
| 230904 | + zRet = rbuStrndup("", pRc); |
| 230905 | + }else{ |
| 230906 | + zRet = sqlite3_mprintf("\"%w\"", zType); |
| 230907 | + if( zRet==0 ) *pRc = SQLITE_NOMEM; |
| 230908 | + } |
| 230909 | + } |
| 230910 | + return zRet; |
| 230911 | +} |
| 229553 | 230912 | |
| 229554 | 230913 | /* |
| 229555 | 230914 | ** Finalize the statement passed as the second argument. |
| 229556 | 230915 | ** |
| 229557 | 230916 | ** If the sqlite3_finalize() call indicates that an error occurs, and the |
| | @@ -229734,10 +231093,29 @@ |
| 229734 | 231093 | |
| 229735 | 231094 | rbuFinalize(p, pList); |
| 229736 | 231095 | if( bIndex==0 ) pIter->abIndexed = 0; |
| 229737 | 231096 | } |
| 229738 | 231097 | |
| 231098 | +/* |
| 231099 | +** Return true if table zTbl in the main database of the target is a |
| 231100 | +** STRICT table, or false otherwise. If an error occurs, an error code |
| 231101 | +** and message are left in the RBU handle and zero is returned. |
| 231102 | +*/ |
| 231103 | +static int rbuTableIsStrict(sqlite3rbu *p, const char *zTbl){ |
| 231104 | + int bStrict = 0; |
| 231105 | + sqlite3_stmt *pStmt = 0; |
| 231106 | + if( p->rc==SQLITE_OK ){ |
| 231107 | + p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg, |
| 231108 | + sqlite3_mprintf("PRAGMA main.table_list(%Q)", zTbl) |
| 231109 | + ); |
| 231110 | + if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 231111 | + bStrict = sqlite3_column_int(pStmt, 5); |
| 231112 | + } |
| 231113 | + rbuFinalize(p, pStmt); |
| 231114 | + } |
| 231115 | + return bStrict; |
| 231116 | +} |
| 229739 | 231117 | |
| 229740 | 231118 | /* |
| 229741 | 231119 | ** If they are not already populated, populate the pIter->azTblCol[], |
| 229742 | 231120 | ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to |
| 229743 | 231121 | ** the table (not index) that the iterator currently points to. |
| | @@ -229752,10 +231130,11 @@ |
| 229752 | 231130 | int nCol = 0; |
| 229753 | 231131 | int i; /* for() loop iterator variable */ |
| 229754 | 231132 | int bRbuRowid = 0; /* If input table has column "rbu_rowid" */ |
| 229755 | 231133 | int iOrder = 0; |
| 229756 | 231134 | int iTnum = 0; |
| 231135 | + int bStrict = 0; /* True if target is a STRICT table */ |
| 229757 | 231136 | |
| 229758 | 231137 | /* Figure out the type of table this step will deal with. */ |
| 229759 | 231138 | assert( pIter->eType==0 ); |
| 229760 | 231139 | rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum); |
| 229761 | 231140 | if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){ |
| | @@ -229802,14 +231181,16 @@ |
| 229802 | 231181 | p->zErrmsg = sqlite3_mprintf( |
| 229803 | 231182 | "table %q %s rbu_rowid column", pIter->zDataTbl, |
| 229804 | 231183 | (bRbuRowid ? "may not have" : "requires") |
| 229805 | 231184 | ); |
| 229806 | 231185 | } |
| 231186 | + |
| 231187 | + bStrict = rbuTableIsStrict(p, pIter->zTbl); |
| 229807 | 231188 | |
| 229808 | 231189 | /* Check that all non-HIDDEN columns in the destination table are also |
| 229809 | | - ** present in the input table. Populate the abTblPk[], azTblType[] and |
| 229810 | | - ** aiTblOrder[] arrays at the same time. |
| 231190 | + ** present in the input table. Populate the abTblPk[], azTblType[], |
| 231191 | + ** azTblDflt[] and aiTblOrder[] arrays at the same time. |
| 229811 | 231192 | ** |
| 229812 | 231193 | ** RBU does not support tables with GENERATED columns. If the target |
| 229813 | 231194 | ** table has any, return an error. */ |
| 229814 | 231195 | if( p->rc==SQLITE_OK ){ |
| 229815 | 231196 | p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg, |
| | @@ -229838,17 +231219,22 @@ |
| 229838 | 231219 | ); |
| 229839 | 231220 | }else{ |
| 229840 | 231221 | int iPk = sqlite3_column_int(pStmt, 5); |
| 229841 | 231222 | int bNotNull = sqlite3_column_int(pStmt, 3); |
| 229842 | 231223 | const char *zType = (const char*)sqlite3_column_text(pStmt, 2); |
| 231224 | + const char *zDflt = (const char*)sqlite3_column_text(pStmt, 4); |
| 229843 | 231225 | |
| 229844 | 231226 | if( i!=iOrder ){ |
| 229845 | 231227 | SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]); |
| 229846 | 231228 | SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]); |
| 229847 | 231229 | } |
| 229848 | 231230 | |
| 229849 | | - pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc); |
| 231231 | + /* In a STRICT table, ANY means no affinity. In the non-STRICT |
| 231232 | + ** imposter it would mean NUMERIC, so declare it without a type. */ |
| 231233 | + if( bStrict && sqlite3_stricmp(zType, "ANY")==0 ) zType = ""; |
| 231234 | + pIter->azTblType[iOrder] = rbuQuoteType(zType, &p->rc); |
| 231235 | + pIter->azTblDflt[iOrder] = rbuStrndup(zDflt, &p->rc); |
| 229850 | 231236 | assert( iPk>=0 ); |
| 229851 | 231237 | pIter->abTblPk[iOrder] = (u8)iPk; |
| 229852 | 231238 | pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0); |
| 229853 | 231239 | iOrder++; |
| 229854 | 231240 | } |
| | @@ -230517,11 +231903,11 @@ |
| 230517 | 231903 | } |
| 230518 | 231904 | zCols = rbuMPrintf(p, "%z, id INTEGER", zCols); |
| 230519 | 231905 | rbuFinalize(p, pXInfo); |
| 230520 | 231906 | |
| 230521 | 231907 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum); |
| 230522 | | - rbuMPrintfExec(p, p->dbMain, |
| 231908 | + rbuMPrintfOneStmt(p, p->dbMain, |
| 230523 | 231909 | "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID", |
| 230524 | 231910 | zCols, zPk |
| 230525 | 231911 | ); |
| 230526 | 231912 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0); |
| 230527 | 231913 | } |
| | @@ -230567,13 +231953,15 @@ |
| 230567 | 231953 | if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){ |
| 230568 | 231954 | /* If the target table column is an "INTEGER PRIMARY KEY", add |
| 230569 | 231955 | ** "PRIMARY KEY" to the imposter table column declaration. */ |
| 230570 | 231956 | zPk = "PRIMARY KEY "; |
| 230571 | 231957 | } |
| 230572 | | - zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s", |
| 231958 | + zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s%s%s", |
| 230573 | 231959 | zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl, |
| 230574 | | - (pIter->abNotNull[iCol] ? " NOT NULL" : "") |
| 231960 | + (pIter->abNotNull[iCol] ? " NOT NULL" : ""), |
| 231961 | + (pIter->azTblDflt[iCol] ? " DEFAULT " : ""), |
| 231962 | + (pIter->azTblDflt[iCol] ? pIter->azTblDflt[iCol] : "") |
| 230575 | 231963 | ); |
| 230576 | 231964 | zComma = ", "; |
| 230577 | 231965 | } |
| 230578 | 231966 | |
| 230579 | 231967 | if( pIter->eType==RBU_PK_WITHOUT_ROWID ){ |
| | @@ -230582,11 +231970,11 @@ |
| 230582 | 231970 | zSql = rbuMPrintf(p, "%z, %z", zSql, zPk); |
| 230583 | 231971 | } |
| 230584 | 231972 | } |
| 230585 | 231973 | |
| 230586 | 231974 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum); |
| 230587 | | - rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s", |
| 231975 | + rbuMPrintfOneStmt(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s", |
| 230588 | 231976 | pIter->zTbl, zSql, |
| 230589 | 231977 | (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "") |
| 230590 | 231978 | ); |
| 230591 | 231979 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0); |
| 230592 | 231980 | } |
| | @@ -230786,11 +232174,11 @@ |
| 230786 | 232174 | zBind = rbuObjIterGetBindlist(p, nBind); |
| 230787 | 232175 | |
| 230788 | 232176 | /* Create the imposter table used to write to this index. */ |
| 230789 | 232177 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1); |
| 230790 | 232178 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum); |
| 230791 | | - rbuMPrintfExec(p, p->dbMain, |
| 232179 | + rbuMPrintfOneStmt(p, p->dbMain, |
| 230792 | 232180 | "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID", |
| 230793 | 232181 | zTbl, zImposterCols, zImposterPK |
| 230794 | 232182 | ); |
| 230795 | 232183 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0); |
| 230796 | 232184 | |
| | @@ -230913,11 +232301,11 @@ |
| 230913 | 232301 | if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ |
| 230914 | 232302 | zRbuRowid = ", rbu_rowid"; |
| 230915 | 232303 | } |
| 230916 | 232304 | |
| 230917 | 232305 | /* Create the rbu_tmp_xxx table and the triggers to populate it. */ |
| 230918 | | - rbuMPrintfExec(p, p->dbRbu, |
| 232306 | + rbuMPrintfOneStmt(p, p->dbRbu, |
| 230919 | 232307 | "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS " |
| 230920 | 232308 | "SELECT *%s FROM '%q' WHERE 0;" |
| 230921 | 232309 | , p->zStateDb, pIter->zDataTbl |
| 230922 | 232310 | , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "") |
| 230923 | 232311 | , pIter->zDataTbl |
| | @@ -230942,11 +232330,11 @@ |
| 230942 | 232330 | zWrite, zTbl, zOldlist, |
| 230943 | 232331 | zWrite, zTbl, zNewlist |
| 230944 | 232332 | ); |
| 230945 | 232333 | |
| 230946 | 232334 | if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ |
| 230947 | | - rbuMPrintfExec(p, p->dbMain, |
| 232335 | + rbuMPrintfOneStmt(p, p->dbMain, |
| 230948 | 232336 | "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" " |
| 230949 | 232337 | "BEGIN " |
| 230950 | 232338 | " SELECT rbu_tmp_insert(0, %s);" |
| 230951 | 232339 | "END;", |
| 230952 | 232340 | zWrite, zTbl, zNewlist |
| | @@ -231227,11 +232615,11 @@ |
| 231227 | 232615 | } |
| 231228 | 232616 | |
| 231229 | 232617 | /* If using separate RBU and state databases, attach the state database to |
| 231230 | 232618 | ** the RBU db handle now. */ |
| 231231 | 232619 | if( p->zState ){ |
| 231232 | | - rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState); |
| 232620 | + rbuMPrintfOneStmt(p, p->dbRbu, "ATTACH %Q AS stat", p->zState); |
| 231233 | 232621 | memcpy(p->zStateDb, "stat", 4); |
| 231234 | 232622 | }else{ |
| 231235 | 232623 | memcpy(p->zStateDb, "main", 4); |
| 231236 | 232624 | } |
| 231237 | 232625 | |
| | @@ -231353,11 +232741,11 @@ |
| 231353 | 232741 | } |
| 231354 | 232742 | |
| 231355 | 232743 | if( p->rc==SQLITE_OK ){ |
| 231356 | 232744 | p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p); |
| 231357 | 232745 | } |
| 231358 | | - rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema"); |
| 232746 | + rbuMPrintfOneStmt(p, p->dbMain, "SELECT * FROM sqlite_schema"); |
| 231359 | 232747 | |
| 231360 | 232748 | /* Mark the database file just opened as an RBU target database. If |
| 231361 | 232749 | ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use. |
| 231362 | 232750 | ** This is an error. */ |
| 231363 | 232751 | if( p->rc==SQLITE_OK ){ |
| | @@ -231961,11 +233349,11 @@ |
| 231961 | 233349 | iCookie = sqlite3_column_int(pStmt, 0); |
| 231962 | 233350 | } |
| 231963 | 233351 | rbuFinalize(p, pStmt); |
| 231964 | 233352 | } |
| 231965 | 233353 | if( p->rc==SQLITE_OK ){ |
| 231966 | | - rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1); |
| 233354 | + rbuMPrintfOneStmt(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1); |
| 231967 | 233355 | } |
| 231968 | 233356 | } |
| 231969 | 233357 | } |
| 231970 | 233358 | |
| 231971 | 233359 | /* |
| | @@ -232039,11 +233427,11 @@ |
| 232039 | 233427 | sqlite3_stmt *pPragma = 0; |
| 232040 | 233428 | p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg, |
| 232041 | 233429 | sqlite3_mprintf("PRAGMA main.%s", zPragma) |
| 232042 | 233430 | ); |
| 232043 | 233431 | if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){ |
| 232044 | | - p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d", |
| 233432 | + p->rc = rbuMPrintfOneStmt(p, p->dbMain, "PRAGMA main.%s = %d", |
| 232045 | 233433 | zPragma, sqlite3_column_int(pPragma, 0) |
| 232046 | 233434 | ); |
| 232047 | 233435 | } |
| 232048 | 233436 | rbuFinalize(p, pPragma); |
| 232049 | 233437 | } |
| | @@ -232130,11 +233518,11 @@ |
| 232130 | 233518 | if( pIter->bCleanup ){ |
| 232131 | 233519 | /* Clean up the rbu_tmp_xxx table for the previous table. It |
| 232132 | 233520 | ** cannot be dropped as there are currently active SQL statements. |
| 232133 | 233521 | ** But the contents can be deleted. */ |
| 232134 | 233522 | if( rbuIsVacuum(p)==0 && pIter->abIndexed ){ |
| 232135 | | - rbuMPrintfExec(p, p->dbRbu, |
| 233523 | + rbuMPrintfOneStmt(p, p->dbRbu, |
| 232136 | 233524 | "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl |
| 232137 | 233525 | ); |
| 232138 | 233526 | } |
| 232139 | 233527 | }else{ |
| 232140 | 233528 | rbuObjIterPrepareAll(p, pIter, 0); |
| | @@ -234286,11 +235674,13 @@ |
| 234286 | 235674 | if( p->iPgno==1 ) nHdr += 100; |
| 234287 | 235675 | p->nCell = get2byte(&aHdr[3]); |
| 234288 | 235676 | p->nMxPayload = 0; |
| 234289 | 235677 | szPage = sqlite3BtreeGetPageSize(pBt); |
| 234290 | 235678 | |
| 234291 | | - nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; |
| 235679 | + nUnused = get2byte(&aHdr[5]); |
| 235680 | + if( nUnused==0 ) nUnused = 65536; |
| 235681 | + nUnused -= nHdr + 2*p->nCell; |
| 234292 | 235682 | nUnused += (int)aHdr[7]; |
| 234293 | 235683 | iOff = get2byte(&aHdr[1]); |
| 234294 | 235684 | while( iOff ){ |
| 234295 | 235685 | int iNext; |
| 234296 | 235686 | if( iOff>=szPage ) goto statPageIsCorrupt; |
| | @@ -235036,11 +236426,16 @@ |
| 235036 | 236426 | if( NEVER(pBt==0) ) return SQLITE_OK; |
| 235037 | 236427 | pCsr->pPager = sqlite3BtreePager(pBt); |
| 235038 | 236428 | pCsr->szPage = sqlite3BtreeGetPageSize(pBt); |
| 235039 | 236429 | pCsr->mxPgno = sqlite3BtreeLastPage(pBt); |
| 235040 | 236430 | if( idxNum & 1 ){ |
| 235041 | | - i64 iPg = sqlite3_value_int64(argv[idxNum>>1]); |
| 236431 | + i64 iPg; |
| 236432 | + if( sqlite3_value_numeric_type(argv[idxNum>>1])==SQLITE_INTEGER ){ |
| 236433 | + iPg = sqlite3_value_int64(argv[idxNum>>1]); |
| 236434 | + }else{ |
| 236435 | + iPg = 0; |
| 236436 | + } |
| 235042 | 236437 | assert( argc>(idxNum>>1) ); |
| 235043 | 236438 | if( iPg<1 || iPg>pCsr->mxPgno ){ |
| 235044 | 236439 | pCsr->pgno = 1; |
| 235045 | 236440 | pCsr->mxPgno = 0; |
| 235046 | 236441 | }else{ |
| | @@ -235798,14 +237193,17 @@ |
| 235798 | 237193 | }else if( mFlags==CARRAY_BLOB ){ |
| 235799 | 237194 | struct iovec *p = (struct iovec*)pNew->aData; |
| 235800 | 237195 | unsigned char *z = (unsigned char*)&p[nData]; |
| 235801 | 237196 | for(i=0; i<nData; i++){ |
| 235802 | 237197 | size_t n = ((struct iovec*)aData)[i].iov_len; |
| 237198 | + void *a = ((struct iovec*)aData)[i].iov_base; |
| 235803 | 237199 | p[i].iov_len = n; |
| 235804 | | - p[i].iov_base = z; |
| 235805 | | - z += n; |
| 235806 | | - memcpy(p[i].iov_base, ((struct iovec*)aData)[i].iov_base, n); |
| 237200 | + p[i].iov_base = a ? z : 0; |
| 237201 | + if( n ){ |
| 237202 | + z += n; |
| 237203 | + memcpy(p[i].iov_base, a, n); |
| 237204 | + } |
| 235807 | 237205 | } |
| 235808 | 237206 | }else if( sz>0 ){ |
| 235809 | 237207 | memcpy(pNew->aData, aData, sz); |
| 235810 | 237208 | } |
| 235811 | 237209 | pNew->xDel = sqlite3_free; |
| | @@ -236494,11 +237892,12 @@ |
| 236494 | 237892 | int n; |
| 236495 | 237893 | assert( a!=0 ); |
| 236496 | 237894 | e = *a; |
| 236497 | 237895 | if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9; |
| 236498 | 237896 | if( e==SQLITE_TEXT || e==SQLITE_BLOB ){ |
| 236499 | | - return sessionVarintGet(&a[1], &n) + 1 + n; |
| 237897 | + const int v = sessionVarintGet(&a[1], &n); |
| 237898 | + return v + 1 + n; |
| 236500 | 237899 | } |
| 236501 | 237900 | return 1; |
| 236502 | 237901 | } |
| 236503 | 237902 | |
| 236504 | 237903 | /* |
| | @@ -239796,13 +241195,19 @@ |
| 239796 | 241195 | ** there are no fields in the old.* record that are not (a) PK fields, |
| 239797 | 241196 | ** or (b) also present in the new.* record. |
| 239798 | 241197 | ** |
| 239799 | 241198 | ** Such records are technically corrupt, but the rebaser was at one |
| 239800 | 241199 | ** point generating them. Under most circumstances this is benign, but |
| 239801 | | - ** can cause spurious SQLITE_RANGE errors when applying the changeset. */ |
| 241200 | + ** can cause spurious SQLITE_RANGE errors when applying the changeset. |
| 241201 | + ** |
| 241202 | + ** Also check that all PK fields are present in the old.* record. If |
| 241203 | + ** they are not, the changeset is corrupt. */ |
| 239802 | 241204 | if( p->bPatchset==0 && p->op==SQLITE_UPDATE){ |
| 239803 | 241205 | for(i=0; i<p->nCol; i++){ |
| 241206 | + if( p->abPK[i] && p->apValue[i]==0 ){ |
| 241207 | + return (p->rc = SQLITE_CORRUPT_BKPT); |
| 241208 | + } |
| 239804 | 241209 | if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){ |
| 239805 | 241210 | sqlite3ValueFree(p->apValue[i]); |
| 239806 | 241211 | p->apValue[i] = 0; |
| 239807 | 241212 | } |
| 239808 | 241213 | } |
| | @@ -241050,23 +242455,26 @@ |
| 241050 | 242455 | ** before reattempting the INSERT. */ |
| 241051 | 242456 | else if( bReplace ){ |
| 241052 | 242457 | assert( pIter->op==SQLITE_INSERT ); |
| 241053 | 242458 | rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0); |
| 241054 | 242459 | if( rc==SQLITE_OK ){ |
| 242460 | + int rc2; |
| 241055 | 242461 | rc = sessionBindRow(pIter, |
| 241056 | 242462 | sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete); |
| 241057 | 242463 | sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1); |
| 241058 | | - } |
| 241059 | | - if( rc==SQLITE_OK ){ |
| 241060 | | - sqlite3_step(pApply->pDelete); |
| 241061 | | - rc = sqlite3_reset(pApply->pDelete); |
| 241062 | | - } |
| 241063 | | - if( rc==SQLITE_OK ){ |
| 241064 | | - rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0); |
| 241065 | | - } |
| 241066 | | - if( rc==SQLITE_OK ){ |
| 241067 | | - rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0); |
| 242464 | + if( rc==SQLITE_OK ){ |
| 242465 | + sqlite3_step(pApply->pDelete); |
| 242466 | + rc = sqlite3_reset(pApply->pDelete); |
| 242467 | + } |
| 242468 | + if( rc==SQLITE_OK ){ |
| 242469 | + rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0); |
| 242470 | + } |
| 242471 | + if( rc!=SQLITE_OK ){ |
| 242472 | + sqlite3_exec(db, "ROLLBACK TO replace_op", 0, 0, 0); |
| 242473 | + } |
| 242474 | + rc2 = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0); |
| 242475 | + if( rc==SQLITE_OK ) rc = rc2; |
| 241068 | 242476 | } |
| 241069 | 242477 | } |
| 241070 | 242478 | } |
| 241071 | 242479 | |
| 241072 | 242480 | return rc; |
| | @@ -243213,13 +244621,10 @@ |
| 243213 | 244621 | *pzErr = sqlite3_mprintf("no such table: %s", zTab); |
| 243214 | 244622 | } |
| 243215 | 244623 | } |
| 243216 | 244624 | if( rc==SQLITE_OK ){ |
| 243217 | 244625 | int nReq = pTab->nCol * (eOp==SQLITE_UPDATE ? 2 : 1); |
| 243218 | | - pGrp->cd.pTab = pTab; |
| 243219 | | - pGrp->cd.eOp = eOp; |
| 243220 | | - pGrp->cd.bIndirect = bIndirect; |
| 243221 | 244626 | |
| 243222 | 244627 | if( pGrp->cd.nBufAlloc<nReq ){ |
| 243223 | 244628 | SessionBuffer *aBuf = (SessionBuffer*)sqlite3_realloc( |
| 243224 | 244629 | pGrp->cd.aBuf, nReq * sizeof(SessionBuffer) |
| 243225 | 244630 | ); |
| | @@ -243231,10 +244636,18 @@ |
| 243231 | 244636 | ); |
| 243232 | 244637 | pGrp->cd.aBuf = aBuf; |
| 243233 | 244638 | pGrp->cd.nBufAlloc = nReq; |
| 243234 | 244639 | } |
| 243235 | 244640 | } |
| 244641 | + |
| 244642 | + /* Only mark a change as under construction once the column buffers |
| 244643 | + ** have been allocated. */ |
| 244644 | + if( rc==SQLITE_OK ){ |
| 244645 | + pGrp->cd.pTab = pTab; |
| 244646 | + pGrp->cd.eOp = eOp; |
| 244647 | + pGrp->cd.bIndirect = bIndirect; |
| 244648 | + } |
| 243236 | 244649 | |
| 243237 | 244650 | #ifdef SQLITE_DEBUG |
| 243238 | 244651 | { |
| 243239 | 244652 | /* Assert that all column values are currently undefined */ |
| 243240 | 244653 | int ii; |
| | @@ -243273,12 +244686,13 @@ |
| 243273 | 244686 | SessionBuffer *pBuf = &pGrp->cd.aBuf[iCol]; |
| 243274 | 244687 | if( pGrp->cd.eOp==SQLITE_UPDATE && bNew ){ |
| 243275 | 244688 | pBuf += pGrp->cd.pTab->nCol; |
| 243276 | 244689 | } |
| 243277 | 244690 | pBuf->nBuf = 0; |
| 243278 | | - sessionBufferGrow(pBuf, nReq, &rc); |
| 243279 | | - pBuf->nBuf = nReq; |
| 244691 | + if( sessionBufferGrow(pBuf, nReq, &rc)==0 ){ |
| 244692 | + pBuf->nBuf = (int)nReq; |
| 244693 | + } |
| 243280 | 244694 | *ppBuf = pBuf; |
| 243281 | 244695 | } |
| 243282 | 244696 | return rc; |
| 243283 | 244697 | } |
| 243284 | 244698 | |
| | @@ -248832,11 +250246,11 @@ |
| 248832 | 250246 | zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x("); |
| 248833 | 250247 | for(i=0; zSql && i<pConfig->nCol; i++){ |
| 248834 | 250248 | const char *zSep = (i==0?"":", "); |
| 248835 | 250249 | zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]); |
| 248836 | 250250 | } |
| 248837 | | - zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)", |
| 250251 | + zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q BLOB HIDDEN, %s HIDDEN)", |
| 248838 | 250252 | zSql, pConfig->zName, FTS5_RANK_NAME |
| 248839 | 250253 | ); |
| 248840 | 250254 | |
| 248841 | 250255 | assert( zSql || rc==SQLITE_NOMEM ); |
| 248842 | 250256 | if( zSql ){ |
| | @@ -249135,11 +250549,13 @@ |
| 249135 | 250549 | pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE; |
| 249136 | 250550 | pConfig->nDeleteMerge = FTS5_DEFAULT_DELETE_AUTOMERGE; |
| 249137 | 250551 | |
| 249138 | 250552 | zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName); |
| 249139 | 250553 | if( zSql ){ |
| 249140 | | - rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0); |
| 250554 | + rc = sqlite3_prepare_v3( |
| 250555 | + pConfig->db, zSql, -1, SQLITE_PREPARE_FROM_DDL, &p, 0 |
| 250556 | + ); |
| 249141 | 250557 | sqlite3_free(zSql); |
| 249142 | 250558 | } |
| 249143 | 250559 | |
| 249144 | 250560 | assert( rc==SQLITE_OK || p==0 ); |
| 249145 | 250561 | if( rc==SQLITE_OK ){ |
| | @@ -249185,11 +250601,11 @@ |
| 249185 | 250601 | char *zMsg = 0; |
| 249186 | 250602 | |
| 249187 | 250603 | va_start(ap, zFmt); |
| 249188 | 250604 | zMsg = sqlite3_vmprintf(zFmt, ap); |
| 249189 | 250605 | if( pConfig->pzErrmsg ){ |
| 249190 | | - assert( *pConfig->pzErrmsg==0 ); |
| 250606 | + sqlite3_free(*pConfig->pzErrmsg); |
| 249191 | 250607 | *pConfig->pzErrmsg = zMsg; |
| 249192 | 250608 | }else{ |
| 249193 | 250609 | sqlite3_free(zMsg); |
| 249194 | 250610 | } |
| 249195 | 250611 | |
| | @@ -253657,10 +255073,13 @@ |
| 253657 | 255073 | /* Variables populated based on current entry. */ |
| 253658 | 255074 | Fts5Buffer term; /* Current term */ |
| 253659 | 255075 | i64 iRowid; /* Current rowid */ |
| 253660 | 255076 | int nPos; /* Number of bytes in current position list */ |
| 253661 | 255077 | u8 bDel; /* True if the delete flag is set */ |
| 255078 | + |
| 255079 | + /* True if the current entry shadows an entry that has the delete flag set */ |
| 255080 | + u8 bDelDup; |
| 253662 | 255081 | }; |
| 253663 | 255082 | |
| 253664 | 255083 | static int fts5IndexCorruptRowid(Fts5Index *pIdx, i64 iRowid){ |
| 253665 | 255084 | pIdx->rc = FTS5_CORRUPT; |
| 253666 | 255085 | sqlite3Fts5ConfigErrmsg(pIdx->pConfig, |
| | @@ -254050,11 +255469,12 @@ |
| 254050 | 255469 | char *zSql |
| 254051 | 255470 | ){ |
| 254052 | 255471 | if( p->rc==SQLITE_OK ){ |
| 254053 | 255472 | if( zSql ){ |
| 254054 | 255473 | int rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1, |
| 254055 | | - SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB, |
| 255474 | + SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB |
| 255475 | + |SQLITE_PREPARE_FROM_DDL, |
| 254056 | 255476 | ppStmt, 0); |
| 254057 | 255477 | /* If this prepare() call fails with SQLITE_ERROR, then one of the |
| 254058 | 255478 | ** %_idx or %_data tables has been removed or modified. Call this |
| 254059 | 255479 | ** corruption. */ |
| 254060 | 255480 | p->rc = (rc==SQLITE_ERROR ? SQLITE_CORRUPT : rc); |
| | @@ -254064,10 +255484,21 @@ |
| 254064 | 255484 | } |
| 254065 | 255485 | sqlite3_free(zSql); |
| 254066 | 255486 | return p->rc; |
| 254067 | 255487 | } |
| 254068 | 255488 | |
| 255489 | +/* |
| 255490 | +** If *pRc is other than SQLITE_OK when this function is called, it is a |
| 255491 | +** no-op. Otherwise, call sqlite3_step() and then sqlite3_reset() on the |
| 255492 | +** supplied statement pointer. Set (*pRc) to the result of sqlite3_reset(). |
| 255493 | +*/ |
| 255494 | +static void fts5StepAndReset(int *pRc, sqlite3_stmt *pStmt){ |
| 255495 | + if( *pRc==SQLITE_OK ){ |
| 255496 | + sqlite3_step(pStmt); |
| 255497 | + *pRc = sqlite3_reset(pStmt); |
| 255498 | + } |
| 255499 | +} |
| 254069 | 255500 | |
| 254070 | 255501 | /* |
| 254071 | 255502 | ** INSERT OR REPLACE a record into the %_data table. |
| 254072 | 255503 | */ |
| 254073 | 255504 | static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){ |
| | @@ -254081,13 +255512,12 @@ |
| 254081 | 255512 | )); |
| 254082 | 255513 | if( p->rc ) return; |
| 254083 | 255514 | } |
| 254084 | 255515 | |
| 254085 | 255516 | sqlite3_bind_int64(p->pWriter, 1, iRowid); |
| 254086 | | - sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC); |
| 254087 | | - sqlite3_step(p->pWriter); |
| 254088 | | - p->rc = sqlite3_reset(p->pWriter); |
| 255517 | + p->rc = sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC); |
| 255518 | + fts5StepAndReset(&p->rc, p->pWriter); |
| 254089 | 255519 | sqlite3_bind_null(p->pWriter, 2); |
| 254090 | 255520 | } |
| 254091 | 255521 | |
| 254092 | 255522 | /* |
| 254093 | 255523 | ** Execute the following SQL: |
| | @@ -254690,10 +256120,21 @@ |
| 254690 | 256120 | } |
| 254691 | 256121 | fts5StructurePromoteTo(p, iPromote, szPromote, pStruct); |
| 254692 | 256122 | } |
| 254693 | 256123 | } |
| 254694 | 256124 | |
| 256125 | +/* |
| 256126 | +** Return the page number iPgno+nDelta. If this is larger than FTS5_MAX_PGNO, |
| 256127 | +** which can only happen if the doclist-index record is corrupt, return |
| 256128 | +** FTS5_MAX_PGNO+1 instead. No leaf page with that number may exist, so |
| 256129 | +** this causes the corruption to be detected when the caller attempts to |
| 256130 | +** use the page number. |
| 256131 | +*/ |
| 256132 | +static int fts5AddPgno(int iPgno, int nDelta){ |
| 256133 | + i64 iRet = (i64)iPgno + nDelta; |
| 256134 | + return (int)(iRet>FTS5_MAX_PGNO ? FTS5_MAX_PGNO+1 : iRet); |
| 256135 | +} |
| 254695 | 256136 | |
| 254696 | 256137 | /* |
| 254697 | 256138 | ** Advance the iterator passed as the only argument. If the end of the |
| 254698 | 256139 | ** doclist-index page is reached, return non-zero. |
| 254699 | 256140 | */ |
| | @@ -254712,11 +256153,11 @@ |
| 254712 | 256153 | if( pData->p[iOff] ) break; |
| 254713 | 256154 | } |
| 254714 | 256155 | |
| 254715 | 256156 | if( iOff<pData->nn ){ |
| 254716 | 256157 | u64 iVal; |
| 254717 | | - pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1; |
| 256158 | + pLvl->iLeafPgno = fts5AddPgno(pLvl->iLeafPgno, iOff-pLvl->iOff+1); |
| 254718 | 256159 | iOff += fts5GetVarint(&pData->p[iOff], &iVal); |
| 254719 | 256160 | pLvl->iRowid += iVal; |
| 254720 | 256161 | pLvl->iOff = iOff; |
| 254721 | 256162 | }else{ |
| 254722 | 256163 | pLvl->bEof = 1; |
| | @@ -254822,11 +256263,11 @@ |
| 254822 | 256263 | ii++; |
| 254823 | 256264 | } |
| 254824 | 256265 | ii += sqlite3Fts5GetVarint(&a[ii], &delta); |
| 254825 | 256266 | |
| 254826 | 256267 | if( ii>=iOff ) break; |
| 254827 | | - pLvl->iLeafPgno += nZero+1; |
| 256268 | + pLvl->iLeafPgno = fts5AddPgno(pLvl->iLeafPgno, nZero+1); |
| 254828 | 256269 | pLvl->iRowid += delta; |
| 254829 | 256270 | pLvl->iOff = ii; |
| 254830 | 256271 | } |
| 254831 | 256272 | } |
| 254832 | 256273 | |
| | @@ -255828,11 +257269,12 @@ |
| 255828 | 257269 | /* This block sets stack variable iPg to the leaf page number that may |
| 255829 | 257270 | ** contain term (pTerm/nTerm), if it is present in the segment. */ |
| 255830 | 257271 | pIdxSelect = fts5IdxSelectStmt(p); |
| 255831 | 257272 | if( p->rc ) return; |
| 255832 | 257273 | sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid); |
| 255833 | | - sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC); |
| 257274 | + p->rc = sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC); |
| 257275 | + if( p->rc ) return; |
| 255834 | 257276 | if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){ |
| 255835 | 257277 | i64 val = sqlite3_column_int(pIdxSelect, 0); |
| 255836 | 257278 | iPg = (int)(val>>1); |
| 255837 | 257279 | bDlidx = (val & 0x0001); |
| 255838 | 257280 | } |
| | @@ -255920,12 +257362,12 @@ |
| 255920 | 257362 | |
| 255921 | 257363 | pSel = fts5IdxNextStmt(p); |
| 255922 | 257364 | if( pSel ){ |
| 255923 | 257365 | assert( p->rc==SQLITE_OK ); |
| 255924 | 257366 | sqlite3_bind_int(pSel, 1, pSeg->iSegid); |
| 255925 | | - sqlite3_bind_blob(pSel, 2, pTerm, nTerm, SQLITE_STATIC); |
| 255926 | | - |
| 257367 | + p->rc = sqlite3_bind_blob(pSel, 2, pTerm, nTerm, SQLITE_STATIC); |
| 257368 | + if( p->rc ) return; |
| 255927 | 257369 | if( sqlite3_step(pSel)==SQLITE_ROW ){ |
| 255928 | 257370 | i64 val = sqlite3_column_int64(pSel, 0); |
| 255929 | 257371 | iPg = (int)(val>>1); |
| 255930 | 257372 | bDlidx = (val & 0x0001); |
| 255931 | 257373 | } |
| | @@ -256204,10 +257646,14 @@ |
| 256204 | 257646 | if( res==0 ){ |
| 256205 | 257647 | assert_nc( i2>i1 ); |
| 256206 | 257648 | assert_nc( i2!=0 ); |
| 256207 | 257649 | pRes->bTermEq = 1; |
| 256208 | 257650 | if( p1->iRowid==p2->iRowid ){ |
| 257651 | + /* p2 is about to be advanced past this entry. If it, or an entry |
| 257652 | + ** it shadowed, has the delete flag set, record this on p1. */ |
| 257653 | + p1->bDelDup |= (p2->bDel | p2->bDelDup); |
| 257654 | + p2->bDelDup = 0; |
| 256209 | 257655 | return i2; |
| 256210 | 257656 | } |
| 256211 | 257657 | res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1; |
| 256212 | 257658 | } |
| 256213 | 257659 | assert( res!=0 ); |
| | @@ -256721,10 +258167,15 @@ |
| 256721 | 258167 | int pgno = pSeg->iLeafPgno; |
| 256722 | 258168 | int pgnoSave = 0; |
| 256723 | 258169 | |
| 256724 | 258170 | /* This function does not work with detail=none databases. */ |
| 256725 | 258171 | assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE ); |
| 258172 | + |
| 258173 | + if( nChunk<0 ){ |
| 258174 | + FTS5_CORRUPT_IDX(p); |
| 258175 | + return; |
| 258176 | + } |
| 256726 | 258177 | |
| 256727 | 258178 | if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){ |
| 256728 | 258179 | pgnoSave = pgno+1; |
| 256729 | 258180 | } |
| 256730 | 258181 | |
| | @@ -257416,14 +258867,15 @@ |
| 257416 | 258867 | |
| 257417 | 258868 | if( p->rc==SQLITE_OK ){ |
| 257418 | 258869 | const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:""); |
| 257419 | 258870 | /* The following was already done in fts5WriteInit(): */ |
| 257420 | 258871 | /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */ |
| 257421 | | - sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC); |
| 258872 | + p->rc = sqlite3_bind_blob( |
| 258873 | + p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC |
| 258874 | + ); |
| 257422 | 258875 | sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1)); |
| 257423 | | - sqlite3_step(p->pIdxWriter); |
| 257424 | | - p->rc = sqlite3_reset(p->pIdxWriter); |
| 258876 | + fts5StepAndReset(&p->rc, p->pIdxWriter); |
| 257425 | 258877 | sqlite3_bind_null(p->pIdxWriter, 2); |
| 257426 | 258878 | } |
| 257427 | 258879 | pWriter->iBtPage = 0; |
| 257428 | 258880 | } |
| 257429 | 258881 | |
| | @@ -257966,10 +259418,18 @@ |
| 257966 | 259418 | ){ |
| 257967 | 259419 | Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ]; |
| 257968 | 259420 | int nPos; /* position-list size field value */ |
| 257969 | 259421 | int nTerm; |
| 257970 | 259422 | const u8 *pTerm; |
| 259423 | + int bDel; /* True to set delete flag on output */ |
| 259424 | + |
| 259425 | + /* If this entry has the delete flag set, or shadows an entry with the |
| 259426 | + ** delete flag set, set the delete flag on the output. Unless the output |
| 259427 | + ** will be the oldest segment in the table - in that case the delete flag |
| 259428 | + ** may be discarded. */ |
| 259429 | + bDel = bOldest ? 0 : (pSegIter->bDel || pSegIter->bDelDup); |
| 259430 | + pSegIter->bDelDup = 0; |
| 257971 | 259431 | |
| 257972 | 259432 | pTerm = fts5MultiIterTerm(pIter, &nTerm); |
| 257973 | 259433 | if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){ |
| 257974 | 259434 | if( pnRem && writer.nLeafWritten>nRem ){ |
| 257975 | 259435 | break; |
| | @@ -257977,11 +259437,11 @@ |
| 257977 | 259437 | fts5BufferSet(&p->rc, &term, nTerm, pTerm); |
| 257978 | 259438 | bTermWritten =0; |
| 257979 | 259439 | } |
| 257980 | 259440 | |
| 257981 | 259441 | /* Check for key annihilation. */ |
| 257982 | | - if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue; |
| 259442 | + if( pSegIter->nPos==0 && (bOldest || bDel==0) ) continue; |
| 257983 | 259443 | |
| 257984 | 259444 | if( p->rc==SQLITE_OK && bTermWritten==0 ){ |
| 257985 | 259445 | /* This is a new term. Append a term to the output segment. */ |
| 257986 | 259446 | fts5WriteAppendTerm(p, &writer, nTerm, pTerm); |
| 257987 | 259447 | bTermWritten = 1; |
| | @@ -257990,19 +259450,19 @@ |
| 257990 | 259450 | /* Append the rowid to the output */ |
| 257991 | 259451 | /* WRITEPOSLISTSIZE */ |
| 257992 | 259452 | fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter)); |
| 257993 | 259453 | |
| 257994 | 259454 | if( eDetail==FTS5_DETAIL_NONE ){ |
| 257995 | | - if( pSegIter->bDel ){ |
| 259455 | + if( bDel ){ |
| 257996 | 259456 | fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0); |
| 257997 | 259457 | if( pSegIter->nPos>0 ){ |
| 257998 | 259458 | fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0); |
| 257999 | 259459 | } |
| 258000 | 259460 | } |
| 258001 | 259461 | }else{ |
| 258002 | 259462 | /* Append the position-list data to the output */ |
| 258003 | | - nPos = pSegIter->nPos*2 + pSegIter->bDel; |
| 259463 | + nPos = pSegIter->nPos*2 + bDel; |
| 258004 | 259464 | fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos); |
| 258005 | 259465 | fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback); |
| 258006 | 259466 | } |
| 258007 | 259467 | } |
| 258008 | 259468 | |
| | @@ -258437,11 +259897,12 @@ |
| 258437 | 259897 | |
| 258438 | 259898 | iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta); |
| 258439 | 259899 | assert_nc( iSOP<=pSeg->iLeafOffset ); |
| 258440 | 259900 | |
| 258441 | 259901 | if( bDetailNone ){ |
| 258442 | | - while( iSOP<pSeg->iLeafOffset ){ |
| 259902 | + int iEnd = pSeg->iLeafOffset - (pSeg->bDel ? 1 + pSeg->nPos : 0); |
| 259903 | + while( iSOP<iEnd ){ |
| 258443 | 259904 | if( aPg[iSOP]==0x00 ) iSOP++; |
| 258444 | 259905 | if( aPg[iSOP]==0x00 ) iSOP++; |
| 258445 | 259906 | iStart = iSOP; |
| 258446 | 259907 | iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta); |
| 258447 | 259908 | } |
| | @@ -258502,11 +259963,11 @@ |
| 258502 | 259963 | } |
| 258503 | 259964 | } |
| 258504 | 259965 | |
| 258505 | 259966 | if( pSeg->bDel ){ |
| 258506 | 259967 | iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta); |
| 258507 | | - aPg[iOff++] = 0x01; |
| 259968 | + aPg[iOff++] = bDetailNone ? 0x00 : 0x01; |
| 258508 | 259969 | }else if( bLastInDoclist==0 ){ |
| 258509 | 259970 | if( iNextOff!=iPgIdx ){ |
| 258510 | 259971 | u64 iNextDelta = 0; |
| 258511 | 259972 | iNextOff += fts5GetVarint(&aPg[iNextOff], &iNextDelta); |
| 258512 | 259973 | iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta + iNextDelta); |
| | @@ -261715,11 +263176,13 @@ |
| 261715 | 263176 | fts5DlidxIterEof(p, pDlidx)==0; |
| 261716 | 263177 | fts5DlidxIterNext(p, pDlidx) |
| 261717 | 263178 | ){ |
| 261718 | 263179 | |
| 261719 | 263180 | /* Check any rowid-less pages that occur before the current leaf. */ |
| 261720 | | - for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){ |
| 263181 | + for(iPg=iPrevLeaf+1; p->rc==SQLITE_OK && iPg<fts5DlidxIterPgno(pDlidx); |
| 263182 | + iPg++ |
| 263183 | + ){ |
| 261721 | 263184 | iKey = FTS5_SEGMENT_ROWID(iSegid, iPg); |
| 261722 | 263185 | pLeaf = fts5LeafRead(p, iKey); |
| 261723 | 263186 | if( pLeaf ){ |
| 261724 | 263187 | if( fts5LeafFirstRowidOff(pLeaf)!=0 ) FTS5_CORRUPT_ROWID(p, iKey); |
| 261725 | 263188 | fts5DataRelease(pLeaf); |
| | @@ -263917,11 +265380,12 @@ |
| 263917 | 265380 | if( zRankArgs ){ |
| 263918 | 265381 | char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs); |
| 263919 | 265382 | if( zSql ){ |
| 263920 | 265383 | sqlite3_stmt *pStmt = 0; |
| 263921 | 265384 | rc = sqlite3_prepare_v3(pConfig->db, zSql, -1, |
| 263922 | | - SQLITE_PREPARE_PERSISTENT, &pStmt, 0); |
| 265385 | + SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_FROM_DDL, &pStmt, 0 |
| 265386 | + ); |
| 263923 | 265387 | sqlite3_free(zSql); |
| 263924 | 265388 | assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 ); |
| 263925 | 265389 | if( rc==SQLITE_OK ){ |
| 263926 | 265390 | if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 263927 | 265391 | sqlite3_int64 nByte; |
| | @@ -264859,10 +266323,11 @@ |
| 264859 | 266323 | */ |
| 264860 | 266324 | static int fts5RollbackMethod(sqlite3_vtab *pVtab){ |
| 264861 | 266325 | int rc; |
| 264862 | 266326 | Fts5FullTable *pTab = (Fts5FullTable*)pVtab; |
| 264863 | 266327 | fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0); |
| 266328 | + fts5TripCursors(pTab); |
| 264864 | 266329 | rc = sqlite3Fts5StorageRollback(pTab->pStorage); |
| 264865 | 266330 | pTab->p.pConfig->pgsz = 0; |
| 264866 | 266331 | return rc; |
| 264867 | 266332 | } |
| 264868 | 266333 | |
| | @@ -266335,11 +267800,11 @@ |
| 266335 | 267800 | int nArg, /* Number of args */ |
| 266336 | 267801 | sqlite3_value **apUnused /* Function arguments */ |
| 266337 | 267802 | ){ |
| 266338 | 267803 | assert( nArg==0 ); |
| 266339 | 267804 | UNUSED_PARAM2(nArg, apUnused); |
| 266340 | | - sqlite3_result_text(pCtx, "fts5: 2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b", -1, SQLITE_TRANSIENT); |
| 267805 | + sqlite3_result_text(pCtx, "fts5: 2026-11-15 00:55:04 ad4ee20509dc4e2803f35093b03df55cb3e39c8083b0d4ad231e1432516c7841", -1, SQLITE_TRANSIENT); |
| 266341 | 267806 | } |
| 266342 | 267807 | |
| 266343 | 267808 | /* |
| 266344 | 267809 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 266345 | 267810 | ** |
| | @@ -266810,11 +268275,11 @@ |
| 266810 | 268275 | } |
| 266811 | 268276 | |
| 266812 | 268277 | if( zSql==0 ){ |
| 266813 | 268278 | rc = SQLITE_NOMEM; |
| 266814 | 268279 | }else{ |
| 266815 | | - int f = SQLITE_PREPARE_PERSISTENT; |
| 268280 | + int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_FROM_DDL; |
| 266816 | 268281 | if( eStmt>FTS5_STMT_LOOKUP2 ) f |= SQLITE_PREPARE_NO_VTAB; |
| 266817 | 268282 | p->pConfig->bLock++; |
| 266818 | 268283 | rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0); |
| 266819 | 268284 | p->pConfig->bLock--; |
| 266820 | 268285 | sqlite3_free(zSql); |
| | @@ -267376,14 +268841,16 @@ |
| 267376 | 268841 | rc = sqlite3Fts5IndexGetOrigin(p->pIndex, &iOrigin); |
| 267377 | 268842 | sqlite3_bind_int64(pReplace, 3, iOrigin); |
| 267378 | 268843 | } |
| 267379 | 268844 | } |
| 267380 | 268845 | if( rc==SQLITE_OK ){ |
| 267381 | | - sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC); |
| 267382 | | - sqlite3_step(pReplace); |
| 267383 | | - rc = sqlite3_reset(pReplace); |
| 267384 | | - sqlite3_bind_null(pReplace, 2); |
| 268846 | + rc = sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC); |
| 268847 | + if( rc==SQLITE_OK ){ |
| 268848 | + sqlite3_step(pReplace); |
| 268849 | + rc = sqlite3_reset(pReplace); |
| 268850 | + sqlite3_bind_null(pReplace, 2); |
| 268851 | + } |
| 267385 | 268852 | } |
| 267386 | 268853 | } |
| 267387 | 268854 | return rc; |
| 267388 | 268855 | } |
| 267389 | 268856 | |
| | @@ -267765,11 +269232,13 @@ |
| 267765 | 269232 | ); |
| 267766 | 269233 | if( zSql==0 ){ |
| 267767 | 269234 | rc = SQLITE_NOMEM; |
| 267768 | 269235 | }else{ |
| 267769 | 269236 | sqlite3_stmt *pCnt = 0; |
| 267770 | | - rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0); |
| 269237 | + rc = sqlite3_prepare_v3( |
| 269238 | + pConfig->db, zSql, -1, SQLITE_PREPARE_FROM_DDL, &pCnt, 0 |
| 269239 | + ); |
| 267771 | 269240 | if( rc==SQLITE_OK ){ |
| 267772 | 269241 | if( SQLITE_ROW==sqlite3_step(pCnt) ){ |
| 267773 | 269242 | *pnRow = sqlite3_column_int64(pCnt, 0); |
| 267774 | 269243 | } |
| 267775 | 269244 | rc = sqlite3_finalize(pCnt); |
| 267776 | 269245 | |