| | @@ -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 | | -** c8aa208bf5b00f3ab93c5edd510dfd0d7edd with changes in files: |
| 21 | +** 2f1f4f73535386549c12694dc57cfe555eec 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.54.0" |
| 471 | 471 | #define SQLITE_VERSION_NUMBER 3054000 |
| 472 | | -#define SQLITE_SOURCE_ID "2026-07-08 15:43:42 c8aa208bf5b00f3ab93c5edd510dfd0d7edd271c064f159616ee409b308f7eb9" |
| 472 | +#define SQLITE_SOURCE_ID "2026-07-24 16:28:47 2f1f4f73535386549c12694dc57cfe555eec689ae6824c6241aaf8d5befcd74d" |
| 473 | 473 | #define SQLITE_SCM_BRANCH "trunk" |
| 474 | 474 | #define SQLITE_SCM_TAGS "" |
| 475 | | -#define SQLITE_SCM_DATETIME "2026-07-08T15:43:42.959Z" |
| 475 | +#define SQLITE_SCM_DATETIME "2026-07-24T16:28:47.830Z" |
| 476 | 476 | |
| 477 | 477 | /* |
| 478 | 478 | ** CAPI3REF: Run-Time Library Version Numbers |
| 479 | 479 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 480 | 480 | ** |
| | @@ -4751,10 +4751,14 @@ |
| 4751 | 4751 | ** nesting depth for separate triggers.</dd>)^ |
| 4752 | 4752 | ** |
| 4753 | 4753 | ** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt> |
| 4754 | 4754 | ** <dd>The maximum number of auxiliary worker threads that a single |
| 4755 | 4755 | ** [prepared statement] may start.</dd>)^ |
| 4756 | +** |
| 4757 | +** [[SQLITE_LIMIT_SCHEMA]] ^(<dt>SQLITE_LIMIT_SCHEMA</dt> |
| 4758 | +** <dd>The maximum number of objects (tables, indexes, triggers, and views) |
| 4759 | +** defined by the database schema.</dd>)^ |
| 4756 | 4760 | ** </dl> |
| 4757 | 4761 | */ |
| 4758 | 4762 | #define SQLITE_LIMIT_LENGTH 0 |
| 4759 | 4763 | #define SQLITE_LIMIT_SQL_LENGTH 1 |
| 4760 | 4764 | #define SQLITE_LIMIT_COLUMN 2 |
| | @@ -4766,10 +4770,11 @@ |
| 4766 | 4770 | #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8 |
| 4767 | 4771 | #define SQLITE_LIMIT_VARIABLE_NUMBER 9 |
| 4768 | 4772 | #define SQLITE_LIMIT_TRIGGER_DEPTH 10 |
| 4769 | 4773 | #define SQLITE_LIMIT_WORKER_THREADS 11 |
| 4770 | 4774 | #define SQLITE_LIMIT_PARSER_DEPTH 12 |
| 4775 | +#define SQLITE_LIMIT_SCHEMA 13 |
| 4771 | 4776 | |
| 4772 | 4777 | /* |
| 4773 | 4778 | ** CAPI3REF: Prepare Flags |
| 4774 | 4779 | ** |
| 4775 | 4780 | ** These constants define various flags that can be passed into the |
| | @@ -6798,11 +6803,15 @@ |
| 6798 | 6803 | ** to by the second parameter and which is N bytes long where N is the |
| 6799 | 6804 | ** third parameter. |
| 6800 | 6805 | ** |
| 6801 | 6806 | ** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N) |
| 6802 | 6807 | ** interfaces set the result of the application-defined function to be |
| 6803 | | -** a BLOB containing all zero bytes and N bytes in size. |
| 6808 | +** a BLOB containing all zero bytes and N bytes in size. The |
| 6809 | +** zeroblob64(C,N) interface returns a [result code], which is normally |
| 6810 | +** [SQLITE_OK] but might be some other value if the requested operation |
| 6811 | +** could not be complete, for example if insufficient memory is available |
| 6812 | +** or if the value of N is out of range. |
| 6804 | 6813 | ** |
| 6805 | 6814 | ** ^The sqlite3_result_double() interface sets the result from |
| 6806 | 6815 | ** an application-defined function to be a floating point value specified |
| 6807 | 6816 | ** by its 2nd argument. |
| 6808 | 6817 | ** |
| | @@ -15031,10 +15040,37 @@ |
| 15031 | 15040 | */ |
| 15032 | 15041 | #ifndef SQLITE_MAX_TRIGGER_DEPTH |
| 15033 | 15042 | # define SQLITE_MAX_TRIGGER_DEPTH 1000 |
| 15034 | 15043 | #endif |
| 15035 | 15044 | |
| 15045 | +/* |
| 15046 | +** Maximum number of objects defined by a single database schema. |
| 15047 | +** Objects include: |
| 15048 | +** |
| 15049 | +** * tables (including the sqlite_schema table) |
| 15050 | +** * virtual tables |
| 15051 | +** * named indexes |
| 15052 | +** * indexes created automatically by UNIQUE and PRIMARY KEY constraints |
| 15053 | +** * triggers |
| 15054 | +** * views |
| 15055 | +** |
| 15056 | +** The total of all of the above is the number of objects in the schema, |
| 15057 | +** and that number may not exceed this value. |
| 15058 | +** |
| 15059 | +** The maximum number of objects is restricted to forestall theoretical |
| 15060 | +** signed integer overflow attacks using databases with billions of |
| 15061 | +** schema objects. Such attacks are "theoretical" because memory and |
| 15062 | +** disk space constraints would be reached long before billions of schema |
| 15063 | +** objects could be created. Even so, it seems good to have a well defined |
| 15064 | +** upper limit on the complexity of the schema, for defense in depth. |
| 15065 | +** No real-world application should ever get anywhere close to hitting |
| 15066 | +** this limit. |
| 15067 | +*/ |
| 15068 | +#ifndef SQLITE_MAX_SCHEMA |
| 15069 | +# define SQLITE_MAX_SCHEMA 10000000 |
| 15070 | +#endif |
| 15071 | + |
| 15036 | 15072 | /************** End of sqliteLimit.h *****************************************/ |
| 15037 | 15073 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 15038 | 15074 | |
| 15039 | 15075 | /* Disable nuisance warnings on Borland compilers */ |
| 15040 | 15076 | #if defined(__BORLANDC__) |
| | @@ -15289,11 +15325,13 @@ |
| 15289 | 15325 | ** bitmask tests, testcase() can be used to make sure each bit |
| 15290 | 15326 | ** is significant and used at least once. On switch statements |
| 15291 | 15327 | ** where multiple cases go to the same block of code, testcase() |
| 15292 | 15328 | ** can insure that all cases are evaluated. |
| 15293 | 15329 | */ |
| 15294 | | -#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG) |
| 15330 | +#if defined(SQLITE_MUTATION_TEST) |
| 15331 | +# define testcase(X) |
| 15332 | +#elif defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG) |
| 15295 | 15333 | # ifndef SQLITE_AMALGAMATION |
| 15296 | 15334 | extern unsigned int sqlite3CoverageCounter; |
| 15297 | 15335 | # endif |
| 15298 | 15336 | # define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__; } |
| 15299 | 15337 | #else |
| | @@ -18636,11 +18674,11 @@ |
| 18636 | 18674 | |
| 18637 | 18675 | /* |
| 18638 | 18676 | ** The number of different kinds of things that can be limited |
| 18639 | 18677 | ** using the sqlite3_limit() interface. |
| 18640 | 18678 | */ |
| 18641 | | -#define SQLITE_N_LIMIT (SQLITE_LIMIT_PARSER_DEPTH+1) |
| 18679 | +#define SQLITE_N_LIMIT (SQLITE_LIMIT_SCHEMA+1) |
| 18642 | 18680 | |
| 18643 | 18681 | /* |
| 18644 | 18682 | ** Lookaside malloc is a set of fixed-size buffers that can be used |
| 18645 | 18683 | ** to satisfy small transient memory allocation requests for objects |
| 18646 | 18684 | ** associated with a particular database connection. The use of |
| | @@ -22010,10 +22048,11 @@ |
| 22010 | 22048 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 22011 | 22049 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...); |
| 22012 | 22050 | #endif |
| 22013 | 22051 | #if defined(SQLITE_TEST) |
| 22014 | 22052 | SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); |
| 22053 | +SQLITE_PRIVATE const char *sqlite3TestPtrToText(void*); |
| 22015 | 22054 | #endif |
| 22016 | 22055 | |
| 22017 | 22056 | #if defined(SQLITE_DEBUG) |
| 22018 | 22057 | SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...); |
| 22019 | 22058 | SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); |
| | @@ -22234,10 +22273,11 @@ |
| 22234 | 22273 | SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*); |
| 22235 | 22274 | SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*); |
| 22236 | 22275 | SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*); |
| 22237 | 22276 | SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*); |
| 22238 | 22277 | SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**); |
| 22278 | +SQLITE_PRIVATE int sqlite3IndexBloomable(const Index*,int); |
| 22239 | 22279 | SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, |
| 22240 | 22280 | Expr*, int, int, u8); |
| 22241 | 22281 | SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int); |
| 22242 | 22282 | SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); |
| 22243 | 22283 | SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, |
| | @@ -23646,10 +23686,13 @@ |
| 23646 | 23686 | "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT), |
| 23647 | 23687 | #endif |
| 23648 | 23688 | #ifdef SQLITE_MAX_PAGE_SIZE |
| 23649 | 23689 | "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE), |
| 23650 | 23690 | #endif |
| 23691 | +#ifdef SQLITE_MAX_SCHEMA |
| 23692 | + "MAX_SCHEMA=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA), |
| 23693 | +#endif |
| 23651 | 23694 | #ifdef SQLITE_MAX_SCHEMA_RETRY |
| 23652 | 23695 | "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY), |
| 23653 | 23696 | #endif |
| 23654 | 23697 | #ifdef SQLITE_MAX_SQL_LENGTH |
| 23655 | 23698 | "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH), |
| | @@ -26531,11 +26574,11 @@ |
| 26531 | 26574 | ** on the appropriate weekday, this is a no-op. |
| 26532 | 26575 | */ |
| 26533 | 26576 | if( sqlite3_strnicmp(z, "weekday ", 8)==0 |
| 26534 | 26577 | && sqlite3AtoF(&z[8], &r)>0 |
| 26535 | 26578 | && r>=-6.0 && r<=6.0 |
| 26536 | | - && (n=(int)r)==r |
| 26579 | + && sqlite3RealSameAsInt(r,(i64)(n=(int)r)) |
| 26537 | 26580 | ){ |
| 26538 | 26581 | sqlite3_int64 Z; |
| 26539 | 26582 | computeYMD_HMS(p); |
| 26540 | 26583 | p->tz = 0; |
| 26541 | 26584 | p->validJD = 0; |
| | @@ -46092,11 +46135,10 @@ |
| 46092 | 46135 | i64 nNew /* Required mapping size */ |
| 46093 | 46136 | ){ |
| 46094 | 46137 | const char *zErr = "mmap"; |
| 46095 | 46138 | int h = pFd->h; /* File descriptor open on db file */ |
| 46096 | 46139 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
| 46097 | | - i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
| 46098 | 46140 | u8 *pNew = 0; /* Location of new mapping */ |
| 46099 | 46141 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 46100 | 46142 | |
| 46101 | 46143 | assert( pFd->nFetchOut==0 ); |
| 46102 | 46144 | assert( nNew>pFd->mmapSize ); |
| | @@ -46110,25 +46152,16 @@ |
| 46110 | 46152 | #endif |
| 46111 | 46153 | |
| 46112 | 46154 | if( pOrig ){ |
| 46113 | 46155 | #if HAVE_MREMAP |
| 46114 | 46156 | i64 nReuse = pFd->mmapSize; |
| 46157 | + pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
| 46158 | + zErr = "mremap"; |
| 46115 | 46159 | #else |
| 46116 | 46160 | const int szSyspage = osGetpagesize(); |
| 46117 | 46161 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
| 46118 | | -#endif |
| 46119 | 46162 | u8 *pReq = &pOrig[nReuse]; |
| 46120 | | - |
| 46121 | | - /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 46122 | | - if( nReuse!=nOrig ){ |
| 46123 | | - osMunmap(pReq, nOrig-nReuse); |
| 46124 | | - } |
| 46125 | | - |
| 46126 | | -#if HAVE_MREMAP |
| 46127 | | - pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
| 46128 | | - zErr = "mremap"; |
| 46129 | | -#else |
| 46130 | 46163 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 46131 | 46164 | if( pNew!=MAP_FAILED ){ |
| 46132 | 46165 | if( pNew!=pReq ){ |
| 46133 | 46166 | osMunmap(pNew, nNew - nReuse); |
| 46134 | 46167 | pNew = 0; |
| | @@ -46759,40 +46792,33 @@ |
| 46759 | 46792 | verifyDbFile(pNew); |
| 46760 | 46793 | } |
| 46761 | 46794 | return rc; |
| 46762 | 46795 | } |
| 46763 | 46796 | |
| 46764 | | -/* |
| 46765 | | -** Directories to consider for temp files. |
| 46766 | | -*/ |
| 46767 | | -static const char *azTempDirs[] = { |
| 46768 | | - 0, |
| 46769 | | - 0, |
| 46770 | | - "/var/tmp", |
| 46771 | | - "/usr/tmp", |
| 46772 | | - "/tmp", |
| 46773 | | - "." |
| 46774 | | -}; |
| 46775 | | - |
| 46776 | | -/* |
| 46777 | | -** Initialize first two members of azTempDirs[] array. |
| 46778 | | -*/ |
| 46779 | | -static void unixTempFileInit(void){ |
| 46780 | | - azTempDirs[0] = getenv("SQLITE_TMPDIR"); |
| 46781 | | - azTempDirs[1] = getenv("TMPDIR"); |
| 46782 | | -} |
| 46783 | | - |
| 46784 | 46797 | /* |
| 46785 | 46798 | ** Return the name of a directory in which to put temporary files. |
| 46786 | 46799 | ** If no suitable temporary file directory can be found, return NULL. |
| 46800 | +** |
| 46801 | +** The return value might be a string obtained from getenv() and so |
| 46802 | +** the return value should not be used after any call to setenv() or |
| 46803 | +** putenv() as that value might have been freed. |
| 46787 | 46804 | */ |
| 46788 | 46805 | static const char *unixTempFileDir(void){ |
| 46789 | | - unsigned int i = 0; |
| 46806 | + unsigned int i; |
| 46790 | 46807 | struct stat buf; |
| 46791 | | - const char *zDir = sqlite3_temp_directory; |
| 46808 | + const char *zDir; |
| 46792 | 46809 | |
| 46793 | | - while(1){ |
| 46810 | + for(i=0; i<7; i++){ |
| 46811 | + switch( i ){ |
| 46812 | + case 0: zDir = sqlite3_temp_directory; break; |
| 46813 | + case 1: zDir = getenv("SQLITE_TMPDIR"); break; |
| 46814 | + case 2: zDir = getenv("TMPDIR"); break; |
| 46815 | + case 3: zDir = "/var/tmp"; break; |
| 46816 | + case 4: zDir = "/usr/tmp"; break; |
| 46817 | + case 5: zDir = "/tmp"; break; |
| 46818 | + default: zDir = "."; break; |
| 46819 | + } |
| 46794 | 46820 | if( zDir!=0 |
| 46795 | 46821 | #if OS_VXWORKS |
| 46796 | 46822 | && zDir[0]=='/' |
| 46797 | 46823 | #endif |
| 46798 | 46824 | && osStat(zDir, &buf)==0 |
| | @@ -46799,12 +46825,10 @@ |
| 46799 | 46825 | && S_ISDIR(buf.st_mode) |
| 46800 | 46826 | && osAccess(zDir, 03)==0 |
| 46801 | 46827 | ){ |
| 46802 | 46828 | return zDir; |
| 46803 | 46829 | } |
| 46804 | | - if( i>=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break; |
| 46805 | | - zDir = azTempDirs[i++]; |
| 46806 | 46830 | } |
| 46807 | 46831 | return 0; |
| 46808 | 46832 | } |
| 46809 | 46833 | |
| 46810 | 46834 | /* |
| | @@ -49083,13 +49107,10 @@ |
| 49083 | 49107 | ** DMS UNIX_SHM_BASE+8 128 |
| 49084 | 49108 | */ |
| 49085 | 49109 | assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */ |
| 49086 | 49110 | #endif |
| 49087 | 49111 | |
| 49088 | | - /* Initialize temp file dir array. */ |
| 49089 | | - unixTempFileInit(); |
| 49090 | | - |
| 49091 | 49112 | return SQLITE_OK; |
| 49092 | 49113 | } |
| 49093 | 49114 | |
| 49094 | 49115 | /* |
| 49095 | 49116 | ** Shutdown the operating system interface. |
| | @@ -93648,14 +93669,15 @@ |
| 93648 | 93669 | SQLITE_API void sqlite3_result_str(sqlite3_context *pCtx, sqlite3_str *pStr, int eOwn){ |
| 93649 | 93670 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 93650 | 93671 | if( pCtx==0 ) return; |
| 93651 | 93672 | if( pStr==0 ) return; |
| 93652 | 93673 | #endif |
| 93674 | + testcase( pStr==(sqlite3_str*)&sqlite3OomStr ); |
| 93653 | 93675 | if( pStr->accError==0 ){ |
| 93654 | 93676 | if( pStr->nChar==0 ){ |
| 93655 | 93677 | setResultStrOrError(pCtx, "", 0, SQLITE_UTF8_ZT, SQLITE_STATIC); |
| 93656 | | - if( eOwn ) sqlite3_str_reset(pStr); |
| 93678 | + sqlite3_str_reset(pStr); |
| 93657 | 93679 | }else{ |
| 93658 | 93680 | const char *zText = sqlite3_str_value(pStr); |
| 93659 | 93681 | /* Only internal code has the ability to capture a pointer to |
| 93660 | 93682 | ** an sqlite3_str object that uses static buffer. And none of |
| 93661 | 93683 | ** those internal use cases every invoke the sqlite3_result_str() |
| | @@ -93666,26 +93688,21 @@ |
| 93666 | 93688 | setResultStrOrError(pCtx, zText, pStr->nChar, |
| 93667 | 93689 | SQLITE_UTF8, SQLITE_TRANSIENT); |
| 93668 | 93690 | }else{ |
| 93669 | 93691 | setResultStrOrError(pCtx, zText, pStr->nChar, |
| 93670 | 93692 | SQLITE_UTF8_ZT, SQLITE_DYNAMIC); |
| 93693 | + sqlite3StrAccumInit(pStr, pStr->db, 0, 0, pStr->mxAlloc); |
| 93671 | 93694 | } |
| 93672 | 93695 | } |
| 93673 | 93696 | }else if( pStr->accError==SQLITE_NOMEM ){ |
| 93674 | 93697 | sqlite3_result_error_nomem(pCtx); |
| 93675 | 93698 | }else{ |
| 93676 | 93699 | assert( pStr->accError==SQLITE_TOOBIG ); |
| 93677 | 93700 | sqlite3_result_error_toobig(pCtx); |
| 93678 | 93701 | } |
| 93679 | | - if( eOwn ){ |
| 93680 | | - testcase( pStr==(sqlite3_str*)&sqlite3OomStr ); |
| 93681 | | - if( pStr->accError==0 ){ |
| 93682 | | - sqlite3StrAccumInit(pStr, pStr->db, 0, 0, pStr->mxAlloc); |
| 93683 | | - } |
| 93684 | | - if( eOwn==SQLITE_FINISH ){ |
| 93685 | | - sqlite3_str_free(pStr); |
| 93686 | | - } |
| 93702 | + if( eOwn==SQLITE_FINISH ){ |
| 93703 | + sqlite3_str_free(pStr); |
| 93687 | 93704 | } |
| 93688 | 93705 | } |
| 93689 | 93706 | |
| 93690 | 93707 | #ifndef SQLITE_UNTESTABLE |
| 93691 | 93708 | /* Force the INT64 value currently stored as the result to be |
| | @@ -96574,10 +96591,14 @@ |
| 96574 | 96591 | } |
| 96575 | 96592 | |
| 96576 | 96593 | /* |
| 96577 | 96594 | ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning |
| 96578 | 96595 | ** with pOp->p3. Return the hash. |
| 96596 | +** |
| 96597 | +** IMPORTANT RESTRICTION (tag-202607231411): This hash is only valid if the |
| 96598 | +** collating sequence for TEXT is BINARY. Hence, Bloom filters that use this |
| 96599 | +** hash will not work for look-ups that use any other collating sequence. |
| 96579 | 96600 | */ |
| 96580 | 96601 | static u64 filterHash(const Mem *aMem, const Op *pOp){ |
| 96581 | 96602 | int i, mx; |
| 96582 | 96603 | u64 h = 0; |
| 96583 | 96604 | |
| | @@ -96586,15 +96607,32 @@ |
| 96586 | 96607 | const Mem *p = &aMem[i]; |
| 96587 | 96608 | if( p->flags & (MEM_Int|MEM_IntReal) ){ |
| 96588 | 96609 | h += p->u.i; |
| 96589 | 96610 | }else if( p->flags & MEM_Real ){ |
| 96590 | 96611 | h += sqlite3VdbeIntValue(p); |
| 96591 | | - }else if( p->flags & (MEM_Str|MEM_Blob) ){ |
| 96592 | | - /* All strings have the same hash and all blobs have the same hash, |
| 96593 | | - ** though, at least, those hashes are different from each other and |
| 96594 | | - ** from NULL. */ |
| 96595 | | - h += 4093 + (p->flags & (MEM_Str|MEM_Blob)); |
| 96612 | + }else if( p->flags & MEM_Str ){ |
| 96613 | + u64 x; |
| 96614 | + h += p->n; |
| 96615 | + if( p->n >= sizeof(x) ){ |
| 96616 | + memcpy(&x, p->z, sizeof(x)); |
| 96617 | + h += x; |
| 96618 | + memcpy(&x, p->z + p->n - sizeof(x), sizeof(x)); |
| 96619 | + h += x; |
| 96620 | + }else{ |
| 96621 | + x = 0; |
| 96622 | + memcpy(&x, p->z, p->n); |
| 96623 | + h += x; |
| 96624 | + } |
| 96625 | + }else if( p->flags & MEM_Blob ){ |
| 96626 | + int n = p->n; |
| 96627 | + u64 x = 0; |
| 96628 | + if( n ){ |
| 96629 | + memcpy(&x, p->z, MIN(n, sizeof(x))); |
| 96630 | + h += x; |
| 96631 | + } |
| 96632 | + h += n; |
| 96633 | + if( p->flags & MEM_Zero ) h += p->u.nZero; |
| 96596 | 96634 | } |
| 96597 | 96635 | } |
| 96598 | 96636 | return h; |
| 96599 | 96637 | } |
| 96600 | 96638 | |
| | @@ -101417,11 +101455,11 @@ |
| 101417 | 101455 | */ |
| 101418 | 101456 | case OP_SeekRowid: { /* jump0, in3, ncycle */ |
| 101419 | 101457 | VdbeCursor *pC; |
| 101420 | 101458 | BtCursor *pCrsr; |
| 101421 | 101459 | int res; |
| 101422 | | - u64 iKey; |
| 101460 | + i64 iKey; |
| 101423 | 101461 | |
| 101424 | 101462 | pIn3 = &aMem[pOp->p3]; |
| 101425 | 101463 | testcase( pIn3->flags & MEM_Int ); |
| 101426 | 101464 | testcase( pIn3->flags & MEM_IntReal ); |
| 101427 | 101465 | testcase( pIn3->flags & MEM_Real ); |
| | @@ -101431,13 +101469,23 @@ |
| 101431 | 101469 | ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted |
| 101432 | 101470 | ** into an integer without loss of information. Take care to avoid |
| 101433 | 101471 | ** changing the datatype of pIn3, however, as it is used by other |
| 101434 | 101472 | ** parts of the prepared statement. */ |
| 101435 | 101473 | Mem x = pIn3[0]; |
| 101436 | | - applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding); |
| 101437 | | - if( (x.flags & MEM_Int)==0 ) goto jump_to_p2; |
| 101438 | | - iKey = x.u.i; |
| 101474 | + if( x.flags & MEM_Str ){ |
| 101475 | + applyNumericAffinity(&x, 1); |
| 101476 | + } |
| 101477 | + if( x.flags & MEM_Int ){ |
| 101478 | + iKey = x.u.i; |
| 101479 | + }else |
| 101480 | + if( (x.flags & MEM_Real)==0 |
| 101481 | + || x.u.r < -9223372036854775808.0 |
| 101482 | + || x.u.r > 9223372036854775807.0 |
| 101483 | + || (double)(iKey = sqlite3RealToI64(x.u.r))!=x.u.r |
| 101484 | + ){ |
| 101485 | + goto jump_to_p2; |
| 101486 | + } |
| 101439 | 101487 | goto notExistsWithKey; |
| 101440 | 101488 | } |
| 101441 | 101489 | /* Fall through into OP_NotExists */ |
| 101442 | 101490 | /* no break */ deliberate_fall_through |
| 101443 | 101491 | case OP_NotExists: /* jump, in3, ncycle */ |
| | @@ -110677,18 +110725,17 @@ |
| 110677 | 110725 | if( cnt==1 ){ |
| 110678 | 110726 | assert( pNC!=0 ); |
| 110679 | 110727 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 110680 | 110728 | if( db->xAuth ){ |
| 110681 | 110729 | if( pFJMatch ){ |
| 110682 | | - int ii; |
| 110683 | 110730 | assert( pExpr->op==TK_FUNCTION ); |
| 110684 | 110731 | assert( sqlite3_stricmp(pExpr->u.zToken,"coalesce")==0 ); |
| 110685 | 110732 | assert( pExpr->x.pList==pFJMatch ); |
| 110686 | 110733 | assert( pFJMatch->nExpr>0 ); |
| 110687 | | - for(ii=0; ii<pFJMatch->nExpr; ii++){ |
| 110688 | | - assert( pFJMatch->a[0].pExpr->op==TK_COLUMN ); |
| 110689 | | - sqlite3AuthRead(pParse, pFJMatch->a[0].pExpr, pSchema, pNC->pSrcList); |
| 110734 | + for(i=0; i<pFJMatch->nExpr; i++){ |
| 110735 | + assert( pFJMatch->a[i].pExpr->op==TK_COLUMN ); |
| 110736 | + sqlite3AuthRead(pParse,pFJMatch->a[i].pExpr,pSchema,pNC->pSrcList); |
| 110690 | 110737 | } |
| 110691 | 110738 | }else if( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ){ |
| 110692 | 110739 | sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
| 110693 | 110740 | } |
| 110694 | 110741 | } |
| | @@ -110802,15 +110849,24 @@ |
| 110802 | 110849 | static SQLITE_NOINLINE void resolveSetExprSubtypeArg(ExprList *pList){ |
| 110803 | 110850 | int nn, ii; |
| 110804 | 110851 | nn = pList ? pList->nExpr : 0; |
| 110805 | 110852 | for(ii=0; ii<nn; ii++){ |
| 110806 | 110853 | Expr *pExpr = pList->a[ii].pExpr; |
| 110807 | | - ExprSetProperty(pExpr, EP_SubtArg); |
| 110808 | | - if( pExpr->op==TK_SELECT ){ |
| 110809 | | - assert( ExprUseXSelect(pExpr) ); |
| 110810 | | - assert( pExpr->x.pSelect!=0 ); |
| 110811 | | - resolveSetExprSubtypeArg(pExpr->x.pSelect->pEList); |
| 110854 | + while( 1 /*exit-by-break*/ ){ |
| 110855 | + ExprSetProperty(pExpr, EP_SubtArg); |
| 110856 | + if( pExpr->op==TK_SELECT ){ |
| 110857 | + assert( ExprUseXSelect(pExpr) ); |
| 110858 | + assert( pExpr->x.pSelect!=0 ); |
| 110859 | + resolveSetExprSubtypeArg(pExpr->x.pSelect->pEList); |
| 110860 | + break; |
| 110861 | + } |
| 110862 | + if( pExpr->op==TK_UPLUS ){ |
| 110863 | + pExpr = pExpr->pLeft; |
| 110864 | + assert( pExpr!=0 ); |
| 110865 | + }else{ |
| 110866 | + break; |
| 110867 | + } |
| 110812 | 110868 | } |
| 110813 | 110869 | } |
| 110814 | 110870 | } |
| 110815 | 110871 | |
| 110816 | 110872 | /* |
| | @@ -115893,10 +115949,12 @@ |
| 115893 | 115949 | }else{ |
| 115894 | 115950 | VdbeComment((v, "RHS of IN operator")); |
| 115895 | 115951 | } |
| 115896 | 115952 | #endif |
| 115897 | 115953 | pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1); |
| 115954 | + assert( pKeyInfo!=0 || pParse->nErr ); |
| 115955 | + if( pKeyInfo==0 ) return; |
| 115898 | 115956 | |
| 115899 | 115957 | if( ExprUseXSelect(pExpr) ){ |
| 115900 | 115958 | /* Case 1: expr IN (SELECT ...) |
| 115901 | 115959 | ** |
| 115902 | 115960 | ** Generate code to write the results of the select into the temporary |
| | @@ -115917,21 +115975,35 @@ |
| 115917 | 115975 | int rc; |
| 115918 | 115976 | int addrBloom = 0; |
| 115919 | 115977 | sqlite3SelectDestInit(&dest, SRT_Set, iTab); |
| 115920 | 115978 | dest.zAffSdst = exprINAffinity(pParse, pExpr); |
| 115921 | 115979 | pSelect->iLimit = 0; |
| 115980 | + assert( pEList!=0 ); |
| 115981 | + assert( pEList->nExpr>0 ); |
| 115982 | + assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 115983 | + for(i=0; i<nVal; i++){ |
| 115984 | + Expr *p = sqlite3VectorFieldSubexpr(pLeft, i); |
| 115985 | + CollSeq *pColl; |
| 115986 | + pKeyInfo->aColl[i] = pColl = sqlite3BinaryCompareCollSeq( |
| 115987 | + pParse, p, pEList->a[i].pExpr |
| 115988 | + ); |
| 115989 | + if( !sqlite3IsBinary(pColl) ){ |
| 115990 | + allowBloom = 0; /* tag-202607231411 */ |
| 115991 | + } |
| 115992 | + } |
| 115922 | 115993 | if( addrOnce |
| 115923 | 115994 | && allowBloom |
| 115924 | 115995 | && OptimizationEnabled(pParse->db, SQLITE_BloomFilter) |
| 115925 | 115996 | ){ |
| 115926 | 115997 | int regBloom = ++pParse->nMem; |
| 115927 | 115998 | addrBloom = sqlite3VdbeAddOp2(v, OP_Blob, 10000, regBloom); |
| 115928 | 115999 | VdbeComment((v, "Bloom filter")); |
| 115929 | 116000 | dest.iSDParm2 = regBloom; |
| 116001 | + sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); |
| 116002 | + pKeyInfo = 0; |
| 115930 | 116003 | } |
| 115931 | 116004 | testcase( pSelect->selFlags & SF_Distinct ); |
| 115932 | | - testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ |
| 115933 | 116005 | pCopy = sqlite3SelectDup(pParse->db, pSelect, 0); |
| 115934 | 116006 | rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest); |
| 115935 | 116007 | sqlite3SelectDelete(pParse->db, pCopy); |
| 115936 | 116008 | sqlite3DbFree(pParse->db, dest.zAffSdst); |
| 115937 | 116009 | if( addrBloom ){ |
| | @@ -115945,20 +116017,10 @@ |
| 115945 | 116017 | } |
| 115946 | 116018 | if( rc ){ |
| 115947 | 116019 | sqlite3KeyInfoUnref(pKeyInfo); |
| 115948 | 116020 | return; |
| 115949 | 116021 | } |
| 115950 | | - assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ |
| 115951 | | - assert( pEList!=0 ); |
| 115952 | | - assert( pEList->nExpr>0 ); |
| 115953 | | - assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 115954 | | - for(i=0; i<nVal; i++){ |
| 115955 | | - Expr *p = sqlite3VectorFieldSubexpr(pLeft, i); |
| 115956 | | - pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( |
| 115957 | | - pParse, p, pEList->a[i].pExpr |
| 115958 | | - ); |
| 115959 | | - } |
| 115960 | 116022 | } |
| 115961 | 116023 | }else if( ALWAYS(pExpr->x.pList!=0) ){ |
| 115962 | 116024 | /* Case 2: expr IN (exprlist) |
| 115963 | 116025 | ** |
| 115964 | 116026 | ** For each expression, build an index key from the evaluation and |
| | @@ -115975,14 +116037,13 @@ |
| 115975 | 116037 | if( affinity<=SQLITE_AFF_NONE ){ |
| 115976 | 116038 | affinity = SQLITE_AFF_BLOB; |
| 115977 | 116039 | }else if( affinity==SQLITE_AFF_REAL ){ |
| 115978 | 116040 | affinity = SQLITE_AFF_NUMERIC; |
| 115979 | 116041 | } |
| 115980 | | - if( pKeyInfo ){ |
| 115981 | | - assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 115982 | | - pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
| 115983 | | - } |
| 116042 | + assert( pKeyInfo!=0 ); |
| 116043 | + assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 116044 | + pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
| 115984 | 116045 | |
| 115985 | 116046 | /* Loop through each expression in <exprlist>. */ |
| 115986 | 116047 | r1 = sqlite3GetTempReg(pParse); |
| 115987 | 116048 | r2 = sqlite3GetTempReg(pParse); |
| 115988 | 116049 | for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
| | @@ -116935,11 +116996,11 @@ |
| 116935 | 116996 | if( pExpr->op!=TK_FUNCTION ){ |
| 116936 | 116997 | return WRC_Prune; |
| 116937 | 116998 | } |
| 116938 | 116999 | assert( ExprUseXList(pExpr) ); |
| 116939 | 117000 | db = pWalker->pParse->db; |
| 116940 | | - n = ALWAYS(pExpr->x.pList) ? pExpr->x.pList->nExpr : 0; |
| 117001 | + n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0; |
| 116941 | 117002 | pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0); |
| 116942 | 117003 | if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){ |
| 116943 | 117004 | pWalker->eCode = 1; |
| 116944 | 117005 | return WRC_Abort; |
| 116945 | 117006 | } |
| | @@ -122344,19 +122405,41 @@ |
| 122344 | 122405 | |
| 122345 | 122406 | |
| 122346 | 122407 | /* |
| 122347 | 122408 | ** Argument z points into the body of a constraint - specifically the |
| 122348 | 122409 | ** second token of the constraint definition. For a named constraint, |
| 122349 | | -** z points to the first token past the CONSTRAINT keyword. For an |
| 122410 | +** z points to the second token of the constraint definition. For an |
| 122350 | 122411 | ** unnamed NOT NULL constraint, z points to the first byte past the NOT |
| 122351 | 122412 | ** keyword. |
| 122413 | +** |
| 122414 | +** Argument eTok may be the token value of the first token of the constraint |
| 122415 | +** (e.g. TK_CHECK or TK_REFERENCES) or zero. If it is either TK_REFERENCES |
| 122416 | +** or TK_FOREIGN, special parsing is enabled to find the end of the foreign-key |
| 122417 | +** constraint definition. |
| 122352 | 122418 | ** |
| 122353 | 122419 | ** Return the number of bytes until the end of the constraint. |
| 122354 | 122420 | */ |
| 122355 | | -static int getConstraint(const u8 *z){ |
| 122421 | +static int getConstraint(const u8 *z, int eTok){ |
| 122356 | 122422 | int iOff = 0; |
| 122357 | 122423 | int t = 0; |
| 122424 | + |
| 122425 | +#ifndef SQLITE_OMIT_FOREIGN_KEY |
| 122426 | + if( eTok==TK_FOREIGN ){ |
| 122427 | + /* For a FOREIGN KEY constraint, use getConstraint() to parse everything |
| 122428 | + ** up to the REFERENCES keyword. Then getConstraintToken() to consume |
| 122429 | + ** the TK_REFERENCES token itself. Then fall through to the special |
| 122430 | + ** handling for TK_REFERENCES below. */ |
| 122431 | + iOff = getConstraint(z, 0); |
| 122432 | + iOff += getConstraintToken(&z[iOff], &eTok); |
| 122433 | + } |
| 122434 | + |
| 122435 | + if( eTok==TK_REFERENCES ){ |
| 122436 | + /* REFERENCES is followed by a table name. Gobble this up here in |
| 122437 | + ** case the table name is a fallback token like TK_GENERATED. */ |
| 122438 | + iOff += getConstraintToken(&z[iOff], &t); |
| 122439 | + } |
| 122440 | +#endif |
| 122358 | 122441 | |
| 122359 | 122442 | /* Now, the current constraint proceeds until the next occurence of one |
| 122360 | 122443 | ** of the following tokens: |
| 122361 | 122444 | ** |
| 122362 | 122445 | ** CONSTRAINT, PRIMARY, NOT, UNIQUE, CHECK, DEFAULT, |
| | @@ -122525,24 +122608,24 @@ |
| 122525 | 122608 | || t==TK_COMMA || t==TK_RP || t==TK_GENERATED || t==TK_AS |
| 122526 | 122609 | ){ |
| 122527 | 122610 | t = TK_CHECK; |
| 122528 | 122611 | }else{ |
| 122529 | 122612 | iOff += nTok; |
| 122530 | | - iOff += getConstraint(&zSql[iOff]); |
| 122613 | + iOff += getConstraint(&zSql[iOff], t); |
| 122531 | 122614 | } |
| 122532 | 122615 | |
| 122533 | 122616 | if( cmp==0 || (iNotNull>=0 && t==TK_NOT) ){ |
| 122534 | | - if( t!=TK_NOT && t!=TK_CHECK ){ |
| 122617 | + if( t!=TK_NOT && t!=TK_CHECK && t!=TK_REFERENCES && t!=TK_FOREIGN ){ |
| 122535 | 122618 | errorMPrintf(ctx, "constraint may not be dropped: %s", zCons); |
| 122536 | 122619 | return; |
| 122537 | 122620 | } |
| 122538 | 122621 | iEnd = iOff; |
| 122539 | 122622 | break; |
| 122540 | 122623 | } |
| 122541 | 122624 | |
| 122542 | 122625 | }else if( t==TK_NOT && iNotNull==ii ){ |
| 122543 | | - iEnd = iOff + getConstraint(&zSql[iOff]); |
| 122626 | + iEnd = iOff + getConstraint(&zSql[iOff], 0); |
| 122544 | 122627 | break; |
| 122545 | 122628 | }else if( t==TK_RP || t==TK_ILLEGAL ){ |
| 122546 | 122629 | iEnd = -1; |
| 122547 | 122630 | break; |
| 122548 | 122631 | }else if( t==TK_COMMA ){ |
| | @@ -128170,10 +128253,27 @@ |
| 128170 | 128253 | pIdx->aSortOrder = (u8*)zExtra; |
| 128171 | 128254 | pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */ |
| 128172 | 128255 | pIdx->isResized = 1; |
| 128173 | 128256 | return SQLITE_OK; |
| 128174 | 128257 | } |
| 128258 | + |
| 128259 | +/* |
| 128260 | +** Return true if the index pIdx can support a Bloom filter on its |
| 128261 | +** first N columns. Specifically, return true if all of the first N |
| 128262 | +** columns have the BINARY collating sequence or no collating sequence |
| 128263 | +** at all, and return false if there are any non-BINARY collating |
| 128264 | +** seqeuences on any of the first N columns. tag-202607231411 |
| 128265 | +*/ |
| 128266 | +SQLITE_PRIVATE int sqlite3IndexBloomable(const Index *pIdx, int N){ |
| 128267 | + int i; |
| 128268 | + assert( pIdx!=0 ); |
| 128269 | + assert( N <= pIdx->nColumn ); |
| 128270 | + for(i=0; i<N; i++){ |
| 128271 | + if( sqlite3StrICmp(pIdx->azColl[i],"BINARY")!=0 ) return 0; |
| 128272 | + } |
| 128273 | + return 1; |
| 128274 | +} |
| 128175 | 128275 | |
| 128176 | 128276 | /* |
| 128177 | 128277 | ** Estimate the total row width for a table. |
| 128178 | 128278 | */ |
| 128179 | 128279 | static void estimateTableWidth(Table *pTab){ |
| | @@ -130038,12 +130138,13 @@ |
| 130038 | 130138 | } |
| 130039 | 130139 | } |
| 130040 | 130140 | if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| 130041 | 130141 | if( !ifNotExist ){ |
| 130042 | 130142 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 130143 | + }else if( db->init.busy ){ |
| 130144 | + sqlite3ErrorMsg(pParse,""); /* corruptSchema() will do the error */ |
| 130043 | 130145 | }else{ |
| 130044 | | - assert( !db->init.busy ); |
| 130045 | 130146 | sqlite3CodeVerifySchema(pParse, iDb); |
| 130046 | 130147 | sqlite3ForceNotReadOnly(pParse); |
| 130047 | 130148 | } |
| 130048 | 130149 | goto exit_create_index; |
| 130049 | 130150 | } |
| | @@ -136304,17 +136405,21 @@ |
| 136304 | 136405 | static void percentSort( |
| 136305 | 136406 | double *a, /* Array to sort */ |
| 136306 | 136407 | unsigned int n, /* Number of elements in array a[] */ |
| 136307 | 136408 | int iReq /* Element caller cares about (or -ve) */ |
| 136308 | 136409 | ){ |
| 136309 | | - int iLt; /* Entries before a[iLt] are less than rPivot */ |
| 136310 | | - int iGt; /* Entries at or after a[iGt] are greater than rPivot */ |
| 136410 | + int iLt; /* Entries before a[iLt] are less than or equal to rPivot */ |
| 136411 | + int iGt; /* Entries a[iGt] and after are greater or equal to rPivot */ |
| 136311 | 136412 | int i; /* Loop counter */ |
| 136312 | 136413 | double rPivot; /* The pivot value */ |
| 136313 | 136414 | |
| 136314 | 136415 | assert( n>=2 ); |
| 136315 | 136416 | do{ |
| 136417 | + /* Put the first, middle, and last elements in sorted order. |
| 136418 | + ** After doing so, return immediately if the array contains |
| 136419 | + ** three or fewer elements as there is nothing more to do. |
| 136420 | + */ |
| 136316 | 136421 | if( a[0]>a[n-1] ){ |
| 136317 | 136422 | SWAP_DOUBLE(a[0],a[n-1]) |
| 136318 | 136423 | } |
| 136319 | 136424 | if( n==2 ) return; |
| 136320 | 136425 | iGt = n-1; |
| | @@ -136323,10 +136428,15 @@ |
| 136323 | 136428 | SWAP_DOUBLE(a[0],a[i]) |
| 136324 | 136429 | }else if( a[i]>a[iGt] ){ |
| 136325 | 136430 | SWAP_DOUBLE(a[i],a[iGt]) |
| 136326 | 136431 | } |
| 136327 | 136432 | if( n==3 ) return; |
| 136433 | + |
| 136434 | + /* Take the value of the middle element as the pivot. Shuffle |
| 136435 | + ** values around so that all elements less than the pivot come |
| 136436 | + ** before all elements greater than the pivot. |
| 136437 | + */ |
| 136328 | 136438 | rPivot = a[i]; |
| 136329 | 136439 | iLt = i = 1; |
| 136330 | 136440 | do{ |
| 136331 | 136441 | if( a[i]<rPivot ){ |
| 136332 | 136442 | if( i>iLt ) SWAP_DOUBLE(a[i],a[iLt]) |
| | @@ -136340,12 +136450,16 @@ |
| 136340 | 136450 | }else{ |
| 136341 | 136451 | i++; |
| 136342 | 136452 | } |
| 136343 | 136453 | }while( i<iGt ); |
| 136344 | 136454 | |
| 136455 | + assert( iLt>0 && iLt<iGt && iGt<n ); |
| 136456 | + testcase( iGt>iLt+1 ); |
| 136345 | 136457 | assert( a[iLt]==rPivot ); |
| 136346 | | - assert( iGt>iLt ); |
| 136458 | + assert( a[iLt-1]<=rPivot ); |
| 136459 | + assert( a[iGt]>=rPivot ); |
| 136460 | + assert( a[iLt+1]>=rPivot ); |
| 136347 | 136461 | |
| 136348 | 136462 | if( iReq>=0 ){ |
| 136349 | 136463 | /* In this case, the only elements that the caller requires sorted into |
| 136350 | 136464 | ** the correct positions are elements a[iReq] and a[iReq+1]. At this |
| 136351 | 136465 | ** point we know that element a[iLt] is in the correct position and |
| | @@ -140577,10 +140691,26 @@ |
| 140577 | 140691 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 140578 | 140692 | regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0); |
| 140579 | 140693 | }else{ |
| 140580 | 140694 | pTrigger = 0; |
| 140581 | 140695 | regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0); |
| 140696 | + } |
| 140697 | + if( regTrigCnt ){ |
| 140698 | + /* At this point regTrigCnt is non-zero if there are DELETE triggers |
| 140699 | + ** or FK triggers. But we only care about these things if there is |
| 140700 | + ** a chance that a row will be deleted by an ON CONFLICT REPLACE |
| 140701 | + ** constraint. So zero regTrigCnt if no such constraint can be found. */ |
| 140702 | + if( overrideError!=OE_Replace ){ |
| 140703 | + if( overrideError!=OE_Default ){ |
| 140704 | + regTrigCnt = 0; |
| 140705 | + }else if( pkChng==0 || pPk || pTab->keyConf!=OE_Replace ){ |
| 140706 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 140707 | + if( pIdx->onError==OE_Replace ) break; |
| 140708 | + } |
| 140709 | + if( pIdx==0 ) regTrigCnt = 0; |
| 140710 | + } |
| 140711 | + } |
| 140582 | 140712 | } |
| 140583 | 140713 | if( regTrigCnt ){ |
| 140584 | 140714 | /* Replace triggers might exist. Allocate the counter and |
| 140585 | 140715 | ** initialize it to zero. */ |
| 140586 | 140716 | regTrigCnt = ++pParse->nMem; |
| | @@ -140629,13 +140759,17 @@ |
| 140629 | 140759 | ** the UNIQUE constraints have run. |
| 140630 | 140760 | */ |
| 140631 | 140761 | if( onError==OE_Replace /* IPK rule is REPLACE */ |
| 140632 | 140762 | && onError!=overrideError /* Rules for other constraints are different */ |
| 140633 | 140763 | && pTab->pIndex /* There exist other constraints */ |
| 140634 | | - && !upsertIpkDelay /* IPK check already deferred by UPSERT */ |
| 140635 | 140764 | ){ |
| 140636 | | - ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; |
| 140765 | + if( upsertIpkDelay ){ |
| 140766 | + ipkTop = upsertIpkDelay + 1; |
| 140767 | + upsertIpkDelay = 0; |
| 140768 | + }else{ |
| 140769 | + ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; |
| 140770 | + } |
| 140637 | 140771 | VdbeComment((v, "defer IPK REPLACE until last")); |
| 140638 | 140772 | } |
| 140639 | 140773 | |
| 140640 | 140774 | if( isUpdate ){ |
| 140641 | 140775 | /* pkChng!=0 does not mean that the rowid has changed, only that |
| | @@ -140725,15 +140859,15 @@ |
| 140725 | 140859 | sqlite3VdbeGoto(v, ignoreDest); |
| 140726 | 140860 | break; |
| 140727 | 140861 | } |
| 140728 | 140862 | } |
| 140729 | 140863 | sqlite3VdbeResolveLabel(v, addrRowidOk); |
| 140730 | | - if( pUpsert && pUpsertClause!=pUpsert ){ |
| 140731 | | - upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto); |
| 140732 | | - }else if( ipkTop ){ |
| 140864 | + if( ipkTop ){ |
| 140733 | 140865 | ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); |
| 140734 | 140866 | sqlite3VdbeJumpHere(v, ipkTop-1); |
| 140867 | + }else if( pUpsert && pUpsertClause!=pUpsert ){ |
| 140868 | + upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto); |
| 140735 | 140869 | } |
| 140736 | 140870 | } |
| 140737 | 140871 | |
| 140738 | 140872 | /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 140739 | 140873 | ** index and making sure that duplicate entries do not already exist. |
| | @@ -140749,10 +140883,11 @@ |
| 140749 | 140883 | int regIdx; /* Range of registers holding content for pIdx */ |
| 140750 | 140884 | int regR; /* Range of registers holding conflicting PK */ |
| 140751 | 140885 | int iThisCur; /* Cursor for this UNIQUE index */ |
| 140752 | 140886 | int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */ |
| 140753 | 140887 | int addrConflictCk; /* First opcode in the conflict check logic */ |
| 140888 | + int nConflictCk; /* Number of opcodes in conflict check logic */ |
| 140754 | 140889 | |
| 140755 | 140890 | if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */ |
| 140756 | 140891 | if( pUpsert ){ |
| 140757 | 140892 | pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx); |
| 140758 | 140893 | if( upsertIpkDelay && pUpsertClause==pUpsert ){ |
| | @@ -140924,10 +141059,15 @@ |
| 140924 | 141059 | VdbeCoverageIf(v, op==OP_Ne); |
| 140925 | 141060 | } |
| 140926 | 141061 | } |
| 140927 | 141062 | } |
| 140928 | 141063 | } |
| 141064 | + |
| 141065 | + nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; |
| 141066 | + assert( nConflictCk>0 || db->mallocFailed ); |
| 141067 | + testcase( nConflictCk<=0 ); |
| 141068 | + testcase( nConflictCk>1 ); |
| 140929 | 141069 | |
| 140930 | 141070 | /* Generate code that executes if the new index entry is not unique */ |
| 140931 | 141071 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
| 140932 | 141072 | || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update ); |
| 140933 | 141073 | switch( onError ){ |
| | @@ -140950,17 +141090,11 @@ |
| 140950 | 141090 | testcase( onError==OE_Ignore ); |
| 140951 | 141091 | sqlite3VdbeGoto(v, ignoreDest); |
| 140952 | 141092 | break; |
| 140953 | 141093 | } |
| 140954 | 141094 | default: { |
| 140955 | | - int nConflictCk; /* Number of opcodes in conflict check logic */ |
| 140956 | | - |
| 140957 | 141095 | assert( onError==OE_Replace ); |
| 140958 | | - nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; |
| 140959 | | - assert( nConflictCk>0 || db->mallocFailed ); |
| 140960 | | - testcase( nConflictCk<=0 ); |
| 140961 | | - testcase( nConflictCk>1 ); |
| 140962 | 141096 | if( regTrigCnt ){ |
| 140963 | 141097 | sqlite3MultiWrite(pParse); |
| 140964 | 141098 | nReplaceTrig++; |
| 140965 | 141099 | } |
| 140966 | 141100 | if( pTrigger && isUpdate ){ |
| | @@ -140970,62 +141104,62 @@ |
| 140970 | 141104 | regR, nPkField, 0, OE_Replace, |
| 140971 | 141105 | (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); |
| 140972 | 141106 | if( pTrigger && isUpdate ){ |
| 140973 | 141107 | sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur); |
| 140974 | 141108 | } |
| 140975 | | - if( regTrigCnt ){ |
| 140976 | | - int addrBypass; /* Jump destination to bypass recheck logic */ |
| 140977 | | - |
| 140978 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ |
| 140979 | | - addrBypass = sqlite3VdbeAddOp0(v, OP_Goto); /* Bypass recheck */ |
| 140980 | | - VdbeComment((v, "bypass recheck")); |
| 140981 | | - |
| 140982 | | - /* Here we insert code that will be invoked after all constraint |
| 140983 | | - ** checks have run, if and only if one or more replace triggers |
| 140984 | | - ** fired. */ |
| 140985 | | - sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 140986 | | - lblRecheckOk = sqlite3VdbeMakeLabel(pParse); |
| 140987 | | - if( pIdx->pPartIdxWhere ){ |
| 140988 | | - /* Bypass the recheck if this partial index is not defined |
| 140989 | | - ** for the current row */ |
| 140990 | | - sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk); |
| 140991 | | - VdbeCoverage(v); |
| 140992 | | - } |
| 140993 | | - /* Copy the constraint check code from above, except change |
| 140994 | | - ** the constraint-ok jump destination to be the address of |
| 140995 | | - ** the next retest block */ |
| 140996 | | - while( nConflictCk>0 ){ |
| 140997 | | - VdbeOp x; /* Conflict check opcode to copy */ |
| 140998 | | - /* The sqlite3VdbeAddOp4() call might reallocate the opcode array. |
| 140999 | | - ** Hence, make a complete copy of the opcode, rather than using |
| 141000 | | - ** a pointer to the opcode. */ |
| 141001 | | - x = *sqlite3VdbeGetOp(v, addrConflictCk); |
| 141002 | | - if( x.opcode!=OP_IdxRowid ){ |
| 141003 | | - int p2; /* New P2 value for copied conflict check opcode */ |
| 141004 | | - const char *zP4; |
| 141005 | | - if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){ |
| 141006 | | - p2 = lblRecheckOk; |
| 141007 | | - }else{ |
| 141008 | | - p2 = x.p2; |
| 141009 | | - } |
| 141010 | | - zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z; |
| 141011 | | - sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type); |
| 141012 | | - sqlite3VdbeChangeP5(v, x.p5); |
| 141013 | | - VdbeCoverageIf(v, p2!=x.p2); |
| 141014 | | - } |
| 141015 | | - nConflictCk--; |
| 141016 | | - addrConflictCk++; |
| 141017 | | - } |
| 141018 | | - /* If the retest fails, issue an abort */ |
| 141019 | | - sqlite3UniqueConstraint(pParse, OE_Abort, pIdx); |
| 141020 | | - |
| 141021 | | - sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */ |
| 141022 | | - } |
| 141023 | 141109 | seenReplace = 1; |
| 141024 | 141110 | break; |
| 141025 | 141111 | } |
| 141026 | 141112 | } |
| 141113 | + if( regTrigCnt ){ |
| 141114 | + int addrBypass; /* Jump destination to bypass recheck logic */ |
| 141115 | + |
| 141116 | + sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ |
| 141117 | + addrBypass = sqlite3VdbeAddOp0(v, OP_Goto); /* Bypass recheck */ |
| 141118 | + VdbeComment((v, "bypass recheck")); |
| 141119 | + |
| 141120 | + /* Here we insert code that will be invoked after all constraint |
| 141121 | + ** checks have run, if and only if one or more replace triggers |
| 141122 | + ** fired. */ |
| 141123 | + sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 141124 | + lblRecheckOk = sqlite3VdbeMakeLabel(pParse); |
| 141125 | + if( pIdx->pPartIdxWhere ){ |
| 141126 | + /* Bypass the recheck if this partial index is not defined |
| 141127 | + ** for the current row */ |
| 141128 | + sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk); |
| 141129 | + VdbeCoverage(v); |
| 141130 | + } |
| 141131 | + /* Copy the constraint check code from above, except change |
| 141132 | + ** the constraint-ok jump destination to be the address of |
| 141133 | + ** the next retest block */ |
| 141134 | + while( nConflictCk>0 ){ |
| 141135 | + VdbeOp x; /* Conflict check opcode to copy */ |
| 141136 | + /* The sqlite3VdbeAddOp4() call might reallocate the opcode array. |
| 141137 | + ** Hence, make a complete copy of the opcode, rather than using |
| 141138 | + ** a pointer to the opcode. */ |
| 141139 | + x = *sqlite3VdbeGetOp(v, addrConflictCk); |
| 141140 | + if( x.opcode!=OP_IdxRowid ){ |
| 141141 | + int p2; /* New P2 value for copied conflict check opcode */ |
| 141142 | + const char *zP4; |
| 141143 | + if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){ |
| 141144 | + p2 = lblRecheckOk; |
| 141145 | + }else{ |
| 141146 | + p2 = x.p2; |
| 141147 | + } |
| 141148 | + zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z; |
| 141149 | + sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type); |
| 141150 | + sqlite3VdbeChangeP5(v, x.p5); |
| 141151 | + VdbeCoverageIf(v, p2!=x.p2); |
| 141152 | + } |
| 141153 | + nConflictCk--; |
| 141154 | + addrConflictCk++; |
| 141155 | + } |
| 141156 | + /* If the retest fails, issue an abort */ |
| 141157 | + sqlite3UniqueConstraint(pParse, OE_Abort, pIdx); |
| 141158 | + |
| 141159 | + sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */ |
| 141160 | + } |
| 141027 | 141161 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 141028 | 141162 | if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); |
| 141029 | 141163 | if( pUpsertClause |
| 141030 | 141164 | && upsertIpkReturn |
| 141031 | 141165 | && sqlite3UpsertNextIsIPK(pUpsertClause) |
| | @@ -141059,12 +141193,12 @@ |
| 141059 | 141193 | VdbeCoverage(v); |
| 141060 | 141194 | sqlite3RowidConstraint(pParse, OE_Abort, pTab); |
| 141061 | 141195 | }else{ |
| 141062 | 141196 | sqlite3VdbeGoto(v, addrRecheck); |
| 141063 | 141197 | } |
| 141064 | | - sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 141065 | 141198 | } |
| 141199 | + if( regTrigCnt ) sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 141066 | 141200 | |
| 141067 | 141201 | /* Generate the table record */ |
| 141068 | 141202 | if( HasRowid(pTab) ){ |
| 141069 | 141203 | int regRec = aRegIdx[ix]; |
| 141070 | 141204 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec); |
| | @@ -147540,10 +147674,11 @@ |
| 147540 | 147674 | corruptSchema(pData, argv, 0); |
| 147541 | 147675 | return 1; |
| 147542 | 147676 | } |
| 147543 | 147677 | |
| 147544 | 147678 | assert( iDb>=0 && iDb<db->nDb ); |
| 147679 | + assert( db->aDb[iDb].pSchema!=0 ); |
| 147545 | 147680 | if( argv[3]==0 ){ |
| 147546 | 147681 | corruptSchema(pData, argv, 0); |
| 147547 | 147682 | }else if( argv[4] |
| 147548 | 147683 | && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]] |
| 147549 | 147684 | && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){ |
| | @@ -147614,10 +147749,20 @@ |
| 147614 | 147749 | ){ |
| 147615 | 147750 | if( sqlite3Config.bExtraSchemaChecks ){ |
| 147616 | 147751 | corruptSchema(pData, argv, "invalid rootpage"); |
| 147617 | 147752 | } |
| 147618 | 147753 | } |
| 147754 | + } |
| 147755 | + if( pData->pzErrMsg[0]==0 ){ |
| 147756 | + Schema *pX = db->aDb[iDb].pSchema; |
| 147757 | + if( pX->tblHash.count + pX->idxHash.count + pX->trigHash.count |
| 147758 | + > (u32)db->aLimit[SQLITE_LIMIT_SCHEMA] |
| 147759 | + ){ |
| 147760 | + *pData->pzErrMsg = sqlite3MPrintf(db, "too many schema objects"); |
| 147761 | + pData->rc = SQLITE_ERROR; |
| 147762 | + return 1; |
| 147763 | + } |
| 147619 | 147764 | } |
| 147620 | 147765 | return 0; |
| 147621 | 147766 | } |
| 147622 | 147767 | |
| 147623 | 147768 | /* |
| | @@ -149659,10 +149804,52 @@ |
| 149659 | 149804 | } |
| 149660 | 149805 | pSort->nDefer = (u8)nDefer; |
| 149661 | 149806 | *ppExtra = pExtra; |
| 149662 | 149807 | } |
| 149663 | 149808 | #endif |
| 149809 | + |
| 149810 | +#ifdef SQLITE_DEBUG |
| 149811 | +/* |
| 149812 | +** This is a byte-code validation check that only runs when SQLITE_DEBUG |
| 149813 | +** is defined. This routine looks backwards through the bytecode |
| 149814 | +** for the definition of cursor with index iCur. It extracts the KeyInfo from |
| 149815 | +** that cursor (it must be an index cursor) and verifies that the cursor |
| 149816 | +** does not use any collating seqeuences other than BINARY for its first |
| 149817 | +** nCol columns. |
| 149818 | +** |
| 149819 | +** This routine is used inside of an assert(). So it should return true |
| 149820 | +** on success and false if the invariant is not satisfied. |
| 149821 | +** |
| 149822 | +** tag-202607231411 |
| 149823 | +*/ |
| 149824 | +static int sqlite3CursorBloomable(Parse *pParse, int iCur, int nCol){ |
| 149825 | + int i,k; |
| 149826 | + Vdbe *v = pParse->pVdbe; |
| 149827 | + if( pParse->nErr ) return 1; |
| 149828 | + assert( v ); |
| 149829 | + for(k=sqlite3VdbeCurrentAddr(v)-1; k>0; k--){ |
| 149830 | + const VdbeOp *pOp = sqlite3VdbeGetOp(v, k); |
| 149831 | + const KeyInfo *pKeyInfo; |
| 149832 | + if( pOp->p1!=iCur ) continue; |
| 149833 | + if( pOp->opcode!=OP_OpenRead |
| 149834 | + && pOp->opcode!=OP_OpenWrite |
| 149835 | + && pOp->opcode!=OP_OpenEphemeral |
| 149836 | + ){ |
| 149837 | + continue; |
| 149838 | + } |
| 149839 | + assert( pOp->p4type==P4_KEYINFO ); |
| 149840 | + pKeyInfo = (const KeyInfo*)pOp->p4.pKeyInfo; |
| 149841 | + assert( pKeyInfo!=0 ); |
| 149842 | + for(i=0; i<nCol; i++){ |
| 149843 | + assert( sqlite3IsBinary(pKeyInfo->aColl[i]) ); |
| 149844 | + } |
| 149845 | + return 1; |
| 149846 | + } |
| 149847 | + return 0; |
| 149848 | +} |
| 149849 | +#endif /* SQLITE_DEBUG */ |
| 149850 | + |
| 149664 | 149851 | |
| 149665 | 149852 | /* |
| 149666 | 149853 | ** This routine generates the code for the inside of the inner loop |
| 149667 | 149854 | ** of a SELECT. |
| 149668 | 149855 | ** |
| | @@ -149930,10 +150117,11 @@ |
| 149930 | 150117 | assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol ); |
| 149931 | 150118 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, |
| 149932 | 150119 | r1, pDest->zAffSdst, nResultCol); |
| 149933 | 150120 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
| 149934 | 150121 | if( pDest->iSDParm2 ){ |
| 150122 | + assert( sqlite3CursorBloomable(pParse,iParm,nResultCol) ); |
| 149935 | 150123 | sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pDest->iSDParm2, 0, |
| 149936 | 150124 | regResult, nResultCol); |
| 149937 | 150125 | ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER")); |
| 149938 | 150126 | } |
| 149939 | 150127 | sqlite3ReleaseTempReg(pParse, r1); |
| | @@ -151739,10 +151927,11 @@ |
| 151739 | 151927 | sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, |
| 151740 | 151928 | r1, pDest->zAffSdst, pIn->nSdst); |
| 151741 | 151929 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1, |
| 151742 | 151930 | pIn->iSdst, pIn->nSdst); |
| 151743 | 151931 | if( pDest->iSDParm2>0 ){ |
| 151932 | + assert( sqlite3CursorBloomable(pParse, pDest->iSDParm, pIn->nSdst) ); |
| 151744 | 151933 | sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pDest->iSDParm2, 0, |
| 151745 | 151934 | pIn->iSdst, pIn->nSdst); |
| 151746 | 151935 | ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER")); |
| 151747 | 151936 | } |
| 151748 | 151937 | sqlite3ReleaseTempReg(pParse, r1); |
| | @@ -155688,11 +155877,12 @@ |
| 155688 | 155877 | assert( ExprUseXList(pExpr) ); |
| 155689 | 155878 | if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */ |
| 155690 | 155879 | if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */ |
| 155691 | 155880 | if( ExprHasProperty(pExpr, EP_WinFunc) ) return 0;/* Not a window function */ |
| 155692 | 155881 | pFrom = p->pSrc->a; |
| 155693 | | - if( pFrom->fg.isSubquery==0 ) return 0; /* FROM is a subquery */ |
| 155882 | + if( pFrom->fg.isSubquery==0 ) return 0; /* FROM is a subquery */ |
| 155883 | + if( (p->selFlags & SF_Correlated)!=0 ) return 0; /* Not a correlated subq */ |
| 155694 | 155884 | pSub = pFrom->u4.pSubq->pSelect; |
| 155695 | 155885 | if( pSub->pPrior==0 ) return 0; /* Must be a compound */ |
| 155696 | 155886 | if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */ |
| 155697 | 155887 | do{ |
| 155698 | 155888 | if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */ |
| | @@ -157919,12 +158109,13 @@ |
| 157919 | 158109 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 157920 | 158110 | if( !IN_RENAME_OBJECT ){ |
| 157921 | 158111 | if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ |
| 157922 | 158112 | if( !noErr ){ |
| 157923 | 158113 | sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); |
| 158114 | + }else if( db->init.busy ){ |
| 158115 | + sqlite3ErrorMsg(pParse,""); /* Err msg generated by corruptSchema() */ |
| 157924 | 158116 | }else{ |
| 157925 | | - assert( !db->init.busy ); |
| 157926 | 158117 | sqlite3CodeVerifySchema(pParse, iDb); |
| 157927 | 158118 | VVA_ONLY( pParse->ifNotExists = 1; ) |
| 157928 | 158119 | } |
| 157929 | 158120 | goto trigger_cleanup; |
| 157930 | 158121 | } |
| | @@ -163424,14 +163615,11 @@ |
| 163424 | 163615 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 163425 | 163616 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); |
| 163426 | 163617 | SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 163427 | 163618 | SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 163428 | 163619 | SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); |
| 163429 | | - |
| 163430 | | - |
| 163431 | | - |
| 163432 | | - |
| 163620 | +SQLITE_PRIVATE int sqlite3WhereLoopBloomable(const WhereLoop*); |
| 163433 | 163621 | |
| 163434 | 163622 | /* |
| 163435 | 163623 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 163436 | 163624 | ** operators that are of interest to the query planner. An |
| 163437 | 163625 | ** OR-ed combination of these values can be used when searching for |
| | @@ -164896,10 +165084,11 @@ |
| 164896 | 165084 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 164897 | 165085 | regRowid = sqlite3GetTempReg(pParse); |
| 164898 | 165086 | regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid); |
| 164899 | 165087 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt); |
| 164900 | 165088 | VdbeCoverage(pParse->pVdbe); |
| 165089 | + assert( sqlite3WhereLoopBloomable(pLoop) ); |
| 164901 | 165090 | sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, |
| 164902 | 165091 | addrNxt, regRowid, 1); |
| 164903 | 165092 | VdbeCoverage(pParse->pVdbe); |
| 164904 | 165093 | }else{ |
| 164905 | 165094 | u16 nEq = pLoop->u.btree.nEq; |
| | @@ -164909,10 +165098,11 @@ |
| 164909 | 165098 | assert( pLoop->wsFlags & WHERE_INDEXED ); |
| 164910 | 165099 | assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 ); |
| 164911 | 165100 | r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff); |
| 164912 | 165101 | codeApplyAffinity(pParse, r1, nEq, zStartAff); |
| 164913 | 165102 | sqlite3DbFree(pParse->db, zStartAff); |
| 165103 | + assert( sqlite3WhereLoopBloomable(pLoop) ); |
| 164914 | 165104 | sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, |
| 164915 | 165105 | addrNxt, r1, nEq); |
| 164916 | 165106 | VdbeCoverage(pParse->pVdbe); |
| 164917 | 165107 | } |
| 164918 | 165108 | pLevel->regFilter = 0; |
| | @@ -165512,10 +165702,11 @@ |
| 165512 | 165702 | if( regBignull ){ |
| 165513 | 165703 | sqlite3VdbeAddOp2(v, OP_Integer, 1, regBignull); |
| 165514 | 165704 | VdbeComment((v, "NULL-scan pass ctr")); |
| 165515 | 165705 | } |
| 165516 | 165706 | if( pLevel->regFilter ){ |
| 165707 | + assert( sqlite3WhereLoopBloomable(pLoop) ); |
| 165517 | 165708 | sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt, |
| 165518 | 165709 | regBase, nEq); |
| 165519 | 165710 | VdbeCoverage(v); |
| 165520 | 165711 | filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady); |
| 165521 | 165712 | } |
| | @@ -166249,12 +166440,14 @@ |
| 166249 | 166440 | jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk); |
| 166250 | 166441 | VdbeCoverage(v); |
| 166251 | 166442 | VdbeComment((v, "match against %s", pTab->zName)); |
| 166252 | 166443 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r+1, nPk, r); |
| 166253 | 166444 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk); |
| 166254 | | - sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk); |
| 166255 | | - sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 166445 | + if( pRJ->regBloom ){ |
| 166446 | + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk); |
| 166447 | + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 166448 | + } |
| 166256 | 166449 | sqlite3VdbeJumpHere(v, jmp1); |
| 166257 | 166450 | sqlite3ReleaseTempRange(pParse, r, nPk+1); |
| 166258 | 166451 | } |
| 166259 | 166452 | |
| 166260 | 166453 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| | @@ -166402,11 +166595,11 @@ |
| 166402 | 166595 | WHERE_RIGHT_JOIN, 0); |
| 166403 | 166596 | if( pSubWInfo ){ |
| 166404 | 166597 | int iCur = pLevel->iTabCur; |
| 166405 | 166598 | int r = ++pParse->nMem; |
| 166406 | 166599 | int nPk; |
| 166407 | | - int jmp; |
| 166600 | + int jmp = 0; |
| 166408 | 166601 | int addrCont = sqlite3WhereContinueLabel(pSubWInfo); |
| 166409 | 166602 | Table *pTab = pTabItem->pSTab; |
| 166410 | 166603 | if( HasRowid(pTab) ){ |
| 166411 | 166604 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r); |
| 166412 | 166605 | nPk = 1; |
| | @@ -166418,15 +166611,17 @@ |
| 166418 | 166611 | for(iPk=0; iPk<nPk; iPk++){ |
| 166419 | 166612 | int iCol = pPk->aiColumn[iPk]; |
| 166420 | 166613 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk); |
| 166421 | 166614 | } |
| 166422 | 166615 | } |
| 166423 | | - jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk); |
| 166424 | | - VdbeCoverage(v); |
| 166616 | + if( pRJ->regBloom ){ |
| 166617 | + jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk); |
| 166618 | + VdbeCoverage(v); |
| 166619 | + } |
| 166425 | 166620 | sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk); |
| 166426 | 166621 | VdbeCoverage(v); |
| 166427 | | - sqlite3VdbeJumpHere(v, jmp); |
| 166622 | + if( jmp ) sqlite3VdbeJumpHere(v, jmp); |
| 166428 | 166623 | sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn); |
| 166429 | 166624 | sqlite3WhereEnd(pSubWInfo); |
| 166430 | 166625 | } |
| 166431 | 166626 | sqlite3ExprDelete(pParse->db, pSubWhere); |
| 166432 | 166627 | ExplainQueryPlanPop(pParse); |
| | @@ -168414,10 +168609,20 @@ |
| 168414 | 168609 | } |
| 168415 | 168610 | sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType); |
| 168416 | 168611 | whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); |
| 168417 | 168612 | } |
| 168418 | 168613 | } |
| 168614 | + |
| 168615 | +/* |
| 168616 | +** Return true if the WhereLoop pLoop can be use a Bloom filter. |
| 168617 | +** tag-202607231411 |
| 168618 | +*/ |
| 168619 | +SQLITE_PRIVATE int sqlite3WhereLoopBloomable(const WhereLoop *pLoop){ |
| 168620 | + if( pLoop->wsFlags & WHERE_IPK ) return 1; |
| 168621 | + if( NEVER((pLoop->wsFlags & WHERE_INDEXED)==0) ) return 0; |
| 168622 | + return sqlite3IndexBloomable(pLoop->u.btree.pIndex, pLoop->u.btree.nEq); |
| 168623 | +} |
| 168419 | 168624 | |
| 168420 | 168625 | /************** End of whereexpr.c *******************************************/ |
| 168421 | 168626 | /************** Begin file where.c *******************************************/ |
| 168422 | 168627 | /* |
| 168423 | 168628 | ** 2001 September 15 |
| | @@ -169426,11 +169631,11 @@ |
| 169426 | 169631 | WhereLoop *pLoop; /* The Loop object */ |
| 169427 | 169632 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 169428 | 169633 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 169429 | 169634 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 169430 | 169635 | u8 sentWarning = 0; /* True if a warning has been issued */ |
| 169431 | | - u8 useBloomFilter = 0; /* True to also add a Bloom filter */ |
| 169636 | + u8 useBloomFilter = 1; /* True to also add a Bloom filter */ |
| 169432 | 169637 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 169433 | 169638 | int iContinue = 0; /* Jump here to skip excluded rows */ |
| 169434 | 169639 | SrcList *pTabList; /* The complete FROM clause */ |
| 169435 | 169640 | SrcItem *pSrc; /* The FROM clause term to get the next index */ |
| 169436 | 169641 | int addrCounter = 0; /* Address where integer counter is initialized */ |
| | @@ -169553,22 +169758,17 @@ |
| 169553 | 169758 | Expr *pX = pTerm->pExpr; |
| 169554 | 169759 | idxCols |= cMask; |
| 169555 | 169760 | pIdx->aiColumn[n] = pTerm->u.x.leftColumn; |
| 169556 | 169761 | pColl = sqlite3ExprCompareCollSeq(pParse, pX); |
| 169557 | 169762 | assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */ |
| 169763 | + if( !sqlite3IsBinary(pColl) ){ |
| 169764 | + /* Disallow the use of a Bloom filter if any non-BINARY collating |
| 169765 | + ** sequence is involved. tag-202607231411 */ |
| 169766 | + useBloomFilter = 0; |
| 169767 | + } |
| 169558 | 169768 | pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY; |
| 169559 | 169769 | n++; |
| 169560 | | - if( ALWAYS(pX->pLeft!=0) |
| 169561 | | - && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT |
| 169562 | | - ){ |
| 169563 | | - /* TUNING: only use a Bloom filter on an automatic index |
| 169564 | | - ** if one or more key columns has the ability to hold numeric |
| 169565 | | - ** values, since strings all have the same hash in the Bloom |
| 169566 | | - ** filter implementation and hence a Bloom filter on a text column |
| 169567 | | - ** is not usually helpful. */ |
| 169568 | | - useBloomFilter = 1; |
| 169569 | | - } |
| 169570 | 169770 | } |
| 169571 | 169771 | } |
| 169572 | 169772 | } |
| 169573 | 169773 | assert( (u32)n==pLoop->u.btree.nEq ); |
| 169574 | 169774 | |
| | @@ -169634,10 +169834,11 @@ |
| 169634 | 169834 | regRecord = sqlite3GetTempReg(pParse); |
| 169635 | 169835 | regBase = sqlite3GenerateIndexKey( |
| 169636 | 169836 | pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0 |
| 169637 | 169837 | ); |
| 169638 | 169838 | if( pLevel->regFilter ){ |
| 169839 | + assert( sqlite3WhereLoopBloomable(pLoop) ); |
| 169639 | 169840 | sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, |
| 169640 | 169841 | regBase, pLoop->u.btree.nEq); |
| 169641 | 169842 | } |
| 169642 | 169843 | sqlite3VdbeScanStatusCounters(v, addrExp, addrExp, sqlite3VdbeCurrentAddr(v)); |
| 169643 | 169844 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| | @@ -169749,10 +169950,11 @@ |
| 169749 | 169950 | if( sz<10000 ){ |
| 169750 | 169951 | sz = 10000; |
| 169751 | 169952 | }else if( sz>10000000 ){ |
| 169752 | 169953 | sz = 10000000; |
| 169753 | 169954 | } |
| 169955 | + assert( sqlite3WhereLoopBloomable(pLoop) ); |
| 169754 | 169956 | sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter); |
| 169755 | 169957 | |
| 169756 | 169958 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
| 169757 | 169959 | pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm]; |
| 169758 | 169960 | for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){ |
| | @@ -169771,10 +169973,11 @@ |
| 169771 | 169973 | }else{ |
| 169772 | 169974 | Index *pIdx = pLoop->u.btree.pIndex; |
| 169773 | 169975 | int n = pLoop->u.btree.nEq; |
| 169774 | 169976 | int r1 = sqlite3GetTempRange(pParse, n); |
| 169775 | 169977 | int jj; |
| 169978 | + assert( pIdx!=0 ); |
| 169776 | 169979 | for(jj=0; jj<n; jj++){ |
| 169777 | 169980 | assert( pIdx->pTable==pItem->pSTab ); |
| 169778 | 169981 | sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iCur, jj, r1+jj); |
| 169779 | 169982 | } |
| 169780 | 169983 | sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n); |
| | @@ -173427,11 +173630,11 @@ |
| 173427 | 173630 | } |
| 173428 | 173631 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 173429 | 173632 | if( IsVirtual(pItem->pSTab) ){ |
| 173430 | 173633 | SrcItem *p; |
| 173431 | 173634 | for(p=&pItem[1]; p<pEnd; p++){ |
| 173432 | | - if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){ |
| 173635 | + if( (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){ |
| 173433 | 173636 | mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); |
| 173434 | 173637 | } |
| 173435 | 173638 | } |
| 173436 | 173639 | rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable); |
| 173437 | 173640 | }else |
| | @@ -175057,23 +175260,21 @@ |
| 175057 | 175260 | Table *pTab = pItem->pSTab; |
| 175058 | 175261 | if( (pTab->tabFlags & TF_HasStat1)==0 ) break; |
| 175059 | 175262 | pTab->tabFlags |= TF_MaybeReanalyze; |
| 175060 | 175263 | if( i>=1 |
| 175061 | 175264 | && (pLoop->wsFlags & reqFlags)==reqFlags |
| 175062 | | - /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */ |
| 175063 | | - && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0) |
| 175265 | + && sqlite3WhereLoopBloomable(pLoop) |
| 175266 | + && nSearch > pTab->nRowLogEst |
| 175064 | 175267 | ){ |
| 175065 | | - if( nSearch > pTab->nRowLogEst ){ |
| 175066 | | - testcase( pItem->fg.jointype & JT_LEFT ); |
| 175067 | | - pLoop->wsFlags |= WHERE_BLOOMFILTER; |
| 175068 | | - pLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 175069 | | - WHERETRACE(0xffffffff, ( |
| 175070 | | - "-> use Bloom-filter on loop %c because there are ~%.1e " |
| 175071 | | - "lookups into %s which has only ~%.1e rows\n", |
| 175072 | | - pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName, |
| 175073 | | - (double)sqlite3LogEstToInt(pTab->nRowLogEst))); |
| 175074 | | - } |
| 175268 | + testcase( pItem->fg.jointype & JT_LEFT ); |
| 175269 | + pLoop->wsFlags |= WHERE_BLOOMFILTER; |
| 175270 | + pLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 175271 | + WHERETRACE(0xffffffff, ( |
| 175272 | + "-> use Bloom-filter on loop %c because there are ~%.1e " |
| 175273 | + "lookups into %s which has only ~%.1e rows\n", |
| 175274 | + pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName, |
| 175275 | + (double)sqlite3LogEstToInt(pTab->nRowLogEst))); |
| 175075 | 175276 | } |
| 175076 | 175277 | nSearch += pLoop->nOut; |
| 175077 | 175278 | } |
| 175078 | 175279 | } |
| 175079 | 175280 | |
| | @@ -175819,19 +176020,19 @@ |
| 175819 | 176020 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
| 175820 | 176021 | if( (pTabItem->fg.jointype & JT_RIGHT)!=0 |
| 175821 | 176022 | && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0 |
| 175822 | 176023 | ){ |
| 175823 | 176024 | WhereRightJoin *pRJ = pLevel->pRJ; |
| 176025 | + int bBloomable = 0; |
| 175824 | 176026 | pRJ->iMatch = pParse->nTab++; |
| 175825 | | - pRJ->regBloom = ++pParse->nMem; |
| 175826 | | - sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom); |
| 175827 | 176027 | pRJ->regReturn = ++pParse->nMem; |
| 175828 | 176028 | sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn); |
| 175829 | 176029 | assert( pTab==pTabItem->pSTab ); |
| 175830 | 176030 | if( HasRowid(pTab) ){ |
| 175831 | 176031 | KeyInfo *pInfo; |
| 175832 | 176032 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1); |
| 176033 | + bBloomable = 1; |
| 175833 | 176034 | pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0); |
| 175834 | 176035 | if( pInfo ){ |
| 175835 | 176036 | pInfo->aColl[0] = 0; |
| 175836 | 176037 | pInfo->aSortFlags[0] = 0; |
| 175837 | 176038 | sqlite3VdbeAppendP4(v, pInfo, P4_KEYINFO); |
| | @@ -175838,10 +176039,17 @@ |
| 175838 | 176039 | } |
| 175839 | 176040 | }else{ |
| 175840 | 176041 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 175841 | 176042 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol); |
| 175842 | 176043 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 176044 | + bBloomable = sqlite3IndexBloomable(pPk,pPk->nKeyCol); |
| 176045 | + } |
| 176046 | + if( bBloomable ){ |
| 176047 | + pRJ->regBloom = ++pParse->nMem; |
| 176048 | + sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom); |
| 176049 | + }else{ |
| 176050 | + pRJ->regBloom = 0; |
| 175843 | 176051 | } |
| 175844 | 176052 | pLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 175845 | 176053 | /* The nature of RIGHT JOIN processing is such that it messes up |
| 175846 | 176054 | ** the output order. So omit any ORDER BY/GROUP BY elimination |
| 175847 | 176055 | ** optimizations. We need to do an actual sort for RIGHT JOIN. */ |
| | @@ -187183,67 +187391,64 @@ |
| 187183 | 187391 | SQLITE_API char *sqlite3_data_directory = 0; |
| 187184 | 187392 | |
| 187185 | 187393 | /* |
| 187186 | 187394 | ** Initialize SQLite. |
| 187187 | 187395 | ** |
| 187188 | | -** This routine must be called to initialize the memory allocation, |
| 187189 | | -** VFS, and mutex subsystems prior to doing any serious work with |
| 187190 | | -** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT |
| 187396 | +** The sqlite3_initialize() routine must be called to initialize the |
| 187397 | +** memory allocation, VFS, and mutex subsystems prior to doing any |
| 187398 | +** serious work. As long as you do not compile with SQLITE_OMIT_AUTOINIT |
| 187191 | 187399 | ** this routine will be called automatically by key routines such as |
| 187192 | 187400 | ** sqlite3_open(). |
| 187193 | 187401 | ** |
| 187194 | 187402 | ** This routine is a no-op except on its very first call for the process, |
| 187195 | | -** or for the first call after a call to sqlite3_shutdown. |
| 187403 | +** or for the first call after a call to sqlite3_shutdown. Most calls |
| 187404 | +** to sqlite3_initialize() are, in fact, no-ops. For that reason, the |
| 187405 | +** routine is broken into two pieces: |
| 187196 | 187406 | ** |
| 187197 | | -** The first thread to call this routine runs the initialization to |
| 187198 | | -** completion. If subsequent threads call this routine before the first |
| 187407 | +** sqlite3Initialize() Does the actual work of initialization |
| 187408 | +** |
| 187409 | +** sqlite3_initialize() Checks to see if initialization is needed |
| 187410 | +** and invokes sqlite3Initialize() if it is. |
| 187411 | +** |
| 187412 | +** The sqlite3_initialize() interface is called frequently, but |
| 187413 | +** sqlite3Initialize() runs rarely. The function is broken up this way |
| 187414 | +** to avoid wasting CPU cycles with unnecessary stack setup for local |
| 187415 | +** variables in cases where it is not needed. |
| 187416 | +** |
| 187417 | +** The first thread to call sqlite3_initialize() runs the initialization to |
| 187418 | +** completion. If subsequent threads call sqlite3_initialize() before the first |
| 187199 | 187419 | ** thread has finished the initialization process, then the subsequent |
| 187200 | 187420 | ** threads must block until the first thread finishes with the initialization. |
| 187201 | 187421 | ** |
| 187202 | 187422 | ** The first thread might call this routine recursively. Recursive |
| 187203 | 187423 | ** calls to this routine should not block, of course. Otherwise the |
| 187204 | 187424 | ** initialization process would never complete. |
| 187205 | 187425 | ** |
| 187206 | | -** Let X be the first thread to enter this routine. Let Y be some other |
| 187207 | | -** thread. Then while the initial invocation of this routine by X is |
| 187208 | | -** incomplete, it is required that: |
| 187426 | +** Let X be the first thread to enter sqlite3_initialize(). Let Y be some |
| 187427 | +** other thread. While the initial invocation of sqlite3_initialize() by X |
| 187428 | +** is incomplete, it is required that: |
| 187209 | 187429 | ** |
| 187210 | | -** * Calls to this routine from Y must block until the outer-most |
| 187430 | +** * Calls to sqlite3_initialize() from Y must block until the outer-most |
| 187211 | 187431 | ** call by X completes. |
| 187212 | 187432 | ** |
| 187213 | | -** * Recursive calls to this routine from thread X return immediately |
| 187214 | | -** without blocking. |
| 187433 | +** * Recursive calls to sqlite3_initialize() from thread X return |
| 187434 | +** immediately without blocking. |
| 187215 | 187435 | */ |
| 187216 | | -SQLITE_API int sqlite3_initialize(void){ |
| 187217 | | - MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */ |
| 187218 | | - int rc; /* Result code */ |
| 187436 | +static SQLITE_NOINLINE int sqlite3Initialize(void){ |
| 187437 | + MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */ |
| 187438 | + int rc; /* Result code */ |
| 187219 | 187439 | #ifdef SQLITE_EXTRA_INIT |
| 187220 | | - int bRunExtraInit = 0; /* Extra initialization needed */ |
| 187221 | | -#endif |
| 187222 | | - |
| 187223 | | -#ifdef SQLITE_OMIT_WSD |
| 187224 | | - rc = sqlite3_wsd_init(4096, 24); |
| 187225 | | - if( rc!=SQLITE_OK ){ |
| 187226 | | - return rc; |
| 187227 | | - } |
| 187440 | + int bRunExtraInit = 0; /* Extra initialization needed */ |
| 187228 | 187441 | #endif |
| 187229 | 187442 | |
| 187230 | 187443 | /* If the following assert() fails on some obscure processor/compiler |
| 187231 | | - ** combination, the work-around is to set the correct pointer |
| 187232 | | - ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */ |
| 187444 | + ** combination to warn that SQLite has been mis-compiled. If you hit |
| 187445 | + ** this assert(), that means you need to recompile with the |
| 187446 | + ** -DSQLITE_PTRSIZE=n compile-time option to set the correct pointer size. |
| 187447 | + */ |
| 187233 | 187448 | assert( SQLITE_PTRSIZE==sizeof(char*) ); |
| 187234 | 187449 | |
| 187235 | | - /* If SQLite is already completely initialized, then this call |
| 187236 | | - ** to sqlite3_initialize() should be a no-op. But the initialization |
| 187237 | | - ** must be complete. So isInit must not be set until the very end |
| 187238 | | - ** of this routine. |
| 187239 | | - */ |
| 187240 | | - if( sqlite3GlobalConfig.isInit ){ |
| 187241 | | - sqlite3MemoryBarrier(); |
| 187242 | | - return SQLITE_OK; |
| 187243 | | - } |
| 187244 | | - |
| 187245 | 187450 | /* Make sure the mutex subsystem is initialized. If unable to |
| 187246 | 187451 | ** initialize the mutex subsystem, return early with the error. |
| 187247 | 187452 | ** If the system is so sick that we are unable to allocate a mutex, |
| 187248 | 187453 | ** there is not much SQLite is going to be able to do. |
| 187249 | 187454 | ** |
| | @@ -187383,10 +187588,30 @@ |
| 187383 | 187588 | int SQLITE_EXTRA_INIT(const char*); |
| 187384 | 187589 | rc = SQLITE_EXTRA_INIT(0); |
| 187385 | 187590 | } |
| 187386 | 187591 | #endif |
| 187387 | 187592 | return rc; |
| 187593 | +} |
| 187594 | +SQLITE_API int sqlite3_initialize(void){ |
| 187595 | + /* If this build does not support writable static data (WSD) natively |
| 187596 | + ** then we have to invoke the (application-supplied) WSD initialization |
| 187597 | + ** routine before doing anything else. */ |
| 187598 | +#ifdef SQLITE_OMIT_WSD |
| 187599 | + rc = sqlite3_wsd_init(4096, 24); |
| 187600 | + if( rc!=SQLITE_OK ){ |
| 187601 | + return rc; |
| 187602 | + } |
| 187603 | +#endif |
| 187604 | + |
| 187605 | + if( sqlite3GlobalConfig.isInit ){ |
| 187606 | + /* SQLite has already been initialized. Fast early-out. */ |
| 187607 | + sqlite3MemoryBarrier(); |
| 187608 | + return SQLITE_OK; |
| 187609 | + }else{ |
| 187610 | + /* Invoke sqlite3Initialize() to do the actual work. */ |
| 187611 | + return sqlite3Initialize(); |
| 187612 | + } |
| 187388 | 187613 | } |
| 187389 | 187614 | |
| 187390 | 187615 | /* |
| 187391 | 187616 | ** Undo the effects of sqlite3_initialize(). Must not be called while |
| 187392 | 187617 | ** there are outstanding database connections or memory allocations or |
| | @@ -189999,10 +190224,11 @@ |
| 189999 | 190224 | SQLITE_MAX_LIKE_PATTERN_LENGTH, |
| 190000 | 190225 | SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */ |
| 190001 | 190226 | SQLITE_MAX_TRIGGER_DEPTH, |
| 190002 | 190227 | SQLITE_MAX_WORKER_THREADS, |
| 190003 | 190228 | SQLITE_MAX_PARSER_DEPTH, |
| 190229 | + SQLITE_MAX_SCHEMA, |
| 190004 | 190230 | }; |
| 190005 | 190231 | |
| 190006 | 190232 | /* |
| 190007 | 190233 | ** Make sure the hard limits are set to reasonable values |
| 190008 | 190234 | */ |
| | @@ -190081,11 +190307,12 @@ |
| 190081 | 190307 | assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== |
| 190082 | 190308 | SQLITE_MAX_LIKE_PATTERN_LENGTH ); |
| 190083 | 190309 | assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); |
| 190084 | 190310 | assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); |
| 190085 | 190311 | assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS ); |
| 190086 | | - assert( SQLITE_LIMIT_PARSER_DEPTH==(SQLITE_N_LIMIT-1) ); |
| 190312 | + assert( aHardLimit[SQLITE_LIMIT_SCHEMA]==SQLITE_MAX_SCHEMA ); |
| 190313 | + assert( SQLITE_LIMIT_SCHEMA==(SQLITE_N_LIMIT-1) ); |
| 190087 | 190314 | |
| 190088 | 190315 | |
| 190089 | 190316 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
| 190090 | 190317 | return -1; |
| 190091 | 190318 | } |
| | @@ -213541,11 +213768,11 @@ |
| 213541 | 213768 | /* |
| 213542 | 213769 | ** Expand pParse->aBlob so that it holds at least N bytes. |
| 213543 | 213770 | ** |
| 213544 | 213771 | ** Return the number of errors. |
| 213545 | 213772 | */ |
| 213546 | | -static int jsonBlobExpand(JsonParse *pParse, u32 N){ |
| 213773 | +static int jsonBlobExpand(JsonParse *pParse, u64 N){ |
| 213547 | 213774 | u8 *aNew; |
| 213548 | 213775 | u64 t; |
| 213549 | 213776 | assert( N>pParse->nBlobAlloc ); |
| 213550 | 213777 | if( pParse->nBlobAlloc==0 ){ |
| 213551 | 213778 | t = 100; |
| | @@ -213569,11 +213796,11 @@ |
| 213569 | 213796 | ** |
| 213570 | 213797 | ** Return true on success. Return false on OOM. |
| 213571 | 213798 | */ |
| 213572 | 213799 | static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){ |
| 213573 | 213800 | u8 *aOld; |
| 213574 | | - u32 nSize; |
| 213801 | + u64 nSize; |
| 213575 | 213802 | assert( !pParse->bReadOnly ); |
| 213576 | 213803 | if( pParse->oom ) return 0; |
| 213577 | 213804 | if( pParse->nBlobAlloc>0 ) return 1; |
| 213578 | 213805 | aOld = pParse->aBlob; |
| 213579 | 213806 | nSize = pParse->nBlob + nExtra; |
| | @@ -213590,11 +213817,11 @@ |
| 213590 | 213817 | */ |
| 213591 | 213818 | static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte( |
| 213592 | 213819 | JsonParse *pParse, |
| 213593 | 213820 | u8 c |
| 213594 | 213821 | ){ |
| 213595 | | - jsonBlobExpand(pParse, pParse->nBlob+1); |
| 213822 | + jsonBlobExpand(pParse, (u64)pParse->nBlob+1); |
| 213596 | 213823 | if( pParse->oom==0 ){ |
| 213597 | 213824 | assert( pParse->nBlob+1<=pParse->nBlobAlloc ); |
| 213598 | 213825 | pParse->aBlob[pParse->nBlob++] = c; |
| 213599 | 213826 | } |
| 213600 | 213827 | } |
| | @@ -213617,11 +213844,11 @@ |
| 213617 | 213844 | JsonParse *pParse, |
| 213618 | 213845 | u8 eType, |
| 213619 | 213846 | u64 szPayload, |
| 213620 | 213847 | const void *aPayload |
| 213621 | 213848 | ){ |
| 213622 | | - if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return; |
| 213849 | + if( jsonBlobExpand(pParse, (u64)pParse->nBlob+(u64)szPayload+9) ) return; |
| 213623 | 213850 | jsonBlobAppendNode(pParse, eType, szPayload, aPayload); |
| 213624 | 213851 | } |
| 213625 | 213852 | |
| 213626 | 213853 | |
| 213627 | 213854 | /* Append a node type byte together with the payload size and |
| | @@ -213707,11 +213934,11 @@ |
| 213707 | 213934 | }else{ |
| 213708 | 213935 | nNeeded = 4; |
| 213709 | 213936 | } |
| 213710 | 213937 | delta = nNeeded - nExtra; |
| 213711 | 213938 | if( delta ){ |
| 213712 | | - u32 newSize = pParse->nBlob + delta; |
| 213939 | + u64 newSize = (u64)pParse->nBlob + delta; |
| 213713 | 213940 | if( delta>0 ){ |
| 213714 | 213941 | if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){ |
| 213715 | 213942 | return 0; /* OOM error. Error state recorded in pParse->oom. */ |
| 213716 | 213943 | } |
| 213717 | 213944 | a = &pParse->aBlob[i]; |
| | @@ -215055,11 +215282,11 @@ |
| 215055 | 215282 | ){ |
| 215056 | 215283 | return; |
| 215057 | 215284 | } |
| 215058 | 215285 | if( d!=0 ){ |
| 215059 | 215286 | if( pParse->nBlob + d > pParse->nBlobAlloc ){ |
| 215060 | | - jsonBlobExpand(pParse, pParse->nBlob+d); |
| 215287 | + jsonBlobExpand(pParse, (u64)pParse->nBlob+d); |
| 215061 | 215288 | if( pParse->oom ) return; |
| 215062 | 215289 | } |
| 215063 | 215290 | memmove(&pParse->aBlob[iDel+nIns], |
| 215064 | 215291 | &pParse->aBlob[iDel+nDel], |
| 215065 | 215292 | pParse->nBlob - (iDel+nDel)); |
| | @@ -251402,10 +251629,11 @@ |
| 251402 | 251629 | i64 szData = (sizeof(Fts5Data) + 7) & ~7; |
| 251403 | 251630 | i64 nAlloc = szData + nByte + FTS5_DATA_PADDING; |
| 251404 | 251631 | pRet = (Fts5Data*)sqlite3_malloc64(nAlloc); |
| 251405 | 251632 | if( pRet ){ |
| 251406 | 251633 | pRet->nn = nByte; |
| 251634 | + pRet->szLeaf = 0; |
| 251407 | 251635 | aOut = pRet->p = (u8*)pRet + szData; |
| 251408 | 251636 | }else{ |
| 251409 | 251637 | rc = SQLITE_NOMEM; |
| 251410 | 251638 | } |
| 251411 | 251639 | |
| | @@ -251414,14 +251642,12 @@ |
| 251414 | 251642 | } |
| 251415 | 251643 | if( rc!=SQLITE_OK ){ |
| 251416 | 251644 | sqlite3_free(pRet); |
| 251417 | 251645 | pRet = 0; |
| 251418 | 251646 | }else{ |
| 251419 | | - /* TODO1: Fix this */ |
| 251420 | 251647 | pRet->p[nByte] = 0x00; |
| 251421 | 251648 | pRet->p[nByte+1] = 0x00; |
| 251422 | | - pRet->szLeaf = fts5GetU16(&pRet->p[2]); |
| 251423 | 251649 | } |
| 251424 | 251650 | } |
| 251425 | 251651 | p->rc = rc; |
| 251426 | 251652 | p->nRead++; |
| 251427 | 251653 | } |
| | @@ -251438,13 +251664,21 @@ |
| 251438 | 251664 | */ |
| 251439 | 251665 | static void fts5DataRelease(Fts5Data *pData){ |
| 251440 | 251666 | sqlite3_free(pData); |
| 251441 | 251667 | } |
| 251442 | 251668 | |
| 251669 | +/* |
| 251670 | +** Read a leaf-page record. This is similar to fts5DataRead(), except that |
| 251671 | +** it fills in the Fts5Data.szLeaf value before returning. |
| 251672 | +*/ |
| 251443 | 251673 | static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){ |
| 251444 | 251674 | Fts5Data *pRet = fts5DataRead(p, iRowid); |
| 251445 | 251675 | if( pRet ){ |
| 251676 | + assert( pRet->szLeaf==0 ); |
| 251677 | + if( pRet->nn>=4 ){ |
| 251678 | + pRet->szLeaf = fts5GetU16(&pRet->p[2]); |
| 251679 | + } |
| 251446 | 251680 | if( pRet->szLeaf<4 || pRet->szLeaf>pRet->nn ){ |
| 251447 | 251681 | FTS5_CORRUPT_ROWID(p, iRowid); |
| 251448 | 251682 | fts5DataRelease(pRet); |
| 251449 | 251683 | pRet = 0; |
| 251450 | 251684 | } |
| | @@ -255681,11 +255915,11 @@ |
| 255681 | 255915 | for(pgno=iPgno; p->rc==SQLITE_OK && pgno<=pSeg->pgnoLast; pgno++){ |
| 255682 | 255916 | i64 iRowid = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno); |
| 255683 | 255917 | int iNext = 0; |
| 255684 | 255918 | u8 *aPg = 0; |
| 255685 | 255919 | |
| 255686 | | - pLeaf = fts5DataRead(p, iRowid); |
| 255920 | + pLeaf = fts5LeafRead(p, iRowid); |
| 255687 | 255921 | if( pLeaf==0 ) break; |
| 255688 | 255922 | aPg = pLeaf->p; |
| 255689 | 255923 | |
| 255690 | 255924 | iNext = fts5GetU16(&aPg[0]); |
| 255691 | 255925 | if( iNext!=0 ){ |
| | @@ -255976,19 +256210,19 @@ |
| 255976 | 256210 | |
| 255977 | 256211 | assert_nc( pSeg->iLeafPgno>pSeg->iTermLeafPgno ); |
| 255978 | 256212 | /* The entry being removed may be the only position list in |
| 255979 | 256213 | ** its doclist. */ |
| 255980 | 256214 | for(iPgno=pSeg->iLeafPgno-1; iPgno>pSeg->iTermLeafPgno; iPgno-- ){ |
| 255981 | | - Fts5Data *pPg = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno)); |
| 256215 | + Fts5Data *pPg = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno)); |
| 255982 | 256216 | int bEmpty = (pPg && pPg->nn==4); |
| 255983 | 256217 | fts5DataRelease(pPg); |
| 255984 | 256218 | if( bEmpty==0 ) break; |
| 255985 | 256219 | } |
| 255986 | 256220 | |
| 255987 | 256221 | if( iPgno==pSeg->iTermLeafPgno ){ |
| 255988 | 256222 | i64 iId = FTS5_SEGMENT_ROWID(iSegid, pSeg->iTermLeafPgno); |
| 255989 | | - Fts5Data *pTerm = fts5DataRead(p, iId); |
| 256223 | + Fts5Data *pTerm = fts5LeafRead(p, iId); |
| 255990 | 256224 | if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){ |
| 255991 | 256225 | u8 *aTermIdx = &pTerm->p[pTerm->szLeaf]; |
| 255992 | 256226 | int nTermIdx = pTerm->nn - pTerm->szLeaf; |
| 255993 | 256227 | int iTermIdx = 0; |
| 255994 | 256228 | i64 iTermOff = 0; |
| | @@ -258936,11 +259170,11 @@ |
| 258936 | 259170 | int i; |
| 258937 | 259171 | |
| 258938 | 259172 | /* Now check that the iter.nEmpty leaves following the current leaf |
| 258939 | 259173 | ** (a) exist and (b) contain no terms. */ |
| 258940 | 259174 | for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){ |
| 258941 | | - Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i)); |
| 259175 | + Fts5Data *pLeaf = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i)); |
| 258942 | 259176 | if( pLeaf ){ |
| 258943 | 259177 | if( !fts5LeafIsTermless(pLeaf) |
| 258944 | 259178 | || (i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf)) |
| 258945 | 259179 | ){ |
| 258946 | 259180 | FTS5_CORRUPT_ROWID(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i)); |
| | @@ -263716,11 +263950,11 @@ |
| 263716 | 263950 | int nArg, /* Number of args */ |
| 263717 | 263951 | sqlite3_value **apUnused /* Function arguments */ |
| 263718 | 263952 | ){ |
| 263719 | 263953 | assert( nArg==0 ); |
| 263720 | 263954 | UNUSED_PARAM2(nArg, apUnused); |
| 263721 | | - sqlite3_result_text(pCtx, "fts5: 2026-07-07 14:53:53 c3b0fff1b76b8c354b34fef17ff639968698e97234d6ab7af15e0e1a835486e9", -1, SQLITE_TRANSIENT); |
| 263955 | + sqlite3_result_text(pCtx, "fts5: 2026-07-24 16:28:47 2f1f4f73535386549c12694dc57cfe555eec689ae6824c6241aaf8d5befcd74d", -1, SQLITE_TRANSIENT); |
| 263722 | 263956 | } |
| 263723 | 263957 | |
| 263724 | 263958 | /* |
| 263725 | 263959 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 263726 | 263960 | ** |
| 263727 | 263961 | |