koffi 0.9.26 → 0.9.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  /******************************************************************************
2
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.3. By combining all the individual C code files into this
4
4
  ** single large file, the entire code can be compiled as a single translation
5
5
  ** unit. This allows many compilers to do optimizations that would not be
6
6
  ** possible if the files were compiled separately. Performance improvements
@@ -452,9 +452,9 @@ extern "C" {
452
452
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453
453
  ** [sqlite_version()] and [sqlite_source_id()].
454
454
  */
455
- #define SQLITE_VERSION "3.38.0"
456
- #define SQLITE_VERSION_NUMBER 3038000
457
- #define SQLITE_SOURCE_ID "2022-02-22 18:58:40 40fa792d359f84c3b9e9d6623743e1a59826274e221df1bde8f47086968a1bab"
455
+ #define SQLITE_VERSION "3.38.3"
456
+ #define SQLITE_VERSION_NUMBER 3038003
457
+ #define SQLITE_SOURCE_ID "2022-04-27 12:03:15 9547e2c38a1c6f751a77d4d796894dec4dc5d8f5d79b1cd39e1ffc50df7b3be4"
458
458
 
459
459
  /*
460
460
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -5285,6 +5285,10 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5285
5285
  ** even empty strings, are always zero-terminated. ^The return
5286
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
5292
  ** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5289
5293
  ** [unprotected sqlite3_value] object. In a multithreaded environment,
5290
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
5302
  ** [application-defined SQL functions] or [virtual tables], not within
5299
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
5306
  ** ^For example, if the internal representation is FLOAT and a text result
5303
5307
  ** is requested, [sqlite3_snprintf()] is used internally to perform the
5304
5308
  ** conversion automatically. ^(The following table details the conversions
@@ -5323,7 +5327,7 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5323
5327
  ** <tr><td> TEXT <td> BLOB <td> No change
5324
5328
  ** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5325
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
5331
  ** </table>
5328
5332
  ** </blockquote>)^
5329
5333
  **
@@ -10073,7 +10077,7 @@ SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10073
10077
  ** ^When xBestIndex returns, the sqlite3_value object returned by
10074
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
10081
  ** "Right-Hand Side".
10078
10082
  */
10079
10083
  SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
@@ -19925,6 +19929,7 @@ SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
19925
19929
  SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
19926
19930
  SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
19927
19931
  SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
19932
+ SQLITE_PRIVATE int sqlite3ExprIsTableConstraint(Expr*,const SrcItem*);
19928
19933
  #ifdef SQLITE_ENABLE_CURSOR_HINTS
19929
19934
  SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
19930
19935
  #endif
@@ -20364,7 +20369,12 @@ SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
20364
20369
  SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
20365
20370
  SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
20366
20371
  SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
20372
+
20367
20373
  SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20374
+ #if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
20375
+ && !defined(SQLITE_OMIT_VIRTUALTABLE)
20376
+ SQLITE_PRIVATE void sqlite3VtabWriteAll(sqlite3_index_info*);
20377
+ #endif
20368
20378
  SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20369
20379
  SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20370
20380
  SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
@@ -23654,7 +23664,7 @@ static int toLocaltime(
23654
23664
  p->D = sLocal.tm_mday;
23655
23665
  p->h = sLocal.tm_hour;
23656
23666
  p->m = sLocal.tm_min;
23657
- p->s = sLocal.tm_sec;
23667
+ p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
23658
23668
  p->validYMD = 1;
23659
23669
  p->validHMS = 1;
23660
23670
  p->validJD = 0;
@@ -29328,8 +29338,9 @@ SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const cha
29328
29338
  ** Free any prior content in *pz and replace it with a copy of zNew.
29329
29339
  */
29330
29340
  SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
29341
+ char *z = sqlite3DbStrDup(db, zNew);
29331
29342
  sqlite3DbFree(db, *pz);
29332
- *pz = sqlite3DbStrDup(db, zNew);
29343
+ *pz = z;
29333
29344
  }
29334
29345
 
29335
29346
  /*
@@ -39682,11 +39693,17 @@ static int unixShmLock(
39682
39693
  int flags /* What to do with the lock */
