summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2022-04-23 18:08:04 +0200
committerGravatar Vincent Rischmann2022-04-23 18:08:04 +0200
commit39dfec5e47c46a0b5e733322ddbc561968af6b5b (patch)
tree5dddbb799873ce8ec67213da023f61e079d109af
parentremove the 'opening' logs (diff)
downloadzig-sqlite-39dfec5e47c46a0b5e733322ddbc561968af6b5b.tar.gz
zig-sqlite-39dfec5e47c46a0b5e733322ddbc561968af6b5b.tar.xz
zig-sqlite-39dfec5e47c46a0b5e733322ddbc561968af6b5b.zip
update sqlite bundled source code to 3.38.2
Diffstat (limited to '')
-rw-r--r--c/sqlite3.c343
-rw-r--r--c/sqlite3.h16
2 files changed, 248 insertions, 111 deletions
diff --git a/c/sqlite3.c b/c/sqlite3.c
index 5394db4..0b227f0 100644
--- a/c/sqlite3.c
+++ b/c/sqlite3.c
@@ -1,6 +1,6 @@
1/****************************************************************************** 1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite 2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.38.0. By combining all the individual C code files into this 3** version 3.38.2. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation 4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be 5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements 6** possible if the files were compiled separately. Performance improvements
@@ -452,9 +452,9 @@ extern "C" {
452** [sqlite3_libversion_number()], [sqlite3_sourceid()], 452** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453** [sqlite_version()] and [sqlite_source_id()]. 453** [sqlite_version()] and [sqlite_source_id()].
454*/ 454*/
455#define SQLITE_VERSION "3.38.0" 455#define SQLITE_VERSION "3.38.2"
456#define SQLITE_VERSION_NUMBER 3038000 456#define SQLITE_VERSION_NUMBER 3038002
457#define SQLITE_SOURCE_ID "2022-02-22 18:58:40 40fa792d359f84c3b9e9d6623743e1a59826274e221df1bde8f47086968a1bab" 457#define SQLITE_SOURCE_ID "2022-03-26 13:51:10 d33c709cc0af66bc5b6dc6216eba9f1f0b40960b9ae83694c986fbf4c1d6f08f"
458 458
459/* 459/*
460** CAPI3REF: Run-Time Library Version Numbers 460** CAPI3REF: Run-Time Library Version Numbers
@@ -5285,6 +5285,10 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5285** even empty strings, are always zero-terminated. ^The return 5285** even empty strings, are always zero-terminated. ^The return
5286** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. 5286** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5287** 5287**
5288** ^Strings returned by sqlite3_column_text16() always have the endianness
5289** which is native to the platform, regardless of the text encoding set
5290** for the database.
5291**
5288** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an 5292** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5289** [unprotected sqlite3_value] object. In a multithreaded environment, 5293** [unprotected sqlite3_value] object. In a multithreaded environment,
5290** an unprotected sqlite3_value object may only be used safely with 5294** an unprotected sqlite3_value object may only be used safely with
@@ -5298,7 +5302,7 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5298** [application-defined SQL functions] or [virtual tables], not within 5302** [application-defined SQL functions] or [virtual tables], not within
5299** top-level application code. 5303** top-level application code.
5300** 5304**
5301** The these routines may attempt to convert the datatype of the result. 5305** These routines may attempt to convert the datatype of the result.
5302** ^For example, if the internal representation is FLOAT and a text result 5306** ^For example, if the internal representation is FLOAT and a text result
5303** is requested, [sqlite3_snprintf()] is used internally to perform the 5307** is requested, [sqlite3_snprintf()] is used internally to perform the
5304** conversion automatically. ^(The following table details the conversions 5308** conversion automatically. ^(The following table details the conversions
@@ -5323,7 +5327,7 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5323** <tr><td> TEXT <td> BLOB <td> No change 5327** <tr><td> TEXT <td> BLOB <td> No change
5324** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER 5328** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5325** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL 5329** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5326** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed 5330** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator
5327** </table> 5331** </table>
5328** </blockquote>)^ 5332** </blockquote>)^
5329** 5333**
@@ -10073,7 +10077,7 @@ SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10073** ^When xBestIndex returns, the sqlite3_value object returned by 10077** ^When xBestIndex returns, the sqlite3_value object returned by
10074** sqlite3_vtab_rhs_value() is automatically deallocated. 10078** sqlite3_vtab_rhs_value() is automatically deallocated.
10075** 10079**
10076** The "_rhs_" in the name of this routine is an appreviation for 10080** The "_rhs_" in the name of this routine is an abbreviation for
10077** "Right-Hand Side". 10081** "Right-Hand Side".
10078*/ 10082*/
10079SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); 10083SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
@@ -20364,7 +20368,12 @@ SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
20364SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*); 20368SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
20365SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *); 20369SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
20366SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); 20370SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
20371
20367SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); 20372SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20373#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
20374 && !defined(SQLITE_OMIT_VIRTUALTABLE)
20375SQLITE_PRIVATE void sqlite3VtabWriteAll(sqlite3_index_info*);
20376#endif
20368SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); 20377SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20369SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); 20378SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20370SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); 20379SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
@@ -23654,7 +23663,7 @@ static int toLocaltime(
23654 p->D = sLocal.tm_mday; 23663 p->D = sLocal.tm_mday;
23655 p->h = sLocal.tm_hour; 23664 p->h = sLocal.tm_hour;
23656 p->m = sLocal.tm_min; 23665 p->m = sLocal.tm_min;
23657 p->s = sLocal.tm_sec; 23666 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
23658 p->validYMD = 1; 23667 p->validYMD = 1;
23659 p->validHMS = 1; 23668 p->validHMS = 1;
23660 p->validJD = 0; 23669 p->validJD = 0;
@@ -39682,11 +39691,17 @@ static int unixShmLock(
39682 int flags /* What to do with the lock */ 39691 int flags /* What to do with the lock */
39683){ 39692){
39684 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ 39693 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
39685 unixShm *p = pDbFd->pShm; /* The shared memory being locked */ 39694 unixShm *p; /* The shared memory being locked */
39686 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ 39695 unixShmNode *pShmNode; /* The underlying file iNode */
39687 int rc = SQLITE_OK; /* Result code */ 39696 int rc = SQLITE_OK; /* Result code */
39688 u16 mask; /* Mask of locks to take or release */ 39697 u16 mask; /* Mask of locks to take or release */
39689 int *aLock = pShmNode->aLock; 39698 int *aLock;
39699
39700 p = pDbFd->pShm;
39701 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
39702 pShmNode = p->pShmNode;
39703 if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
39704 aLock = pShmNode->aLock;
39690 39705
39691 assert( pShmNode==pDbFd->pInode->pShmNode ); 39706 assert( pShmNode==pDbFd->pInode->pShmNode );
39692 assert( pShmNode->pInode==pDbFd->pInode ); 39707 assert( pShmNode->pInode==pDbFd->pInode );
@@ -46974,10 +46989,14 @@ static int winShmLock(
46974 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */ 46989 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
46975 winShm *p = pDbFd->pShm; /* The shared memory being locked */ 46990 winShm *p = pDbFd->pShm; /* The shared memory being locked */
46976 winShm *pX; /* For looping over all siblings */ 46991 winShm *pX; /* For looping over all siblings */
46977 winShmNode *pShmNode = p->pShmNode; 46992 winShmNode *pShmNode;
46978 int rc = SQLITE_OK; /* Result code */ 46993 int rc = SQLITE_OK; /* Result code */
46979 u16 mask; /* Mask of locks to take or release */ 46994 u16 mask; /* Mask of locks to take or release */
46980 46995
46996 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
46997 pShmNode = p->pShmNode;
46998 if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
46999
46981 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); 47000 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
46982 assert( n>=1 ); 47001 assert( n>=1 );
46983 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) 47002 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
@@ -50907,7 +50926,8 @@ SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
50907** make it so. 50926** make it so.
50908*/ 50927*/
50909SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ 50928SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
50910 assert( p->nRef>0 ); 50929 assert( p->nRef>0 || p->pCache->bPurgeable==0 );
50930 testcase( p->nRef==0 );
50911 assert( sqlite3PcachePageSanity(p) ); 50931 assert( sqlite3PcachePageSanity(p) );
50912 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/ 50932 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
50913 p->flags &= ~PGHDR_DONT_WRITE; 50933 p->flags &= ~PGHDR_DONT_WRITE;
@@ -56039,6 +56059,9 @@ static int pager_playback(Pager *pPager, int isHot){
56039 goto end_playback; 56059 goto end_playback;
56040 } 56060 }
56041 pPager->dbSize = mxPg; 56061 pPager->dbSize = mxPg;
56062 if( pPager->mxPgno<mxPg ){
56063 pPager->mxPgno = mxPg;
56064 }
56042 } 56065 }
56043 56066
56044 /* Copy original pages out of the journal and back into the 56067 /* Copy original pages out of the journal and back into the
@@ -65398,7 +65421,9 @@ struct MemPage {
65398 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */ 65421 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
65399 BtShared *pBt; /* Pointer to BtShared that this page is part of */ 65422 BtShared *pBt; /* Pointer to BtShared that this page is part of */
65400 u8 *aData; /* Pointer to disk image of the page data */ 65423 u8 *aData; /* Pointer to disk image of the page data */
65401 u8 *aDataEnd; /* One byte past the end of usable data */ 65424 u8 *aDataEnd; /* One byte past the end of the entire page - not just
65425 ** the usable space, the entire page. Used to prevent
65426 ** corruption-induced of buffer overflow. */
65402 u8 *aCellIdx; /* The cell index area */ 65427 u8 *aCellIdx; /* The cell index area */
65403 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ 65428 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
65404 DbPage *pDbPage; /* Pager page handle */ 65429 DbPage *pDbPage; /* Pager page handle */
@@ -68183,7 +68208,7 @@ static int btreeInitPage(MemPage *pPage){
68183 pPage->nOverflow = 0; 68208 pPage->nOverflow = 0;
68184 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize; 68209 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
68185 pPage->aCellIdx = data + pPage->childPtrSize + 8; 68210 pPage->aCellIdx = data + pPage->childPtrSize + 8;
68186 pPage->aDataEnd = pPage->aData + pBt->usableSize; 68211 pPage->aDataEnd = pPage->aData + pBt->pageSize;
68187 pPage->aDataOfst = pPage->aData + pPage->childPtrSize; 68212 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
68188 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the 68213 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
68189 ** number of cells on the page. */ 68214 ** number of cells on the page. */
@@ -68218,7 +68243,7 @@ static void zeroPage(MemPage *pPage, int flags){
68218 u8 hdr = pPage->hdrOffset; 68243 u8 hdr = pPage->hdrOffset;
68219 u16 first; 68244 u16 first;
68220 68245
68221 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); 68246 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
68222 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 68247 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
68223 assert( sqlite3PagerGetData(pPage->pDbPage) == data ); 68248 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
68224 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 68249 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -68234,7 +68259,7 @@ static void zeroPage(MemPage *pPage, int flags){
68234 pPage->nFree = (u16)(pBt->usableSize - first); 68259 pPage->nFree = (u16)(pBt->usableSize - first);
68235 decodeFlags(pPage, flags); 68260 decodeFlags(pPage, flags);
68236 pPage->cellOffset = first; 68261 pPage->cellOffset = first;
68237 pPage->aDataEnd = &data[pBt->usableSize]; 68262 pPage->aDataEnd = &data[pBt->pageSize];
68238 pPage->aCellIdx = &data[first]; 68263 pPage->aCellIdx = &data[first];
68239 pPage->aDataOfst = &data[pPage->childPtrSize]; 68264 pPage->aDataOfst = &data[pPage->childPtrSize];
68240 pPage->nOverflow = 0; 68265 pPage->nOverflow = 0;
@@ -68360,7 +68385,7 @@ static int getAndInitPage(
68360 goto getAndInitPage_error2; 68385 goto getAndInitPage_error2;
68361 } 68386 }
68362 } 68387 }
68363 assert( (*ppPage)->pgno==pgno ); 68388 assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
68364 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); 68389 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
68365 68390
68366 /* If obtaining a child page for a cursor, we must verify that the page is 68391 /* If obtaining a child page for a cursor, we must verify that the page is
@@ -71447,7 +71472,7 @@ static int moveToRoot(BtCursor *pCur){
71447 pCur->curIntKey = pCur->pPage->intKey; 71472 pCur->curIntKey = pCur->pPage->intKey;
71448 } 71473 }
71449 pRoot = pCur->pPage; 71474 pRoot = pCur->pPage;
71450 assert( pRoot->pgno==pCur->pgnoRoot ); 71475 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
71451 71476
71452 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor 71477 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
71453 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is 71478 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
@@ -74978,24 +75003,6 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
74978 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags ); 75003 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
74979 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 ); 75004 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
74980 75005
74981 if( pCur->eState==CURSOR_FAULT ){
74982 assert( pCur->skipNext!=SQLITE_OK );
74983 return pCur->skipNext;
74984 }
74985
74986 assert( cursorOwnsBtShared(pCur) );
74987 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
74988 && pBt->inTransaction==TRANS_WRITE
74989 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
74990 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
74991
74992 /* Assert that the caller has been consistent. If this cursor was opened
74993 ** expecting an index b-tree, then the caller should be inserting blob
74994 ** keys with no associated data. If the cursor was opened expecting an
74995 ** intkey table, the caller should be inserting integer keys with a
74996 ** blob of associated data. */
74997 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
74998
74999 /* Save the positions of any other cursors open on this table. 75006 /* Save the positions of any other cursors open on this table.
75000 ** 75007 **
75001 ** In some cases, the call to btreeMoveto() below is a no-op. For 75008 ** In some cases, the call to btreeMoveto() below is a no-op. For
@@ -75020,6 +75027,24 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
75020 } 75027 }
75021 } 75028 }
75022 75029
75030 if( pCur->eState>=CURSOR_REQUIRESEEK ){
75031 rc = moveToRoot(pCur);
75032 if( rc && rc!=SQLITE_EMPTY ) return rc;
75033 }
75034
75035 assert( cursorOwnsBtShared(pCur) );
75036 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
75037 && pBt->inTransaction==TRANS_WRITE
75038 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
75039 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
75040
75041 /* Assert that the caller has been consistent. If this cursor was opened
75042 ** expecting an index b-tree, then the caller should be inserting blob
75043 ** keys with no associated data. If the cursor was opened expecting an
75044 ** intkey table, the caller should be inserting integer keys with a
75045 ** blob of associated data. */
75046 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
75047
75023 if( pCur->pKeyInfo==0 ){ 75048 if( pCur->pKeyInfo==0 ){
75024 assert( pX->pKey==0 ); 75049 assert( pX->pKey==0 );
75025 /* If this is an insert into a table b-tree, invalidate any incrblob 75050 /* If this is an insert into a table b-tree, invalidate any incrblob
@@ -75108,14 +75133,13 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
75108 } 75133 }
75109 } 75134 }
75110 assert( pCur->eState==CURSOR_VALID 75135 assert( pCur->eState==CURSOR_VALID
75111 || (pCur->eState==CURSOR_INVALID && loc) 75136 || (pCur->eState==CURSOR_INVALID && loc) );
75112 || CORRUPT_DB );
75113 75137
75114 pPage = pCur->pPage; 75138 pPage = pCur->pPage;
75115 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) ); 75139 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
75116 assert( pPage->leaf || !pPage->intKey ); 75140 assert( pPage->leaf || !pPage->intKey );
75117 if( pPage->nFree<0 ){ 75141 if( pPage->nFree<0 ){
75118 if( NEVER(pCur->eState>CURSOR_INVALID) ){ 75142 if( pCur->eState>CURSOR_INVALID ){
75119 rc = SQLITE_CORRUPT_BKPT; 75143 rc = SQLITE_CORRUPT_BKPT;
75120 }else{ 75144 }else{
75121 rc = btreeComputeFreeSpace(pPage); 75145 rc = btreeComputeFreeSpace(pPage);
@@ -75396,12 +75420,16 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
75396 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); 75420 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
75397 assert( !hasReadConflicts(p, pCur->pgnoRoot) ); 75421 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
75398 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 ); 75422 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
75399 if( pCur->eState==CURSOR_REQUIRESEEK ){ 75423 if( pCur->eState!=CURSOR_VALID ){
75400 rc = btreeRestoreCursorPosition(pCur); 75424 if( pCur->eState>=CURSOR_REQUIRESEEK ){
75401 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID ); 75425 rc = btreeRestoreCursorPosition(pCur);
75402 if( rc || pCur->eState!=CURSOR_VALID ) return rc; 75426 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
75427 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
75428 }else{
75429 return SQLITE_CORRUPT_BKPT;
75430 }
75403 } 75431 }
75404 assert( CORRUPT_DB || pCur->eState==CURSOR_VALID ); 75432 assert( pCur->eState==CURSOR_VALID );
75405 75433
75406 iCellDepth = pCur->iPage; 75434 iCellDepth = pCur->iPage;
75407 iCellIdx = pCur->ix; 75435 iCellIdx = pCur->ix;
@@ -78736,6 +78764,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
78736 void (*xDestructor)(void*) 78764 void (*xDestructor)(void*)
78737){ 78765){
78738 assert( pMem->flags==MEM_Null ); 78766 assert( pMem->flags==MEM_Null );
78767 vdbeMemClear(pMem);
78739 pMem->u.zPType = zPType ? zPType : ""; 78768 pMem->u.zPType = zPType ? zPType : "";
78740 pMem->z = pPtr; 78769 pMem->z = pPtr;
78741 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term; 78770 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
@@ -89990,10 +90019,18 @@ case OP_Offset: { /* out3 */
89990 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 90019 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89991 pC = p->apCsr[pOp->p1]; 90020 pC = p->apCsr[pOp->p1];
89992 pOut = &p->aMem[pOp->p3]; 90021 pOut = &p->aMem[pOp->p3];
89993 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){ 90022 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
89994 sqlite3VdbeMemSetNull(pOut); 90023 sqlite3VdbeMemSetNull(pOut);
89995 }else{ 90024 }else{
89996 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); 90025 if( pC->deferredMoveto ){
90026 rc = sqlite3VdbeFinishMoveto(pC);
90027 if( rc ) goto abort_due_to_error;
90028 }
90029 if( sqlite3BtreeEof(pC->uc.pCursor) ){
90030 sqlite3VdbeMemSetNull(pOut);
90031 }else{
90032 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
90033 }
89997 } 90034 }
89998 break; 90035 break;
89999} 90036}
@@ -93072,6 +93109,10 @@ case OP_Rowid: { /* out2 */
93072** Move the cursor P1 to a null row. Any OP_Column operations 93109** Move the cursor P1 to a null row. Any OP_Column operations
93073** that occur while the cursor is on the null row will always 93110** that occur while the cursor is on the null row will always
93074** write a NULL. 93111** write a NULL.
93112**
93113** Or, if P1 is a Pseudo-Cursor (a cursor opened using OP_OpenPseudo)
93114** just reset the cache for that cursor. This causes the row of
93115** content held by the pseudo-cursor to be reparsed.
93075*/ 93116*/
93076case OP_NullRow: { 93117case OP_NullRow: {
93077 VdbeCursor *pC; 93118 VdbeCursor *pC;
@@ -109918,19 +109959,21 @@ static int renameParseSql(
109918){ 109959){
109919 int rc; 109960 int rc;
109920 109961
109921 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109922
109923 /* Parse the SQL statement passed as the first argument. If no error
109924 ** occurs and the parse does not result in a new table, index or
109925 ** trigger object, the database must be corrupt. */
109926 sqlite3ParseObjectInit(p, db); 109962 sqlite3ParseObjectInit(p, db);
109963 if( zSql==0 ){
109964 return SQLITE_NOMEM;
109965 }
109966 if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){
109967 return SQLITE_CORRUPT_BKPT;
109968 }
109969 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109927 p->eParseMode = PARSE_MODE_RENAME; 109970 p->eParseMode = PARSE_MODE_RENAME;
109928 p->db = db; 109971 p->db = db;
109929 p->nQueryLoop = 1; 109972 p->nQueryLoop = 1;
109930 rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM; 109973 rc = sqlite3RunParser(p, zSql);
109931 if( db->mallocFailed ) rc = SQLITE_NOMEM; 109974 if( db->mallocFailed ) rc = SQLITE_NOMEM;
109932 if( rc==SQLITE_OK 109975 if( rc==SQLITE_OK
109933 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0 109976 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
109934 ){ 109977 ){
109935 rc = SQLITE_CORRUPT_BKPT; 109978 rc = SQLITE_CORRUPT_BKPT;
109936 } 109979 }
@@ -116679,6 +116722,11 @@ SQLITE_PRIVATE void sqlite3EndTable(
116679 int addrInsLoop; /* Top of the loop for inserting rows */ 116722 int addrInsLoop; /* Top of the loop for inserting rows */
116680 Table *pSelTab; /* A table that describes the SELECT results */ 116723 Table *pSelTab; /* A table that describes the SELECT results */
116681 116724
116725 if( IN_SPECIAL_PARSE ){
116726 pParse->rc = SQLITE_ERROR;
116727 pParse->nErr++;
116728 return;
116729 }
116682 regYield = ++pParse->nMem; 116730 regYield = ++pParse->nMem;
116683 regRec = ++pParse->nMem; 116731 regRec = ++pParse->nMem;
116684 regRowid = ++pParse->nMem; 116732 regRowid = ++pParse->nMem;
@@ -123258,8 +123306,8 @@ SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
123258 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 123306 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
123259 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 123307 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
123260#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 123308#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
123261 FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET| 123309 {1, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_OFFSET|SQLITE_FUNC_TYPEOF,
123262 SQLITE_FUNC_TYPEOF), 123310 0, 0, noopFunc, 0, 0, 0, "sqlite_offset", {0} },
123263#endif 123311#endif
123264 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 123312 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
123265 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 123313 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
@@ -125063,7 +125111,7 @@ SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
125063 } 125111 }
125064 125112
125065 for(i=j=0; i<pTab->nCol; i++){ 125113 for(i=j=0; i<pTab->nCol; i++){
125066 assert( pTab->aCol[i].affinity!=0 ); 125114 assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
125067 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ 125115 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
125068 zColAff[j++] = pTab->aCol[i].affinity; 125116 zColAff[j++] = pTab->aCol[i].affinity;
125069 } 125117 }
@@ -125677,7 +125725,11 @@ SQLITE_PRIVATE void sqlite3Insert(
125677 ** 125725 **
125678 ** This is the 2nd template. 125726 ** This is the 2nd template.
125679 */ 125727 */
125680 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ 125728 if( pColumn==0
125729 && pSelect!=0
125730 && pTrigger==0
125731 && xferOptimization(pParse, pTab, pSelect, onError, iDb)
125732 ){
125681 assert( !pTrigger ); 125733 assert( !pTrigger );
125682 assert( pList==0 ); 125734 assert( pList==0 );
125683 goto insert_end; 125735 goto insert_end;
@@ -127648,18 +127700,13 @@ static int xferOptimization(
127648 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ 127700 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
127649 int regData, regRowid; /* Registers holding data and rowid */ 127701 int regData, regRowid; /* Registers holding data and rowid */
127650 127702
127651 if( pSelect==0 ){ 127703 assert( pSelect!=0 );
127652 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
127653 }
127654 if( pParse->pWith || pSelect->pWith ){ 127704 if( pParse->pWith || pSelect->pWith ){
127655 /* Do not attempt to process this query if there are an WITH clauses 127705 /* Do not attempt to process this query if there are an WITH clauses
127656 ** attached to it. Proceeding may generate a false "no such table: xxx" 127706 ** attached to it. Proceeding may generate a false "no such table: xxx"
127657 ** error if pSelect reads from a CTE named "xxx". */ 127707 ** error if pSelect reads from a CTE named "xxx". */
127658 return 0; 127708 return 0;
127659 } 127709 }
127660 if( sqlite3TriggerList(pParse, pDest) ){
127661 return 0; /* tab1 must not have triggers */
127662 }
127663#ifndef SQLITE_OMIT_VIRTUALTABLE 127710#ifndef SQLITE_OMIT_VIRTUALTABLE
127664 if( IsVirtual(pDest) ){ 127711 if( IsVirtual(pDest) ){
127665 return 0; /* tab1 must not be a virtual table */ 127712 return 0; /* tab1 must not be a virtual table */
@@ -133505,7 +133552,7 @@ SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFl
133505 sqlite3ResetAllSchemasOfConnection(db); 133552 sqlite3ResetAllSchemasOfConnection(db);
133506 pDb = &db->aDb[iDb]; 133553 pDb = &db->aDb[iDb];
133507 }else 133554 }else
133508 if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){ 133555 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
133509 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider 133556 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
133510 ** the schema loaded, even if errors (other than OOM) occurred. In 133557 ** the schema loaded, even if errors (other than OOM) occurred. In
133511 ** this situation the current sqlite3_prepare() operation will fail, 133558 ** this situation the current sqlite3_prepare() operation will fail,
@@ -142372,6 +142419,7 @@ static TriggerStep *triggerStepAllocate(
142372 sqlite3 *db = pParse->db; 142419 sqlite3 *db = pParse->db;
142373 TriggerStep *pTriggerStep; 142420 TriggerStep *pTriggerStep;
142374 142421
142422 if( pParse->nErr ) return 0;
142375 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1); 142423 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
142376 if( pTriggerStep ){ 142424 if( pTriggerStep ){
142377 char *z = (char*)&pTriggerStep[1]; 142425 char *z = (char*)&pTriggerStep[1];
@@ -146250,6 +146298,7 @@ SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
146250 146298
146251 sqlite3ParseObjectInit(&sParse, db); 146299 sqlite3ParseObjectInit(&sParse, db);
146252 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB; 146300 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
146301 sParse.disableTriggers = 1;
146253 /* We should never be able to reach this point while loading the 146302 /* We should never be able to reach this point while loading the
146254 ** schema. Nevertheless, defend against that (turn off db->init.busy) 146303 ** schema. Nevertheless, defend against that (turn off db->init.busy)
146255 ** in case a bug arises. */ 146304 ** in case a bug arises. */
@@ -148185,6 +148234,7 @@ static int codeAllEqualityTerms(
148185 VdbeCoverageIf(v, bRev!=0); 148234 VdbeCoverageIf(v, bRev!=0);
148186 VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); 148235 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
148187 j = sqlite3VdbeAddOp0(v, OP_Goto); 148236 j = sqlite3VdbeAddOp0(v, OP_Goto);
148237 assert( pLevel->addrSkip==0 );
148188 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), 148238 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
148189 iIdxCur, 0, regBase, nSkip); 148239 iIdxCur, 0, regBase, nSkip);
148190 VdbeCoverageIf(v, bRev==0); 148240 VdbeCoverageIf(v, bRev==0);
@@ -148782,6 +148832,7 @@ static SQLITE_NOINLINE void filterPullDown(
148782 WhereLevel *pLevel = &pWInfo->a[iLevel]; 148832 WhereLevel *pLevel = &pWInfo->a[iLevel];
148783 WhereLoop *pLoop = pLevel->pWLoop; 148833 WhereLoop *pLoop = pLevel->pWLoop;
148784 if( pLevel->regFilter==0 ) continue; 148834 if( pLevel->regFilter==0 ) continue;
148835 if( pLevel->pWLoop->nSkip ) continue;
148785 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set 148836 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
148786 ** vvvvv--' pLevel->regFilter if this were true. */ 148837 ** vvvvv--' pLevel->regFilter if this were true. */
148787 if( NEVER(pLoop->prereq & notReady) ) continue; 148838 if( NEVER(pLoop->prereq & notReady) ) continue;
@@ -148916,7 +148967,6 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
148916 int iReg; /* P3 Value for OP_VFilter */ 148967 int iReg; /* P3 Value for OP_VFilter */
148917 int addrNotFound; 148968 int addrNotFound;
148918 int nConstraint = pLoop->nLTerm; 148969 int nConstraint = pLoop->nLTerm;
148919 int iIn; /* Counter for IN constraints */
148920 148970
148921 iReg = sqlite3GetTempRange(pParse, nConstraint+2); 148971 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
148922 addrNotFound = pLevel->addrBrk; 148972 addrNotFound = pLevel->addrBrk;
@@ -148962,50 +149012,54 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
148962 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext; 149012 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
148963 pLevel->p2 = sqlite3VdbeCurrentAddr(v); 149013 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
148964 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); 149014 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
148965 if( pLoop->wsFlags & WHERE_IN_ABLE ){ 149015
148966 iIn = pLevel->u.in.nIn; 149016 for(j=0; j<nConstraint; j++){
148967 }else{
148968 iIn = 0;
148969 }
148970 for(j=nConstraint-1; j>=0; j--){
148971 int bIn; /* True to generate byte code to loop over RHS IN values */
148972 pTerm = pLoop->aLTerm[j]; 149017 pTerm = pLoop->aLTerm[j];
149018 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
149019 disableTerm(pLevel, pTerm);
149020 continue;
149021 }
148973 if( (pTerm->eOperator & WO_IN)!=0 149022 if( (pTerm->eOperator & WO_IN)!=0
148974 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0 149023 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
149024 && !db->mallocFailed
148975 ){ 149025 ){
148976 bIn = 1;
148977 }else{
148978 bIn = 0;
148979 }
148980 if( bIn ) iIn--;
148981 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
148982 disableTerm(pLevel, pTerm);
148983 }else if( bIn && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1 ){
148984 Expr *pCompare; /* The comparison operator */ 149026 Expr *pCompare; /* The comparison operator */
148985 Expr *pRight; /* RHS of the comparison */ 149027 Expr *pRight; /* RHS of the comparison */
148986 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ 149028 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
149029 int iIn; /* IN loop corresponding to the j-th constraint */
148987 149030
148988 /* Reload the constraint value into reg[iReg+j+2]. The same value 149031 /* Reload the constraint value into reg[iReg+j+2]. The same value
148989 ** was loaded into the same register prior to the OP_VFilter, but 149032 ** was loaded into the same register prior to the OP_VFilter, but
148990 ** the xFilter implementation might have changed the datatype or 149033 ** the xFilter implementation might have changed the datatype or
148991 ** encoding of the value in the register, so it *must* be reloaded. */ 149034 ** encoding of the value in the register, so it *must* be reloaded.
148992 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed ); 149035 */
148993 if( !db->mallocFailed ){ 149036 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
148994 assert( iIn>=0 && iIn<pLevel->u.in.nIn );
148995 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop); 149037 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
148996 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid ); 149038 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
148997 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 ); 149039 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
148998 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 ); 149040 ){
148999 testcase( pOp->opcode==OP_Rowid ); 149041 testcase( pOp->opcode==OP_Rowid );
149000 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); 149042 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
149043 break;
149044 }
149001 } 149045 }
149002 149046
149003 /* Generate code that will continue to the next row if 149047 /* Generate code that will continue to the next row if
149004 ** the IN constraint is not satisfied */ 149048 ** the IN constraint is not satisfied
149049 */
149005 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0); 149050 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
149006 assert( pCompare!=0 || db->mallocFailed ); 149051 if( !db->mallocFailed ){
149007 if( pCompare ){ 149052 int iFld = pTerm->u.x.iField;
149008 pCompare->pLeft = pTerm->pExpr->pLeft; 149053 Expr *pLeft = pTerm->pExpr->pLeft;
149054 assert( pLeft!=0 );
149055 if( iFld>0 ){
149056 assert( pLeft->op==TK_VECTOR );
149057 assert( ExprUseXList(pLeft) );
149058 assert( iFld<=pLeft->x.pList->nExpr );
149059 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
149060 }else{
149061 pCompare->pLeft = pLeft;
149062 }
149009 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0); 149063 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
149010 if( pRight ){ 149064 if( pRight ){
149011 pRight->iTable = iReg+j+2; 149065 pRight->iTable = iReg+j+2;
@@ -149014,11 +149068,11 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
149014 ); 149068 );
149015 } 149069 }
149016 pCompare->pLeft = 0; 149070 pCompare->pLeft = 0;
149017 sqlite3ExprDelete(db, pCompare);
149018 } 149071 }
149072 sqlite3ExprDelete(db, pCompare);
149019 } 149073 }
149020 } 149074 }
149021 assert( iIn==0 || db->mallocFailed ); 149075
149022 /* These registers need to be preserved in case there is an IN operator 149076 /* These registers need to be preserved in case there is an IN operator
149023 ** loop. So we could deallocate the registers here (and potentially 149077 ** loop. So we could deallocate the registers here (and potentially
149024 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems 149078 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
@@ -149728,6 +149782,14 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
149728 ** the initialization of the right-hand operand of the vector comparison 149782 ** the initialization of the right-hand operand of the vector comparison
149729 ** might not occur, or might occur only in an OR branch that is not 149783 ** might not occur, or might occur only in an OR branch that is not
149730 ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1. 149784 ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
149785 **
149786 ** 2022-03-03: Do not push down expressions that involve subqueries.
149787 ** The subquery might get coded as a subroutine. Any table-references
149788 ** in the subquery might be resolved to index-references for the index on
149789 ** the OR branch in which the subroutine is coded. But if the subroutine
149790 ** is invoked from a different OR branch that uses a different index, such
149791 ** index-references will not work. tag-20220303a
149792 ** https://sqlite.org/forum/forumpost/36937b197273d403
149731 */ 149793 */
149732 if( pWC->nTerm>1 ){ 149794 if( pWC->nTerm>1 ){
149733 int iTerm; 149795 int iTerm;
@@ -149741,7 +149803,7 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
149741 continue; 149803 continue;
149742 } 149804 }
149743 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; 149805 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
149744 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); 149806 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
149745 pExpr = sqlite3ExprDup(db, pExpr, 0); 149807 pExpr = sqlite3ExprDup(db, pExpr, 0);
149746 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); 149808 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
149747 } 149809 }
@@ -153018,7 +153080,10 @@ static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
153018 pLoop->wsFlags &= ~WHERE_BLOOMFILTER; 153080 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
153019 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break; 153081 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
153020 while( ++iLevel < pWInfo->nLevel ){ 153082 while( ++iLevel < pWInfo->nLevel ){
153083 const SrcItem *pTabItem;
153021 pLevel = &pWInfo->a[iLevel]; 153084 pLevel = &pWInfo->a[iLevel];
153085 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
153086 if( pTabItem->fg.jointype & JT_LEFT ) continue;
153022 pLoop = pLevel->pWLoop; 153087 pLoop = pLevel->pWLoop;
153023 if( NEVER(pLoop==0) ) continue; 153088 if( NEVER(pLoop==0) ) continue;
153024 if( pLoop->prereq & notReady ) continue; 153089 if( pLoop->prereq & notReady ) continue;
@@ -154531,8 +154596,17 @@ static void whereLoopOutputAdjust(
154531 /* If there are extra terms in the WHERE clause not used by an index 154596 /* If there are extra terms in the WHERE clause not used by an index
154532 ** that depend only on the table being scanned, and that will tend to 154597 ** that depend only on the table being scanned, and that will tend to
154533 ** cause many rows to be omitted, then mark that table as 154598 ** cause many rows to be omitted, then mark that table as
154534 ** "self-culling". */ 154599 ** "self-culling".
154535 pLoop->wsFlags |= WHERE_SELFCULL; 154600 **
154601 ** 2022-03-24: Self-culling only applies if either the extra terms
154602 ** are straight comparison operators that are non-true with NULL
154603 ** operand, or if the loop is not a LEFT JOIN.
154604 */
154605 if( (pTerm->eOperator & 0x3f)!=0
154606 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0
154607 ){
154608 pLoop->wsFlags |= WHERE_SELFCULL;
154609 }
154536 } 154610 }
154537 if( pTerm->truthProb<=0 ){ 154611 if( pTerm->truthProb<=0 ){
154538 /* If a truth probability is specified using the likelihood() hints, 154612 /* If a truth probability is specified using the likelihood() hints,
@@ -155701,7 +155775,6 @@ SQLITE_API int sqlite3_vtab_rhs_value(
155701 return rc; 155775 return rc;
155702} 155776}
155703 155777
155704
155705/* 155778/*
155706** Return true if ORDER BY clause may be handled as DISTINCT. 155779** Return true if ORDER BY clause may be handled as DISTINCT.
155707*/ 155780*/
@@ -155713,6 +155786,22 @@ SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){
155713 return pHidden->eDistinct; 155786 return pHidden->eDistinct;
155714} 155787}
155715 155788
155789#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
155790 && !defined(SQLITE_OMIT_VIRTUALTABLE)
155791/*
155792** Cause the prepared statement that is associated with a call to
155793** xBestIndex to open write transactions on all attached schemas.
155794** This is used by the (built-in) sqlite_dbpage virtual table.
155795*/
155796SQLITE_PRIVATE void sqlite3VtabWriteAll(sqlite3_index_info *pIdxInfo){
155797 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
155798 Parse *pParse = pHidden->pParse;
155799 int nDb = pParse->db->nDb;
155800 int i;
155801 for(i=0; i<nDb; i++) sqlite3BeginWriteOperation(pParse, 0, i);
155802}
155803#endif
155804
155716/* 155805/*
155717** Add all WhereLoop objects for a table of the join identified by 155806** Add all WhereLoop objects for a table of the join identified by
155718** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. 155807** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
@@ -157816,6 +157905,26 @@ whereBeginError:
157816 } 157905 }
157817#endif 157906#endif
157818 157907
157908#ifdef SQLITE_DEBUG
157909/*
157910** Return true if cursor iCur is opened by instruction k of the
157911** bytecode. Used inside of assert() only.
157912*/
157913static int cursorIsOpen(Vdbe *v, int iCur, int k){
157914 while( k>=0 ){
157915 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
157916 if( pOp->p1!=iCur ) continue;
157917 if( pOp->opcode==OP_Close ) return 0;
157918 if( pOp->opcode==OP_OpenRead ) return 1;
157919 if( pOp->opcode==OP_OpenWrite ) return 1;
157920 if( pOp->opcode==OP_OpenDup ) return 1;
157921 if( pOp->opcode==OP_OpenAutoindex ) return 1;
157922 if( pOp->opcode==OP_OpenEphemeral ) return 1;
157923 }
157924 return 0;
157925}
157926#endif /* SQLITE_DEBUG */
157927
157819/* 157928/*
157820** Generate the end of the WHERE loop. See comments on 157929** Generate the end of the WHERE loop. See comments on
157821** sqlite3WhereBegin() for additional information. 157930** sqlite3WhereBegin() for additional information.
@@ -158068,6 +158177,11 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
158068 ){ 158177 ){
158069 int x = pOp->p2; 158178 int x = pOp->p2;
158070 assert( pIdx->pTable==pTab ); 158179 assert( pIdx->pTable==pTab );
158180#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
158181 if( pOp->opcode==OP_Offset ){
158182 /* Do not need to translate the column number */
158183 }else
158184#endif
158071 if( !HasRowid(pTab) ){ 158185 if( !HasRowid(pTab) ){
158072 Index *pPk = sqlite3PrimaryKeyIndex(pTab); 158186 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
158073 x = pPk->aiColumn[x]; 158187 x = pPk->aiColumn[x];
@@ -158081,9 +158195,22 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
158081 pOp->p2 = x; 158195 pOp->p2 = x;
158082 pOp->p1 = pLevel->iIdxCur; 158196 pOp->p1 = pLevel->iIdxCur;
158083 OpcodeRewriteTrace(db, k, pOp); 158197 OpcodeRewriteTrace(db, k, pOp);
158198 }else{
158199 /* Unable to translate the table reference into an index
158200 ** reference. Verify that this is harmless - that the
158201 ** table being referenced really is open.
158202 */
158203#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
158204 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
158205 || cursorIsOpen(v,pOp->p1,k)
158206 || pOp->opcode==OP_Offset
158207 );
158208#else
158209 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
158210 || cursorIsOpen(v,pOp->p1,k)
158211 );
158212#endif
158084 } 158213 }
158085 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
158086 || pWInfo->eOnePass );
158087 }else if( pOp->opcode==OP_Rowid ){ 158214 }else if( pOp->opcode==OP_Rowid ){
158088 pOp->p1 = pLevel->iIdxCur; 158215 pOp->p1 = pLevel->iIdxCur;
158089 pOp->opcode = OP_IdxRowid; 158216 pOp->opcode = OP_IdxRowid;
@@ -159071,7 +159198,11 @@ static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
159071*/ 159198*/
159072SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){ 159199SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
159073 int rc = SQLITE_OK; 159200 int rc = SQLITE_OK;
159074 if( p->pWin && p->pPrior==0 && ALWAYS((p->selFlags & SF_WinRewrite)==0) ){ 159201 if( p->pWin
159202 && p->pPrior==0
159203 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
159204 && ALWAYS(!IN_RENAME_OBJECT)
159205 ){
159075 Vdbe *v = sqlite3GetVdbe(pParse); 159206 Vdbe *v = sqlite3GetVdbe(pParse);
159076 sqlite3 *db = pParse->db; 159207 sqlite3 *db = pParse->db;
159077 Select *pSub = 0; /* The subquery */ 159208 Select *pSub = 0; /* The subquery */
@@ -159146,6 +159277,7 @@ SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
159146 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ 159277 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
159147 ExprList *pArgs; 159278 ExprList *pArgs;
159148 assert( ExprUseXList(pWin->pOwner) ); 159279 assert( ExprUseXList(pWin->pOwner) );
159280 assert( pWin->pFunc!=0 );
159149 pArgs = pWin->pOwner->x.pList; 159281 pArgs = pWin->pOwner->x.pList;
159150 if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){ 159282 if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
159151 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist); 159283 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
@@ -209957,6 +210089,7 @@ static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
209957 ){ 210089 ){
209958 pIdxInfo->orderByConsumed = 1; 210090 pIdxInfo->orderByConsumed = 1;
209959 } 210091 }
210092 sqlite3VtabWriteAll(pIdxInfo);
209960 return SQLITE_OK; 210093 return SQLITE_OK;
209961} 210094}
209962 210095
@@ -232311,7 +232444,7 @@ static int fts5SorterNext(Fts5Cursor *pCsr){
232311 rc = sqlite3_step(pSorter->pStmt); 232444 rc = sqlite3_step(pSorter->pStmt);
232312 if( rc==SQLITE_DONE ){ 232445 if( rc==SQLITE_DONE ){
232313 rc = SQLITE_OK; 232446 rc = SQLITE_OK;
232314 CsrFlagSet(pCsr, FTS5CSR_EOF); 232447 CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT);
232315 }else if( rc==SQLITE_ROW ){ 232448 }else if( rc==SQLITE_ROW ){
232316 const u8 *a; 232449 const u8 *a;
232317 const u8 *aBlob; 232450 const u8 *aBlob;
@@ -234300,7 +234433,7 @@ static void fts5SourceIdFunc(
234300){ 234433){
234301 assert( nArg==0 ); 234434 assert( nArg==0 );
234302 UNUSED_PARAM2(nArg, apUnused); 234435 UNUSED_PARAM2(nArg, apUnused);
234303 sqlite3_result_text(pCtx, "fts5: 2022-02-22 18:58:40 40fa792d359f84c3b9e9d6623743e1a59826274e221df1bde8f47086968a1bab", -1, SQLITE_TRANSIENT); 234436 sqlite3_result_text(pCtx, "fts5: 2022-03-26 13:51:10 d33c709cc0af66bc5b6dc6216eba9f1f0b40960b9ae83694c986fbf4c1d6f08f", -1, SQLITE_TRANSIENT);
234304} 234437}
234305 234438
234306/* 234439/*
diff --git a/c/sqlite3.h b/c/sqlite3.h
index 34636b9..33dbec2 100644
--- a/c/sqlite3.h
+++ b/c/sqlite3.h
@@ -146,9 +146,9 @@ extern "C" {
146** [sqlite3_libversion_number()], [sqlite3_sourceid()], 146** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147** [sqlite_version()] and [sqlite_source_id()]. 147** [sqlite_version()] and [sqlite_source_id()].
148*/ 148*/
149#define SQLITE_VERSION "3.38.0" 149#define SQLITE_VERSION "3.38.2"
150#define SQLITE_VERSION_NUMBER 3038000 150#define SQLITE_VERSION_NUMBER 3038002
151#define SQLITE_SOURCE_ID "2022-02-22 18:58:40 40fa792d359f84c3b9e9d6623743e1a59826274e221df1bde8f47086968a1bab" 151#define SQLITE_SOURCE_ID "2022-03-26 13:51:10 d33c709cc0af66bc5b6dc6216eba9f1f0b40960b9ae83694c986fbf4c1d6f08f"
152 152
153/* 153/*
154** CAPI3REF: Run-Time Library Version Numbers 154** CAPI3REF: Run-Time Library Version Numbers
@@ -4979,6 +4979,10 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
4979** even empty strings, are always zero-terminated. ^The return 4979** even empty strings, are always zero-terminated. ^The return
4980** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. 4980** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4981** 4981**
4982** ^Strings returned by sqlite3_column_text16() always have the endianness
4983** which is native to the platform, regardless of the text encoding set
4984** for the database.
4985**
4982** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an 4986** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
4983** [unprotected sqlite3_value] object. In a multithreaded environment, 4987** [unprotected sqlite3_value] object. In a multithreaded environment,
4984** an unprotected sqlite3_value object may only be used safely with 4988** an unprotected sqlite3_value object may only be used safely with
@@ -4992,7 +4996,7 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
4992** [application-defined SQL functions] or [virtual tables], not within 4996** [application-defined SQL functions] or [virtual tables], not within
4993** top-level application code. 4997** top-level application code.
4994** 4998**
4995** The these routines may attempt to convert the datatype of the result. 4999** These routines may attempt to convert the datatype of the result.
4996** ^For example, if the internal representation is FLOAT and a text result 5000** ^For example, if the internal representation is FLOAT and a text result
4997** is requested, [sqlite3_snprintf()] is used internally to perform the 5001** is requested, [sqlite3_snprintf()] is used internally to perform the
4998** conversion automatically. ^(The following table details the conversions 5002** conversion automatically. ^(The following table details the conversions
@@ -5017,7 +5021,7 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5017** <tr><td> TEXT <td> BLOB <td> No change 5021** <tr><td> TEXT <td> BLOB <td> No change
5018** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER 5022** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5019** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL 5023** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5020** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed 5024** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator
5021** </table> 5025** </table>
5022** </blockquote>)^ 5026** </blockquote>)^
5023** 5027**
@@ -9767,7 +9771,7 @@ SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
9767** ^When xBestIndex returns, the sqlite3_value object returned by 9771** ^When xBestIndex returns, the sqlite3_value object returned by
9768** sqlite3_vtab_rhs_value() is automatically deallocated. 9772** sqlite3_vtab_rhs_value() is automatically deallocated.
9769** 9773**
9770** The "_rhs_" in the name of this routine is an appreviation for 9774** The "_rhs_" in the name of this routine is an abbreviation for
9771** "Right-Hand Side". 9775** "Right-Hand Side".
9772*/ 9776*/
9773SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); 9777SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);