| | @@ -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 | | -** c75eff906c11d8f17ae198e5f6cfea34636d with changes in files: |
| 21 | +** d08cabd778f7844d04f3852d93197e80c0d0 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-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918" |
| 472 | +#define SQLITE_SOURCE_ID "2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b" |
| 473 | 473 | #define SQLITE_SCM_BRANCH "trunk" |
| 474 | 474 | #define SQLITE_SCM_TAGS "" |
| 475 | | -#define SQLITE_SCM_DATETIME "2026-10-17T12:10:02.088Z" |
| 475 | +#define SQLITE_SCM_DATETIME "2026-10-24T22:25:07.764Z" |
| 476 | 476 | |
| 477 | 477 | /* |
| 478 | 478 | ** CAPI3REF: Run-Time Library Version Numbers |
| 479 | 479 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 480 | 480 | ** |
| | @@ -4768,10 +4768,19 @@ |
| 4768 | 4768 | ** defined by the database schema.</dd>)^ |
| 4769 | 4769 | ** |
| 4770 | 4770 | ** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt> |
| 4771 | 4771 | ** <dd>The maximum number of SQL statements that can be contained within |
| 4772 | 4772 | ** a single trigger.</dd>)^ |
| 4773 | +** |
| 4774 | +** [[SQLITE_LIMIT_CTES]] ^(<dt>SQLITE_LIMIT_CTES</dt> |
| 4775 | +** <dd>The maximum number of [common table expressions] (CTEs) allowed |
| 4776 | +** in a single SQL statements. This limit is used to limit possible |
| 4777 | +** exponential growth in the number of required cursors required for |
| 4778 | +** nested joins of CTEs. The limit is an absolute upper bound, and |
| 4779 | +** SQLite may refuse to compile a statement if the number of CTEs simply |
| 4780 | +** approaches this limit. Set this limit to 0 to disable the restriction. |
| 4781 | +** </dd>)^ |
| 4773 | 4782 | ** </dl> |
| 4774 | 4783 | */ |
| 4775 | 4784 | #define SQLITE_LIMIT_LENGTH 0 |
| 4776 | 4785 | #define SQLITE_LIMIT_SQL_LENGTH 1 |
| 4777 | 4786 | #define SQLITE_LIMIT_COLUMN 2 |
| | @@ -4785,10 +4794,11 @@ |
| 4785 | 4794 | #define SQLITE_LIMIT_TRIGGER_DEPTH 10 |
| 4786 | 4795 | #define SQLITE_LIMIT_WORKER_THREADS 11 |
| 4787 | 4796 | #define SQLITE_LIMIT_PARSER_DEPTH 12 |
| 4788 | 4797 | #define SQLITE_LIMIT_SCHEMA 13 |
| 4789 | 4798 | #define SQLITE_LIMIT_TRIGGER_STEPS 14 |
| 4799 | +#define SQLITE_LIMIT_CTES 15 |
| 4790 | 4800 | |
| 4791 | 4801 | /* |
| 4792 | 4802 | ** CAPI3REF: Prepare Flags |
| 4793 | 4803 | ** |
| 4794 | 4804 | ** These constants define various flags that can be passed into the |
| | @@ -6197,10 +6207,14 @@ |
| 6197 | 6207 | ** CAPI3REF: Text Encodings |
| 6198 | 6208 | ** |
| 6199 | 6209 | ** These constants define integer codes that represent the various |
| 6200 | 6210 | ** text encodings supported by SQLite. |
| 6201 | 6211 | ** |
| 6212 | +** If any value other than the ones defined here are passed into the |
| 6213 | +** "encoding" argument of an API function, then the results are |
| 6214 | +** unspecified and subject to change. |
| 6215 | +** |
| 6202 | 6216 | ** <dl> |
| 6203 | 6217 | ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd> |
| 6204 | 6218 | ** |
| 6205 | 6219 | ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16 |
| 6206 | 6220 | ** with each code point being expressed "little endian" - the least significant |
| | @@ -7179,10 +7193,14 @@ |
| 7179 | 7193 | ** VFS and operating system. Some system treat a negative argument as an |
| 7180 | 7194 | ** instruction to sleep forever. Others understand it to mean do not sleep |
| 7181 | 7195 | ** at all. ^In SQLite version 3.42.0 and later, a negative |
| 7182 | 7196 | ** argument passed into sqlite3_sleep() is changed to zero before it is relayed |
| 7183 | 7197 | ** down into the xSleep method of the VFS. |
| 7198 | +** |
| 7199 | +** The maximum sleep time is 2147483 milliseconds (about 35 minutes and |
| 7200 | +** 47 seconds). If the argument to sqlite3_sleep() is greater than 2147483, |
| 7201 | +** the sleep time is capped at 2147483 milliseconds. |
| 7184 | 7202 | */ |
| 7185 | 7203 | SQLITE_API int sqlite3_sleep(int); |
| 7186 | 7204 | |
| 7187 | 7205 | /* |
| 7188 | 7206 | ** CAPI3REF: Name Of The Folder Holding Temporary Files |
| | @@ -11573,11 +11591,11 @@ |
| 11573 | 11591 | ** buffer. If the bit had been clear, the returned buffer will not |
| 11574 | 11592 | ** be accessed by SQLite after the call. |
| 11575 | 11593 | ** |
| 11576 | 11594 | ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the |
| 11577 | 11595 | ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory |
| 11578 | | -** allocation error occurs. |
| 11596 | +** allocation or I/O error occurs. |
| 11579 | 11597 | ** |
| 11580 | 11598 | ** This interface is omitted if SQLite is compiled with the |
| 11581 | 11599 | ** [SQLITE_OMIT_DESERIALIZE] option. |
| 11582 | 11600 | */ |
| 11583 | 11601 | SQLITE_API unsigned char *sqlite3_serialize( |
| | @@ -15089,10 +15107,31 @@ |
| 15089 | 15107 | */ |
| 15090 | 15108 | #ifndef SQLITE_MAX_TRIGGER_STEPS |
| 15091 | 15109 | # define SQLITE_MAX_TRIGGER_STEPS 65000 |
| 15092 | 15110 | #endif |
| 15093 | 15111 | |
| 15112 | +/* |
| 15113 | +** Maximum number of CTE cursors (approximately). This limit will be |
| 15114 | +** exceeded if any CTE uses a cursor number larger than this value, even |
| 15115 | +** of some of the cursors are used for something other than CTEs. |
| 15116 | +** |
| 15117 | +** The purpose of this limit is to prevent resource exhaustion caused |
| 15118 | +** by hostile SQL inputs such as: |
| 15119 | +** |
| 15120 | +** WITH c0 AS (SELECT 1 AS x), |
| 15121 | +** c1 AS (SELECT a.x AS x FROM c0 a, c0 b), |
| 15122 | +** c2 AS (SELECT a.x AS x FROM c1 a, c1 b), |
| 15123 | +** ... |
| 15124 | +** c17 AS (SELECT a.x AS x FROM c16 a, c16 b) |
| 15125 | +** SELECT count(*) FROM c17; |
| 15126 | +** |
| 15127 | +** See Bug 2026-10-04T05:35:14Z |
| 15128 | +*/ |
| 15129 | +#ifndef SQLITE_MAX_CTES |
| 15130 | +# define SQLITE_MAX_CTES 50000 |
| 15131 | +#endif |
| 15132 | + |
| 15094 | 15133 | /************** End of sqliteLimit.h *****************************************/ |
| 15095 | 15134 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 15096 | 15135 | |
| 15097 | 15136 | /* Disable nuisance warnings on Borland compilers */ |
| 15098 | 15137 | #if defined(__BORLANDC__) |
| | @@ -18737,11 +18776,11 @@ |
| 18737 | 18776 | |
| 18738 | 18777 | /* |
| 18739 | 18778 | ** The number of different kinds of things that can be limited |
| 18740 | 18779 | ** using the sqlite3_limit() interface. |
| 18741 | 18780 | */ |
| 18742 | | -#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_STEPS+1) |
| 18781 | +#define SQLITE_N_LIMIT (SQLITE_LIMIT_CTES+1) |
| 18743 | 18782 | |
| 18744 | 18783 | /* |
| 18745 | 18784 | ** Lookaside malloc is a set of fixed-size buffers that can be used |
| 18746 | 18785 | ** to satisfy small transient memory allocation requests for objects |
| 18747 | 18786 | ** associated with a particular database connection. The use of |
| | @@ -19006,14 +19045,17 @@ |
| 19006 | 19045 | sqlite3 *pNextBlocked; /* Next in list of all blocked connections */ |
| 19007 | 19046 | #endif |
| 19008 | 19047 | }; |
| 19009 | 19048 | |
| 19010 | 19049 | /* |
| 19011 | | -** A macro to discover the encoding of a database. |
| 19050 | +** A macro to discover the encoding of a database or for managing |
| 19051 | +** encoding values. |
| 19012 | 19052 | */ |
| 19013 | 19053 | #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc) |
| 19014 | 19054 | #define ENC(db) ((db)->enc) |
| 19055 | +#define ENC_MASK 0x1f /* Usable encoding bits */ |
| 19056 | +#define ENC_KEEPBOM 0x80 /* Do not remove BOM */ |
| 19015 | 19057 | |
| 19016 | 19058 | /* |
| 19017 | 19059 | ** A u64 constant where the lower 32 bits are all zeros. Only the |
| 19018 | 19060 | ** upper 32 bits are included in the argument. Necessary because some |
| 19019 | 19061 | ** C-compilers still do not accept LL integer literals. |
| | @@ -21141,10 +21183,11 @@ |
| 21141 | 21183 | #endif |
| 21142 | 21184 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 21143 | 21185 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 21144 | 21186 | TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ |
| 21145 | 21187 | ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */ |
| 21188 | + Trigger *pTrigCoded; /* Trigger whose sub-program is being coded */ |
| 21146 | 21189 | |
| 21147 | 21190 | /************************************************************************** |
| 21148 | 21191 | ** Fields above must be initialized to zero. The fields that follow, |
| 21149 | 21192 | ** down to the beginning of the recursive section, do not need to be |
| 21150 | 21193 | ** initialized as they will be set before being used. The boundary is |
| | @@ -25222,11 +25265,11 @@ |
| 25222 | 25265 | SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem*); |
| 25223 | 25266 | SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*); |
| 25224 | 25267 | SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*); |
| 25225 | 25268 | SQLITE_PRIVATE int sqlite3MemRealValueRC(Mem*, double*); |
| 25226 | 25269 | SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull); |
| 25227 | | -SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*); |
| 25270 | +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*,int bFromText); |
| 25228 | 25271 | SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); |
| 25229 | 25272 | SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 25230 | 25273 | SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8); |
| 25231 | 25274 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*); |
| 25232 | 25275 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*); |
| | @@ -26108,10 +26151,11 @@ |
| 26108 | 26151 | neg = 0; |
| 26109 | 26152 | } |
| 26110 | 26153 | if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ |
| 26111 | 26154 | return 1; |
| 26112 | 26155 | } |
| 26156 | + if( neg ) Y = -Y; |
| 26113 | 26157 | zDate += 10; |
| 26114 | 26158 | while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; } |
| 26115 | 26159 | if( parseHhMmSs(zDate, p)==0 ){ |
| 26116 | 26160 | if( p->h==24 ){ |
| 26117 | 26161 | int mxD = daysOfMonth(Y,M); |
| | @@ -26131,11 +26175,11 @@ |
| 26131 | 26175 | }else{ |
| 26132 | 26176 | return 1; |
| 26133 | 26177 | } |
| 26134 | 26178 | p->validJD = 0; |
| 26135 | 26179 | p->validYMD = 1; |
| 26136 | | - p->Y = neg ? -Y : Y; |
| 26180 | + p->Y = Y; |
| 26137 | 26181 | p->M = M; |
| 26138 | 26182 | p->D = D; |
| 26139 | 26183 | computeFloor(p); |
| 26140 | 26184 | if( p->tz ){ |
| 26141 | 26185 | computeJD(p); |
| | @@ -30620,10 +30664,16 @@ |
| 30620 | 30664 | pTo->xMutexLeave = pFrom->xMutexLeave; |
| 30621 | 30665 | pTo->xMutexHeld = pFrom->xMutexHeld; |
| 30622 | 30666 | pTo->xMutexNotheld = pFrom->xMutexNotheld; |
| 30623 | 30667 | sqlite3MemoryBarrier(); |
| 30624 | 30668 | pTo->xMutexAlloc = pFrom->xMutexAlloc; |
| 30669 | + }else{ |
| 30670 | + /* Another thread may have just installed the default methods above. |
| 30671 | + ** This barrier pairs with the one prior to setting xMutexAlloc, so |
| 30672 | + ** that the load of xMutexInit below cannot be satisfied before the |
| 30673 | + ** load of xMutexAlloc on processors with weak memory ordering. */ |
| 30674 | + sqlite3MemoryBarrier(); |
| 30625 | 30675 | } |
| 30626 | 30676 | assert( sqlite3GlobalConfig.mutex.xMutexInit ); |
| 30627 | 30677 | rc = sqlite3GlobalConfig.mutex.xMutexInit(); |
| 30628 | 30678 | |
| 30629 | 30679 | #ifdef SQLITE_DEBUG |
| | @@ -38212,11 +38262,13 @@ |
| 38212 | 38262 | ** the right of the decimal point, or to a maximum of mxRound total |
| 38213 | 38263 | ** significant digits. |
| 38214 | 38264 | ** |
| 38215 | 38265 | ** If iRound>0 round to min(iRound,mxRound) significant digits total. |
| 38216 | 38266 | ** |
| 38217 | | -** mxRound must be positive. |
| 38267 | +** mxRound must be positive. iRound must not be less than -100000000 |
| 38268 | +** (the SQLITE_FP_PRECISION_LIMIT maximum in printf.c) so that the |
| 38269 | +** computation of the number of significant digits cannot overflow. |
| 38218 | 38270 | ** |
| 38219 | 38271 | ** The significant digits of the decimal representation are |
| 38220 | 38272 | ** stored in p->z[] which is a often (but not always) a pointer |
| 38221 | 38273 | ** into the middle of p->zBuf[]. There are p->n significant digits. |
| 38222 | 38274 | ** The p->z[] array is *not* zero-terminated. |
| | @@ -38229,10 +38281,11 @@ |
| 38229 | 38281 | char *zBuf; /* Local alias for p->zBuf */ |
| 38230 | 38282 | char *z; /* Local alias for p->z */ |
| 38231 | 38283 | |
| 38232 | 38284 | p->isSpecial = 0; |
| 38233 | 38285 | assert( mxRound>0 ); |
| 38286 | + assert( iRound>=-100000000 ); |
| 38234 | 38287 | |
| 38235 | 38288 | /* Convert negative numbers to positive. Deal with Infinity, 0.0, and |
| 38236 | 38289 | ** NaN. */ |
| 38237 | 38290 | if( r<0.0 ){ |
| 38238 | 38291 | p->sign = '-'; |
| | @@ -47295,10 +47348,13 @@ |
| 47295 | 47348 | */ |
| 47296 | 47349 | zName = unixTempFileDir(); |
| 47297 | 47350 | if( zName |
| 47298 | 47351 | && (fd = robust_open(zName, O_RDWR|O_EXCL|O_TMPFILE, 0600))>=0 |
| 47299 | 47352 | ){ |
| 47353 | + if( pOutFlags ){ |
| 47354 | + *pOutFlags = flags; |
| 47355 | + } |
| 47300 | 47356 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags|UNIXFILE_NOLOCK); |
| 47301 | 47357 | goto open_finished; |
| 47302 | 47358 | } |
| 47303 | 47359 | #endif |
| 47304 | 47360 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
| | @@ -47816,11 +47872,16 @@ |
| 47816 | 47872 | if( microseconds>=1000000 ) sleep(microseconds/1000000); |
| 47817 | 47873 | if( microseconds%1000000 ) usleep(microseconds%1000000); |
| 47818 | 47874 | UNUSED_PARAMETER(NotUsed); |
| 47819 | 47875 | return microseconds; |
| 47820 | 47876 | #else |
| 47821 | | - int seconds = (microseconds+999999)/1000000; |
| 47877 | + int seconds; |
| 47878 | + if( microseconds>2146000000 ){ |
| 47879 | + seconds = 2147; |
| 47880 | + }else{ |
| 47881 | + seconds = (microseconds+999999)/1000000; |
| 47882 | + } |
| 47822 | 47883 | sleep(seconds); |
| 47823 | 47884 | UNUSED_PARAMETER(NotUsed); |
| 47824 | 47885 | return seconds*1000000; |
| 47825 | 47886 | #endif |
| 47826 | 47887 | } |
| | @@ -55359,11 +55420,12 @@ |
| 55359 | 55420 | memdbLeave(pStore); |
| 55360 | 55421 | return p; |
| 55361 | 55422 | } |
| 55362 | 55423 | |
| 55363 | 55424 | /* |
| 55364 | | -** Return the serialization of a database |
| 55425 | +** Return the serialization of a database. Or return NULL if there |
| 55426 | +** is an OOM or I/O error. |
| 55365 | 55427 | */ |
| 55366 | 55428 | SQLITE_API unsigned char *sqlite3_serialize( |
| 55367 | 55429 | sqlite3 *db, /* The database connection */ |
| 55368 | 55430 | const char *zSchema, /* Which database within the connection */ |
| 55369 | 55431 | sqlite3_int64 *piSize, /* Write size here, if not NULL */ |
| | @@ -55435,12 +55497,16 @@ |
| 55435 | 55497 | DbPage *pPage = 0; |
| 55436 | 55498 | unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1); |
| 55437 | 55499 | rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0); |
| 55438 | 55500 | if( rc==SQLITE_OK ){ |
| 55439 | 55501 | memcpy(pTo, sqlite3PagerGetData(pPage), szPage); |
| 55440 | | - }else{ |
| 55502 | + }else if( pgno==PENDING_BYTE/szPage+1 ){ |
| 55441 | 55503 | memset(pTo, 0, szPage); |
| 55504 | + }else{ |
| 55505 | + sqlite3_free(pOut); |
| 55506 | + pOut = 0; |
| 55507 | + nPage = 0; |
| 55442 | 55508 | } |
| 55443 | 55509 | sqlite3PagerUnref(pPage); |
| 55444 | 55510 | } |
| 55445 | 55511 | } |
| 55446 | 55512 | } |
| | @@ -57089,38 +57155,66 @@ |
| 57089 | 57155 | ** When reading a corrupt database file, it is possible that SQLite might |
| 57090 | 57156 | ** read a few bytes (no more than 16 bytes) past the end of the page buffer. |
| 57091 | 57157 | ** It will only read past the end of the page buffer, never write. This |
| 57092 | 57158 | ** object is positioned immediately after the page buffer to serve as an |
| 57093 | 57159 | ** overrun area, so that overreads are harmless. |
| 57160 | +** |
| 57161 | +** Under some circumstances, using a strategically corrupt db file, one |
| 57162 | +** or two varints may be read from past the end of the page buffer and |
| 57163 | +** returned to the user (as rowid values). For this reason, we ensure that |
| 57164 | +** the first few elements of PgHdr1 are not pointers. If they were, then |
| 57165 | +** a determined attacker might be able to read the value of the pointer, |
| 57166 | +** which can be used as part of a larger exploit. Furthermore, because the |
| 57167 | +** first two elements of PgHdr1 are isBulkLocal and isAnchor - u16 variables |
| 57168 | +** that always contain either 1 or 0 - the PgHdr1 structure always begins |
| 57169 | +** with 4 bytes with the high-bit cleared, and so code attempting to read |
| 57170 | +** varints can never progress past this point and read any more sensitive |
| 57171 | +** information further inside the structure. |
| 57094 | 57172 | ** |
| 57095 | 57173 | ** Variables isBulkLocal and isAnchor were once type "u8". That works, |
| 57096 | 57174 | ** but causes a 2-byte gap in the structure for most architectures (since |
| 57097 | 57175 | ** pointers must be either 4 or 8-byte aligned). As this structure is located |
| 57098 | 57176 | ** in memory directly after the associated page data, if the database is |
| 57099 | 57177 | ** corrupt, code at the b-tree layer may overread the page buffer and |
| 57100 | 57178 | ** read part of this structure before the corruption is detected. This |
| 57101 | 57179 | ** can cause a valgrind error if the uninitialized gap is accessed. Using u16 |
| 57102 | 57180 | ** ensures there is no such gap, and therefore no bytes of uninitialized |
| 57103 | | -** memory in the structure. |
| 57181 | +** memory in the structure. Keeping the u16 type also lets isBulkLocal, |
| 57182 | +** isAnchor and iKey fill exactly the 8 bytes (2+2+4) that precede the |
| 57183 | +** 8-byte-aligned "page" member, so no uninitialized padding is exposed to |
| 57184 | +** an overread. |
| 57104 | 57185 | ** |
| 57105 | 57186 | ** The pLruNext and pLruPrev pointers form a double-linked circular list |
| 57106 | 57187 | ** of all pages that are unpinned. The PGroup.lru element (which should be |
| 57107 | 57188 | ** the only element on the list with PgHdr1.isAnchor set to 1) forms the |
| 57108 | 57189 | ** beginning and the end of the list. |
| 57109 | 57190 | */ |
| 57110 | 57191 | struct PgHdr1 { |
| 57111 | | - sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ |
| 57112 | | - unsigned int iKey; /* Key value (page number) */ |
| 57113 | 57192 | u16 isBulkLocal; /* This page from bulk local storage */ |
| 57114 | 57193 | u16 isAnchor; /* This is the PGroup.lru element */ |
| 57194 | + unsigned int iKey; /* Key value (page number) */ |
| 57195 | + sqlite3_pcache_page page; /* Base class. pBuf & pExtra. Deliberately not the |
| 57196 | + ** first member - see the note above. */ |
| 57115 | 57197 | PgHdr1 *pNext; /* Next in hash table chain */ |
| 57116 | 57198 | PCache1 *pCache; /* Cache that currently owns this page */ |
| 57117 | 57199 | PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */ |
| 57118 | 57200 | PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 57119 | 57201 | /* NB: pLruPrev is only valid if pLruNext!=0 */ |
| 57120 | 57202 | }; |
| 57121 | 57203 | |
| 57204 | +/* |
| 57205 | +** Given the sqlite3_pcache_page object handed out to the pcache.c layer, |
| 57206 | +** recover the PgHdr1 that contains it. PgHdr1.page is no longer required to |
| 57207 | +** be the first member of PgHdr1, so this goes through the "page" member |
| 57208 | +** rather than casting the structure pointer. The fetch methods produce the |
| 57209 | +** other direction directly with &pPage->page, only on paths where pPage is |
| 57210 | +** known to be non-NULL. PAGE_TO_PGHDR1() is likewise only ever given a |
| 57211 | +** non-NULL page that this module previously handed out. |
| 57212 | +*/ |
| 57213 | +#define PAGE_TO_PGHDR1(pPg) \ |
| 57214 | + ((PgHdr1*)(((char*)(pPg)) - offsetof(PgHdr1,page))) |
| 57215 | + |
| 57122 | 57216 | /* |
| 57123 | 57217 | ** A page is pinned if it is not on the LRU list. To be "pinned" means |
| 57124 | 57218 | ** that the page is in active use and must not be deallocated. |
| 57125 | 57219 | */ |
| 57126 | 57220 | #define PAGE_IS_PINNED(p) ((p)->pLruNext==0) |
| | @@ -57567,11 +57661,11 @@ |
| 57567 | 57661 | ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup |
| 57568 | 57662 | ** LRU list, then this function is a no-op. |
| 57569 | 57663 | ** |
| 57570 | 57664 | ** The PGroup mutex must be held when this function is called. |
| 57571 | 57665 | */ |
| 57572 | | -static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ |
| 57666 | +static sqlite3_pcache_page *pcache1PinPage(PgHdr1 *pPage){ |
| 57573 | 57667 | assert( pPage!=0 ); |
| 57574 | 57668 | assert( PAGE_IS_UNPINNED(pPage) ); |
| 57575 | 57669 | assert( pPage->pLruNext ); |
| 57576 | 57670 | assert( pPage->pLruPrev ); |
| 57577 | 57671 | assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) ); |
| | @@ -57581,11 +57675,11 @@ |
| 57581 | 57675 | /* pPage->pLruPrev = 0; |
| 57582 | 57676 | ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */ |
| 57583 | 57677 | assert( pPage->isAnchor==0 ); |
| 57584 | 57678 | assert( pPage->pCache->pGroup->lru.isAnchor==1 ); |
| 57585 | 57679 | pPage->pCache->nRecyclable--; |
| 57586 | | - return pPage; |
| 57680 | + return &pPage->page; |
| 57587 | 57681 | } |
| 57588 | 57682 | |
| 57589 | 57683 | |
| 57590 | 57684 | /* |
| 57591 | 57685 | ** Remove the page supplied as an argument from the hash table |
| | @@ -57864,11 +57958,11 @@ |
| 57864 | 57958 | ** |
| 57865 | 57959 | ** This steps are broken out into a separate procedure because they are |
| 57866 | 57960 | ** usually not needed, and by avoiding the stack initialization required |
| 57867 | 57961 | ** for these steps, the main pcache1Fetch() procedure can run faster. |
| 57868 | 57962 | */ |
| 57869 | | -static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( |
| 57963 | +static SQLITE_NOINLINE sqlite3_pcache_page *pcache1FetchStage2( |
| 57870 | 57964 | PCache1 *pCache, |
| 57871 | 57965 | unsigned int iKey, |
| 57872 | 57966 | int createFlag |
| 57873 | 57967 | ){ |
| 57874 | 57968 | unsigned int nPinned; |
| | @@ -57930,12 +58024,13 @@ |
| 57930 | 58024 | *(void **)pPage->page.pExtra = 0; |
| 57931 | 58025 | pCache->apHash[h] = pPage; |
| 57932 | 58026 | if( iKey>pCache->iMaxKey ){ |
| 57933 | 58027 | pCache->iMaxKey = iKey; |
| 57934 | 58028 | } |
| 58029 | + return &pPage->page; |
| 57935 | 58030 | } |
| 57936 | | - return pPage; |
| 58031 | + return 0; |
| 57937 | 58032 | } |
| 57938 | 58033 | |
| 57939 | 58034 | /* |
| 57940 | 58035 | ** Implementation of the sqlite3_pcache.xFetch method. |
| 57941 | 58036 | ** |
| | @@ -57993,11 +58088,11 @@ |
| 57993 | 58088 | ** There are two versions of this routine. pcache1FetchWithMutex() is |
| 57994 | 58089 | ** the general case. pcache1FetchNoMutex() is a faster implementation for |
| 57995 | 58090 | ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper |
| 57996 | 58091 | ** invokes the appropriate routine. |
| 57997 | 58092 | */ |
| 57998 | | -static PgHdr1 *pcache1FetchNoMutex( |
| 58093 | +static sqlite3_pcache_page *pcache1FetchNoMutex( |
| 57999 | 58094 | sqlite3_pcache *p, |
| 58000 | 58095 | unsigned int iKey, |
| 58001 | 58096 | int createFlag |
| 58002 | 58097 | ){ |
| 58003 | 58098 | PCache1 *pCache = (PCache1 *)p; |
| | @@ -58016,27 +58111,27 @@ |
| 58016 | 58111 | ** subsequent steps to try to create the page. */ |
| 58017 | 58112 | if( pPage ){ |
| 58018 | 58113 | if( PAGE_IS_UNPINNED(pPage) ){ |
| 58019 | 58114 | return pcache1PinPage(pPage); |
| 58020 | 58115 | }else{ |
| 58021 | | - return pPage; |
| 58116 | + return &pPage->page; |
| 58022 | 58117 | } |
| 58023 | 58118 | }else if( createFlag ){ |
| 58024 | 58119 | /* Steps 3, 4, and 5 implemented by this subroutine */ |
| 58025 | 58120 | return pcache1FetchStage2(pCache, iKey, createFlag); |
| 58026 | 58121 | }else{ |
| 58027 | 58122 | return 0; |
| 58028 | 58123 | } |
| 58029 | 58124 | } |
| 58030 | 58125 | #if PCACHE1_MIGHT_USE_GROUP_MUTEX |
| 58031 | | -static PgHdr1 *pcache1FetchWithMutex( |
| 58126 | +static sqlite3_pcache_page *pcache1FetchWithMutex( |
| 58032 | 58127 | sqlite3_pcache *p, |
| 58033 | 58128 | unsigned int iKey, |
| 58034 | 58129 | int createFlag |
| 58035 | 58130 | ){ |
| 58036 | 58131 | PCache1 *pCache = (PCache1 *)p; |
| 58037 | | - PgHdr1 *pPage; |
| 58132 | + sqlite3_pcache_page *pPage; |
| 58038 | 58133 | |
| 58039 | 58134 | pcache1EnterMutex(pCache->pGroup); |
| 58040 | 58135 | pPage = pcache1FetchNoMutex(p, iKey, createFlag); |
| 58041 | 58136 | assert( pPage==0 || pCache->iMaxKey>=iKey ); |
| 58042 | 58137 | pcache1LeaveMutex(pCache->pGroup); |
| | @@ -58050,23 +58145,22 @@ |
| 58050 | 58145 | ){ |
| 58051 | 58146 | #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) |
| 58052 | 58147 | PCache1 *pCache = (PCache1 *)p; |
| 58053 | 58148 | #endif |
| 58054 | 58149 | |
| 58055 | | - assert( offsetof(PgHdr1,page)==0 ); |
| 58056 | 58150 | assert( pCache->bPurgeable || createFlag!=1 ); |
| 58057 | 58151 | assert( pCache->bPurgeable || pCache->nMin==0 ); |
| 58058 | 58152 | assert( pCache->bPurgeable==0 || pCache->nMin==10 ); |
| 58059 | 58153 | assert( pCache->nMin==0 || pCache->bPurgeable ); |
| 58060 | 58154 | assert( pCache->nHash>0 ); |
| 58061 | 58155 | #if PCACHE1_MIGHT_USE_GROUP_MUTEX |
| 58062 | 58156 | if( pCache->pGroup->mutex ){ |
| 58063 | | - return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); |
| 58157 | + return pcache1FetchWithMutex(p, iKey, createFlag); |
| 58064 | 58158 | }else |
| 58065 | 58159 | #endif |
| 58066 | 58160 | { |
| 58067 | | - return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); |
| 58161 | + return pcache1FetchNoMutex(p, iKey, createFlag); |
| 58068 | 58162 | } |
| 58069 | 58163 | } |
| 58070 | 58164 | |
| 58071 | 58165 | |
| 58072 | 58166 | /* |
| | @@ -58078,11 +58172,11 @@ |
| 58078 | 58172 | sqlite3_pcache *p, |
| 58079 | 58173 | sqlite3_pcache_page *pPg, |
| 58080 | 58174 | int reuseUnlikely |
| 58081 | 58175 | ){ |
| 58082 | 58176 | PCache1 *pCache = (PCache1 *)p; |
| 58083 | | - PgHdr1 *pPage = (PgHdr1 *)pPg; |
| 58177 | + PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg); |
| 58084 | 58178 | PGroup *pGroup = pCache->pGroup; |
| 58085 | 58179 | |
| 58086 | 58180 | assert( pPage->pCache==pCache ); |
| 58087 | 58181 | pcache1EnterMutex(pGroup); |
| 58088 | 58182 | |
| | @@ -58114,20 +58208,20 @@ |
| 58114 | 58208 | sqlite3_pcache_page *pPg, |
| 58115 | 58209 | unsigned int iOld, |
| 58116 | 58210 | unsigned int iNew |
| 58117 | 58211 | ){ |
| 58118 | 58212 | PCache1 *pCache = (PCache1 *)p; |
| 58119 | | - PgHdr1 *pPage = (PgHdr1 *)pPg; |
| 58213 | + PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg); |
| 58120 | 58214 | PgHdr1 **pp; |
| 58121 | 58215 | unsigned int hOld, hNew; |
| 58122 | 58216 | assert( pPage->iKey==iOld ); |
| 58123 | 58217 | assert( pPage->pCache==pCache ); |
| 58124 | 58218 | assert( iOld!=iNew ); /* The page number really is changing */ |
| 58125 | 58219 | |
| 58126 | 58220 | pcache1EnterMutex(pCache->pGroup); |
| 58127 | 58221 | |
| 58128 | | - assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */ |
| 58222 | + assert( pcache1FetchNoMutex(p, iOld, 0)==&pPage->page ); /* pPg really iOld */ |
| 58129 | 58223 | assert( pCache->nHash>0 && (pCache->nHash & (pCache->nHash-1))==0 ); |
| 58130 | 58224 | hOld = iOld & (pCache->nHash-1); |
| 58131 | 58225 | pp = &pCache->apHash[hOld]; |
| 58132 | 58226 | while( (*pp)!=pPage ){ |
| 58133 | 58227 | pp = &(*pp)->pNext; |
| | @@ -68381,10 +68475,11 @@ |
| 68381 | 68475 | SEH_FREE_ON_ERROR(0, aFrame); |
| 68382 | 68476 | if( !aFrame ){ |
| 68383 | 68477 | rc = SQLITE_NOMEM_BKPT; |
| 68384 | 68478 | goto recovery_error; |
| 68385 | 68479 | } |
| 68480 | + memset(aFrame, 0, szFrame + WALINDEX_PGSZ); |
| 68386 | 68481 | aData = &aFrame[WAL_FRAME_HDRSIZE]; |
| 68387 | 68482 | aPrivate = (u32*)&aData[szPage]; |
| 68388 | 68483 | |
| 68389 | 68484 | /* Read all frames from the log file. */ |
| 68390 | 68485 | iLastFrame = (nSize - WAL_HDRSIZE) / szFrame; |
| | @@ -69654,12 +69749,16 @@ |
| 69654 | 69749 | ** with a writer. So get a WRITE lock and try again. |
| 69655 | 69750 | */ |
| 69656 | 69751 | if( badHdr ){ |
| 69657 | 69752 | if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){ |
| 69658 | 69753 | if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ |
| 69754 | + /* Retry the header now that the WRITE_LOCK is held. If it still |
| 69755 | + ** fails, the header really is corrupt and recovery is required. |
| 69756 | + ** Otherwise, continue with the now valid header. */ |
| 69757 | + if( page0 ) badHdr = walIndexTryHdr(pWal, pChanged); |
| 69659 | 69758 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 69660 | | - rc = SQLITE_READONLY_RECOVERY; |
| 69759 | + if( badHdr ) rc = SQLITE_READONLY_RECOVERY; |
| 69661 | 69760 | } |
| 69662 | 69761 | }else{ |
| 69663 | 69762 | int bWriteLock = pWal->writeLock; |
| 69664 | 69763 | if( bWriteLock |
| 69665 | 69764 | || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) |
| | @@ -70136,29 +70235,39 @@ |
| 70136 | 70235 | assert( thisMark!=READMARK_NOT_USED ); |
| 70137 | 70236 | mxReadMark = thisMark; |
| 70138 | 70237 | mxI = i; |
| 70139 | 70238 | } |
| 70140 | 70239 | } |
| 70141 | | - if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 70142 | | - && (mxReadMark<mxFrame || mxI==0) |
| 70143 | | - ){ |
| 70144 | | - for(i=1; i<WAL_NREADER; i++){ |
| 70145 | | - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 70146 | | - if( rc==SQLITE_OK ){ |
| 70147 | | - AtomicStore(pInfo->aReadMark+i,mxFrame); |
| 70148 | | - mxReadMark = mxFrame; |
| 70149 | | - mxI = i; |
| 70150 | | - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 70151 | | - break; |
| 70152 | | - }else if( rc!=SQLITE_BUSY ){ |
| 70153 | | - return rc; |
| 70154 | | - } |
| 70155 | | - } |
| 70156 | | - } |
| 70240 | + |
| 70241 | + if( (pWal->readOnly & WAL_SHM_RDONLY)==0 ){ |
| 70242 | + if( mxReadMark<mxFrame || mxI==0 ){ |
| 70243 | + for(i=1; i<WAL_NREADER; i++){ |
| 70244 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 70245 | + if( rc==SQLITE_OK ){ |
| 70246 | + AtomicStore(pInfo->aReadMark+i,mxFrame); |
| 70247 | + mxReadMark = mxFrame; |
| 70248 | + mxI = i; |
| 70249 | + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 70250 | + break; |
| 70251 | + }else if( rc!=SQLITE_BUSY ){ |
| 70252 | + return rc; |
| 70253 | + } |
| 70254 | + } |
| 70255 | + } |
| 70256 | + }else if( mxI==0 ){ |
| 70257 | + /* If the loop above could not find a usable slot (mxI==0), then check |
| 70258 | + ** if the wal-index header has been modified by another connection since |
| 70259 | + ** it was read. If not, return SQLITE_READONLY_CANTINIT. If it has |
| 70260 | + ** been modified, fall through and return WAL_RETRY. */ |
| 70261 | + walShmBarrier(pWal); |
| 70262 | + if( 0==memcmp((void*)walIndexHdr(pWal),&pWal->hdr,sizeof(WalIndexHdr)) ){ |
| 70263 | + return SQLITE_READONLY_CANTINIT; |
| 70264 | + } |
| 70265 | + } |
| 70266 | + |
| 70157 | 70267 | if( mxI==0 ){ |
| 70158 | | - assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); |
| 70159 | | - return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT; |
| 70268 | + return WAL_RETRY; |
| 70160 | 70269 | } |
| 70161 | 70270 | |
| 70162 | 70271 | (void)walEnableBlockingMs(pWal, nBlockTmout); |
| 70163 | 70272 | rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 70164 | 70273 | walDisableBlocking(pWal); |
| | @@ -74930,11 +75039,12 @@ |
| 74930 | 75039 | return SQLITE_OK; |
| 74931 | 75040 | } |
| 74932 | 75041 | |
| 74933 | 75042 | /* |
| 74934 | 75043 | ** Do additional sanity check after btreeInitPage() if |
| 74935 | | -** PRAGMA cell_size_check=ON |
| 75044 | +** PRAGMA cell_size_check=ON. Raise a corruption error and |
| 75045 | +** leave the page uninitialized if any problems are found. |
| 74936 | 75046 | */ |
| 74937 | 75047 | static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){ |
| 74938 | 75048 | int iCellFirst; /* First allowable cell or freeblock offset */ |
| 74939 | 75049 | int iCellLast; /* Last possible cell or freeblock offset */ |
| 74940 | 75050 | int i; /* Index into the cell pointer array */ |
| | @@ -74953,15 +75063,17 @@ |
| 74953 | 75063 | for(i=0; i<pPage->nCell; i++){ |
| 74954 | 75064 | pc = get2byteAligned(&data[cellOffset+i*2]); |
| 74955 | 75065 | testcase( pc==iCellFirst ); |
| 74956 | 75066 | testcase( pc==iCellLast ); |
| 74957 | 75067 | if( pc<iCellFirst || pc>iCellLast ){ |
| 75068 | + pPage->isInit = 0; |
| 74958 | 75069 | return SQLITE_CORRUPT_PAGE(pPage); |
| 74959 | 75070 | } |
| 74960 | 75071 | sz = pPage->xCellSize(pPage, &data[pc]); |
| 74961 | 75072 | testcase( pc+sz==usableSize ); |
| 74962 | 75073 | if( pc+sz>usableSize ){ |
| 75074 | + pPage->isInit = 0; |
| 74963 | 75075 | return SQLITE_CORRUPT_PAGE(pPage); |
| 74964 | 75076 | } |
| 74965 | 75077 | } |
| 74966 | 75078 | return SQLITE_OK; |
| 74967 | 75079 | } |
| | @@ -82791,10 +82903,16 @@ |
| 82791 | 82903 | pCur->skipNext = -1; |
| 82792 | 82904 | pCur->ix = pPage->nCell-1; |
| 82793 | 82905 | }else{ |
| 82794 | 82906 | pCur->skipNext = 1; |
| 82795 | 82907 | } |
| 82908 | + /* The pCur->info structure is currently populated with the details |
| 82909 | + ** of the cell that was just removed. But pCur->ix now points to a |
| 82910 | + ** different cell in the b-tree. So zero info.nSize here so that if |
| 82911 | + ** saveCursorPosition() is called on this cursor it does not trust |
| 82912 | + ** the contents of pCur->info. */ |
| 82913 | + pCur->info.nSize = 0; |
| 82796 | 82914 | }else{ |
| 82797 | 82915 | rc = moveToRoot(pCur); |
| 82798 | 82916 | if( bPreserve ){ |
| 82799 | 82917 | btreeReleaseAllCursorPages(pCur); |
| 82800 | 82918 | pCur->eState = CURSOR_REQUIRESEEK; |
| | @@ -85989,12 +86107,32 @@ |
| 85989 | 86107 | } |
| 85990 | 86108 | |
| 85991 | 86109 | /* |
| 85992 | 86110 | ** The MEM structure is already a MEM_Real or MEM_IntReal. Try to |
| 85993 | 86111 | ** make it a MEM_Int if we can. |
| 86112 | +** |
| 86113 | +** This routine only converts REAL values into INTEGER if the REAL |
| 86114 | +** has no fractional part and if the value can be exactly expressed |
| 86115 | +** as a signed 64-bit integer. |
| 86116 | +** |
| 86117 | +** The bFromText parameter is true if the MEM was originally text. |
| 86118 | +** If the value was originally text then this routine will not convert |
| 86119 | +** -9223372036854775808.0 into -9223372036854775808 because if the value |
| 86120 | +** was originally text, it would have been converted into an integer |
| 86121 | +** already, and since it was not, that means that the original text |
| 86122 | +** content had the last four significant digits greater than "5808" |
| 86123 | +** and the hence the text value was not really -9223372036854775808.0 |
| 86124 | +** but rather some nearby value that happens to have the same IEEE 754 |
| 86125 | +** representation. We don't want text '-9223372036854775900.0' to get |
| 86126 | +** converted into -9223372036854775808. |
| 86127 | +** |
| 86128 | +** But if bFromText is false, then the value started out as REAL and |
| 86129 | +** so it is ok to convert -9223372036854775808.0 into -9223372036854775808 |
| 86130 | +** in that case, because -9223372036854775808.0 is the exact value of |
| 86131 | +** the underlying IEEE 754 representation. |
| 85994 | 86132 | */ |
| 85995 | | -SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){ |
| 86133 | +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem, int bFromText){ |
| 85996 | 86134 | assert( pMem!=0 ); |
| 85997 | 86135 | assert( pMem->flags & (MEM_Real|MEM_IntReal) ); |
| 85998 | 86136 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); |
| 85999 | 86137 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 86000 | 86138 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| | @@ -86012,11 +86150,13 @@ |
| 86012 | 86150 | ** |
| 86013 | 86151 | ** The second and third terms in the following conditional enforces |
| 86014 | 86152 | ** the second condition under the assumption that addition overflow causes |
| 86015 | 86153 | ** values to wrap around. |
| 86016 | 86154 | */ |
| 86017 | | - if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){ |
| 86155 | + if( pMem->u.r==ix && ix<LARGEST_INT64 |
| 86156 | + && (!bFromText || ix>SMALLEST_INT64) |
| 86157 | + ){ |
| 86018 | 86158 | pMem->u.i = ix; |
| 86019 | 86159 | MemSetTypeFlag(pMem, MEM_Int); |
| 86020 | 86160 | } |
| 86021 | 86161 | } |
| 86022 | 86162 | } |
| | @@ -86476,15 +86616,17 @@ |
| 86476 | 86616 | void (*xDel)(void*) /* Destructor function */ |
| 86477 | 86617 | ){ |
| 86478 | 86618 | i64 nByte = n; /* New value for pMem->n */ |
| 86479 | 86619 | int iLimit; /* Maximum allowed string or blob size */ |
| 86480 | 86620 | u16 flags; /* New value for pMem->flags */ |
| 86621 | + u8 xenc; /* Encoding without the ENC_KEEPBOM bit */ |
| 86481 | 86622 | |
| 86482 | 86623 | assert( pMem!=0 ); |
| 86483 | 86624 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 86484 | 86625 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); |
| 86485 | | - assert( enc!=0 || n>=0 ); |
| 86626 | + xenc = enc & ENC_MASK; |
| 86627 | + assert( xenc!=0 || n>=0 ); |
| 86486 | 86628 | |
| 86487 | 86629 | /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ |
| 86488 | 86630 | if( !z ){ |
| 86489 | 86631 | sqlite3VdbeMemSetNull(pMem); |
| 86490 | 86632 | return SQLITE_OK; |
| | @@ -86494,20 +86636,20 @@ |
| 86494 | 86636 | iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 86495 | 86637 | }else{ |
| 86496 | 86638 | iLimit = SQLITE_MAX_LENGTH; |
| 86497 | 86639 | } |
| 86498 | 86640 | if( nByte<0 ){ |
| 86499 | | - assert( enc!=0 ); |
| 86500 | | - if( enc==SQLITE_UTF8 ){ |
| 86641 | + assert( xenc!=0 ); |
| 86642 | + if( xenc==SQLITE_UTF8 ){ |
| 86501 | 86643 | nByte = strlen(z); |
| 86502 | 86644 | }else{ |
| 86503 | 86645 | for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} |
| 86504 | 86646 | } |
| 86505 | 86647 | flags= MEM_Str|MEM_Term; |
| 86506 | | - }else if( enc==0 ){ |
| 86648 | + }else if( xenc==0 ){ |
| 86507 | 86649 | flags = MEM_Blob; |
| 86508 | | - enc = SQLITE_UTF8; |
| 86650 | + xenc = SQLITE_UTF8; |
| 86509 | 86651 | }else{ |
| 86510 | 86652 | flags = MEM_Str; |
| 86511 | 86653 | } |
| 86512 | 86654 | if( nByte>iLimit ){ |
| 86513 | 86655 | if( xDel && xDel!=SQLITE_TRANSIENT ){ |
| | @@ -86526,11 +86668,11 @@ |
| 86526 | 86668 | ** management (one of MEM_Dyn or MEM_Static). |
| 86527 | 86669 | */ |
| 86528 | 86670 | if( xDel==SQLITE_TRANSIENT ){ |
| 86529 | 86671 | i64 nAlloc = nByte; |
| 86530 | 86672 | if( flags&MEM_Term ){ |
| 86531 | | - nAlloc += (enc==SQLITE_UTF8?1:2); |
| 86673 | + nAlloc += (xenc==SQLITE_UTF8?1:2); |
| 86532 | 86674 | } |
| 86533 | 86675 | testcase( nAlloc==0 ); |
| 86534 | 86676 | testcase( nAlloc==31 ); |
| 86535 | 86677 | testcase( nAlloc==32 ); |
| 86536 | 86678 | if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){ |
| | @@ -86550,18 +86692,17 @@ |
| 86550 | 86692 | } |
| 86551 | 86693 | } |
| 86552 | 86694 | |
| 86553 | 86695 | pMem->n = (int)(nByte & 0x7fffffff); |
| 86554 | 86696 | pMem->flags = flags; |
| 86555 | | - pMem->enc = enc; |
| 86697 | + pMem->enc = xenc; |
| 86556 | 86698 | |
| 86557 | 86699 | #ifndef SQLITE_OMIT_UTF16 |
| 86558 | | - /* tag-20261016-1: To retain the BOM, use enc==SQLITE_UTF8, even |
| 86559 | | - ** for UTF-16 text, and then set pMem->enc correctly after this |
| 86560 | | - ** function returns. Note, however, that n must be non-negative |
| 86561 | | - ** for this trick to work. */ |
| 86562 | | - if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ |
| 86700 | + if( xenc>SQLITE_UTF8 |
| 86701 | + && (enc & ENC_KEEPBOM)==0 |
| 86702 | + && sqlite3VdbeMemHandleBom(pMem) |
| 86703 | + ){ |
| 86563 | 86704 | return SQLITE_NOMEM_BKPT; |
| 86564 | 86705 | } |
| 86565 | 86706 | #endif |
| 86566 | 86707 | |
| 86567 | 86708 | |
| | @@ -86675,14 +86816,14 @@ |
| 86675 | 86816 | return SQLITE_NOMEM_BKPT; |
| 86676 | 86817 | } |
| 86677 | 86818 | if( (u64)amt + (u64)offset > (u64)sqlite3BtreeMaxRecordSize(pCur) ){ |
| 86678 | 86819 | return SQLITE_CORRUPT_BKPT; |
| 86679 | 86820 | } |
| 86680 | | - if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){ |
| 86821 | + if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+8)) ){ |
| 86681 | 86822 | rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z); |
| 86682 | 86823 | if( rc==SQLITE_OK ){ |
| 86683 | | - pMem->z[amt] = 0; /* Overrun area used when reading malformed records */ |
| 86824 | + memset(pMem->z+amt,0,8); /* Overrun area for malformed records */ |
| 86684 | 86825 | pMem->flags = MEM_Blob; |
| 86685 | 86826 | pMem->n = (int)amt; |
| 86686 | 86827 | }else{ |
| 86687 | 86828 | sqlite3VdbeMemRelease(pMem); |
| 86688 | 86829 | } |
| | @@ -87067,18 +87208,19 @@ |
| 87067 | 87208 | sqlite3VdbeMemSetInt64(pVal, iVal*negInt); |
| 87068 | 87209 | }else{ |
| 87069 | 87210 | zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); |
| 87070 | 87211 | if( zVal==0 ) goto no_mem; |
| 87071 | 87212 | sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); |
| 87213 | + if( op==TK_FLOAT ){ |
| 87214 | + assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) ); |
| 87215 | + sqlite3AtoF(pVal->z, &pVal->u.r); |
| 87216 | + pVal->flags = MEM_Real; |
| 87217 | + } |
| 87072 | 87218 | } |
| 87073 | 87219 | } |
| 87074 | 87220 | if( affinity==SQLITE_AFF_BLOB ){ |
| 87075 | | - if( op==TK_FLOAT ){ |
| 87076 | | - assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) ); |
| 87077 | | - sqlite3AtoF(pVal->z, &pVal->u.r); |
| 87078 | | - pVal->flags = MEM_Real; |
| 87079 | | - }else if( op==TK_INTEGER ){ |
| 87221 | + if( op==TK_INTEGER ){ |
| 87080 | 87222 | /* This case is required by -9223372036854775808 and other strings |
| 87081 | 87223 | ** that look like integers but cannot be handled by the |
| 87082 | 87224 | ** sqlite3DecOrHexToI64() call above. */ |
| 87083 | 87225 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); |
| 87084 | 87226 | } |
| | @@ -93949,10 +94091,11 @@ |
| 93949 | 94091 | sqlite3_result_error_toobig(pCtx); |
| 93950 | 94092 | return SQLITE_TOOBIG; |
| 93951 | 94093 | } |
| 93952 | 94094 | #ifndef SQLITE_OMIT_INCRBLOB |
| 93953 | 94095 | sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 94096 | + pOut->enc = pCtx->enc; |
| 93954 | 94097 | return SQLITE_OK; |
| 93955 | 94098 | #else |
| 93956 | 94099 | return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 93957 | 94100 | #endif |
| 93958 | 94101 | } |
| | @@ -95041,16 +95184,17 @@ |
| 95041 | 95184 | sqlite3_stmt *pStmt, /* The statement to bind against */ |
| 95042 | 95185 | int i, /* Index of the parameter to bind */ |
| 95043 | 95186 | const void *zData, /* Pointer to the data to be bound */ |
| 95044 | 95187 | i64 nData, /* Number of bytes of data to be bound */ |
| 95045 | 95188 | void (*xDel)(void*), /* Destructor for the data */ |
| 95046 | | - u8 encoding /* Encoding for the data */ |
| 95189 | + u8 encoding /* Encoding for the data + optional ENC_KEEPBOM */ |
| 95047 | 95190 | ){ |
| 95048 | 95191 | Vdbe *p = (Vdbe *)pStmt; |
| 95049 | 95192 | Mem *pVar; |
| 95050 | 95193 | int rc; |
| 95051 | 95194 | |
| 95195 | + assert( (encoding&ENC_KEEPBOM)==0 || (encoding&ENC_MASK)>SQLITE_UTF8 ); |
| 95052 | 95196 | rc = vdbeUnbind(p, (u32)(i-1)); |
| 95053 | 95197 | if( rc==SQLITE_OK ){ |
| 95054 | 95198 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
| 95055 | 95199 | if( zData!=0 ){ |
| 95056 | 95200 | pVar = &p->aVar[i-1]; |
| | @@ -95253,12 +95397,13 @@ |
| 95253 | 95397 | rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); |
| 95254 | 95398 | } |
| 95255 | 95399 | break; |
| 95256 | 95400 | } |
| 95257 | 95401 | case SQLITE_TEXT: { |
| 95402 | + assert( pValue->enc!=0 ); |
| 95258 | 95403 | rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, |
| 95259 | | - pValue->enc); |
| 95404 | + pValue->enc<=SQLITE_UTF8 ? pValue->enc : pValue->enc|ENC_KEEPBOM); |
| 95260 | 95405 | break; |
| 95261 | 95406 | } |
| 95262 | 95407 | default: { |
| 95263 | 95408 | rc = sqlite3_bind_null(pStmt, i); |
| 95264 | 95409 | break; |
| | @@ -95275,10 +95420,11 @@ |
| 95275 | 95420 | #ifndef SQLITE_OMIT_INCRBLOB |
| 95276 | 95421 | sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 95277 | 95422 | #else |
| 95278 | 95423 | rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 95279 | 95424 | #endif |
| 95425 | + p->aVar[i-1].enc = ENC(p->db); |
| 95280 | 95426 | sqlite3_mutex_leave(p->db->mutex); |
| 95281 | 95427 | } |
| 95282 | 95428 | return rc; |
| 95283 | 95429 | } |
| 95284 | 95430 | SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ |
| | @@ -95428,13 +95574,18 @@ |
| 95428 | 95574 | }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){ |
| 95429 | 95575 | /* No reprepare necessary */ |
| 95430 | 95576 | v->explain = eMode; |
| 95431 | 95577 | rc = SQLITE_OK; |
| 95432 | 95578 | }else{ |
| 95579 | + int savedMode = v->explain; |
| 95433 | 95580 | v->explain = eMode; |
| 95434 | 95581 | rc = sqlite3Reprepare(v); |
| 95435 | | - v->haveEqpOps = eMode==2; |
| 95582 | + if( rc ){ |
| 95583 | + v->explain = savedMode; |
| 95584 | + }else{ |
| 95585 | + v->haveEqpOps = eMode==2; |
| 95586 | + } |
| 95436 | 95587 | } |
| 95437 | 95588 | if( v->explain ){ |
| 95438 | 95589 | v->nResColumn = 12 - 4*v->explain; |
| 95439 | 95590 | }else{ |
| 95440 | 95591 | v->nResColumn = v->nResAlloc; |
| | @@ -95622,14 +95773,15 @@ |
| 95622 | 95773 | u32 nRec; |
| 95623 | 95774 | u8 *aRec; |
| 95624 | 95775 | |
| 95625 | 95776 | assert( p->pCsr->eCurType==CURTYPE_BTREE ); |
| 95626 | 95777 | nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); |
| 95627 | | - aRec = sqlite3DbMallocRaw(db, nRec); |
| 95778 | + aRec = sqlite3DbMallocRaw(db, (u64)nRec+8); |
| 95628 | 95779 | if( !aRec ) goto preupdate_old_out; |
| 95629 | 95780 | rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); |
| 95630 | 95781 | if( rc==SQLITE_OK ){ |
| 95782 | + memset(aRec+nRec, 0, 8); |
| 95631 | 95783 | p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec); |
| 95632 | 95784 | if( !p->pUnpacked ) rc = SQLITE_NOMEM; |
| 95633 | 95785 | } |
| 95634 | 95786 | if( rc!=SQLITE_OK ){ |
| 95635 | 95787 | sqlite3DbFree(db, aRec); |
| | @@ -96658,11 +96810,11 @@ |
| 96658 | 96810 | if( (rc&2)==0 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ |
| 96659 | 96811 | pRec->flags |= MEM_Int; |
| 96660 | 96812 | }else{ |
| 96661 | 96813 | pRec->u.r = rValue; |
| 96662 | 96814 | pRec->flags |= MEM_Real; |
| 96663 | | - if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
| 96815 | + if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec, 1); |
| 96664 | 96816 | } |
| 96665 | 96817 | /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the |
| 96666 | 96818 | ** string representation after computing a numeric equivalent, because the |
| 96667 | 96819 | ** string representation might not be the canonical representation for the |
| 96668 | 96820 | ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ |
| | @@ -96702,11 +96854,11 @@ |
| 96702 | 96854 | || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM ); |
| 96703 | 96855 | if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ |
| 96704 | 96856 | if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){ |
| 96705 | 96857 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
| 96706 | 96858 | }else if( affinity<=SQLITE_AFF_REAL ){ |
| 96707 | | - sqlite3VdbeIntegerAffinity(pRec); |
| 96859 | + sqlite3VdbeIntegerAffinity(pRec, 0); |
| 96708 | 96860 | } |
| 96709 | 96861 | } |
| 96710 | 96862 | }else if( affinity==SQLITE_AFF_TEXT ){ |
| 96711 | 96863 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 96712 | 96864 | ** representation (blob and NULL do not get converted) but no string |
| | @@ -97092,22 +97244,18 @@ |
| 97092 | 97244 | pBuf = pCache->pCValue; |
| 97093 | 97245 | } |
| 97094 | 97246 | assert( t>=12 ); |
| 97095 | 97247 | sqlite3RCStrRef(pBuf); |
| 97096 | 97248 | if( t&1 ){ |
| 97097 | | - /* Prevents the removal of a BOM in UTF16 text. Otherwise |
| 97098 | | - ** it causes no harm as long as len>=0, and pDest->enc is set |
| 97099 | | - ** to the correct encoding afterwards: ---- |
| 97100 | | - ** (tag-20261016-1) `vvvvvvvvvvv */ |
| 97101 | | - rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, SQLITE_UTF8, |
| 97249 | + rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding|ENC_KEEPBOM, |
| 97102 | 97250 | sqlite3RCStrUnref); |
| 97103 | 97251 | pDest->flags |= MEM_Term; |
| 97104 | 97252 | }else{ |
| 97105 | 97253 | rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0, |
| 97106 | 97254 | sqlite3RCStrUnref); |
| 97107 | | - } |
| 97108 | | - pDest->enc = encoding; |
| 97255 | + pDest->enc = encoding; |
| 97256 | + } |
| 97109 | 97257 | }else{ |
| 97110 | 97258 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest); |
| 97111 | 97259 | if( rc ) return rc; |
| 97112 | 97260 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 97113 | 97261 | if( (t&1)!=0 && encoding==SQLITE_UTF8 ){ |
| | @@ -111096,10 +111244,13 @@ |
| 111096 | 111244 | if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){ |
| 111097 | 111245 | Upsert *pUpsert = pNC->uNC.pUpsert; |
| 111098 | 111246 | if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){ |
| 111099 | 111247 | pTab = pUpsert->pUpsertSrc->a[0].pSTab; |
| 111100 | 111248 | pExpr->iTable = EXCLUDED_TABLE_NUMBER; |
| 111249 | + if( IN_RENAME_OBJECT ){ |
| 111250 | + sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab); |
| 111251 | + } |
| 111101 | 111252 | } |
| 111102 | 111253 | } |
| 111103 | 111254 | #endif /* SQLITE_OMIT_UPSERT */ |
| 111104 | 111255 | |
| 111105 | 111256 | if( pTab ){ |
| | @@ -112118,11 +112269,11 @@ |
| 112118 | 112269 | NameContext nc; /* Name context for resolving pE */ |
| 112119 | 112270 | sqlite3 *db; /* Database connection */ |
| 112120 | 112271 | int rc; /* Return code from subprocedures */ |
| 112121 | 112272 | u8 savedSuppErr; /* Saved value of db->suppressErr */ |
| 112122 | 112273 | |
| 112123 | | - assert( sqlite3ExprIsInteger(pE, &i, 0, 0)==0 ); |
| 112274 | + assert( sqlite3ExprIsInteger(pE, &i, 0, 1)==0 ); |
| 112124 | 112275 | pEList = pSelect->pEList; |
| 112125 | 112276 | |
| 112126 | 112277 | /* Resolve all names in the ORDER BY term expression |
| 112127 | 112278 | */ |
| 112128 | 112279 | memset(&nc, 0, sizeof(nc)); |
| | @@ -112217,11 +112368,11 @@ |
| 112217 | 112368 | int iCol = -1; |
| 112218 | 112369 | Expr *pE, *pDup; |
| 112219 | 112370 | if( pItem->fg.done ) continue; |
| 112220 | 112371 | pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr); |
| 112221 | 112372 | if( NEVER(pE==0) ) continue; |
| 112222 | | - if( sqlite3ExprIsInteger(pE, &iCol, 0, 0) ){ |
| 112373 | + if( sqlite3ExprIsInteger(pE, &iCol, 0, 1) ){ |
| 112223 | 112374 | if( iCol<=0 || iCol>pEList->nExpr ){ |
| 112224 | 112375 | resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE); |
| 112225 | 112376 | return 1; |
| 112226 | 112377 | } |
| 112227 | 112378 | }else{ |
| | @@ -112400,11 +112551,11 @@ |
| 112400 | 112551 | ** copy of the iCol-th result-set expression. */ |
| 112401 | 112552 | pItem->u.x.iOrderByCol = (u16)iCol; |
| 112402 | 112553 | continue; |
| 112403 | 112554 | } |
| 112404 | 112555 | } |
| 112405 | | - if( sqlite3ExprIsInteger(pE2, &iCol, 0, 0) ){ |
| 112556 | + if( sqlite3ExprIsInteger(pE2, &iCol, 0, 1) ){ |
| 112406 | 112557 | /* The ORDER BY term is an integer constant. Again, set the column |
| 112407 | 112558 | ** number so that sqlite3ResolveOrderGroupBy() will convert the |
| 112408 | 112559 | ** order-by term to a copy of the result-set expression */ |
| 112409 | 112560 | if( iCol<1 || iCol>0xffff ){ |
| 112410 | 112561 | resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2); |
| | @@ -114088,13 +114239,16 @@ |
| 114088 | 114239 | }else{ |
| 114089 | 114240 | u32 f = pLeft->flags | pRight->flags; |
| 114090 | 114241 | if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse|EP_HasFunc))==EP_IsFalse |
| 114091 | 114242 | && !IN_RENAME_OBJECT |
| 114092 | 114243 | ){ |
| 114244 | + Expr *p; |
| 114093 | 114245 | sqlite3ExprDeferredDelete(pParse, pLeft); |
| 114094 | 114246 | sqlite3ExprDeferredDelete(pParse, pRight); |
| 114095 | | - return sqlite3ExprInt32(db, 0); |
| 114247 | + p = sqlite3ExprInt32(db, 0); |
| 114248 | + if( p ) p->op2 = 1; |
| 114249 | + return p; |
| 114096 | 114250 | }else{ |
| 114097 | 114251 | return sqlite3PExpr(pParse, TK_AND, pLeft, pRight); |
| 114098 | 114252 | } |
| 114099 | 114253 | } |
| 114100 | 114254 | } |
| | @@ -115872,17 +116026,43 @@ |
| 115872 | 116026 | ** If pParse is not NULL and expression p is a variable, then the variable |
| 115873 | 116027 | ** is marked so as to cause the statement to be reprepared each time a new |
| 115874 | 116028 | ** value is bound to it. Except, if parameter bRSI is true, then the statement |
| 115875 | 116029 | ** will only be reprepared if the rebind changes the value to or from a |
| 115876 | 116030 | ** "small integer" (either 0 or 1). |
| 116031 | +** |
| 116032 | +** If pParse is NULL but bRSI is non-zero, then to not accept the input |
| 116033 | +** as an integer if the TK_INTEGER opcode has its op2 field set to non-zero. |
| 116034 | +** A non-zero Expr.op2 indicates that the expression is a compile-time |
| 116035 | +** reduction of a constant expression. For example, "123 NOTNULL" would |
| 116036 | +** be rendered by the parser as a TK_INTEGER with value 1, but with op2 set. |
| 116037 | +** |
| 116038 | +** Summary: |
| 116039 | +** |
| 116040 | +** pParse bRsi What happens |
| 116041 | +** -------- ---- ------------------------------------------------ |
| 116042 | +** NULL 0 Reject if TK_VARIABLE. Accept if op2!=0 |
| 116043 | +** |
| 116044 | +** NULL 1 Reject if TK_VARIABLE or op2!=0 |
| 116045 | +** |
| 116046 | +** not-NULL 0 Accept TK_VARIABLE and/or op2!=0 but for TK_VARIABLE, |
| 116047 | +** cause the statement to reprepare on *any* |
| 116048 | +** binding change |
| 116049 | +** |
| 116050 | +** not-NULL 1 As previous, except reprepare only happens if a binding |
| 116051 | +** change move the variable value to or from an integer 0 |
| 116052 | +** or 1. |
| 115877 | 116053 | */ |
| 115878 | 116054 | SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse, int bRSI){ |
| 115879 | 116055 | int iSign = 1; /* Either +1 or -1. */ |
| 115880 | 116056 | while( 1/*exit-by-break*/ ){ |
| 115881 | 116057 | if( NEVER(p==0) ) return 0; |
| 115882 | 116058 | if( ExprUseUValue(p) ){ |
| 116059 | + assert( p->op==TK_INTEGER ); |
| 115883 | 116060 | *pValue = p->u.iValue*iSign; |
| 116061 | + if( bRSI && pParse==0 && p->op2 ){ |
| 116062 | + return 0; |
| 116063 | + } |
| 115884 | 116064 | return 1; |
| 115885 | 116065 | } |
| 115886 | 116066 | if( p->op==TK_UPLUS ){ |
| 115887 | 116067 | p = p->pLeft; |
| 115888 | 116068 | pParse = 0; |
| | @@ -121456,17 +121636,18 @@ |
| 121456 | 121636 | " AND (type != 'index' OR tbl_name = %Q)", |
| 121457 | 121637 | zDb, |
| 121458 | 121638 | zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, |
| 121459 | 121639 | pTab->zName |
| 121460 | 121640 | ); |
| 121461 | | - |
| 121462 | | - sqlite3NestedParse(pParse, |
| 121463 | | - "UPDATE temp." LEGACY_SCHEMA_TABLE " SET " |
| 121464 | | - "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) " |
| 121465 | | - "WHERE type IN ('trigger', 'view')", |
| 121466 | | - zDb, pTab->zName, iCol, zNew, bQuote |
| 121467 | | - ); |
| 121641 | + if( iSchema!=1 ){ |
| 121642 | + sqlite3NestedParse(pParse, |
| 121643 | + "UPDATE temp." LEGACY_SCHEMA_TABLE " SET " |
| 121644 | + "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) " |
| 121645 | + "WHERE type IN ('trigger', 'view')", |
| 121646 | + zDb, pTab->zName, iCol, zNew, bQuote |
| 121647 | + ); |
| 121648 | + } |
| 121468 | 121649 | |
| 121469 | 121650 | /* Drop and reload the database schema. */ |
| 121470 | 121651 | renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename); |
| 121471 | 121652 | renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1); |
| 121472 | 121653 | |
| | @@ -123152,16 +123333,16 @@ |
| 123152 | 123333 | regOut = reg+1+nField; |
| 123153 | 123334 | } |
| 123154 | 123335 | if( i==pTab->iPKey ){ |
| 123155 | 123336 | sqlite3VdbeAddOp2(v, OP_Null, 0, regOut); |
| 123156 | 123337 | }else{ |
| 123157 | | - char aff = pTab->aCol[i].affinity; |
| 123158 | | - if( aff==SQLITE_AFF_REAL ){ |
| 123159 | | - pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC; |
| 123160 | | - } |
| 123161 | 123338 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut); |
| 123162 | | - pTab->aCol[i].affinity = aff; |
| 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); |
| 123163 | 123344 | } |
| 123164 | 123345 | nField++; |
| 123165 | 123346 | } |
| 123166 | 123347 | } |
| 123167 | 123348 | if( nField==0 ){ |
| | @@ -123208,19 +123389,23 @@ |
| 123208 | 123389 | ** z points to the second token of the constraint definition. For an |
| 123209 | 123390 | ** unnamed NOT NULL constraint, z points to the first byte past the NOT |
| 123210 | 123391 | ** keyword. |
| 123211 | 123392 | ** |
| 123212 | 123393 | ** Argument eTok may be the token value of the first token of the constraint |
| 123213 | | -** (e.g. TK_CHECK or TK_REFERENCES) or zero. If it is either TK_REFERENCES |
| 123214 | | -** or TK_FOREIGN, special parsing is enabled to find the end of the foreign-key |
| 123215 | | -** constraint definition. |
| 123394 | +** (e.g. TK_CHECK or TK_REFERENCES or TK_NOT) or zero. If it is either |
| 123395 | +** TK_REFERENCES or TK_FOREIGN, special parsing is enabled to find the |
| 123396 | +** end of the foreign-key constraint definition. If eTok is TK_NOT then |
| 123397 | +** special processing is enabled to find the end of a NOT NULL constraint |
| 123398 | +** including its optional ON CONFLICT clause. |
| 123216 | 123399 | ** |
| 123217 | 123400 | ** Return the number of bytes until the end of the constraint. |
| 123218 | 123401 | */ |
| 123219 | 123402 | static int getConstraint(const u8 *z, int eTok){ |
| 123220 | 123403 | int iOff = 0; |
| 123221 | 123404 | int t = 0; |
| 123405 | + int t2 = 0; |
| 123406 | + int prev = 0; |
| 123222 | 123407 | |
| 123223 | 123408 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 123224 | 123409 | if( eTok==TK_FOREIGN ){ |
| 123225 | 123410 | /* For a FOREIGN KEY constraint, use getConstraint() to parse everything |
| 123226 | 123411 | ** up to the REFERENCES keyword. Then getConstraintToken() to consume |
| | @@ -123232,10 +123417,18 @@ |
| 123232 | 123417 | |
| 123233 | 123418 | if( eTok==TK_REFERENCES ){ |
| 123234 | 123419 | /* REFERENCES is followed by a table name. Gobble this up here in |
| 123235 | 123420 | ** case the table name is a fallback token like TK_GENERATED. */ |
| 123236 | 123421 | iOff += getConstraintToken(&z[iOff], &t); |
| 123422 | + |
| 123423 | + /* If the table names is followed by a parenthesized list of columns, |
| 123424 | + ** also skip that, lest the close ")" end the constraint in the |
| 123425 | + ** while loop below. */ |
| 123426 | + iOff += getWhitespace(&z[iOff]); |
| 123427 | + if( z[iOff]=='(' ){ |
| 123428 | + iOff += getConstraintToken(&z[iOff], &t); |
| 123429 | + } |
| 123237 | 123430 | } |
| 123238 | 123431 | #endif |
| 123239 | 123432 | |
| 123240 | 123433 | /* Now, the current constraint proceeds until the next occurence of one |
| 123241 | 123434 | ** of the following tokens: |
| | @@ -123242,21 +123435,38 @@ |
| 123242 | 123435 | ** |
| 123243 | 123436 | ** CONSTRAINT, PRIMARY, NOT, UNIQUE, CHECK, DEFAULT, |
| 123244 | 123437 | ** COLLATE, REFERENCES, FOREIGN, GENERATED, AS, RP, or COMMA |
| 123245 | 123438 | ** |
| 123246 | 123439 | ** Also exit the loop if ILLEGAL turns up. |
| 123440 | + ** |
| 123441 | + ** Exceptions: |
| 123442 | + ** |
| 123443 | + ** * For NOT NULL constraints, also terminate at DEFERRABLE |
| 123444 | + ** |
| 123445 | + ** * Do not terminate at DEFAULT if the previous token was SET |
| 123247 | 123446 | */ |
| 123248 | 123447 | while( 1 ){ |
| 123249 | 123448 | int n = getConstraintToken(&z[iOff], &t); |
| 123250 | | - if( t==TK_CONSTRAINT || t==TK_PRIMARY || t==TK_NOT || t==TK_UNIQUE |
| 123251 | | - || t==TK_CHECK || t==TK_DEFAULT || t==TK_COLLATE || t==TK_REFERENCES |
| 123449 | + if( t==TK_CONSTRAINT || t==TK_PRIMARY || t==TK_UNIQUE |
| 123450 | + || t==TK_CHECK || t==TK_COLLATE || t==TK_REFERENCES |
| 123451 | + || (t==TK_DEFAULT && prev!=TK_SET) |
| 123252 | 123452 | || t==TK_FOREIGN || t==TK_RP || t==TK_COMMA || t==TK_ILLEGAL |
| 123253 | 123453 | || t==TK_AS || t==TK_GENERATED |
| 123454 | + || (eTok==TK_NOT && t==TK_DEFERRABLE) |
| 123254 | 123455 | ){ |
| 123255 | 123456 | break; |
| 123457 | + } |
| 123458 | + if( t==TK_NOT |
| 123459 | + && (eTok!=TK_REFERENCES |
| 123460 | + || (getConstraintToken(&z[iOff+n],&t2), t2!=TK_DEFERRABLE)) |
| 123461 | + ){ |
| 123462 | + /* NOT ends the constraint unless eTok is TK_REFERENCES and the next |
| 123463 | + ** token is DEFERRABLE */ |
| 123464 | + break; |
| 123256 | 123465 | } |
| 123257 | 123466 | iOff += n; |
| 123467 | + prev = t; |
| 123258 | 123468 | } |
| 123259 | 123469 | |
| 123260 | 123470 | return iOff; |
| 123261 | 123471 | } |
| 123262 | 123472 | |
| | @@ -123419,12 +123629,16 @@ |
| 123419 | 123629 | iEnd = iOff; |
| 123420 | 123630 | break; |
| 123421 | 123631 | } |
| 123422 | 123632 | |
| 123423 | 123633 | }else if( t==TK_NOT && iNotNull==ii ){ |
| 123424 | | - iEnd = iOff + getConstraint(&zSql[iOff], 0); |
| 123425 | | - break; |
| 123634 | + int t2; |
| 123635 | + (void)getConstraintToken(zSql+iOff, &t2); |
| 123636 | + if( t2==TK_NULL ){ |
| 123637 | + iEnd = iOff + getConstraint(zSql+iOff, TK_NOT); |
| 123638 | + break; |
| 123639 | + } |
| 123426 | 123640 | }else if( t==TK_RP || t==TK_ILLEGAL ){ |
| 123427 | 123641 | iEnd = -1; |
| 123428 | 123642 | break; |
| 123429 | 123643 | }else if( t==TK_COMMA ){ |
| 123430 | 123644 | break; |
| | @@ -136891,11 +137105,11 @@ |
| 136891 | 137105 | break; |
| 136892 | 137106 | default: |
| 136893 | 137107 | return; |
| 136894 | 137108 | } |
| 136895 | 137109 | if( argc==2 ){ |
| 136896 | | - switch( sqlite3_value_numeric_type(argv[0]) ){ |
| 137110 | + switch( sqlite3_value_numeric_type(argv[1]) ){ |
| 136897 | 137111 | case SQLITE_INTEGER: |
| 136898 | 137112 | case SQLITE_FLOAT: |
| 136899 | 137113 | b = log(x); |
| 136900 | 137114 | if( b<=0.0 ) return; |
| 136901 | 137115 | x = sqlite3_value_double(argv[1]); |
| | @@ -137625,10 +137839,11 @@ |
| 137625 | 137839 | assert( argc==3 ); |
| 137626 | 137840 | x = sqlite3_value_double(argv[0]); |
| 137627 | 137841 | y = sqlite3_value_int(argv[1]); |
| 137628 | 137842 | z = sqlite3_value_int(argv[2]); |
| 137629 | 137843 | if( z<=0 ) z = 1; |
| 137844 | + if( y<-100000000 ) y = -100000000; /* Same limit as printf() */ |
| 137630 | 137845 | sqlite3FpDecode(&s, x, y, z); |
| 137631 | 137846 | if( s.isSpecial==2 ){ |
| 137632 | 137847 | sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN"); |
| 137633 | 137848 | }else{ |
| 137634 | 137849 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP); |
| | @@ -138446,10 +138661,148 @@ |
| 138446 | 138661 | pExpr->iTable = iCursor; |
| 138447 | 138662 | pExpr->iColumn = iCol; |
| 138448 | 138663 | } |
| 138449 | 138664 | return pExpr; |
| 138450 | 138665 | } |
| 138666 | + |
| 138667 | +/* |
| 138668 | +** Column iCol1 of table p1 is to be compared to column iCol2 of table p2. |
| 138669 | +** Return non-zero if the comparison will use an affinity compatible |
| 138670 | +** with the affinity of column iCol1 of table p1. Or zero if it will not. |
| 138671 | +** |
| 138672 | +** This is used in cases where (p1.iCol1 = p2.iCol2) must be evaluated |
| 138673 | +** using the affinity of p1.iCol1 as part of foreign key processing. |
| 138674 | +*/ |
| 138675 | +static int fkUsesLhsAffinity(Table *p1, int iCol1, Table *p2, int iCol2){ |
| 138676 | + u8 aff1 = iCol1>=0 ? p1->aCol[iCol1].affinity : SQLITE_AFF_NUMERIC; |
| 138677 | + u8 aff2 = p2->aCol[iCol2].affinity; |
| 138678 | + |
| 138679 | + /* If either side has a numeric affinity, then the comparison uses numeric |
| 138680 | + ** affinity. So the affinity matches if aff1 is numeric, but not if aff1 |
| 138681 | + ** is not numeric but aff2 is. */ |
| 138682 | + if( sqlite3IsNumericAffinity(aff1) ) return 1; |
| 138683 | + if( sqlite3IsNumericAffinity(aff2) ) return 0; |
| 138684 | + |
| 138685 | + assert( aff1==SQLITE_AFF_TEXT || aff1==SQLITE_AFF_BLOB ); |
| 138686 | + assert( aff2==SQLITE_AFF_TEXT || aff2==SQLITE_AFF_BLOB ); |
| 138687 | + |
| 138688 | + /* At this point, either the affinities are the same or one is TEXT and |
| 138689 | + ** the other BLOB. In the second case the comparison will be done with |
| 138690 | + ** BLOB affinity, which does not match if aff1 is TEXT. */ |
| 138691 | + return (aff1==aff2 || aff1==SQLITE_AFF_BLOB); |
| 138692 | +} |
| 138693 | + |
| 138694 | +#ifndef SQLITE_OMIT_CAST |
| 138695 | +/* |
| 138696 | +** Return non-zero if a lookup on column iCol of table pTab, using collation |
| 138697 | +** sequence zColl, can be done using either the rowid or an index. That is, |
| 138698 | +** if iCol is the INTEGER PRIMARY KEY of pTab, or if pTab has an index |
| 138699 | +** whose left-most column is iCol and whose collation sequence for that |
| 138700 | +** column is zColl. Return zero otherwise. |
| 138701 | +*/ |
| 138702 | +static int fkChildKeyIsIndexed(Table *pTab, int iCol, const char *zColl){ |
| 138703 | + Index *pIdx; |
| 138704 | + if( iCol==pTab->iPKey ) return 1; |
| 138705 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 138706 | + if( pIdx->aiColumn[0]==iCol |
| 138707 | + && pIdx->pPartIdxWhere==0 |
| 138708 | + && sqlite3StrICmp(pIdx->azColl[0], zColl)==0 |
| 138709 | + ){ |
| 138710 | + return 1; |
| 138711 | + } |
| 138712 | + } |
| 138713 | + return 0; |
| 138714 | +} |
| 138715 | +#endif |
| 138716 | + |
| 138717 | +/* |
| 138718 | +** Return an expression equivalent to (pLeft = pRight), where pLeft is a |
| 138719 | +** parent key value and pRight is an unresolved reference to column |
| 138720 | +** iChildCol of child table pChild. The comparison uses the affinity and |
| 138721 | +** collation sequence of column iCol of parent table pTab. pLeft must |
| 138722 | +** already carry that affinity and collation sequence. Ownership of |
| 138723 | +** both pLeft and pRight passes to this routine. |
| 138724 | +** |
| 138725 | +** If the simple (pLeft = pRight) does not naturally use the affinity of |
| 138726 | +** the parent column, the comparison is coded as (pLeft = +pRight). But |
| 138727 | +** the unary + prevents any index on the child column from being used. |
| 138728 | +** So in that case, if the child column is indexed, an extra term is added: |
| 138729 | +** |
| 138730 | +** (pLeft = +pRight) AND |
| 138731 | +** (pRight COLLATE <coll> IN (+pLeft, +CAST(pLeft AS NUMERIC))) |
| 138732 | +** |
| 138733 | +** Each child value that matches pLeft under the affinity of the parent |
| 138734 | +** column is either equal to pLeft itself or numerically equal to |
| 138735 | +** CAST(pLeft AS NUMERIC). So the IN term matches a superset of the |
| 138736 | +** child rows that the exact (pLeft = +pRight) term does, and since it |
| 138737 | +** compares using the affinity of the child column, it may be used with |
| 138738 | +** an index on the child column. The unary + operators on the RHS of the |
| 138739 | +** IN ensure that no affinity other than that of the child column is |
| 138740 | +** applied. |
| 138741 | +*/ |
| 138742 | +static Expr *fkParentChildEq( |
| 138743 | + Parse *pParse, /* Parsing context */ |
| 138744 | + Expr *pLeft, /* Parent key value */ |
| 138745 | + Expr *pRight, /* Unresolved reference to child key column */ |
| 138746 | + Table *pTab, /* Parent table */ |
| 138747 | + int iCol, /* Parent key column, or <0 for rowid */ |
| 138748 | + Table *pChild, /* Child table */ |
| 138749 | + int iChildCol, /* Child key column */ |
| 138750 | + int bComposite /* This is part of a composite FK */ |
| 138751 | +){ |
| 138752 | + sqlite3 *db = pParse->db; |
| 138753 | + Expr *pEq; |
| 138754 | +#ifndef SQLITE_OMIT_CAST |
| 138755 | + Expr *pIn = 0; |
| 138756 | +#endif |
| 138757 | + |
| 138758 | + if( fkUsesLhsAffinity(pTab, iCol, pChild, iChildCol) ){ |
| 138759 | + return sqlite3PExpr(pParse, TK_EQ, pLeft, pRight); |
| 138760 | + } |
| 138761 | + assert( iCol>=0 ); |
| 138762 | + |
| 138763 | +#ifndef SQLITE_OMIT_CAST |
| 138764 | + { |
| 138765 | + const char *zColl = sqlite3ColumnColl(&pTab->aCol[iCol]); |
| 138766 | + if( zColl==0 ) zColl = sqlite3StrBINARY; |
| 138767 | + |
| 138768 | + /* The IN term is only useful if it can drive a lookup on the child |
| 138769 | + ** table. Omit it otherwise, so that a full scan of the child table |
| 138770 | + ** does not have to evaluate it for every row. */ |
| 138771 | + if( bComposite || fkChildKeyIsIndexed(pChild, iChildCol, zColl) ){ |
| 138772 | + static const Token tNumeric = { "NUMERIC", 7 }; |
| 138773 | + Expr *pCast; |
| 138774 | + ExprList *pList; |
| 138775 | + |
| 138776 | + pCast = sqlite3ExprAlloc(db, TK_CAST, &tNumeric, 0); |
| 138777 | + sqlite3ExprAttachSubtrees(db, pCast, sqlite3ExprDup(db, pLeft, 0), 0); |
| 138778 | + pList = sqlite3ExprListAppend(pParse, 0, |
| 138779 | + sqlite3PExpr(pParse, TK_UPLUS, sqlite3ExprDup(db, pLeft, 0), 0) |
| 138780 | + ); |
| 138781 | + pList = sqlite3ExprListAppend(pParse, pList, |
| 138782 | + sqlite3PExpr(pParse, TK_UPLUS, pCast, 0) |
| 138783 | + ); |
| 138784 | + pIn = sqlite3PExpr(pParse, TK_IN, sqlite3ExprAddCollateString(pParse, |
| 138785 | + sqlite3ExprDup(db, pRight, 0), zColl |
| 138786 | + ), 0); |
| 138787 | + if( pIn ){ |
| 138788 | + pIn->x.pList = pList; |
| 138789 | + sqlite3ExprSetHeightAndFlags(pParse, pIn); |
| 138790 | + }else{ |
| 138791 | + sqlite3ExprListDelete(db, pList); |
| 138792 | + } |
| 138793 | + } |
| 138794 | + } |
| 138795 | +#endif |
| 138796 | + |
| 138797 | + pRight = sqlite3PExpr(pParse, TK_UPLUS, pRight, 0); |
| 138798 | + pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight); |
| 138799 | +#ifndef SQLITE_OMIT_CAST |
| 138800 | + pEq = sqlite3ExprAnd(pParse, pEq, pIn); |
| 138801 | +#endif |
| 138802 | + return pEq; |
| 138803 | +} |
| 138451 | 138804 | |
| 138452 | 138805 | /* |
| 138453 | 138806 | ** This function is called to generate code executed when a row is deleted |
| 138454 | 138807 | ** from the parent table of foreign key constraint pFKey and, if pFKey is |
| 138455 | 138808 | ** deferred, when a row is inserted into the same table. When generating |
| | @@ -138515,20 +138868,22 @@ |
| 138515 | 138868 | */ |
| 138516 | 138869 | for(i=0; i<pFKey->nCol; i++){ |
| 138517 | 138870 | Expr *pLeft; /* Value from parent table row */ |
| 138518 | 138871 | Expr *pRight; /* Column ref to child table */ |
| 138519 | 138872 | Expr *pEq; /* Expression (pLeft = pRight) */ |
| 138520 | | - i16 iCol; /* Index of column in child table */ |
| 138873 | + i16 iCol; /* Index of column in parent table */ |
| 138874 | + i16 iCol2; /* Index of column in child table */ |
| 138521 | 138875 | const char *zCol; /* Name of column in child table */ |
| 138522 | 138876 | |
| 138523 | 138877 | iCol = pIdx ? pIdx->aiColumn[i] : -1; |
| 138524 | 138878 | pLeft = exprTableRegister(pParse, pTab, regData, iCol); |
| 138525 | | - iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; |
| 138526 | | - assert( iCol>=0 ); |
| 138527 | | - zCol = pFKey->pFrom->aCol[iCol].zCnName; |
| 138879 | + iCol2 = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; |
| 138880 | + assert( iCol2>=0 ); |
| 138881 | + zCol = pFKey->pFrom->aCol[iCol2].zCnName; |
| 138528 | 138882 | pRight = sqlite3Expr(db, TK_ID, zCol); |
| 138529 | | - pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight); |
| 138883 | + pEq = fkParentChildEq(pParse, pLeft, pRight, pTab, iCol, |
| 138884 | + pFKey->pFrom, iCol2, pFKey->nCol>0); |
| 138530 | 138885 | pWhere = sqlite3ExprAnd(pParse, pWhere, pEq); |
| 138531 | 138886 | } |
| 138532 | 138887 | |
| 138533 | 138888 | /* If the child table is the same as the parent table, then add terms |
| 138534 | 138889 | ** to the WHERE clause that prevent this entry from being scanned. |
| | @@ -138784,17 +139139,16 @@ |
| 138784 | 139139 | ** Return true if the parser passed as the first argument is being |
| 138785 | 139140 | ** used to code a trigger that is really a "SET NULL" action belonging |
| 138786 | 139141 | ** to trigger pFKey. |
| 138787 | 139142 | */ |
| 138788 | 139143 | static int isSetNullAction(Parse *pParse, FKey *pFKey){ |
| 138789 | | - Parse *pTop = sqlite3ParseToplevel(pParse); |
| 138790 | | - if( pTop->pTriggerPrg ){ |
| 138791 | | - Trigger *p = pTop->pTriggerPrg->pTrigger; |
| 139144 | + Trigger *p = pParse->pTrigCoded; |
| 139145 | + if( p ){ |
| 138792 | 139146 | if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull) |
| 138793 | 139147 | || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull) |
| 138794 | 139148 | ){ |
| 138795 | | - assert( (pTop->db->flags & SQLITE_FkNoAction)==0 ); |
| 139149 | + assert( (pParse->db->flags & SQLITE_FkNoAction)==0 ); |
| 138796 | 139150 | return 1; |
| 138797 | 139151 | } |
| 138798 | 139152 | } |
| 138799 | 139153 | return 0; |
| 138800 | 139154 | } |
| | @@ -139184,30 +139538,39 @@ |
| 139184 | 139538 | Token tOld = { "old", 3 }; /* Literal "old" token */ |
| 139185 | 139539 | Token tNew = { "new", 3 }; /* Literal "new" token */ |
| 139186 | 139540 | Token tFromCol; /* Name of column in child table */ |
| 139187 | 139541 | Token tToCol; /* Name of column in parent table */ |
| 139188 | 139542 | int iFromCol; /* Idx of column in child table */ |
| 139189 | | - Expr *pEq; /* tFromCol = OLD.tToCol */ |
| 139543 | + int iToCol; /* Idx of column in parent table */ |
| 139544 | + Expr *pEq; /* OLD.tToCol = tFromCol */ |
| 139545 | + Expr *pLeft; /* OLD.tToCol */ |
| 139546 | + Expr *pRight; /* tFromCol */ |
| 139190 | 139547 | |
| 139191 | 139548 | iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; |
| 139192 | 139549 | assert( iFromCol>=0 ); |
| 139193 | 139550 | assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) ); |
| 139194 | 139551 | assert( pIdx==0 || pIdx->aiColumn[i]>=0 ); |
| 139195 | | - sqlite3TokenInit(&tToCol, |
| 139196 | | - pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName); |
| 139552 | + iToCol = pIdx ? pIdx->aiColumn[i] : pTab->iPKey; |
| 139553 | + sqlite3TokenInit(&tToCol, pTab->aCol[iToCol].zCnName); |
| 139197 | 139554 | sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName); |
| 139198 | 139555 | |
| 139199 | | - /* Create the expression "OLD.zToCol = zFromCol". It is important |
| 139200 | | - ** that the "OLD.zToCol" term is on the LHS of the = operator, so |
| 139201 | | - ** that the affinity and collation sequence associated with the |
| 139202 | | - ** parent table are used for the comparison. */ |
| 139203 | | - pEq = sqlite3PExpr(pParse, TK_EQ, |
| 139204 | | - sqlite3PExpr(pParse, TK_DOT, |
| 139205 | | - sqlite3ExprAlloc(db, TK_ID, &tOld, 0), |
| 139206 | | - sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)), |
| 139207 | | - sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0) |
| 139556 | + pLeft = sqlite3PExpr(pParse, TK_DOT, |
| 139557 | + sqlite3ExprAlloc(db, TK_ID, &tOld, 0), |
| 139558 | + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) |
| 139208 | 139559 | ); |
| 139560 | + pRight = sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0); |
| 139561 | + |
| 139562 | + /* OLD.* columns do not normally have an affinity. So set the affinity |
| 139563 | + ** of OLD.tToCol (the parent key) explicitly here. */ |
| 139564 | + if( pLeft ) pLeft->affExpr = pTab->aCol[iToCol].affinity; |
| 139565 | + |
| 139566 | + /* Create the expression "OLD.tToCol = tFromCol". It is important |
| 139567 | + ** that the "OLD.tToCol" term is on the LHS of the = operator, so |
| 139568 | + ** that the collation sequence associated with the parent table is |
| 139569 | + ** used for the comparison. */ |
| 139570 | + pEq = fkParentChildEq(pParse, pLeft, pRight, pTab, iToCol, |
| 139571 | + pFKey->pFrom, iFromCol, pFKey->nCol>1); |
| 139209 | 139572 | pWhere = sqlite3ExprAnd(pParse, pWhere, pEq); |
| 139210 | 139573 | |
| 139211 | 139574 | /* For ON UPDATE, construct the next term of the WHEN clause. |
| 139212 | 139575 | ** The final WHEN clause will be like this: |
| 139213 | 139576 | ** |
| | @@ -144652,29 +145015,36 @@ |
| 144652 | 145015 | ** was not on the list. |
| 144653 | 145016 | */ |
| 144654 | 145017 | SQLITE_API int sqlite3_cancel_auto_extension( |
| 144655 | 145018 | void (*xInit)(void) |
| 144656 | 145019 | ){ |
| 145020 | + int n = 0; |
| 145021 | +#ifndef SQLITE_OMIT_AUTOINIT |
| 145022 | + if( sqlite3_initialize() ){ |
| 145023 | + return 0; |
| 145024 | + }else |
| 145025 | +#endif |
| 145026 | + { |
| 144657 | 145027 | #if SQLITE_THREADSAFE |
| 144658 | | - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); |
| 145028 | + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); |
| 144659 | 145029 | #endif |
| 144660 | | - int i; |
| 144661 | | - int n = 0; |
| 144662 | | - wsdAutoextInit; |
| 145030 | + int i; |
| 145031 | + wsdAutoextInit; |
| 144663 | 145032 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 144664 | | - if( xInit==0 ) return 0; |
| 145033 | + if( xInit==0 ) return 0; |
| 144665 | 145034 | #endif |
| 144666 | | - sqlite3_mutex_enter(mutex); |
| 144667 | | - for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ |
| 144668 | | - if( wsdAutoext.aExt[i]==xInit ){ |
| 144669 | | - wsdAutoext.nExt--; |
| 144670 | | - wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; |
| 144671 | | - n++; |
| 144672 | | - break; |
| 144673 | | - } |
| 144674 | | - } |
| 144675 | | - sqlite3_mutex_leave(mutex); |
| 145035 | + sqlite3_mutex_enter(mutex); |
| 145036 | + for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ |
| 145037 | + if( wsdAutoext.aExt[i]==xInit ){ |
| 145038 | + wsdAutoext.nExt--; |
| 145039 | + wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; |
| 145040 | + n++; |
| 145041 | + break; |
| 145042 | + } |
| 145043 | + } |
| 145044 | + sqlite3_mutex_leave(mutex); |
| 145045 | + } |
| 144676 | 145046 | return n; |
| 144677 | 145047 | } |
| 144678 | 145048 | |
| 144679 | 145049 | /* |
| 144680 | 145050 | ** Reset the automatic extension loading mechanism. |
| | @@ -147256,18 +147626,13 @@ |
| 147256 | 147626 | sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1); |
| 147257 | 147627 | } |
| 147258 | 147628 | sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, |
| 147259 | 147629 | 1, 0, &iDataCur, &iIdxCur); |
| 147260 | 147630 | /* reg[7] counts the number of entries in the table. |
| 147261 | | - ** reg[8+i] counts the number of entries in the i-th index |
| 147262 | 147631 | */ |
| 147263 | 147632 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); |
| 147264 | | - for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 147265 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ |
| 147266 | | - } |
| 147267 | | - assert( pParse->nMem>=8+j ); |
| 147268 | | - assert( sqlite3NoTempsInRange(pParse,1,7+j) ); |
| 147633 | + assert( sqlite3NoTempsInRange(pParse,1,7) ); |
| 147269 | 147634 | sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); |
| 147270 | 147635 | loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); |
| 147271 | 147636 | |
| 147272 | 147637 | /* Fetch the right-most column from the table. This will cause |
| 147273 | 147638 | ** the entire record header to be parsed and sanity checked. It |
| | @@ -147476,11 +147841,10 @@ |
| 147476 | 147841 | int ckUniq = sqlite3VdbeMakeLabel(pParse); |
| 147477 | 147842 | if( pPk==pIdx ) continue; |
| 147478 | 147843 | r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, |
| 147479 | 147844 | pPrior, r1); |
| 147480 | 147845 | pPrior = pIdx; |
| 147481 | | - sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */ |
| 147482 | 147846 | /* Verify that an index entry exists for the current table row */ |
| 147483 | 147847 | sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, |
| 147484 | 147848 | pIdx->nColumn); VdbeCoverage(v); |
| 147485 | 147849 | jmp2 = sqlite3VdbeAddOp3(v, OP_IFindKey, iIdxCur+j, ckUniq, r1); |
| 147486 | 147850 | VdbeCoverage(v); |
| | @@ -151940,11 +152304,11 @@ |
| 151940 | 152304 | sqlite3HashInit(&ht); |
| 151941 | 152305 | if( pEList ){ |
| 151942 | 152306 | nCol = pEList->nExpr; |
| 151943 | 152307 | aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); |
| 151944 | 152308 | testcase( aCol==0 ); |
| 151945 | | - if( NEVER(nCol>32767) ) nCol = 32767; |
| 152309 | + if( nCol>32767 ) nCol = 32767; |
| 151946 | 152310 | }else{ |
| 151947 | 152311 | nCol = 0; |
| 151948 | 152312 | aCol = 0; |
| 151949 | 152313 | } |
| 151950 | 152314 | assert( nCol==(i16)nCol ); |
| | @@ -153185,13 +153549,16 @@ |
| 153185 | 153549 | assert( pOrderBy ); |
| 153186 | 153550 | nOrderBy = pOrderBy->nExpr; |
| 153187 | 153551 | |
| 153188 | 153552 | /* For operators other than UNION ALL we have to make sure that |
| 153189 | 153553 | ** the ORDER BY clause covers every term of the result set. Add |
| 153190 | | - ** terms to the ORDER BY clause as necessary. |
| 153554 | + ** terms to the ORDER BY clause as necessary. Do the same for UNION ALL |
| 153555 | + ** if any operator to the left is not UNION ALL, so that the ORDER BY |
| 153556 | + ** carries the collation of every column into that sub-compound. |
| 153191 | 153557 | */ |
| 153192 | | - if( op!=TK_ALL ){ |
| 153558 | + for(pPrior=p; pPrior && pPrior->op==TK_ALL; pPrior=pPrior->pPrior){} |
| 153559 | + if( pPrior && pPrior->pPrior ){ |
| 153193 | 153560 | for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ |
| 153194 | 153561 | struct ExprList_item *pItem; |
| 153195 | 153562 | for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){ |
| 153196 | 153563 | assert( pItem!=0 ); |
| 153197 | 153564 | assert( pItem->u.x.iOrderByCol>0 ); |
| | @@ -153244,13 +153611,15 @@ |
| 153244 | 153611 | pParse->nMem += nExpr+1; |
| 153245 | 153612 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev); |
| 153246 | 153613 | pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1); |
| 153247 | 153614 | if( pKeyDup ){ |
| 153248 | 153615 | assert( sqlite3KeyInfoIsWriteable(pKeyDup) ); |
| 153249 | | - for(i=0; i<nExpr; i++){ |
| 153250 | | - pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i); |
| 153251 | | - pKeyDup->aSortFlags[i] = 0; |
| 153616 | + /* Use the ORDER BY collations, not multiSelectCollSeq(p), because |
| 153617 | + ** p might be just one part of a larger compound, in which case |
| 153618 | + ** the ORDER BY carries the COLLATE of the larger compound. */ |
| 153619 | + for(i=0; ALWAYS(pKeyMerge) && i<nOrderBy; i++){ |
| 153620 | + pKeyDup->aColl[pOrderBy->a[i].u.x.iOrderByCol-1] = pKeyMerge->aColl[i]; |
| 153252 | 153621 | } |
| 153253 | 153622 | } |
| 153254 | 153623 | } |
| 153255 | 153624 | |
| 153256 | 153625 | /* Separate the left and the right query from one another |
| | @@ -155303,20 +155672,20 @@ |
| 155303 | 155672 | *pNew = *p; |
| 155304 | 155673 | p->pSrc = pNewSrc; |
| 155305 | 155674 | p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0)); |
| 155306 | 155675 | p->op = TK_SELECT; |
| 155307 | 155676 | p->pWhere = 0; |
| 155308 | | - pNew->pGroupBy = 0; |
| 155309 | | - pNew->pHaving = 0; |
| 155677 | + p->pGroupBy = 0; |
| 155678 | + p->pHaving = 0; |
| 155310 | 155679 | pNew->pOrderBy = 0; |
| 155311 | 155680 | p->pPrior = 0; |
| 155312 | 155681 | p->pNext = 0; |
| 155313 | 155682 | p->pWith = 0; |
| 155314 | 155683 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 155315 | 155684 | p->pWinDefn = 0; |
| 155316 | 155685 | #endif |
| 155317 | | - p->selFlags &= ~(u32)SF_Compound; |
| 155686 | + p->selFlags &= ~(u32)(SF_Compound|SF_Distinct); |
| 155318 | 155687 | assert( (p->selFlags & SF_Converted)==0 ); |
| 155319 | 155688 | p->selFlags |= SF_Converted; |
| 155320 | 155689 | assert( pNew->pPrior!=0 ); |
| 155321 | 155690 | pNew->pPrior->pNext = pNew; |
| 155322 | 155691 | pNew->pLimit = 0; |
| | @@ -155399,10 +155768,12 @@ |
| 155399 | 155768 | pParse->pWith = pWith; |
| 155400 | 155769 | } |
| 155401 | 155770 | } |
| 155402 | 155771 | return pWith; |
| 155403 | 155772 | } |
| 155773 | + |
| 155774 | +static int selectExpanderWalk(Walker*, Select*); |
| 155404 | 155775 | |
| 155405 | 155776 | /* |
| 155406 | 155777 | ** This function checks to see if argument pFrom refers to a CTE declared by |
| 155407 | 155778 | ** a WITH clause on the stack currently maintained by the parser (on the |
| 155408 | 155779 | ** pParse->pWith linked list). And if currently processing a CTE expression, |
| | @@ -155536,19 +155907,17 @@ |
| 155536 | 155907 | } |
| 155537 | 155908 | if( (pRecTerm->selFlags & SF_Recursive)==0 ) break; |
| 155538 | 155909 | pRecTerm = pRecTerm->pPrior; |
| 155539 | 155910 | } |
| 155540 | 155911 | |
| 155912 | + if( pParse->nTab >= db->aLimit[SQLITE_LIMIT_CTES] |
| 155913 | + && db->aLimit[SQLITE_LIMIT_CTES]>0 |
| 155914 | + ){ |
| 155915 | + sqlite3ErrorMsg(pParse, "too many CTEs"); |
| 155916 | + return 2; |
| 155917 | + } |
| 155541 | 155918 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 155542 | | - if( pParse->nTab>=db->aLimit[SQLITE_LIMIT_EXPR_DEPTH] ){ |
| 155543 | | - /* Bug 2026-10-04T05:35:14Z: Prevent nested CTEs from generating |
| 155544 | | - ** an exponential number of cursors. The error message here will |
| 155545 | | - ** be "Nested too deep", which isn't exactly correct, but it is |
| 155546 | | - ** sufficient, and we don't want to use extra code space for more |
| 155547 | | - ** detail on such an obscure error. */ |
| 155548 | | - pParse->nNestSel = pParse->nTab; |
| 155549 | | - } |
| 155550 | 155919 | pParse->nNestSel++; |
| 155551 | 155920 | if( checkSubqueryNestingDepth(pParse) ) return 2; |
| 155552 | 155921 | #endif |
| 155553 | 155922 | pCte->zCteErr = "circular reference: %s"; |
| 155554 | 155923 | pSavedWith = pParse->pWith; |
| | @@ -155566,11 +155935,11 @@ |
| 155566 | 155935 | if( rc ){ |
| 155567 | 155936 | pParse->pWith = pSavedWith; |
| 155568 | 155937 | return 2; |
| 155569 | 155938 | } |
| 155570 | 155939 | }else{ |
| 155571 | | - if( sqlite3WalkSelect(pWalker, pSel) ){ |
| 155940 | + if( selectExpanderWalk(pWalker, pSel) ){ |
| 155572 | 155941 | pParse->pWith = pSavedWith; |
| 155573 | 155942 | return 2; |
| 155574 | 155943 | } |
| 155575 | 155944 | } |
| 155576 | 155945 | pParse->pWith = pWith; |
| | @@ -155828,11 +156197,14 @@ |
| 155828 | 156197 | #endif |
| 155829 | 156198 | nCol = pTab->nCol; |
| 155830 | 156199 | pTab->nCol = -1; |
| 155831 | 156200 | pWalker->eCode = 1; /* Turn on Select.selId renumbering */ |
| 155832 | 156201 | if( pFrom->fg.isSubquery ){ |
| 155833 | | - sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect); |
| 156202 | + bft hasCompound = pParse->hasCompound; |
| 156203 | + pParse->hasCompound = 1; |
| 156204 | + selectExpanderWalk(pWalker, pFrom->u4.pSubq->pSelect); |
| 156205 | + pParse->hasCompound = hasCompound; |
| 155834 | 156206 | } |
| 155835 | 156207 | pWalker->eCode = eCodeOrig; |
| 155836 | 156208 | pTab->nCol = nCol; |
| 155837 | 156209 | } |
| 155838 | 156210 | #endif |
| | @@ -156118,10 +156490,31 @@ |
| 156118 | 156490 | SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){ |
| 156119 | 156491 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 156120 | 156492 | assert( 0 ); |
| 156121 | 156493 | } |
| 156122 | 156494 | #endif |
| 156495 | + |
| 156496 | +/* |
| 156497 | +** The first argument is a Walker object created by sqlite3SelectExpand(). |
| 156498 | +** This function uses the walker to expand the SELECT statement passed |
| 156499 | +** as the second argument. If necessary, it also converts any compound |
| 156500 | +** SELECT statements that have COLLATE clauses on ORDER BY expressions |
| 156501 | +** to sub-queries. Return the return value of sqlite3WalkSelect(). |
| 156502 | +*/ |
| 156503 | +static int selectExpanderWalk(Walker *pWalker, Select *pSelect){ |
| 156504 | + assert( pWalker->xExprCallback==sqlite3ExprWalkNoop ); |
| 156505 | + assert( pWalker->pParse!=0 ); |
| 156506 | + if( OK_IF_ALWAYS_TRUE(pWalker->pParse->hasCompound) ){ |
| 156507 | + pWalker->xSelectCallback = convertCompoundSelectToSubquery; |
| 156508 | + pWalker->xSelectCallback2 = 0; |
| 156509 | + sqlite3WalkSelect(pWalker, pSelect); |
| 156510 | + } |
| 156511 | + pWalker->xSelectCallback = selectExpander; |
| 156512 | + pWalker->xSelectCallback2 = sqlite3SelectPopWith; |
| 156513 | + return sqlite3WalkSelect(pWalker, pSelect); |
| 156514 | +} |
| 156515 | + |
| 156123 | 156516 | /* |
| 156124 | 156517 | ** This routine "expands" a SELECT statement and all of its subqueries. |
| 156125 | 156518 | ** For additional information on what it means to "expand" a SELECT |
| 156126 | 156519 | ** statement, see the comment on the selectExpand worker callback above. |
| 156127 | 156520 | ** |
| | @@ -156135,19 +156528,12 @@ |
| 156135 | 156528 | */ |
| 156136 | 156529 | static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ |
| 156137 | 156530 | Walker w; |
| 156138 | 156531 | w.xExprCallback = sqlite3ExprWalkNoop; |
| 156139 | 156532 | w.pParse = pParse; |
| 156140 | | - if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){ |
| 156141 | | - w.xSelectCallback = convertCompoundSelectToSubquery; |
| 156142 | | - w.xSelectCallback2 = 0; |
| 156143 | | - sqlite3WalkSelect(&w, pSelect); |
| 156144 | | - } |
| 156145 | | - w.xSelectCallback = selectExpander; |
| 156146 | | - w.xSelectCallback2 = sqlite3SelectPopWith; |
| 156147 | 156533 | w.eCode = 0; |
| 156148 | | - sqlite3WalkSelect(&w, pSelect); |
| 156534 | + selectExpanderWalk(&w, pSelect); |
| 156149 | 156535 | } |
| 156150 | 156536 | |
| 156151 | 156537 | |
| 156152 | 156538 | #ifndef SQLITE_OMIT_SUBQUERY |
| 156153 | 156539 | /* |
| | @@ -157868,15 +158254,25 @@ |
| 157868 | 158254 | /* This is a CTE for which materialization code has already been |
| 157869 | 158255 | ** generated. Invoke the subroutine to compute the materialization, |
| 157870 | 158256 | ** then make the pItem->iCursor be a copy of the ephemeral table that |
| 157871 | 158257 | ** holds the result of the materialization. tag-select-0484 */ |
| 157872 | 158258 | CteUse *pCteUse = pItem->u2.pCteUse; |
| 158259 | + /* The materialization is computed once per run of the statement or |
| 158260 | + ** trigger program and does not change after that. So the subroutine |
| 158261 | + ** call and the OP_OpenDup are needed on the first pass only. The |
| 158262 | + ** OP_NullRow runs on every pass and leaves the duplicate cursor not |
| 158263 | + ** positioned on a row, as OP_OpenDup does. */ |
| 158264 | + int addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 157873 | 158265 | sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e); |
| 157874 | 158266 | if( pItem->iCursor!=pCteUse->iCur ){ |
| 157875 | 158267 | sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur); |
| 157876 | 158268 | VdbeComment((v, "%!S", pItem)); |
| 157877 | 158269 | } |
| 158270 | + sqlite3VdbeJumpHere(v, addrOnce); |
| 158271 | + if( pItem->iCursor!=pCteUse->iCur ){ |
| 158272 | + sqlite3VdbeAddOp1(v, OP_NullRow, pItem->iCursor); |
| 158273 | + } |
| 157878 | 158274 | pSub->nSelectRow = pCteUse->nRowEst; |
| 157879 | 158275 | }else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){ |
| 157880 | 158276 | /* This view has already been materialized by a prior entry in |
| 157881 | 158277 | ** this same FROM clause. Reuse it. tag-select-0486 */ |
| 157882 | 158278 | Subquery *pPriorSubq; |
| | @@ -160259,10 +160655,11 @@ |
| 160259 | 160655 | sqlite3ParseObjectInit(&sSubParse, db); |
| 160260 | 160656 | memset(&sNC, 0, sizeof(sNC)); |
| 160261 | 160657 | sNC.pParse = &sSubParse; |
| 160262 | 160658 | sSubParse.pTriggerTab = pTab; |
| 160263 | 160659 | sSubParse.pToplevel = pTop; |
| 160660 | + sSubParse.pTrigCoded = pTrigger; |
| 160264 | 160661 | sSubParse.zAuthContext = pTrigger->zName; |
| 160265 | 160662 | sSubParse.eTriggerOp = pTrigger->op; |
| 160266 | 160663 | sSubParse.nQueryLoop = pParse->nQueryLoop; |
| 160267 | 160664 | sSubParse.prepFlags = pParse->prepFlags; |
| 160268 | 160665 | sSubParse.oldmask = 0; |
| | @@ -161073,14 +161470,18 @@ |
| 161073 | 161470 | { |
| 161074 | 161471 | int rc; |
| 161075 | 161472 | rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, |
| 161076 | 161473 | j<0 ? "ROWID" : pTab->aCol[j].zCnName, |
| 161077 | 161474 | db->aDb[iDb].zDbSName); |
| 161078 | | - if( rc==SQLITE_DENY ){ |
| 161079 | | - goto update_cleanup; |
| 161080 | | - }else if( rc==SQLITE_IGNORE ){ |
| 161081 | | - aXRef[j] = -1; |
| 161475 | + if( rc!=SQLITE_OK ){ |
| 161476 | + if( rc==SQLITE_DENY ){ |
| 161477 | + goto update_cleanup; |
| 161478 | + }else if( j<0 || j==pTab->iPKey ){ |
| 161479 | + chngRowid = 0; |
| 161480 | + }else{ |
| 161481 | + aXRef[j] = -1; |
| 161482 | + } |
| 161082 | 161483 | } |
| 161083 | 161484 | } |
| 161084 | 161485 | #endif |
| 161085 | 161486 | } |
| 161086 | 161487 | assert( (chngRowid & chngPk)==0 ); |
| | @@ -163496,11 +163897,21 @@ |
| 163496 | 163897 | /* Justification of ALWAYS(): The xConstructor method is required to |
| 163497 | 163898 | ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ |
| 163498 | 163899 | if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ |
| 163499 | 163900 | rc = growVTrans(db); |
| 163500 | 163901 | if( rc==SQLITE_OK ){ |
| 163501 | | - addToVTrans(db, sqlite3GetVTable(db, pTab)); |
| 163902 | + VTable *pVTab = sqlite3GetVTable(db, pTab); |
| 163903 | + const sqlite3_module *pModule = pMod->pModule; |
| 163904 | + int iSvpt = db->nStatement + db->nSavepoint; |
| 163905 | + addToVTrans(db, pVTab); |
| 163906 | + /* As in sqlite3VtabBegin(): open the current savepoint on the new |
| 163907 | + ** table, so that a ROLLBACK TO that undoes the CREATE also invokes |
| 163908 | + ** its xRollbackTo method. */ |
| 163909 | + if( iSvpt && pModule->iVersion>=2 && pModule->xSavepoint ){ |
| 163910 | + pVTab->iSavepoint = iSvpt; |
| 163911 | + rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1); |
| 163912 | + } |
| 163502 | 163913 | } |
| 163503 | 163914 | } |
| 163504 | 163915 | |
| 163505 | 163916 | return rc; |
| 163506 | 163917 | } |
| | @@ -165382,10 +165793,13 @@ |
| 165382 | 165793 | pOrigLhs->a[iField].pExpr = 0; |
| 165383 | 165794 | } |
| 165384 | 165795 | nRhs++; |
| 165385 | 165796 | } |
| 165386 | 165797 | } |
| 165798 | + if( nRhs<pOrigRhs->nExpr ){ |
| 165799 | + ExprClearProperty(pNew, EP_Subrtn); /* See bug 2026-10-16T22:07:15Z */ |
| 165800 | + } |
| 165387 | 165801 | sqlite3ExprListDelete(db, pOrigRhs); |
| 165388 | 165802 | if( pOrigLhs ){ |
| 165389 | 165803 | sqlite3ExprListDelete(db, pOrigLhs); |
| 165390 | 165804 | pNew->pLeft->x.pList = pLhs; |
| 165391 | 165805 | } |
| | @@ -166907,11 +167321,13 @@ |
| 166907 | 167321 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 166908 | 167322 | SWAP(u8, bSeekPastNull, bStopAtNull); |
| 166909 | 167323 | SWAP(u8, nBtm, nTop); |
| 166910 | 167324 | } |
| 166911 | 167325 | |
| 166912 | | - if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){ |
| 167326 | + if( (iLevel>0 || (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0) |
| 167327 | + && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 |
| 167328 | + ){ |
| 166913 | 167329 | /* In case OP_SeekScan is used, ensure that the index cursor does not |
| 166914 | 167330 | ** point to a valid row for the first iteration of this loop. */ |
| 166915 | 167331 | sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur); |
| 166916 | 167332 | } |
| 166917 | 167333 | |
| | @@ -168887,11 +169303,11 @@ |
| 168887 | 169303 | if( pLeft==0 ){ |
| 168888 | 169304 | pNew = 0; /* Collating sequence mismatch */ |
| 168889 | 169305 | }else{ |
| 168890 | 169306 | pDup = sqlite3ExprDup(db, pLeft, 0); |
| 168891 | 169307 | if( sqlite3ExprCollSeq(pParse, pDup)!=pCollSeq |
| 168892 | | - && ALWAYS(pCollSeq!=0) |
| 169308 | + && pCollSeq!=0 |
| 168893 | 169309 | ){ |
| 168894 | 169310 | assert( pCollSeq->zName!=0 ); |
| 168895 | 169311 | pDup = sqlite3ExprAddCollateString(pParse, pDup, pCollSeq->zName); |
| 168896 | 169312 | } |
| 168897 | 169313 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0); |
| | @@ -173972,11 +174388,11 @@ |
| 173972 | 174388 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 173973 | 174389 | SrcList *pTabList; /* The FROM clause */ |
| 173974 | 174390 | SrcItem *pSrc; /* The FROM clause btree term to add */ |
| 173975 | 174391 | WhereLoop *pNew; /* Template WhereLoop object */ |
| 173976 | 174392 | int rc = SQLITE_OK; /* Return code */ |
| 173977 | | - int iSortIdx = 1; /* Index number */ |
| 174393 | + int iSortIdx; /* Index number */ |
| 173978 | 174394 | int b; /* A boolean value */ |
| 173979 | 174395 | LogEst rSize; /* number of rows in the table */ |
| 173980 | 174396 | WhereClause *pWC; /* The parsed WHERE clause */ |
| 173981 | 174397 | Table *pTab; /* Table being queried */ |
| 173982 | 174398 | |
| | @@ -173991,11 +174407,15 @@ |
| 173991 | 174407 | if( pSrc->fg.isIndexedBy ){ |
| 173992 | 174408 | assert( pSrc->fg.isCte==0 ); |
| 173993 | 174409 | /* An INDEXED BY clause specifies a particular index to use */ |
| 173994 | 174410 | pProbe = pSrc->u2.pIBIndex; |
| 173995 | 174411 | }else if( !HasRowid(pTab) ){ |
| 173996 | | - pProbe = pTab->pIndex; |
| 174412 | + if( pSrc->fg.notIndexed ){ |
| 174413 | + pProbe = sqlite3PrimaryKeyIndex(pTab); |
| 174414 | + }else{ |
| 174415 | + pProbe = pTab->pIndex; |
| 174416 | + } |
| 173997 | 174417 | }else{ |
| 173998 | 174418 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 173999 | 174419 | ** variable sPk to represent the rowid primary key index. Make this |
| 174000 | 174420 | ** fake index the first in a chain of Index objects with all of the real |
| 174001 | 174421 | ** indices to follow */ |
| | @@ -174010,15 +174430,11 @@ |
| 174010 | 174430 | sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */ |
| 174011 | 174431 | sPk.idxType = SQLITE_IDXTYPE_IPK; |
| 174012 | 174432 | aiRowEstPk[0] = pTab->nRowLogEst; |
| 174013 | 174433 | aiRowEstPk[1] = 0; |
| 174014 | 174434 | pFirst = pSrc->pSTab->pIndex; |
| 174015 | | - if( pSrc->fg.notIndexed==0 ){ |
| 174016 | | - /* The real indices of the table are only considered if the |
| 174017 | | - ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 174018 | | - sPk.pNext = pFirst; |
| 174019 | | - } |
| 174435 | + sPk.pNext = pFirst; |
| 174020 | 174436 | pProbe = &sPk; |
| 174021 | 174437 | } |
| 174022 | 174438 | rSize = pTab->nRowLogEst; |
| 174023 | 174439 | |
| 174024 | 174440 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| | @@ -174076,12 +174492,14 @@ |
| 174076 | 174492 | } |
| 174077 | 174493 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 174078 | 174494 | |
| 174079 | 174495 | /* Loop over all indices. If there was an INDEXED BY clause, then only |
| 174080 | 174496 | ** consider index pProbe. */ |
| 174081 | | - for(; rc==SQLITE_OK && pProbe; |
| 174082 | | - pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++ |
| 174497 | + for(iSortIdx=1; |
| 174498 | + rc==SQLITE_OK && pProbe; |
| 174499 | + pProbe=(pSrc->fg.isIndexedBy||pSrc->fg.notIndexed)?0:pProbe->pNext, |
| 174500 | + iSortIdx++ |
| 174083 | 174501 | ){ |
| 174084 | 174502 | if( pProbe->pPartIdxWhere!=0 |
| 174085 | 174503 | && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC, |
| 174086 | 174504 | pProbe->pPartIdxWhere) |
| 174087 | 174505 | ){ |
| | @@ -174797,10 +175215,12 @@ |
| 174797 | 175215 | if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK; |
| 174798 | 175216 | |
| 174799 | 175217 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 174800 | 175218 | if( (pTerm->eOperator & WO_OR)!=0 |
| 174801 | 175219 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 175220 | + && (!ExprHasProperty(pTerm->pExpr, EP_OuterON) |
| 175221 | + || pTerm->pExpr->w.iJoin==iCur) |
| 174802 | 175222 | ){ |
| 174803 | 175223 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 174804 | 175224 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 174805 | 175225 | WhereTerm *pOrTerm; |
| 174806 | 175226 | int once = 1; |
| | @@ -178662,15 +179082,20 @@ |
| 178662 | 179082 | assert( p!=0 ); |
| 178663 | 179083 | assert( p->pWin!=0 ); |
| 178664 | 179084 | |
| 178665 | 179085 | /* If this function is being called from within a scalar sub-select |
| 178666 | 179086 | ** that used by the SELECT statement being processed, only process |
| 178667 | | - ** TK_COLUMN expressions that refer to it (the outer SELECT). Do |
| 178668 | | - ** not process aggregates or window functions at all, as they belong |
| 178669 | | - ** to the scalar sub-select. */ |
| 179087 | + ** TK_COLUMN and TK_AGGREGATE expressions that refer to it (the outer |
| 179088 | + ** SELECT). Do not process window functions at all, as they belong to the |
| 179089 | + ** scalar sub-select. */ |
| 178670 | 179090 | if( p->pSubSelect ){ |
| 178671 | | - if( pExpr->op!=TK_COLUMN ){ |
| 179091 | + if( pExpr->op==TK_AGG_FUNCTION ){ |
| 179092 | + /* If op2==walkerDepth, then this expression aggregates over the rows |
| 179093 | + ** of the SELECT being processed, even though it is within a scalar |
| 179094 | + ** sub-select. In that case proceed, otherwise skip this node. */ |
| 179095 | + if( pExpr->op2!=pWalker->walkerDepth ) return WRC_Continue; |
| 179096 | + }else if( pExpr->op!=TK_COLUMN ){ |
| 178672 | 179097 | return WRC_Continue; |
| 178673 | 179098 | }else{ |
| 178674 | 179099 | int nSrc = p->pSrc->nSrc; |
| 178675 | 179100 | int i; |
| 178676 | 179101 | for(i=0; i<nSrc; i++){ |
| | @@ -178747,11 +179172,13 @@ |
| 178747 | 179172 | Select *pSave = p->pSubSelect; |
| 178748 | 179173 | if( pSave==pSelect ){ |
| 178749 | 179174 | return WRC_Continue; |
| 178750 | 179175 | }else{ |
| 178751 | 179176 | p->pSubSelect = pSelect; |
| 179177 | + pWalker->walkerDepth++; |
| 178752 | 179178 | sqlite3WalkSelect(pWalker, pSelect); |
| 179179 | + pWalker->walkerDepth--; |
| 178753 | 179180 | p->pSubSelect = pSave; |
| 178754 | 179181 | } |
| 178755 | 179182 | return WRC_Prune; |
| 178756 | 179183 | } |
| 178757 | 179184 | |
| | @@ -178851,12 +179278,19 @@ |
| 178851 | 179278 | pExpr->op2++; |
| 178852 | 179279 | } |
| 178853 | 179280 | return WRC_Continue; |
| 178854 | 179281 | } |
| 178855 | 179282 | |
| 179283 | +/* |
| 179284 | +** Walker callback used to detect aggregate functions in the ORDER BY |
| 179285 | +** clause of an otherwise non-aggregate SELECT. |
| 179286 | +*/ |
| 178856 | 179287 | static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){ |
| 178857 | | - if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){ |
| 179288 | + if( pExpr->op==TK_AGG_FUNCTION |
| 179289 | + && pExpr->pAggInfo==0 |
| 179290 | + && pExpr->op2==pWalker->walkerDepth |
| 179291 | + ){ |
| 178858 | 179292 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 178859 | 179293 | sqlite3ErrorMsg(pWalker->pParse, |
| 178860 | 179294 | "misuse of aggregate: %s()", pExpr->u.zToken); |
| 178861 | 179295 | } |
| 178862 | 179296 | return WRC_Continue; |
| | @@ -178899,11 +179333,13 @@ |
| 178899 | 179333 | } |
| 178900 | 179334 | sqlite3AggInfoPersistWalkerInit(&w, pParse); |
| 178901 | 179335 | sqlite3WalkSelect(&w, p); |
| 178902 | 179336 | if( (p->selFlags & SF_Aggregate)==0 ){ |
| 178903 | 179337 | w.xExprCallback = disallowAggregatesInOrderByCb; |
| 178904 | | - w.xSelectCallback = 0; |
| 179338 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 179339 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 179340 | + w.walkerDepth = 0; |
| 178905 | 179341 | sqlite3WalkExprList(&w, p->pOrderBy); |
| 178906 | 179342 | } |
| 178907 | 179343 | |
| 178908 | 179344 | p->pSrc = 0; |
| 178909 | 179345 | p->pWhere = 0; |
| | @@ -181273,11 +181709,13 @@ |
| 181273 | 181709 | case TK_INTEGER: |
| 181274 | 181710 | case TK_STRING: |
| 181275 | 181711 | case TK_FLOAT: |
| 181276 | 181712 | case TK_BLOB: |
| 181277 | 181713 | sqlite3ExprDeferredDelete(pParse, pLeft); |
| 181278 | | - return sqlite3ExprInt32(pParse->db, op==TK_NOTNULL); |
| 181714 | + p = sqlite3ExprInt32(pParse->db, op==TK_NOTNULL); |
| 181715 | + if( p ) p->op2 = 1; |
| 181716 | + return p; |
| 181279 | 181717 | default: |
| 181280 | 181718 | break; |
| 181281 | 181719 | } |
| 181282 | 181720 | return sqlite3PExpr(pParse, op, pLeft, 0); |
| 181283 | 181721 | } |
| | @@ -191632,10 +192070,11 @@ |
| 191632 | 192070 | SQLITE_MAX_TRIGGER_DEPTH, |
| 191633 | 192071 | SQLITE_MAX_WORKER_THREADS, |
| 191634 | 192072 | SQLITE_MAX_PARSER_DEPTH, |
| 191635 | 192073 | SQLITE_MAX_SCHEMA, |
| 191636 | 192074 | SQLITE_MAX_TRIGGER_STEPS, |
| 192075 | + SQLITE_MAX_CTES, |
| 191637 | 192076 | }; |
| 191638 | 192077 | |
| 191639 | 192078 | /* |
| 191640 | 192079 | ** Make sure the hard limits are set to reasonable values |
| 191641 | 192080 | */ |
| | @@ -191719,11 +192158,12 @@ |
| 191719 | 192158 | assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); |
| 191720 | 192159 | assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); |
| 191721 | 192160 | assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS ); |
| 191722 | 192161 | assert( aHardLimit[SQLITE_LIMIT_SCHEMA]==SQLITE_MAX_SCHEMA ); |
| 191723 | 192162 | assert( aHardLimit[SQLITE_LIMIT_TRIGGER_STEPS]==SQLITE_MAX_TRIGGER_STEPS ); |
| 191724 | | - assert( SQLITE_LIMIT_TRIGGER_STEPS==(SQLITE_N_LIMIT-1) ); |
| 192163 | + assert( aHardLimit[SQLITE_LIMIT_CTES]==SQLITE_MAX_CTES ); |
| 192164 | + assert( SQLITE_LIMIT_CTES==(SQLITE_N_LIMIT-1) ); |
| 191725 | 192165 | |
| 191726 | 192166 | |
| 191727 | 192167 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
| 191728 | 192168 | return -1; |
| 191729 | 192169 | } |
| | @@ -192834,13 +193274,18 @@ |
| 192834 | 193274 | int rc; |
| 192835 | 193275 | pVfs = sqlite3_vfs_find(0); |
| 192836 | 193276 | if( pVfs==0 ) return 0; |
| 192837 | 193277 | |
| 192838 | 193278 | /* This function works in milliseconds, but the underlying OsSleep() |
| 192839 | | - ** API uses microseconds. Hence the 1000's. |
| 193279 | + ** API uses microseconds. Hence the range limitation and the "*1000". |
| 192840 | 193280 | */ |
| 192841 | | - rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000); |
| 193281 | + if( ms<0 ){ |
| 193282 | + ms = 0; |
| 193283 | + }else if( ms>2147483 ){ |
| 193284 | + ms = 2147483; /* Maximum sleep time of 35 minutes and 47.484 seconds */ |
| 193285 | + } |
| 193286 | + rc = sqlite3OsSleep(pVfs, ms*1000)/1000; |
| 192842 | 193287 | return rc; |
| 192843 | 193288 | } |
| 192844 | 193289 | |
| 192845 | 193290 | /* |
| 192846 | 193291 | ** Enable or disable the extended result codes. |
| | @@ -217559,11 +218004,16 @@ |
| 217559 | 218004 | for(iIn=iOut=0; iIn<sz; iIn++){ |
| 217560 | 218005 | char c = z[iIn]; |
| 217561 | 218006 | if( c=='\\' ){ |
| 217562 | 218007 | u32 v; |
| 217563 | 218008 | u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v); |
| 217564 | | - if( v<=0x7f ){ |
| 218009 | + if( v==0 && iIn+szEscape==sz |
| 218010 | + && (z[iIn+1]=='\n' || z[iIn+1]=='\r' || (u8)z[iIn+1]==0xe2) |
| 218011 | + ){ |
| 218012 | + /* A JSON5 line continuation at the end of the string |
| 218013 | + ** contributes no characters */ |
| 218014 | + }else if( v<=0x7f ){ |
| 217565 | 218015 | zOut[iOut++] = (char)v; |
| 217566 | 218016 | }else if( v<=0x7ff ){ |
| 217567 | 218017 | assert( szEscape>=2 ); |
| 217568 | 218018 | zOut[iOut++] = (char)(0xc0 | (v>>6)); |
| 217569 | 218019 | zOut[iOut++] = 0x80 | (v&0x3f); |
| | @@ -221778,10 +222228,14 @@ |
| 221778 | 222228 | |
| 221779 | 222229 | if( rc ) return rc; |
| 221780 | 222230 | if( NEVER(p==0) ) return SQLITE_OK; |
| 221781 | 222231 | if( p->iCell>=NCELL(pNode) ) return SQLITE_ABORT; |
| 221782 | 222232 | if( i==0 ){ |
| 222233 | + /* If this is an UPDATE that does not assign a value to the first |
| 222234 | + ** column, leave it as a "no-change" value. This allows rtreeUpdate() |
| 222235 | + ** to take the new rowid from aData[1] instead. */ |
| 222236 | + if( sqlite3_vtab_nochange(ctx) ) return SQLITE_OK; |
| 221783 | 222237 | sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell)); |
| 221784 | 222238 | }else if( i<=pRtree->nDim2 ){ |
| 221785 | 222239 | nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c); |
| 221786 | 222240 | #ifndef SQLITE_RTREE_INT_ONLY |
| 221787 | 222241 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| | @@ -221875,10 +222329,24 @@ |
| 221875 | 222329 | pCons->pInfo = pInfo; |
| 221876 | 222330 | return SQLITE_OK; |
| 221877 | 222331 | } |
| 221878 | 222332 | |
| 221879 | 222333 | SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double); |
| 222334 | + |
| 222335 | +/* |
| 222336 | +** Argument pVal is the right-hand side of a "rowid = ?" constraint. If |
| 222337 | +** there is an integer that compares equal to pVal, set *piRowid to it and |
| 222338 | +** return 1. Otherwise - pVal is NULL, a blob, a non-numeric text value or |
| 222339 | +** a real value that is not an integer - return 0. |
| 222340 | +*/ |
| 222341 | +static int rtreeRowidEqValue(sqlite3_value *pVal, i64 *piRowid){ |
| 222342 | + int eType = sqlite3_value_numeric_type(pVal); |
| 222343 | + *piRowid = sqlite3_value_int64(pVal); |
| 222344 | + return eType==SQLITE_INTEGER |
| 222345 | + || (eType==SQLITE_FLOAT |
| 222346 | + && 0==sqlite3IntFloatCompare(*piRowid, sqlite3_value_double(pVal))); |
| 222347 | +} |
| 221880 | 222348 | |
| 221881 | 222349 | /* |
| 221882 | 222350 | ** Rtree virtual table module xFilter method. |
| 221883 | 222351 | */ |
| 221884 | 222352 | static int rtreeFilter( |
| | @@ -221901,17 +222369,13 @@ |
| 221901 | 222369 | pCsr->iStrategy = idxNum; |
| 221902 | 222370 | if( idxNum==1 ){ |
| 221903 | 222371 | /* Special case - lookup by rowid. */ |
| 221904 | 222372 | RtreeNode *pLeaf; /* Leaf on which the required cell resides */ |
| 221905 | 222373 | RtreeSearchPoint *p; /* Search point for the leaf */ |
| 221906 | | - i64 iRowid = sqlite3_value_int64(argv[0]); |
| 222374 | + i64 iRowid; |
| 221907 | 222375 | i64 iNode = 0; |
| 221908 | | - int eType = sqlite3_value_numeric_type(argv[0]); |
| 221909 | | - if( eType==SQLITE_INTEGER |
| 221910 | | - || (eType==SQLITE_FLOAT |
| 221911 | | - && 0==sqlite3IntFloatCompare(iRowid,sqlite3_value_double(argv[0]))) |
| 221912 | | - ){ |
| 222376 | + if( rtreeRowidEqValue(argv[0], &iRowid) ){ |
| 221913 | 222377 | rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode); |
| 221914 | 222378 | }else{ |
| 221915 | 222379 | rc = SQLITE_OK; |
| 221916 | 222380 | pLeaf = 0; |
| 221917 | 222381 | } |
| | @@ -223164,10 +223628,11 @@ |
| 223164 | 223628 | ** conflict-handling mode specified by the user. |
| 223165 | 223629 | */ |
| 223166 | 223630 | if( nData>1 ){ |
| 223167 | 223631 | int ii; |
| 223168 | 223632 | int nn = nData - 4; |
| 223633 | + sqlite3_value *pNewRowid; /* Value of the new rowid, or NULL */ |
| 223169 | 223634 | |
| 223170 | 223635 | if( nn > pRtree->nDim2 ) nn = pRtree->nDim2; |
| 223171 | 223636 | /* Populate the cell.aCoord[] array. The first coordinate is aData[3]. |
| 223172 | 223637 | ** |
| 223173 | 223638 | ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared |
| | @@ -223197,15 +223662,29 @@ |
| 223197 | 223662 | rc = rtreeConstraintError(pRtree, ii+1); |
| 223198 | 223663 | goto constraint; |
| 223199 | 223664 | } |
| 223200 | 223665 | } |
| 223201 | 223666 | } |
| 223667 | + |
| 223668 | + /* The new rowid is normally the value of the first column, aData[2]. |
| 223669 | + ** But SQLite does not treat that column as an alias for the rowid, |
| 223670 | + ** so a new rowid assigned using the name "rowid", "oid" or "_rowid_" |
| 223671 | + ** is passed in aData[1]. Use aData[1] if aData[2] is a "no-change" |
| 223672 | + ** value (an UPDATE that does not assign the first column - see |
| 223673 | + ** rtreeColumn()), or if aData[2] is NULL in an INSERT. */ |
| 223674 | + pNewRowid = aData[2]; |
| 223675 | + if( sqlite3_value_nochange(pNewRowid) |
| 223676 | + || (sqlite3_value_type(aData[0])==SQLITE_NULL |
| 223677 | + && sqlite3_value_type(pNewRowid)==SQLITE_NULL) |
| 223678 | + ){ |
| 223679 | + pNewRowid = aData[1]; |
| 223680 | + } |
| 223202 | 223681 | |
| 223203 | 223682 | /* If a rowid value was supplied, check if it is already present in |
| 223204 | 223683 | ** the table. If so, the constraint has failed. */ |
| 223205 | | - if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){ |
| 223206 | | - cell.iRowid = sqlite3_value_int64(aData[2]); |
| 223684 | + if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL ){ |
| 223685 | + cell.iRowid = sqlite3_value_int64(pNewRowid); |
| 223207 | 223686 | if( sqlite3_value_type(aData[0])==SQLITE_NULL |
| 223208 | 223687 | || sqlite3_value_int64(aData[0])!=cell.iRowid |
| 223209 | 223688 | ){ |
| 223210 | 223689 | int steprc; |
| 223211 | 223690 | sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid); |
| | @@ -224621,10 +225100,12 @@ |
| 224621 | 225100 | return pOut; |
| 224622 | 225101 | }else{ |
| 224623 | 225102 | s.nErr++; |
| 224624 | 225103 | rc = SQLITE_ERROR; |
| 224625 | 225104 | } |
| 225105 | + }else{ |
| 225106 | + rc = SQLITE_ERROR; |
| 224626 | 225107 | } |
| 224627 | 225108 | parse_json_err: |
| 224628 | 225109 | if( pRc ) *pRc = rc; |
| 224629 | 225110 | sqlite3_free(s.a); |
| 224630 | 225111 | return 0; |
| | @@ -224671,13 +225152,15 @@ |
| 224671 | 225152 | geopolySwab32((unsigned char*)&GeoX(p,ii)); |
| 224672 | 225153 | geopolySwab32((unsigned char*)&GeoY(p,ii)); |
| 224673 | 225154 | } |
| 224674 | 225155 | p->hdr[0] ^= 1; |
| 224675 | 225156 | } |
| 225157 | + if( pRc ) *pRc = SQLITE_OK; |
| 224676 | 225158 | } |
| 225159 | + }else{ |
| 225160 | + if( pRc ) *pRc = SQLITE_ERROR; |
| 224677 | 225161 | } |
| 224678 | | - if( pRc ) *pRc = SQLITE_OK; |
| 224679 | 225162 | return p; |
| 224680 | 225163 | }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 224681 | 225164 | const unsigned char *zJson = sqlite3_value_text(pVal); |
| 224682 | 225165 | if( zJson==0 ){ |
| 224683 | 225166 | if( pRc ) *pRc = SQLITE_NOMEM; |
| | @@ -225728,13 +226211,17 @@ |
| 225728 | 226211 | pCsr->iStrategy = idxNum; |
| 225729 | 226212 | if( idxNum==1 ){ |
| 225730 | 226213 | /* Special case - lookup by rowid. */ |
| 225731 | 226214 | RtreeNode *pLeaf; /* Leaf on which the required cell resides */ |
| 225732 | 226215 | RtreeSearchPoint *p; /* Search point for the leaf */ |
| 225733 | | - i64 iRowid = sqlite3_value_int64(argv[0]); |
| 226216 | + i64 iRowid; |
| 225734 | 226217 | i64 iNode = 0; |
| 225735 | | - rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode); |
| 226218 | + if( rtreeRowidEqValue(argv[0], &iRowid) ){ |
| 226219 | + rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode); |
| 226220 | + }else{ |
| 226221 | + pLeaf = 0; |
| 226222 | + } |
| 225736 | 226223 | if( rc==SQLITE_OK && pLeaf!=0 ){ |
| 225737 | 226224 | p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0); |
| 225738 | 226225 | assert( p!=0 ); /* Always returns pCsr->sPoint */ |
| 225739 | 226226 | pCsr->aNode[0] = pLeaf; |
| 225740 | 226227 | p->id = iNode; |
| | @@ -225960,10 +226447,12 @@ |
| 225960 | 226447 | i64 oldRowid; /* The old rowid */ |
| 225961 | 226448 | int oldRowidValid; /* True if oldRowid is valid */ |
| 225962 | 226449 | i64 newRowid; /* The new rowid */ |
| 225963 | 226450 | int newRowidValid; /* True if newRowid is valid */ |
| 225964 | 226451 | int coordChange = 0; /* Change in coordinates */ |
| 226452 | + sqlite3_value *pShape = 0; /* New _shape value */ |
| 226453 | + sqlite3_value *pOldShape = 0; /* Copy of the unchanged _shape */ |
| 225965 | 226454 | |
| 225966 | 226455 | if( pRtree->nNodeRef ){ |
| 225967 | 226456 | /* Unable to write to the btree while another cursor is reading from it, |
| 225968 | 226457 | ** since the write might do a rebalance which would disrupt the read |
| 225969 | 226458 | ** cursor. */ |
| | @@ -225975,18 +226464,43 @@ |
| 225975 | 226464 | oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;; |
| 225976 | 226465 | oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0; |
| 225977 | 226466 | newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL; |
| 225978 | 226467 | newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0; |
| 225979 | 226468 | cell.iRowid = newRowid; |
| 226469 | + |
| 226470 | + if( nData>1 ){ |
| 226471 | + pShape = aData[2]; |
| 226472 | + if( oldRowidValid && oldRowid!=newRowid && sqlite3_value_nochange(pShape) ){ |
| 226473 | + /* The rowid changes but _shape does not. aData[2] holds no value in |
| 226474 | + ** this case, so read the current _shape from the %_rowid table. It |
| 226475 | + ** is needed for the bounding box of the new r-tree entry and for the |
| 226476 | + ** %_rowid record of the new rowid. */ |
| 226477 | + sqlite3_stmt *pRead = 0; |
| 226478 | + rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0, |
| 226479 | + &pRead, 0); |
| 226480 | + if( rc==SQLITE_OK ){ |
| 226481 | + int rc2; |
| 226482 | + sqlite3_bind_int64(pRead, 1, oldRowid); |
| 226483 | + if( sqlite3_step(pRead)==SQLITE_ROW ){ |
| 226484 | + pOldShape = sqlite3_value_dup(sqlite3_column_value(pRead, 2)); |
| 226485 | + if( pOldShape==0 ) rc = SQLITE_NOMEM; |
| 226486 | + pShape = pOldShape; |
| 226487 | + } |
| 226488 | + rc2 = sqlite3_finalize(pRead); |
| 226489 | + if( rc==SQLITE_OK ) rc = rc2; |
| 226490 | + } |
| 226491 | + if( rc ) goto geopoly_update_end; |
| 226492 | + } |
| 226493 | + } |
| 225980 | 226494 | |
| 225981 | 226495 | if( nData>1 /* not a DELETE */ |
| 225982 | 226496 | && (!oldRowidValid /* INSERT */ |
| 225983 | | - || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */ |
| 226497 | + || !sqlite3_value_nochange(pShape) /* UPDATE _shape */ |
| 225984 | 226498 | || oldRowid!=newRowid) /* Rowid change */ |
| 225985 | 226499 | ){ |
| 225986 | | - assert( aData[2]!=0 ); |
| 225987 | | - geopolyBBox(0, aData[2], cell.aCoord, &rc); |
| 226500 | + assert( pShape!=0 ); |
| 226501 | + geopolyBBox(0, pShape, cell.aCoord, &rc); |
| 225988 | 226502 | if( rc ){ |
| 225989 | 226503 | if( rc==SQLITE_ERROR ){ |
| 225990 | 226504 | pVtab->zErrMsg = |
| 225991 | 226505 | sqlite3_mprintf("_shape does not contain a valid polygon"); |
| 225992 | 226506 | } |
| | @@ -226048,21 +226562,21 @@ |
| 226048 | 226562 | sqlite3_stmt *pUp = pRtree->pWriteAux; |
| 226049 | 226563 | int jj; |
| 226050 | 226564 | int nChange = 0; |
| 226051 | 226565 | sqlite3_bind_int64(pUp, 1, cell.iRowid); |
| 226052 | 226566 | assert( pRtree->nAux>=1 ); |
| 226053 | | - if( sqlite3_value_nochange(aData[2]) ){ |
| 226567 | + if( sqlite3_value_nochange(pShape) ){ |
| 226054 | 226568 | sqlite3_bind_null(pUp, 2); |
| 226055 | 226569 | }else{ |
| 226056 | 226570 | GeoPoly *p = 0; |
| 226057 | | - if( sqlite3_value_type(aData[2])==SQLITE_TEXT |
| 226058 | | - && (p = geopolyFuncParam(0, aData[2], &rc))!=0 |
| 226571 | + if( sqlite3_value_type(pShape)==SQLITE_TEXT |
| 226572 | + && (p = geopolyFuncParam(0, pShape, &rc))!=0 |
| 226059 | 226573 | && rc==SQLITE_OK |
| 226060 | 226574 | ){ |
| 226061 | 226575 | sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT); |
| 226062 | 226576 | }else{ |
| 226063 | | - sqlite3_bind_value(pUp, 2, aData[2]); |
| 226577 | + sqlite3_bind_value(pUp, 2, pShape); |
| 226064 | 226578 | } |
| 226065 | 226579 | sqlite3_free(p); |
| 226066 | 226580 | nChange = 1; |
| 226067 | 226581 | } |
| 226068 | 226582 | for(jj=1; jj<nData-2; jj++){ |
| | @@ -226074,10 +226588,11 @@ |
| 226074 | 226588 | rc = sqlite3_reset(pUp); |
| 226075 | 226589 | } |
| 226076 | 226590 | } |
| 226077 | 226591 | |
| 226078 | 226592 | geopoly_update_end: |
| 226593 | + sqlite3_value_free(pOldShape); |
| 226079 | 226594 | rtreeRelease(pRtree); |
| 226080 | 226595 | return rc; |
| 226081 | 226596 | } |
| 226082 | 226597 | |
| 226083 | 226598 | /* |
| | @@ -230763,11 +231278,11 @@ |
| 230763 | 231278 | int bOpen = 0; |
| 230764 | 231279 | int rc; |
| 230765 | 231280 | p->nRbu = 0; |
| 230766 | 231281 | p->pRbuFd = 0; |
| 230767 | 231282 | rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p); |
| 230768 | | - if( rc!=SQLITE_NOTFOUND ) p->rc = rc; |
| 231283 | + if( rc!=SQLITE_NOTFOUND || p->pRbuFd==0 ) p->rc = rc; |
| 230769 | 231284 | if( p->eStage>=RBU_STAGE_MOVE ){ |
| 230770 | 231285 | bOpen = 1; |
| 230771 | 231286 | }else{ |
| 230772 | 231287 | RbuState *pState = rbuLoadState(p); |
| 230773 | 231288 | if( pState ){ |
| | @@ -235257,11 +235772,11 @@ |
| 235257 | 235772 | for(i=0; i<nData; i++){ |
| 235258 | 235773 | sz += ((struct iovec*)aData)[i].iov_len; |
| 235259 | 235774 | } |
| 235260 | 235775 | } |
| 235261 | 235776 | |
| 235262 | | - pNew->aData = sqlite3_malloc64( sz ); |
| 235777 | + pNew->aData = sqlite3_malloc64( sz>0 ? sz : 1 ); |
| 235263 | 235778 | if( pNew->aData==0 ){ |
| 235264 | 235779 | rc = SQLITE_NOMEM; |
| 235265 | 235780 | goto carray_bind_error; |
| 235266 | 235781 | } |
| 235267 | 235782 | |
| | @@ -235288,11 +235803,11 @@ |
| 235288 | 235803 | p[i].iov_len = n; |
| 235289 | 235804 | p[i].iov_base = z; |
| 235290 | 235805 | z += n; |
| 235291 | 235806 | memcpy(p[i].iov_base, ((struct iovec*)aData)[i].iov_base, n); |
| 235292 | 235807 | } |
| 235293 | | - }else{ |
| 235808 | + }else if( sz>0 ){ |
| 235294 | 235809 | memcpy(pNew->aData, aData, sz); |
| 235295 | 235810 | } |
| 235296 | 235811 | pNew->xDel = sqlite3_free; |
| 235297 | 235812 | pNew->pDel = pNew->aData; |
| 235298 | 235813 | }else{ |
| | @@ -241551,11 +242066,11 @@ |
| 241551 | 242066 | } |
| 241552 | 242067 | pNew->nRecord = pOut - pNew->aRecord; |
| 241553 | 242068 | } |
| 241554 | 242069 | }else if( bRebase ){ |
| 241555 | 242070 | if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){ |
| 241556 | | - *ppNew = pExist; |
| 242071 | + pNew = pExist; |
| 241557 | 242072 | }else{ |
| 241558 | 242073 | sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange); |
| 241559 | 242074 | pNew = (SessionChange*)sqlite3_malloc64(nByte); |
| 241560 | 242075 | if( pNew==0 ){ |
| 241561 | 242076 | rc = SQLITE_NOMEM; |
| | @@ -242404,12 +242919,12 @@ |
| 242404 | 242919 | } |
| 242405 | 242920 | if( bData ){ |
| 242406 | 242921 | a2 = aChange; |
| 242407 | 242922 | for(i=0; i<pIter->nCol; i++){ |
| 242408 | 242923 | int n1 = sessionSerialLen(a1); |
| 242409 | | - int n2 = sessionSerialLen(a2); |
| 242410 | | - if( pIter->abPK[i] || a2[0]!=0xFF ){ |
| 242924 | + int n2 = (a2>=a2Eof) ? 0 : sessionSerialLen(a2); |
| 242925 | + if( pIter->abPK[i] || n2<=0 || a2[0]!=0xFF ){ |
| 242411 | 242926 | memcpy(pOut, a1, n1); |
| 242412 | 242927 | pOut += n1; |
| 242413 | 242928 | }else{ |
| 242414 | 242929 | *pOut++ = '\0'; |
| 242415 | 242930 | } |
| | @@ -242463,10 +242978,13 @@ |
| 242463 | 242978 | bNew = 0; |
| 242464 | 242979 | |
| 242465 | 242980 | /* A patchset may not be rebased */ |
| 242466 | 242981 | if( pIter->bPatchset ){ |
| 242467 | 242982 | rc = SQLITE_ERROR; |
| 242983 | + } |
| 242984 | + if( pTab && pTab->nCol>pIter->nCol ){ |
| 242985 | + rc = SQLITE_SCHEMA; |
| 242468 | 242986 | } |
| 242469 | 242987 | |
| 242470 | 242988 | /* Append a table header to the output for this new table */ |
| 242471 | 242989 | sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc); |
| 242472 | 242990 | sessionAppendVarint(&sOut, pIter->nCol, &rc); |
| | @@ -242560,10 +243078,11 @@ |
| 242560 | 243078 | *pnOut = sOut.nBuf; |
| 242561 | 243079 | sOut.aBuf = 0; |
| 242562 | 243080 | } |
| 242563 | 243081 | } |
| 242564 | 243082 | sqlite3_free(sOut.aBuf); |
| 243083 | + if( rc==SQLITE_OK ) rc = pIter->rc; |
| 242565 | 243084 | return rc; |
| 242566 | 243085 | } |
| 242567 | 243086 | |
| 242568 | 243087 | /* |
| 242569 | 243088 | ** Create a new rebaser object. |
| | @@ -247651,10 +248170,13 @@ |
| 247651 | 248170 | p++; |
| 247652 | 248171 | while( p ){ |
| 247653 | 248172 | if( *p=='\'' ){ |
| 247654 | 248173 | p++; |
| 247655 | 248174 | if( *p!='\'' ) break; |
| 248175 | + }else if( *p==0 ){ |
| 248176 | + p = 0; |
| 248177 | + break; |
| 247656 | 248178 | } |
| 247657 | 248179 | p++; |
| 247658 | 248180 | if( *p==0 ) p = 0; |
| 247659 | 248181 | } |
| 247660 | 248182 | break; |
| | @@ -249075,11 +249597,11 @@ |
| 249075 | 249597 | if( zText[jj]=='"' ) zExpr[iOut++] = '"'; |
| 249076 | 249598 | } |
| 249077 | 249599 | zExpr[iOut++] = '"'; |
| 249078 | 249600 | zExpr[iOut++] = ' '; |
| 249079 | 249601 | } |
| 249080 | | - if( zText[i]==aSpec[2] ){ |
| 249602 | + if( i<nText && zText[i]==aSpec[2] ){ |
| 249081 | 249603 | i += 2; |
| 249082 | 249604 | if( zText[i-1]=='^' ) i++; |
| 249083 | 249605 | while( i<nText && zText[i]!=']' ) i++; |
| 249084 | 249606 | } |
| 249085 | 249607 | iFirst = i+1; |
| | @@ -249614,10 +250136,17 @@ |
| 249614 | 250136 | |
| 249615 | 250137 | *pRc = rc; |
| 249616 | 250138 | if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){ |
| 249617 | 250139 | return 1; |
| 249618 | 250140 | } |
| 250141 | + |
| 250142 | + /* Not a match. Clear the position lists of all phrases, as some may |
| 250143 | + ** have been populated before the loop above exited early, and others |
| 250144 | + ** may still contain data left over from a previous row. */ |
| 250145 | + for(i=0; i<pNear->nPhrase; i++){ |
| 250146 | + pNear->apPhrase[i]->poslist.n = 0; |
| 250147 | + } |
| 249619 | 250148 | return 0; |
| 249620 | 250149 | } |
| 249621 | 250150 | } |
| 249622 | 250151 | |
| 249623 | 250152 | |
| | @@ -249729,10 +250258,28 @@ |
| 249729 | 250258 | for(i=0; i<pNode->nChild; i++){ |
| 249730 | 250259 | fts5ExprNodeZeroPoslist(pNode->apChild[i]); |
| 249731 | 250260 | } |
| 249732 | 250261 | } |
| 249733 | 250262 | } |
| 250263 | + |
| 250264 | +/* |
| 250265 | +** Zero the position lists of all phrases within the sub-tree headed by |
| 250266 | +** pNode that are currently positioned at rowid iRowid. Phrases positioned |
| 250267 | +** at other rowids are not modified - their position lists belong to rows |
| 250268 | +** that have not yet been tested by the parent node. |
| 250269 | +*/ |
| 250270 | +static void fts5ExprNodeZeroPoslistAt(Fts5ExprNode *pNode, i64 iRowid){ |
| 250271 | + if( pNode->iRowid!=iRowid ) return; |
| 250272 | + if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ |
| 250273 | + fts5ExprNodeZeroPoslist(pNode); |
| 250274 | + }else{ |
| 250275 | + int i; |
| 250276 | + for(i=0; i<pNode->nChild; i++){ |
| 250277 | + fts5ExprNodeZeroPoslistAt(pNode->apChild[i], iRowid); |
| 250278 | + } |
| 250279 | + } |
| 250280 | +} |
| 249734 | 250281 | |
| 249735 | 250282 | |
| 249736 | 250283 | |
| 249737 | 250284 | /* |
| 249738 | 250285 | ** Compare the values currently indicated by the two nodes as follows: |
| | @@ -250048,14 +250595,14 @@ |
| 250048 | 250595 | pAnd->bNomatch = 1; |
| 250049 | 250596 | } |
| 250050 | 250597 | } |
| 250051 | 250598 | }while( bMatch==0 ); |
| 250052 | 250599 | |
| 250600 | + pAnd->iRowid = iLast; |
| 250053 | 250601 | if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){ |
| 250054 | | - fts5ExprNodeZeroPoslist(pAnd); |
| 250602 | + fts5ExprNodeZeroPoslistAt(pAnd, iLast); |
| 250055 | 250603 | } |
| 250056 | | - pAnd->iRowid = iLast; |
| 250057 | 250604 | return SQLITE_OK; |
| 250058 | 250605 | } |
| 250059 | 250606 | |
| 250060 | 250607 | static int fts5ExprNodeNext_AND( |
| 250061 | 250608 | Fts5Expr *pExpr, |
| | @@ -250197,10 +250744,11 @@ |
| 250197 | 250744 | break; |
| 250198 | 250745 | |
| 250199 | 250746 | default: |
| 250200 | 250747 | assert( pNode->eType==FTS5_NOT ); |
| 250201 | 250748 | pNode->bEof = pNode->apChild[0]->bEof; |
| 250749 | + if( pNode->bEof ) fts5ExprNodeZeroPoslist(pNode->apChild[1]); |
| 250202 | 250750 | break; |
| 250203 | 250751 | } |
| 250204 | 250752 | } |
| 250205 | 250753 | |
| 250206 | 250754 | if( rc==SQLITE_OK ){ |
| | @@ -253453,12 +254001,11 @@ |
| 253453 | 254001 | } |
| 253454 | 254002 | if( rc!=SQLITE_OK ){ |
| 253455 | 254003 | sqlite3_free(pRet); |
| 253456 | 254004 | pRet = 0; |
| 253457 | 254005 | }else{ |
| 253458 | | - pRet->p[nByte] = 0x00; |
| 253459 | | - pRet->p[nByte+1] = 0x00; |
| 254006 | + memset(&pRet->p[nByte], 0, FTS5_DATA_PADDING); |
| 253460 | 254007 | } |
| 253461 | 254008 | } |
| 253462 | 254009 | p->rc = rc; |
| 253463 | 254010 | p->nRead++; |
| 253464 | 254011 | } |
| | @@ -255308,11 +255855,11 @@ |
| 255308 | 255855 | pIter->flags |= FTS5_SEGITER_ONETERM; |
| 255309 | 255856 | if( pIter->pLeaf ){ |
| 255310 | 255857 | if( flags & FTS5INDEX_QUERY_DESC ){ |
| 255311 | 255858 | pIter->flags |= FTS5_SEGITER_REVERSE; |
| 255312 | 255859 | } |
| 255313 | | - if( bDlidx ){ |
| 255860 | + if( bDlidx && iPg==pIter->iTermLeafPgno ){ |
| 255314 | 255861 | fts5SegIterLoadDlidx(p, pIter); |
| 255315 | 255862 | } |
| 255316 | 255863 | if( flags & FTS5INDEX_QUERY_DESC ){ |
| 255317 | 255864 | fts5SegIterReverse(p, pIter); |
| 255318 | 255865 | } |
| | @@ -255707,10 +256254,17 @@ |
| 255707 | 256254 | }else{ |
| 255708 | 256255 | iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid); |
| 255709 | 256256 | pIter->iLeafOffset = iOff; |
| 255710 | 256257 | fts5SegIterLoadNPos(p, pIter); |
| 255711 | 256258 | } |
| 256259 | + break; |
| 256260 | + }else if( !fts5LeafIsTermless(pIter->pLeaf) ){ |
| 256261 | + /* There are no rowids on this page, but there is another term. |
| 256262 | + ** Since there are no more rowids for the current term, set the |
| 256263 | + ** iterator to EOF. */ |
| 256264 | + fts5DataRelease(pIter->pLeaf); |
| 256265 | + pIter->pLeaf = 0; |
| 255712 | 256266 | break; |
| 255713 | 256267 | } |
| 255714 | 256268 | } |
| 255715 | 256269 | } |
| 255716 | 256270 | } |
| | @@ -259800,15 +260354,13 @@ |
| 259800 | 260354 | for(ii=0; ii<pT->nIter; ii++){ |
| 259801 | 260355 | Fts5Iter *p = pT->apIter[ii]; |
| 259802 | 260356 | if( p->base.bEof==0 |
| 259803 | 260357 | && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom)) |
| 259804 | 260358 | ){ |
| 259805 | | - fts5MultiIterNext(pIndex, p, bFrom, iFrom); |
| 259806 | | - while( bFrom && p->base.bEof==0 |
| 259807 | | - && p->base.iRowid<iFrom |
| 259808 | | - && pIndex->rc==SQLITE_OK |
| 259809 | | - ){ |
| 260359 | + if( bFrom ){ |
| 260360 | + fts5MultiIterNextFrom(pIndex, p, iFrom); |
| 260361 | + }else{ |
| 259810 | 260362 | fts5MultiIterNext(pIndex, p, 0, 0); |
| 259811 | 260363 | } |
| 259812 | 260364 | } |
| 259813 | 260365 | } |
| 259814 | 260366 | |
| | @@ -260000,10 +260552,19 @@ |
| 260000 | 260552 | ** queries performed as part of an integrity-check, or by the fts5vocab |
| 260001 | 260553 | ** module. */ |
| 260002 | 260554 | if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){ |
| 260003 | 260555 | bTokendata = 0; |
| 260004 | 260556 | } |
| 260557 | + |
| 260558 | + /* fts5SetupTokendataIter() flushes the in-memory hash table to disk. |
| 260559 | + ** The flush frees the cached Fts5Structure object, which iterators |
| 260560 | + ** opened earlier by this function point into without holding a |
| 260561 | + ** reference (for example one opened for a prefix index query in the |
| 260562 | + ** same MATCH expression). So, on a tokendata=1 table, flush first. */ |
| 260563 | + if( pConfig->bTokendata ){ |
| 260564 | + fts5IndexFlush(p); |
| 260565 | + } |
| 260005 | 260566 | |
| 260006 | 260567 | /* Figure out which index to search and set iIdx accordingly. If this |
| 260007 | 260568 | ** is a prefix query for which there is no prefix index, set iIdx to |
| 260008 | 260569 | ** greater than pConfig->nPrefix to indicate that the query will be |
| 260009 | 260570 | ** satisfied by scanning multiple terms in the main index. |
| | @@ -262818,11 +263379,13 @@ |
| 262818 | 263379 | } |
| 262819 | 263380 | pInfo->aConstraintUsage[i].argvIndex = ++iCons; |
| 262820 | 263381 | pInfo->aConstraintUsage[i].omit = 1; |
| 262821 | 263382 | } |
| 262822 | 263383 | }else if( p->usable ){ |
| 262823 | | - if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){ |
| 263384 | + if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) |
| 263385 | + && pConfig->abUnindexed[iCol]==0 |
| 263386 | + ){ |
| 262824 | 263387 | assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB ); |
| 262825 | 263388 | idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G'; |
| 262826 | 263389 | sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol); |
| 262827 | 263390 | idxStr += strlen(&idxStr[iIdxStr]); |
| 262828 | 263391 | pInfo->aConstraintUsage[i].argvIndex = ++iCons; |
| | @@ -262830,11 +263393,10 @@ |
| 262830 | 263393 | nSeenMatch++; |
| 262831 | 263394 | }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){ |
| 262832 | 263395 | idxStr[iIdxStr++] = '='; |
| 262833 | 263396 | bSeenEq = 1; |
| 262834 | 263397 | pInfo->aConstraintUsage[i].argvIndex = ++iCons; |
| 262835 | | - pInfo->aConstraintUsage[i].omit = 1; |
| 262836 | 263398 | } |
| 262837 | 263399 | } |
| 262838 | 263400 | } |
| 262839 | 263401 | |
| 262840 | 263402 | if( bSeenEq==0 ){ |
| | @@ -264481,19 +265043,21 @@ |
| 264481 | 265043 | if( aPopulator==0 ) rc = SQLITE_NOMEM; |
| 264482 | 265044 | if( rc==SQLITE_OK ){ |
| 264483 | 265045 | rc = fts5SeekCursor(pCsr, 0); |
| 264484 | 265046 | } |
| 264485 | 265047 | for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){ |
| 264486 | | - const char *z = 0; |
| 264487 | | - int n = 0; |
| 264488 | | - rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n); |
| 264489 | | - if( rc==SQLITE_OK ){ |
| 264490 | | - rc = sqlite3Fts5ExprPopulatePoslists( |
| 264491 | | - pConfig, pCsr->pExpr, aPopulator, i, z, n |
| 264492 | | - ); |
| 264493 | | - } |
| 264494 | | - sqlite3Fts5ClearLocale(pConfig); |
| 265048 | + if( pConfig->abUnindexed[i]==0 ){ |
| 265049 | + const char *z = 0; |
| 265050 | + int n = 0; |
| 265051 | + rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n); |
| 265052 | + if( rc==SQLITE_OK ){ |
| 265053 | + rc = sqlite3Fts5ExprPopulatePoslists( |
| 265054 | + pConfig, pCsr->pExpr, aPopulator, i, z, n |
| 265055 | + ); |
| 265056 | + } |
| 265057 | + sqlite3Fts5ClearLocale(pConfig); |
| 265058 | + } |
| 264495 | 265059 | } |
| 264496 | 265060 | sqlite3_free(aPopulator); |
| 264497 | 265061 | |
| 264498 | 265062 | if( pCsr->pSorter ){ |
| 264499 | 265063 | sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid); |
| | @@ -265771,11 +266335,11 @@ |
| 265771 | 266335 | int nArg, /* Number of args */ |
| 265772 | 266336 | sqlite3_value **apUnused /* Function arguments */ |
| 265773 | 266337 | ){ |
| 265774 | 266338 | assert( nArg==0 ); |
| 265775 | 266339 | UNUSED_PARAM2(nArg, apUnused); |
| 265776 | | - sqlite3_result_text(pCtx, "fts5: 2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918", -1, SQLITE_TRANSIENT); |
| 266340 | + sqlite3_result_text(pCtx, "fts5: 2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b", -1, SQLITE_TRANSIENT); |
| 265777 | 266341 | } |
| 265778 | 266342 | |
| 265779 | 266343 | /* |
| 265780 | 266344 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 265781 | 266345 | ** |
| | @@ -266131,13 +266695,15 @@ |
| 266131 | 266695 | */ |
| 266132 | 266696 | static int fts5StorageGetStmt( |
| 266133 | 266697 | Fts5Storage *p, /* Storage handle */ |
| 266134 | 266698 | int eStmt, /* FTS5_STMT_XXX constant */ |
| 266135 | 266699 | sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */ |
| 266136 | | - char **pzErrMsg /* OUT: Error message (if any) */ |
| 266700 | + char **pzErrMsg /* OUT: Error message (if any) (not used) */ |
| 266137 | 266701 | ){ |
| 266138 | 266702 | int rc = SQLITE_OK; |
| 266703 | + |
| 266704 | + (void)pzErrMsg; |
| 266139 | 266705 | |
| 266140 | 266706 | /* If there is no %_docsize table, there should be no requests for |
| 266141 | 266707 | ** statements to operate on it. */ |
| 266142 | 266708 | assert( p->pConfig->bColumnsize || ( |
| 266143 | 266709 | eStmt!=FTS5_STMT_REPLACE_DOCSIZE |
| | @@ -266310,15 +266876,19 @@ |
| 266310 | 266876 | rc = fts5ExecPrintf(pConfig->db, 0, |
| 266311 | 266877 | "DROP TABLE IF EXISTS %Q.'%q_docsize';", |
| 266312 | 266878 | pConfig->zDb, pConfig->zName |
| 266313 | 266879 | ); |
| 266314 | 266880 | } |
| 266315 | | - if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){ |
| 266316 | | - rc = fts5ExecPrintf(pConfig->db, 0, |
| 266317 | | - "DROP TABLE IF EXISTS %Q.'%q_content';", |
| 266318 | | - pConfig->zDb, pConfig->zName |
| 266319 | | - ); |
| 266881 | + if( rc==SQLITE_OK ){ |
| 266882 | + if( pConfig->eContent==FTS5_CONTENT_NORMAL |
| 266883 | + || pConfig->bContentlessUnindexed |
| 266884 | + ){ |
| 266885 | + rc = fts5ExecPrintf(pConfig->db, 0, |
| 266886 | + "DROP TABLE IF EXISTS %Q.'%q_content';", |
| 266887 | + pConfig->zDb, pConfig->zName |
| 266888 | + ); |
| 266889 | + } |
| 266320 | 266890 | } |
| 266321 | 266891 | return rc; |
| 266322 | 266892 | } |
| 266323 | 266893 | |
| 266324 | 266894 | static void fts5StorageRenameOne( |
| | @@ -266343,11 +266913,13 @@ |
| 266343 | 266913 | fts5StorageRenameOne(pConfig, &rc, "idx", zName); |
| 266344 | 266914 | fts5StorageRenameOne(pConfig, &rc, "config", zName); |
| 266345 | 266915 | if( pConfig->bColumnsize ){ |
| 266346 | 266916 | fts5StorageRenameOne(pConfig, &rc, "docsize", zName); |
| 266347 | 266917 | } |
| 266348 | | - if( pConfig->eContent==FTS5_CONTENT_NORMAL ){ |
| 266918 | + if( pConfig->eContent==FTS5_CONTENT_NORMAL |
| 266919 | + || pConfig->bContentlessUnindexed |
| 266920 | + ){ |
| 266349 | 266921 | fts5StorageRenameOne(pConfig, &rc, "content", zName); |
| 266350 | 266922 | } |
| 266351 | 266923 | return rc; |
| 266352 | 266924 | } |
| 266353 | 266925 | |
| | @@ -266423,11 +266995,11 @@ |
| 266423 | 266995 | for(i=0; i<pConfig->nCol; i++){ |
| 266424 | 266996 | if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->abUnindexed[i] ){ |
| 266425 | 266997 | sqlite3_str_appendf(pDefn, ", c%d", i); |
| 266426 | 266998 | } |
| 266427 | 266999 | } |
| 266428 | | - if( pConfig->bLocale ){ |
| 267000 | + if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL ){ |
| 266429 | 267001 | for(i=0; i<pConfig->nCol; i++){ |
| 266430 | 267002 | if( pConfig->abUnindexed[i]==0 ){ |
| 266431 | 267003 | sqlite3_str_appendf(pDefn, ", l%d", i); |
| 266432 | 267004 | } |
| 266433 | 267005 | } |
| | @@ -266764,11 +267336,11 @@ |
| 266764 | 267336 | p->aTotalSize[ii] -= aCol[ii]; |
| 266765 | 267337 | } |
| 266766 | 267338 | } |
| 266767 | 267339 | sqlite3_free(aCol); |
| 266768 | 267340 | } |
| 266769 | | - iOrigin = sqlite3_column_int(pLookup, 1); |
| 267341 | + iOrigin = sqlite3_column_int64(pLookup, 1); |
| 266770 | 267342 | } |
| 266771 | 267343 | rc2 = sqlite3_reset(pLookup); |
| 266772 | 267344 | if( rc==SQLITE_OK ) rc = rc2; |
| 266773 | 267345 | } |
| 266774 | 267346 | |
| | @@ -270300,13 +270872,14 @@ |
| 270300 | 270872 | |
| 270301 | 270873 | #define FTS5_VOCAB_COL 0 |
| 270302 | 270874 | #define FTS5_VOCAB_ROW 1 |
| 270303 | 270875 | #define FTS5_VOCAB_INSTANCE 2 |
| 270304 | 270876 | |
| 270305 | | -#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt" |
| 270306 | | -#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt" |
| 270307 | | -#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset" |
| 270877 | +#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt, PRIMARY KEY(term, col)" |
| 270878 | +#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt, PRIMARY KEY(term)" |
| 270879 | +#define FTS5_VOCAB_INST_SCHEMA \ |
| 270880 | + "term, doc, col, offset, PRIMARY KEY(term, doc, col, offset)" |
| 270308 | 270881 | |
| 270309 | 270882 | /* |
| 270310 | 270883 | ** Bits for the mask used as the idxNum value by xBestIndex/xFilter. |
| 270311 | 270884 | */ |
| 270312 | 270885 | #define FTS5_VOCAB_TERM_EQ 0x0100 |
| | @@ -270394,13 +270967,13 @@ |
| 270394 | 270967 | const char * const *argv, /* xCreate/xConnect argument array */ |
| 270395 | 270968 | sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */ |
| 270396 | 270969 | char **pzErr /* Write any error message here */ |
| 270397 | 270970 | ){ |
| 270398 | 270971 | const char *azSchema[] = { |
| 270399 | | - "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")", |
| 270400 | | - "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")", |
| 270401 | | - "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")" |
| 270972 | + "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ") WITHOUT ROWID", |
| 270973 | + "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ") WITHOUT ROWID", |
| 270974 | + "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ") WITHOUT ROWID" |
| 270402 | 270975 | }; |
| 270403 | 270976 | |
| 270404 | 270977 | Fts5VocabTable *pRet = 0; |
| 270405 | 270978 | int rc = SQLITE_OK; /* Return code */ |
| 270406 | 270979 | int bDb; |
| 270407 | 270980 | |