39683
39694
  ){
39684
39695
  unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
39685
- unixShm *p = pDbFd->pShm; /* The shared memory being locked */
39686
- unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
39696
+ unixShm *p; /* The shared memory being locked */
39697
+ unixShmNode *pShmNode; /* The underlying file iNode */
39687
39698
  int rc = SQLITE_OK; /* Result code */
39688
39699
  u16 mask; /* Mask of locks to take or release */
39689
- int *aLock = pShmNode->aLock;
39700
+ int *aLock;
39701
+
39702
+ p = pDbFd->pShm;
39703
+ if( p==0 ) return SQLITE_IOERR_SHMLOCK;
39704
+ pShmNode = p->pShmNode;
39705
+ if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
39706
+ aLock = pShmNode->aLock;
39690
39707
 
39691
39708
  assert( pShmNode==pDbFd->pInode->pShmNode );
39692
39709
  assert( pShmNode->pInode==pDbFd->pInode );
@@ -46974,10 +46991,14 @@ static int winShmLock(
46974
46991
  winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
46975
46992
  winShm *p = pDbFd->pShm; /* The shared memory being locked */
46976
46993
  winShm *pX; /* For looping over all siblings */
46977
- winShmNode *pShmNode = p->pShmNode;
46994
+ winShmNode *pShmNode;
46978
46995
  int rc = SQLITE_OK; /* Result code */
46979
46996
  u16 mask; /* Mask of locks to take or release */
46980
46997
 
46998
+ if( p==0 ) return SQLITE_IOERR_SHMLOCK;
46999
+ pShmNode = p->pShmNode;
47000
+ if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
47001
+
46981
47002
  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
46982
47003
  assert( n>=1 );
46983
47004
  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
@@ -50907,7 +50928,8 @@ SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
50907
50928
  ** make it so.
50908
50929
  */
50909
50930
  SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
50910
- assert( p->nRef>0 );
50931
+ assert( p->nRef>0 || p->pCache->bPurgeable==0 );
50932
+ testcase( p->nRef==0 );
50911
50933
  assert( sqlite3PcachePageSanity(p) );
50912
50934
  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
50913
50935
  p->flags &= ~PGHDR_DONT_WRITE;
@@ -56039,6 +56061,9 @@ static int pager_playback(Pager *pPager, int isHot){
56039
56061
  goto end_playback;
56040
56062
  }
56041
56063
  pPager->dbSize = mxPg;
56064
+ if( pPager->mxPgno<mxPg ){
56065
+ pPager->mxPgno = mxPg;
56066
+ }
56042
56067
  }
56043
56068
 
56044
56069
  /* Copy original pages out of the journal and back into the
@@ -65398,7 +65423,9 @@ struct MemPage {
65398
65423
  u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
65399
65424
  BtShared *pBt; /* Pointer to BtShared that this page is part of */
65400
65425
  u8 *aData; /* Pointer to disk image of the page data */
65401
- u8 *aDataEnd; /* One byte past the end of usable data */
65426
+ u8 *aDataEnd; /* One byte past the end of the entire page - not just
65427
+ ** the usable space, the entire page. Used to prevent
65428
+ ** corruption-induced of buffer overflow. */
65402
65429
  u8 *aCellIdx; /* The cell index area */
65403
65430
  u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
65404
65431
  DbPage *pDbPage; /* Pager page handle */
@@ -67739,6 +67766,8 @@ static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
67739
67766
  ** fragmented bytes within the page. */
67740
67767
  memcpy(&aData[iAddr], &aData[pc], 2);
67741
67768
  aData[hdr+7] += (u8)x;
67769
+ testcase( pc+x>maxPC );
67770
+ return &aData[pc];
67742
67771
  }else if( x+pc > maxPC ){
67743
67772
  /* This slot extends off the end of the usable part of the page */
67744
67773
  *pRc = SQLITE_CORRUPT_PAGE(pPg);
@@ -68183,7 +68212,7 @@ static int btreeInitPage(MemPage *pPage){
68183
68212
  pPage->nOverflow = 0;
68184
68213
  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
68185
68214
  pPage->aCellIdx = data + pPage->childPtrSize + 8;
68186
- pPage->aDataEnd = pPage->aData + pBt->usableSize;
68215
+ pPage->aDataEnd = pPage->aData + pBt->pageSize;
68187
68216
  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
68188
68217
  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
68189
68218
  ** number of cells on the page. */
@@ -68218,7 +68247,7 @@ static void zeroPage(MemPage *pPage, int flags){
68218
68247
  u8 hdr = pPage->hdrOffset;
68219
68248
  u16 first;
68220
68249
 
68221
- assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
68250
+ assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
68222
68251
  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
68223
68252
  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
68224
68253
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -68234,7 +68263,7 @@ static void zeroPage(MemPage *pPage, int flags){
68234
68263
  pPage->nFree = (u16)(pBt->usableSize - first);
68235
68264
  decodeFlags(pPage, flags);
68236
68265
  pPage->cellOffset = first;
68237
- pPage->aDataEnd = &data[pBt->usableSize];
68266
+ pPage->aDataEnd = &data[pBt->pageSize];
68238
68267
  pPage->aCellIdx = &data[first];
68239
68268
  pPage->aDataOfst = &data[pPage->childPtrSize];
68240
68269
  pPage->nOverflow = 0;
@@ -68360,7 +68389,7 @@ static int getAndInitPage(
68360
68389
  goto getAndInitPage_error2;
68361
68390
  }
68362
68391
  }
68363
- assert( (*ppPage)->pgno==pgno );
68392
+ assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
68364
68393
  assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
68365
68394
 
68366
68395
  /* If obtaining a child page for a cursor, we must verify that the page is
@@ -71447,7 +71476,7 @@ static int moveToRoot(BtCursor *pCur){
71447
71476
  pCur->curIntKey = pCur->pPage->intKey;
71448
71477
  }
71449
71478
  pRoot = pCur->pPage;
71450
- assert( pRoot->pgno==pCur->pgnoRoot );
71479
+ assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
71451
71480
 
71452
71481
  /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
71453
71482
  ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
@@ -71938,7 +71967,7 @@ SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
71938
71967
  assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
71939
71968
  assert( pPage->isInit );
71940
71969
  if( pPage->leaf ){
71941
- assert( pCur->ix<pCur->pPage->nCell );
71970
+ assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
71942
71971
  pCur->ix = (u16)idx;
71943
71972
  *pRes = c;
71944
71973
  rc = SQLITE_OK;
@@ -74462,7 +74491,7 @@ static int balance_nonroot(
74462
74491
  iOvflSpace += sz;
74463
74492
  assert( sz<=pBt->maxLocal+23 );
74464
74493
  assert( iOvflSpace <= (int)pBt->pageSize );
74465
- for(k=0; b.ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
74494
+ for(k=0; b.ixNx[k]<=j && ALWAYS(k<NB*2); k++){}
74466
74495
  pSrcEnd = b.apEnd[k];
74467
74496
  if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){
74468
74497
  rc = SQLITE_CORRUPT_BKPT;
@@ -74978,24 +75007,6 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
74978
75007
  assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
74979
75008
  assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
74980
75009
 
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
75010
  /* Save the positions of any other cursors open on this table.
75000
75011
  **
75001
75012
  ** In some cases, the call to btreeMoveto() below is a no-op. For
@@ -75020,6 +75031,24 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
75020
75031
  }
75021
75032
  }
75022
75033
 
75034
+ if( pCur->eState>=CURSOR_REQUIRESEEK ){
75035
+ rc = moveToRoot(pCur);
75036
+ if( rc && rc!=SQLITE_EMPTY ) return rc;
75037
+ }
75038
+
75039
+ assert( cursorOwnsBtShared(pCur) );
75040
+ assert( (pCur->curFlags & BTCF_WriteFlag)!=0
75041
+ && pBt->inTransaction==TRANS_WRITE
75042
+ && (pBt->btsFlags & BTS_READ_ONLY)==0 );
75043
+ assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
75044
+
75045
+ /* Assert that the caller has been consistent. If this cursor was opened
75046
+ ** expecting an index b-tree, then the caller should be inserting blob
75047
+ ** keys with no associated data. If the cursor was opened expecting an
75048
+ ** intkey table, the caller should be inserting integer keys with a
75049
+ ** blob of associated data. */
75050
+ assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
75051
+
75023
75052
  if( pCur->pKeyInfo==0 ){
75024
75053
  assert( pX->pKey==0 );
75025
75054
  /* If this is an insert into a table b-tree, invalidate any incrblob
@@ -75108,14 +75137,13 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
75108
75137
  }
75109
75138
  }
75110
75139
  assert( pCur->eState==CURSOR_VALID
75111
- || (pCur->eState==CURSOR_INVALID && loc)
75112
- || CORRUPT_DB );
75140
+ || (pCur->eState==CURSOR_INVALID && loc) );
75113
75141
 
75114
75142
  pPage = pCur->pPage;
75115
75143
  assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
75116
75144
  assert( pPage->leaf || !pPage->intKey );
75117
75145
  if( pPage->nFree<0 ){
75118
- if( NEVER(pCur->eState>CURSOR_INVALID) ){
75146
+ if( pCur->eState>CURSOR_INVALID ){
75119
75147
  rc = SQLITE_CORRUPT_BKPT;
75120
75148
  }else{
75121
75149
  rc = btreeComputeFreeSpace(pPage);
@@ -75396,12 +75424,16 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
75396
75424
  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
75397
75425
  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
75398
75426
  assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
75399
- if( pCur->eState==CURSOR_REQUIRESEEK ){
75400
- rc = btreeRestoreCursorPosition(pCur);
75401
- assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
75402
- if( rc || pCur->eState!=CURSOR_VALID ) return rc;
75427
+ if( pCur->eState!=CURSOR_VALID ){
75428
+ if( pCur->eState>=CURSOR_REQUIRESEEK ){
75429
+ rc = btreeRestoreCursorPosition(pCur);
75430
+ assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
75431
+ if( rc || pCur->eState!=CURSOR_VALID ) return rc;
75432
+ }else{
75433
+ return SQLITE_CORRUPT_BKPT;
75434
+ }
75403
75435
  }
75404
- assert( CORRUPT_DB || pCur->eState==CURSOR_VALID );
75436
+ assert( pCur->eState==CURSOR_VALID );
75405
75437
 
75406
75438
  iCellDepth = pCur->iPage;
75407
75439
  iCellIdx = pCur->ix;
@@ -78024,7 +78056,11 @@ SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
78024
78056
  assert( !sqlite3VdbeMemIsRowSet(pMem) );
78025
78057
  assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
78026
78058
  || desiredEnc==SQLITE_UTF16BE );
78027
- if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
78059
+ if( !(pMem->flags&MEM_Str) ){
78060
+ pMem->enc = desiredEnc;
78061
+ return SQLITE_OK;
78062
+ }
78063
+ if( pMem->enc==desiredEnc ){
78028
78064
  return SQLITE_OK;
78029
78065
  }
78030
78066
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
@@ -78736,6 +78772,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
78736
78772
  void (*xDestructor)(void*)
78737
78773
  ){
78738
78774
  assert( pMem->flags==MEM_Null );
78775
+ vdbeMemClear(pMem);
78739
78776
  pMem->u.zPType = zPType ? zPType : "";
78740
78777
  pMem->z = pPtr;
78741
78778
  pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
@@ -89990,10 +90027,18 @@ case OP_Offset: { /* out3 */
89990
90027
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89991
90028
  pC = p->apCsr[pOp->p1];
89992
90029
  pOut = &p->aMem[pOp->p3];
89993
- if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
90030
+ if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
89994
90031
  sqlite3VdbeMemSetNull(pOut);
89995
90032
  }else{
89996
- sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
90033
+ if( pC->deferredMoveto ){
90034
+ rc = sqlite3VdbeFinishMoveto(pC);
90035
+ if( rc ) goto abort_due_to_error;
90036
+ }
90037
+ if( sqlite3BtreeEof(pC->uc.pCursor) ){
90038
+ sqlite3VdbeMemSetNull(pOut);
90039
+ }else{
90040
+ sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
90041
+ }
89997
90042
  }
89998
90043
  break;
89999
90044
  }
@@ -93072,6 +93117,10 @@ case OP_Rowid: { /* out2 */
93072
93117
  ** Move the cursor P1 to a null row. Any OP_Column operations
93073
93118
  ** that occur while the cursor is on the null row will always
93074
93119
  ** write a NULL.
93120
+ **
93121
+ ** Or, if P1 is a Pseudo-Cursor (a cursor opened using OP_OpenPseudo)
93122
+ ** just reset the cache for that cursor. This causes the row of
93123
+ ** content held by the pseudo-cursor to be reparsed.
93075
93124
  */
93076
93125
  case OP_NullRow: {
93077
93126
  VdbeCursor *pC;
@@ -104718,6 +104767,38 @@ SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
104718
104767
  return exprIsConst(p, 3, iCur);
104719
104768
  }
104720
104769
 
104770
+ /*
104771
+ ** Check pExpr to see if it is an invariant constraint on data source pSrc.
104772
+ ** This is an optimization. False negatives will perhaps cause slower
104773
+ ** queries, but false positives will yield incorrect answers. So when in
104774
+ ** double, return 0.
104775
+ **
104776
+ ** To be an invariant constraint, the following must be true:
104777
+ **
104778
+ ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
104779
+ **
104780
+ ** (2) pExpr cannot use subqueries or non-deterministic functions.
104781
+ **
104782
+ ** (*) ** Not applicable to this branch **
104783
+ **
104784
+ ** (4) If pSrc is the right operand of a LEFT JOIN, then...
104785
+ ** (4a) pExpr must come from an ON clause..
104786
+ ** (4b) and specifically the ON clause associated with the LEFT JOIN.
104787
+ **
104788
+ ** (5) If pSrc is not the right operand of a LEFT JOIN or the left
104789
+ ** operand of a RIGHT JOIN, then pExpr must be from the WHERE
104790
+ ** clause, not an ON clause.
104791
+ */
104792
+ SQLITE_PRIVATE int sqlite3ExprIsTableConstraint(Expr *pExpr, const SrcItem *pSrc){
104793
+ if( pSrc->fg.jointype & JT_LEFT ){
104794
+ if( !ExprHasProperty(pExpr, EP_FromJoin) ) return 0; /* rule (4a) */
104795
+ if( pExpr->w.iRightJoinTable!=pSrc->iCursor ) return 0; /* rule (4b) */
104796
+ }else{
104797
+ if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; /* rule (5) */
104798
+ }
104799
+ return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
104800
+ }
104801
+
104721
104802
 
104722
104803
  /*
104723
104804
  ** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
@@ -109918,19 +109999,21 @@ static int renameParseSql(
109918
109999
  ){
109919
110000
  int rc;
109920
110001
 
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
110002
  sqlite3ParseObjectInit(p, db);
110003
+ if( zSql==0 ){
110004
+ return SQLITE_NOMEM;
110005
+ }
110006
+ if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){
110007
+ return SQLITE_CORRUPT_BKPT;
110008
+ }
110009
+ db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109927
110010
  p->eParseMode = PARSE_MODE_RENAME;
109928
110011
  p->db = db;
109929
110012
  p->nQueryLoop = 1;
109930
- rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM;
110013
+ rc = sqlite3RunParser(p, zSql);
109931
110014
  if( db->mallocFailed ) rc = SQLITE_NOMEM;
109932
110015
  if( rc==SQLITE_OK
109933
- && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
110016
+ && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
109934
110017
  ){
109935
110018
  rc = SQLITE_CORRUPT_BKPT;
109936
110019
  }
@@ -116679,6 +116762,11 @@ SQLITE_PRIVATE void sqlite3EndTable(
116679
116762
  int addrInsLoop; /* Top of the loop for inserting rows */
116680
116763
  Table *pSelTab; /* A table that describes the SELECT results */
116681
116764
 
116765
+ if( IN_SPECIAL_PARSE ){
116766
+ pParse->rc = SQLITE_ERROR;
116767
+ pParse->nErr++;
116768
+ return;
116769
+ }
116682
116770
  regYield = ++pParse->nMem;
116683
116771
  regRec = ++pParse->nMem;
116684
116772
  regRowid = ++pParse->nMem;
@@ -123258,8 +123346,8 @@ SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
123258
123346
  INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
123259
123347
  INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
123260
123348
  #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
123261
- FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET|
123262
- SQLITE_FUNC_TYPEOF),
123349
+ {1, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_OFFSET|SQLITE_FUNC_TYPEOF,
123350
+ 0, 0, noopFunc, 0, 0, 0, "sqlite_offset", {0} },
123263
123351
  #endif
123264
123352
  FUNCTION(ltrim, 1, 1, 0, trimFunc ),
123265
123353
  FUNCTION(ltrim, 2, 1, 0, trimFunc ),
@@ -125063,7 +125151,7 @@ SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
125063
125151
  }
125064
125152
 
125065
125153
  for(i=j=0; i<pTab->nCol; i++){
125066
- assert( pTab->aCol[i].affinity!=0 );
125154
+ assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
125067
125155
  if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
125068
125156
  zColAff[j++] = pTab->aCol[i].affinity;
125069
125157
  }
@@ -125677,7 +125765,11 @@ SQLITE_PRIVATE void sqlite3Insert(
125677
125765
  **
125678
125766
  ** This is the 2nd template.
125679
125767
  */
125680
- if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
125768
+ if( pColumn==0
125769
+ && pSelect!=0
125770
+ && pTrigger==0
125771
+ && xferOptimization(pParse, pTab, pSelect, onError, iDb)
125772
+ ){
125681
125773
  assert( !pTrigger );
125682
125774
  assert( pList==0 );
125683
125775
  goto insert_end;
@@ -127648,18 +127740,13 @@ static int xferOptimization(
127648
127740
  int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
127649
127741
  int regData, regRowid; /* Registers holding data and rowid */
127650
127742
 
127651
- if( pSelect==0 ){
127652
- return 0; /* Must be of the form INSERT INTO ... SELECT ... */
127653
- }
127743
+ assert( pSelect!=0 );
127654
127744
  if( pParse->pWith || pSelect->pWith ){
127655
127745
  /* Do not attempt to process this query if there are an WITH clauses
127656
127746
  ** attached to it. Proceeding may generate a false "no such table: xxx"
127657
127747
  ** error if pSelect reads from a CTE named "xxx". */
127658
127748
  return 0;
127659
127749
  }
127660
- if( sqlite3TriggerList(pParse, pDest) ){
127661
- return 0; /* tab1 must not have triggers */
127662
- }
127663
127750
  #ifndef SQLITE_OMIT_VIRTUALTABLE
127664
127751
  if( IsVirtual(pDest) ){
127665
127752
  return 0; /* tab1 must not be a virtual table */
@@ -133505,7 +133592,7 @@ SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFl
133505
133592
  sqlite3ResetAllSchemasOfConnection(db);
133506
133593
  pDb = &db->aDb[iDb];
133507
133594
  }else
133508
- if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
133595
+ if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
133509
133596
  /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
133510
133597
  ** the schema loaded, even if errors (other than OOM) occurred. In
133511
133598
  ** this situation the current sqlite3_prepare() operation will fail,
@@ -138995,8 +139082,7 @@ static int pushDownWhereTerms(
138995
139082
  Parse *pParse, /* Parse context (for malloc() and error reporting) */
138996
139083
  Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
138997
139084
  Expr *pWhere, /* The WHERE clause of the outer query */
138998
- int iCursor, /* Cursor number of the subquery */
138999
- int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
139085
+ SrcItem *pSrc /* The subquery term of the outer FROM clause */
139000
139086
  ){
139001
139087
  Expr *pNew;
139002
139088
  int nChng = 0;
@@ -139031,10 +139117,11 @@ static int pushDownWhereTerms(
139031
139117
  return 0; /* restriction (3) */
139032
139118
  }
139033
139119
  while( pWhere->op==TK_AND ){
139034
- nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
139035
- iCursor, isLeftJoin);
139120
+ nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc);
139036
139121
  pWhere = pWhere->pLeft;
139037
139122
  }
139123
+
139124
+ #if 0 /* Legacy code. Checks now done by sqlite3ExprIsTableConstraint() */
139038
139125
  if( isLeftJoin
139039
139126
  && (ExprHasProperty(pWhere,EP_FromJoin)==0
139040
139127
  || pWhere->w.iRightJoinTable!=iCursor)
@@ -139046,7 +139133,9 @@ static int pushDownWhereTerms(
139046
139133
  ){
139047
139134
  return 0; /* restriction (5) */
139048
139135
  }
139049
- if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
139136
+ #endif
139137
+
139138
+ if( sqlite3ExprIsTableConstraint(pWhere, pSrc) ){
139050
139139
  nChng++;
139051
139140
  pSubq->selFlags |= SF_PushDown;
139052
139141
  while( pSubq ){
@@ -139054,8 +139143,8 @@ static int pushDownWhereTerms(
139054
139143
  pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
139055
139144
  unsetJoinExpr(pNew, -1);
139056
139145
  x.pParse = pParse;
139057
- x.iTable = iCursor;
139058
- x.iNewTable = iCursor;
139146
+ x.iTable = pSrc->iCursor;
139147
+ x.iNewTable = pSrc->iCursor;
139059
139148
  x.isLeftJoin = 0;
139060
139149
  x.pEList = pSubq->pEList;
139061
139150
  pNew = substExpr(&x, pNew);
@@ -140837,8 +140926,7 @@ SQLITE_PRIVATE int sqlite3Select(
140837
140926
  if( OptimizationEnabled(db, SQLITE_PushDown)
140838
140927
  && (pItem->fg.isCte==0
140839
140928
  || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
140840
- && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
140841
- (pItem->fg.jointype & JT_OUTER)!=0)
140929
+ && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
140842
140930
  ){
140843
140931
  #if SELECTTRACE_ENABLED
140844
140932
  if( sqlite3SelectTrace & 0x100 ){
@@ -142372,6 +142460,7 @@ static TriggerStep *triggerStepAllocate(
142372
142460
  sqlite3 *db = pParse->db;
142373
142461
  TriggerStep *pTriggerStep;
142374
142462
 
142463
+ if( pParse->nErr ) return 0;
142375
142464
  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
142376
142465
  if( pTriggerStep ){
142377
142466
  char *z = (char*)&pTriggerStep[1];
@@ -146250,6 +146339,7 @@ SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
146250
146339
 
146251
146340
  sqlite3ParseObjectInit(&sParse, db);
146252
146341
  sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
146342
+ sParse.disableTriggers = 1;
146253
146343
  /* We should never be able to reach this point while loading the
146254
146344
  ** schema. Nevertheless, defend against that (turn off db->init.busy)
146255
146345
  ** in case a bug arises. */
@@ -148185,6 +148275,7 @@ static int codeAllEqualityTerms(
148185
148275
  VdbeCoverageIf(v, bRev!=0);
148186
148276
  VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
148187
148277
  j = sqlite3VdbeAddOp0(v, OP_Goto);
148278
+ assert( pLevel->addrSkip==0 );
148188
148279
  pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
148189
148280
  iIdxCur, 0, regBase, nSkip);
148190
148281
  VdbeCoverageIf(v, bRev==0);
@@ -148782,6 +148873,7 @@ static SQLITE_NOINLINE void filterPullDown(
148782
148873
  WhereLevel *pLevel = &pWInfo->a[iLevel];
148783
148874
  WhereLoop *pLoop = pLevel->pWLoop;
148784
148875
  if( pLevel->regFilter==0 ) continue;
148876
+ if( pLevel->pWLoop->nSkip ) continue;
148785
148877
  /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
148786
148878
  ** vvvvv--' pLevel->regFilter if this were true. */
148787
148879
  if( NEVER(pLoop->prereq & notReady) ) continue;
@@ -148916,7 +149008,6 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
148916
149008
  int iReg; /* P3 Value for OP_VFilter */
148917
149009
  int addrNotFound;
148918
149010
  int nConstraint = pLoop->nLTerm;
148919
- int iIn; /* Counter for IN constraints */
148920
149011
 
148921
149012
  iReg = sqlite3GetTempRange(pParse, nConstraint+2);
148922
149013
  addrNotFound = pLevel->addrBrk;
@@ -148962,50 +149053,54 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
148962
149053
  pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
148963
149054
  pLevel->p2 = sqlite3VdbeCurrentAddr(v);
148964
149055
  assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
148965
- if( pLoop->wsFlags & WHERE_IN_ABLE ){
148966
- iIn = pLevel->u.in.nIn;
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 */
149056
+
149057
+ for(j=0; j<nConstraint; j++){
148972
149058
  pTerm = pLoop->aLTerm[j];
149059
+ if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
149060
+ disableTerm(pLevel, pTerm);
149061
+ continue;
149062
+ }
148973
149063
  if( (pTerm->eOperator & WO_IN)!=0
148974
149064
  && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
149065
+ && !db->mallocFailed
148975
149066
  ){
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
149067
  Expr *pCompare; /* The comparison operator */
148985
149068
  Expr *pRight; /* RHS of the comparison */
148986
149069
  VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
149070
+ int iIn; /* IN loop corresponding to the j-th constraint */
148987
149071
 
148988
149072
  /* Reload the constraint value into reg[iReg+j+2]. The same value
148989
149073
  ** was loaded into the same register prior to the OP_VFilter, but
148990
149074
  ** the xFilter implementation might have changed the datatype or
148991
- ** encoding of the value in the register, so it *must* be reloaded. */
148992
- assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
148993
- if( !db->mallocFailed ){
148994
- assert( iIn>=0 && iIn<pLevel->u.in.nIn );
149075
+ ** encoding of the value in the register, so it *must* be reloaded.
149076
+ */
149077
+ for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
148995
149078
  pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
148996
- assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
148997
- assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
148998
- assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
148999
- testcase( pOp->opcode==OP_Rowid );
149000
- sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
149079
+ if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
149080
+ || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
149081
+ ){
149082
+ testcase( pOp->opcode==OP_Rowid );
149083
+ sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
149084
+ break;
149085
+ }
149001
149086
  }
149002
149087
 
149003
149088
  /* Generate code that will continue to the next row if
149004
- ** the IN constraint is not satisfied */
149089
+ ** the IN constraint is not satisfied
149090
+ */
149005
149091
  pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
149006
- assert( pCompare!=0 || db->mallocFailed );
149007
- if( pCompare ){
149008
- pCompare->pLeft = pTerm->pExpr->pLeft;
149092
+ if( !db->mallocFailed ){
149093
+ int iFld = pTerm->u.x.iField;
149094
+ Expr *pLeft = pTerm->pExpr->pLeft;
149095
+ assert( pLeft!=0 );
149096
+ if( iFld>0 ){
149097
+ assert( pLeft->op==TK_VECTOR );
149098
+ assert( ExprUseXList(pLeft) );
149099
+ assert( iFld<=pLeft->x.pList->nExpr );
149100
+ pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
149101
+ }else{
149102
+ pCompare->pLeft = pLeft;
149103
+ }
149009
149104
  pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
149010
149105
  if( pRight ){
149011
149106
  pRight->iTable = iReg+j+2;
@@ -149014,11 +149109,11 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
149014
149109
  );
149015
149110
  }
149016
149111
  pCompare->pLeft = 0;
149017
- sqlite3ExprDelete(db, pCompare);
149018
149112
  }
149113
+ sqlite3ExprDelete(db, pCompare);
149019
149114
  }
149020
149115
  }
149021
- assert( iIn==0 || db->mallocFailed );
149116
+
149022
149117
  /* These registers need to be preserved in case there is an IN operator
149023
149118
  ** loop. So we could deallocate the registers here (and potentially
149024
149119
  ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
@@ -149728,6 +149823,14 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
149728
149823
  ** the initialization of the right-hand operand of the vector comparison
149729
149824
  ** might not occur, or might occur only in an OR branch that is not
149730
149825
  ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
149826
+ **
149827
+ ** 2022-03-03: Do not push down expressions that involve subqueries.
149828
+ ** The subquery might get coded as a subroutine. Any table-references
149829
+ ** in the subquery might be resolved to index-references for the index on
149830
+ ** the OR branch in which the subroutine is coded. But if the subroutine
149831
+ ** is invoked from a different OR branch that uses a different index, such
149832
+ ** index-references will not work. tag-20220303a
149833
+ ** https://sqlite.org/forum/forumpost/36937b197273d403
149731
149834
  */
149732
149835
  if( pWC->nTerm>1 ){
149733
149836
  int iTerm;
@@ -149741,7 +149844,7 @@ SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
149741
149844
  continue;
149742
149845
  }
149743
149846
  if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
149744
- testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
149847
+ if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
149745
149848
  pExpr = sqlite3ExprDup(db, pExpr, 0);
149746
149849
  pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
149747
149850
  }
@@ -152748,8 +152851,7 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
152748
152851
  ** WHERE clause (or the ON clause of a LEFT join) that constrain which
152749
152852
  ** rows of the target table (pSrc) that can be used. */
152750
152853
  if( (pTerm->wtFlags & TERM_VIRTUAL)==0
152751
- && ((pSrc->fg.jointype&JT_LEFT)==0 || ExprHasProperty(pExpr,EP_FromJoin))
152752
- && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor)
152854
+ && sqlite3ExprIsTableConstraint(pExpr, pSrc)
152753
152855
  ){
152754
152856
  pPartial = sqlite3ExprAnd(pParse, pPartial,
152755
152857
  sqlite3ExprDup(pParse->db, pExpr, 0));
@@ -152988,7 +153090,7 @@ static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
152988
153090
  for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
152989
153091
  Expr *pExpr = pTerm->pExpr;
152990
153092
  if( (pTerm->wtFlags & TERM_VIRTUAL)==0
152991
- && sqlite3ExprIsTableConstant(pExpr, iCur)
153093
+ && sqlite3ExprIsTableConstraint(pExpr, pItem)
152992
153094
  ){
152993
153095
  sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
152994
153096
  }
@@ -153018,7 +153120,10 @@ static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
153018
153120
  pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
153019
153121
  if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
153020
153122
  while( ++iLevel < pWInfo->nLevel ){
153123
+ const SrcItem *pTabItem;
153021
153124
  pLevel = &pWInfo->a[iLevel];
153125
+ pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
153126
+ if( pTabItem->fg.jointype & JT_LEFT ) continue;
153022
153127
  pLoop = pLevel->pWLoop;
153023
153128
  if( NEVER(pLoop==0) ) continue;
153024
153129
  if( pLoop->prereq & notReady ) continue;
@@ -154531,8 +154636,17 @@ static void whereLoopOutputAdjust(
154531
154636
  /* If there are extra terms in the WHERE clause not used by an index
154532
154637
  ** that depend only on the table being scanned, and that will tend to
154533
154638
  ** cause many rows to be omitted, then mark that table as
154534
- ** "self-culling". */
154535
- pLoop->wsFlags |= WHERE_SELFCULL;
154639
+ ** "self-culling".
154640
+ **
154641
+ ** 2022-03-24: Self-culling only applies if either the extra terms
154642
+ ** are straight comparison operators that are non-true with NULL
154643
+ ** operand, or if the loop is not a LEFT JOIN.
154644
+ */
154645
+ if( (pTerm->eOperator & 0x3f)!=0
154646
+ || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0
154647
+ ){
154648
+ pLoop->wsFlags |= WHERE_SELFCULL;
154649
+ }
154536
154650
  }
154537
154651
  if( pTerm->truthProb<=0 ){
154538
154652
  /* If a truth probability is specified using the likelihood() hints,
@@ -155701,7 +155815,6 @@ SQLITE_API int sqlite3_vtab_rhs_value(
155701
155815
  return rc;
155702
155816
  }
155703
155817
 
155704
-
155705
155818
  /*
155706
155819
  ** Return true if ORDER BY clause may be handled as DISTINCT.
155707
155820
  */
@@ -155713,6 +155826,22 @@ SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){
155713
155826
  return pHidden->eDistinct;
155714
155827
  }
155715
155828
 
155829
+ #if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
155830
+ && !defined(SQLITE_OMIT_VIRTUALTABLE)
155831
+ /*
155832
+ ** Cause the prepared statement that is associated with a call to
155833
+ ** xBestIndex to open write transactions on all attached schemas.
155834
+ ** This is used by the (built-in) sqlite_dbpage virtual table.
155835
+ */
155836
+ SQLITE_PRIVATE void sqlite3VtabWriteAll(sqlite3_index_info *pIdxInfo){
155837
+ HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
155838
+ Parse *pParse = pHidden->pParse;
155839
+ int nDb = pParse->db->nDb;
155840
+ int i;
155841
+ for(i=0; i<nDb; i++) sqlite3BeginWriteOperation(pParse, 0, i);
155842
+ }
155843
+ #endif
155844
+
155716
155845
  /*
155717
155846
  ** Add all WhereLoop objects for a table of the join identified by
155718
155847
  ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
@@ -157816,6 +157945,26 @@ whereBeginError:
157816
157945
  }
157817
157946
  #endif
157818
157947
 
157948
+ #ifdef SQLITE_DEBUG
157949
+ /*
157950
+ ** Return true if cursor iCur is opened by instruction k of the
157951
+ ** bytecode. Used inside of assert() only.
157952
+ */
157953
+ static int cursorIsOpen(Vdbe *v, int iCur, int k){
157954
+ while( k>=0 ){
157955
+ VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
157956
+ if( pOp->p1!=iCur ) continue;
157957
+ if( pOp->opcode==OP_Close ) return 0;
157958
+ if( pOp->opcode==OP_OpenRead ) return 1;
157959
+ if( pOp->opcode==OP_OpenWrite ) return 1;
157960
+ if( pOp->opcode==OP_OpenDup ) return 1;
157961
+ if( pOp->opcode==OP_OpenAutoindex ) return 1;
157962
+ if( pOp->opcode==OP_OpenEphemeral ) return 1;
157963
+ }
157964
+ return 0;
157965
+ }
157966
+ #endif /* SQLITE_DEBUG */
157967
+
157819
157968
  /*
157820
157969
  ** Generate the end of the WHERE loop. See comments on
157821
157970
  ** sqlite3WhereBegin() for additional information.
@@ -158068,6 +158217,11 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
158068
158217
  ){
158069
158218
  int x = pOp->p2;
158070
158219
  assert( pIdx->pTable==pTab );
158220
+ #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
158221
+ if( pOp->opcode==OP_Offset ){
158222
+ /* Do not need to translate the column number */
158223
+ }else
158224
+ #endif
158071
158225
  if( !HasRowid(pTab) ){
158072
158226
  Index *pPk = sqlite3PrimaryKeyIndex(pTab);
158073
158227
  x = pPk->aiColumn[x];
@@ -158081,9 +158235,22 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
158081
158235
  pOp->p2 = x;
158082
158236
  pOp->p1 = pLevel->iIdxCur;
158083
158237
  OpcodeRewriteTrace(db, k, pOp);
158238
+ }else{
158239
+ /* Unable to translate the table reference into an index
158240
+ ** reference. Verify that this is harmless - that the
158241
+ ** table being referenced really is open.
158242
+ */
158243
+ #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
158244
+ assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
158245
+ || cursorIsOpen(v,pOp->p1,k)
158246
+ || pOp->opcode==OP_Offset
158247
+ );
158248
+ #else
158249
+ assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
158250
+ || cursorIsOpen(v,pOp->p1,k)
158251
+ );
158252
+ #endif
158084
158253
  }
158085
- assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
158086
- || pWInfo->eOnePass );
158087
158254
  }else if( pOp->opcode==OP_Rowid ){
158088
158255
  pOp->p1 = pLevel->iIdxCur;
158089
158256
  pOp->opcode = OP_IdxRowid;
@@ -159071,7 +159238,11 @@ static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
159071
159238
  */
159072
159239
  SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
159073
159240
  int rc = SQLITE_OK;
159074
- if( p->pWin && p->pPrior==0 && ALWAYS((p->selFlags & SF_WinRewrite)==0) ){
159241
+ if( p->pWin
159242
+ && p->pPrior==0
159243
+ && ALWAYS((p->selFlags & SF_WinRewrite)==0)
159244
+ && ALWAYS(!IN_RENAME_OBJECT)
159245
+ ){
159075
159246
  Vdbe *v = sqlite3GetVdbe(pParse);
159076
159247
  sqlite3 *db = pParse->db;
159077
159248
  Select *pSub = 0; /* The subquery */
@@ -159146,6 +159317,7 @@ SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
159146
159317
  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
159147
159318
  ExprList *pArgs;
159148
159319
  assert( ExprUseXList(pWin->pOwner) );
159320
+ assert( pWin->pFunc!=0 );
159149
159321
  pArgs = pWin->pOwner->x.pList;
159150
159322
  if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
159151
159323
  selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
@@ -159838,7 +160010,7 @@ static void windowAggStep(
159838
160010
 
159839
160011
  for(iEnd=sqlite3VdbeCurrentAddr(v); iOp<iEnd; iOp++){
159840
160012
  VdbeOp *pOp = sqlite3VdbeGetOp(v, iOp);
159841
- if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
160013
+ if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
159842
160014
  pOp->p1 = csr;
159843
160015
  }
159844
160016
  }
@@ -194156,14 +194328,15 @@ static JsonNode *jsonLookupStep(
194156
194328
  *pzErr = zPath;
194157
194329
  return 0;
194158
194330
  }
194331
+ testcase( nKey==0 );
194159
194332
  }else{
194160
194333
  zKey = zPath;
194161
194334
  for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
194162
194335
  nKey = i;
194163
- }
194164
- if( nKey==0 ){
194165
- *pzErr = zPath;
194166
- return 0;
194336
+ if( nKey==0 ){
194337
+ *pzErr = zPath;
194338
+ return 0;
194339
+ }
194167
194340
  }
194168
194341
  j = 1;
194169
194342
  for(;;){
@@ -195311,6 +195484,33 @@ static int jsonEachNext(sqlite3_vtab_cursor *cur){
195311
195484
  return SQLITE_OK;
195312
195485
  }
195313
195486
 
195487
+ /* Append an object label to the JSON Path being constructed
195488
+ ** in pStr.
195489
+ */
195490
+ static void jsonAppendObjectPathElement(
195491
+ JsonString *pStr,
195492
+ JsonNode *pNode
195493
+ ){
195494
+ int jj, nn;
195495
+ const char *z;
195496
+ assert( pNode->eType==JSON_STRING );
195497
+ assert( pNode->jnFlags & JNODE_LABEL );
195498
+ assert( pNode->eU==1 );
195499
+ z = pNode->u.zJContent;
195500
+ nn = pNode->n;
195501
+ assert( nn>=2 );
195502
+ assert( z[0]=='"' );
195503
+ assert( z[nn-1]=='"' );
195504
+ if( nn>2 && sqlite3Isalpha(z[1]) ){
195505
+ for(jj=2; jj<nn-1 && sqlite3Isalnum(z[jj]); jj++){}
195506
+ if( jj==nn-1 ){
195507
+ z++;
195508
+ nn -= 2;
195509
+ }
195510
+ }
195511
+ jsonPrintf(nn+2, pStr, ".%.*s", nn, z);
195512
+ }
195513
+
195314
195514
  /* Append the name of the path for element i to pStr
195315
195515
  */
195316
195516
  static void jsonEachComputePath(
@@ -195335,10 +195535,7 @@ static void jsonEachComputePath(
195335
195535
  }else{
195336
195536
  assert( pUp->eType==JSON_OBJECT );
195337
195537
  if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
195338
- assert( pNode->eType==JSON_STRING );
195339
- assert( pNode->jnFlags & JNODE_LABEL );
195340
- assert( pNode->eU==1 );
195341
- jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
195538
+ jsonAppendObjectPathElement(pStr, pNode);
195342
195539
  }
195343
195540
  }
195344
195541
 
@@ -195409,8 +195606,7 @@ static int jsonEachColumn(
195409
195606
  if( p->eType==JSON_ARRAY ){
195410
195607
  jsonPrintf(30, &x, "[%d]", p->iRowid);
195411
195608
  }else if( p->eType==JSON_OBJECT ){
195412
- assert( pThis->eU==1 );
195413
- jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
195609
+ jsonAppendObjectPathElement(&x, pThis);
195414
195610
  }
195415
195611
  }
195416
195612
  jsonResult(&x);
@@ -209957,6 +210153,7 @@ static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
209957
210153
  ){
209958
210154
  pIdxInfo->orderByConsumed = 1;
209959
210155
  }
210156
+ sqlite3VtabWriteAll(pIdxInfo);
209960
210157
  return SQLITE_OK;
209961
210158
  }
209962
210159
 
@@ -232311,7 +232508,7 @@ static int fts5SorterNext(Fts5Cursor *pCsr){
232311
232508
  rc = sqlite3_step(pSorter->pStmt);
232312
232509
  if( rc==SQLITE_DONE ){
232313
232510
  rc = SQLITE_OK;
232314
- CsrFlagSet(pCsr, FTS5CSR_EOF);
232511
+ CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT);
232315
232512
  }else if( rc==SQLITE_ROW ){
232316
232513
  const u8 *a;
232317
232514
  const u8 *aBlob;
@@ -234300,7 +234497,7 @@ static void fts5SourceIdFunc(
234300
234497
  ){
234301
234498
  assert( nArg==0 );
234302
234499
  UNUSED_PARAM2(nArg, apUnused);
234303
- sqlite3_result_text(pCtx, "fts5: 2022-02-22 18:58:40 40fa792d359f84c3b9e9d6623743e1a59826274e221df1bde8f47086968a1bab", -1, SQLITE_TRANSIENT);
234500
+ sqlite3_result_text(pCtx, "fts5: 2022-04-27 12:03:15 9547e2c38a1c6f751a77d4d796894dec4dc5d8f5d79b1cd39e1ffc50df7b3be4", -1, SQLITE_TRANSIENT);
234304
234501
  }
234305
234502
 
234306
234503
  /*