Fossil SCM

Update the built-in SQLite to the latest trunk version, for testing.

drh 2024-01-01 19:25 trunk
Commit 6da255034b30b4b42cd9f5e91f2b24cce0412867fa7d3a5dd61a5a3b1c1cab09
+1 -1
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -22170,11 +22170,10 @@
2217022170
sqlite3_open_v2(zDbFilename, &p->db,
2217122171
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
2217222172
break;
2217322173
}
2217422174
}
22175
- globalDb = p->db;
2217622175
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
2217722176
eputf("Error: unable to open database \"%s\": %s\n",
2217822177
zDbFilename, sqlite3_errmsg(p->db));
2217922178
if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
2218022179
exit(1);
@@ -22187,10 +22186,11 @@
2218722186
}else{
2218822187
eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
2218922188
zDbFilename);
2219022189
}
2219122190
}
22191
+ globalDb = p->db;
2219222192
sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
2219322193
2219422194
/* Reflect the use or absence of --unsafe-testing invocation. */
2219522195
{
2219622196
int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
2219722197
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -22170,11 +22170,10 @@
22170 sqlite3_open_v2(zDbFilename, &p->db,
22171 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
22172 break;
22173 }
22174 }
22175 globalDb = p->db;
22176 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22177 eputf("Error: unable to open database \"%s\": %s\n",
22178 zDbFilename, sqlite3_errmsg(p->db));
22179 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
22180 exit(1);
@@ -22187,10 +22186,11 @@
22187 }else{
22188 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
22189 zDbFilename);
22190 }
22191 }
 
22192 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
22193
22194 /* Reflect the use or absence of --unsafe-testing invocation. */
22195 {
22196 int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
22197
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -22170,11 +22170,10 @@
22170 sqlite3_open_v2(zDbFilename, &p->db,
22171 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
22172 break;
22173 }
22174 }
 
22175 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22176 eputf("Error: unable to open database \"%s\": %s\n",
22177 zDbFilename, sqlite3_errmsg(p->db));
22178 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
22179 exit(1);
@@ -22187,10 +22186,11 @@
22186 }else{
22187 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
22188 zDbFilename);
22189 }
22190 }
22191 globalDb = p->db;
22192 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
22193
22194 /* Reflect the use or absence of --unsafe-testing invocation. */
22195 {
22196 int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
22197
+419 -207
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
1616
** if you want a wrapper to interface SQLite with your choice of programming
1717
** language. The code for the "sqlite3" command-line shell is also in a
1818
** separate file. This file contains only code for the core SQLite library.
1919
**
2020
** The content in this amalgamation comes from Fossil check-in
21
-** 27d4a89a5ff96b7b7fc5dc9650e1269f7c7e.
21
+** bcac937526d9a6ef914a74b4d6757fa91cd7.
2222
*/
2323
#define SQLITE_CORE 1
2424
#define SQLITE_AMALGAMATION 1
2525
#ifndef SQLITE_PRIVATE
2626
# define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459459
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460460
** [sqlite_version()] and [sqlite_source_id()].
461461
*/
462462
#define SQLITE_VERSION "3.45.0"
463463
#define SQLITE_VERSION_NUMBER 3045000
464
-#define SQLITE_SOURCE_ID "2023-12-14 16:34:47 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9"
464
+#define SQLITE_SOURCE_ID "2024-01-01 19:20:00 bcac937526d9a6ef914a74b4d6757fa91cd74edab871bcd934fde4a2f9b6debd"
465465
466466
/*
467467
** CAPI3REF: Run-Time Library Version Numbers
468468
** KEYWORDS: sqlite3_version sqlite3_sourceid
469469
**
@@ -8350,13 +8350,15 @@
83508350
** can enter.)^ If the same thread tries to enter any mutex other
83518351
** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
83528352
**
83538353
** ^(Some systems (for example, Windows 95) do not support the operation
83548354
** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8355
-** will always return SQLITE_BUSY. The SQLite core only ever uses
8356
-** sqlite3_mutex_try() as an optimization so this is acceptable
8357
-** behavior.)^
8355
+** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8356
+** sqlite3_mutex_try() as an optimization, so this is acceptable
8357
+** behavior. The exceptions are unix builds that set the
8358
+** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8359
+** sqlite3_mutex_try() is required.)^
83588360
**
83598361
** ^The sqlite3_mutex_leave() routine exits a mutex that was
83608362
** previously entered by the same thread. The behavior
83618363
** is undefined if the mutex is not currently entered by the
83628364
** calling thread or is not currently allocated.
@@ -13125,23 +13127,28 @@
1312513127
**
1312613128
** This function may be quite inefficient if used with an FTS5 table
1312713129
** created with the "columnsize=0" option.
1312813130
**
1312913131
** xColumnText:
13130
-** This function attempts to retrieve the text of column iCol of the
13131
-** current document. If successful, (*pz) is set to point to a buffer
13132
+** If parameter iCol is less than zero, or greater than or equal to the
13133
+** number of columns in the table, SQLITE_RANGE is returned.
13134
+**
13135
+** Otherwise, this function attempts to retrieve the text of column iCol of
13136
+** the current document. If successful, (*pz) is set to point to a buffer
1313213137
** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
1313313138
** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
1313413139
** if an error occurs, an SQLite error code is returned and the final values
1313513140
** of (*pz) and (*pn) are undefined.
1313613141
**
1313713142
** xPhraseCount:
1313813143
** Returns the number of phrases in the current query expression.
1313913144
**
1314013145
** xPhraseSize:
13141
-** Returns the number of tokens in phrase iPhrase of the query. Phrases
13142
-** are numbered starting from zero.
13146
+** If parameter iCol is less than zero, or greater than or equal to the
13147
+** number of phrases in the current query, as returned by xPhraseCount,
13148
+** 0 is returned. Otherwise, this function returns the number of tokens in
13149
+** phrase iPhrase of the query. Phrases are numbered starting from zero.
1314313150
**
1314413151
** xInstCount:
1314513152
** Set *pnInst to the total number of occurrences of all phrases within
1314613153
** the query within the current row. Return SQLITE_OK if successful, or
1314713154
** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -13153,16 +13160,17 @@
1315313160
**
1315413161
** xInst:
1315513162
** Query for the details of phrase match iIdx within the current row.
1315613163
** Phrase matches are numbered starting from zero, so the iIdx argument
1315713164
** should be greater than or equal to zero and smaller than the value
13158
-** output by xInstCount().
13165
+** output by xInstCount(). If iIdx is less than zero or greater than
13166
+** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
1315913167
**
13160
-** Usually, output parameter *piPhrase is set to the phrase number, *piCol
13168
+** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
1316113169
** to the column in which it occurs and *piOff the token offset of the
13162
-** first token of the phrase. Returns SQLITE_OK if successful, or an error
13163
-** code (i.e. SQLITE_NOMEM) if an error occurs.
13170
+** first token of the phrase. SQLITE_OK is returned if successful, or an
13171
+** error code (i.e. SQLITE_NOMEM) if an error occurs.
1316413172
**
1316513173
** This API can be quite slow if used with an FTS5 table created with the
1316613174
** "detail=none" or "detail=column" option.
1316713175
**
1316813176
** xRowid:
@@ -13183,10 +13191,14 @@
1318313191
** row visited, the callback function passed as the fourth argument
1318413192
** is invoked. The context and API objects passed to the callback
1318513193
** function may be used to access the properties of each matched row.
1318613194
** Invoking Api.xUserData() returns a copy of the pointer passed as
1318713195
** the third argument to pUserData.
13196
+**
13197
+** If parameter iPhrase is less than zero, or greater than or equal to
13198
+** the number of phrases in the query, as returned by xPhraseCount(),
13199
+** this function returns SQLITE_RANGE.
1318813200
**
1318913201
** If the callback function returns any value other than SQLITE_OK, the
1319013202
** query is abandoned and the xQueryPhrase function returns immediately.
1319113203
** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
1319213204
** Otherwise, the error code is propagated upwards.
@@ -13304,22 +13316,30 @@
1330413316
** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
1330513317
** This is used to access token iToken of phrase iPhrase of the current
1330613318
** query. Before returning, output parameter *ppToken is set to point
1330713319
** to a buffer containing the requested token, and *pnToken to the
1330813320
** size of this buffer in bytes.
13321
+**
13322
+** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13323
+** or equal to the number of phrases in the query as reported by
13324
+** xPhraseCount(), or if iToken is equal to or greater than the number of
13325
+** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13326
+ are both zeroed.
1330913327
**
1331013328
** The output text is not a copy of the query text that specified the
1331113329
** token. It is the output of the tokenizer module. For tokendata=1
1331213330
** tables, this includes any embedded 0x00 and trailing data.
1331313331
**
1331413332
** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
1331513333
** This is used to access token iToken of phrase hit iIdx within the
13316
-** current row. Output variable (*ppToken) is set to point to a buffer
13317
-** containing the matching document token, and (*pnToken) to the size
13318
-** of that buffer in bytes. This API is not available if the specified
13319
-** token matches a prefix query term. In that case both output variables
13320
-** are always set to 0.
13334
+** current row. If iIdx is less than zero or greater than or equal to the
13335
+** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13336
+** output variable (*ppToken) is set to point to a buffer containing the
13337
+** matching document token, and (*pnToken) to the size of that buffer in
13338
+** bytes. This API is not available if the specified token matches a
13339
+** prefix query term. In that case both output variables are always set
13340
+** to 0.
1332113341
**
1332213342
** The output text is not a copy of the document text that was tokenized.
1332313343
** It is the output of the tokenizer module. For tokendata=1 tables, this
1332413344
** includes any embedded 0x00 and trailing data.
1332513345
**
@@ -13991,10 +14011,23 @@
1399114011
#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
1399214012
# define SQLITE_USE_SEH 1
1399314013
#else
1399414014
# undef SQLITE_USE_SEH
1399514015
#endif
14016
+
14017
+/*
14018
+** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly
14019
+** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14020
+*/
14021
+#if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1
14022
+ /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14023
+# undef SQLITE_DIRECT_OVERFLOW_READ
14024
+#else
14025
+ /* In all other cases, enable */
14026
+# define SQLITE_DIRECT_OVERFLOW_READ 1
14027
+#endif
14028
+
1399614029
1399714030
/*
1399814031
** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
1399914032
** 0 means mutexes are permanently disable and the library is never
1400014033
** threadsafe. 1 means the library is serialized which is the highest
@@ -15874,11 +15907,11 @@
1587415907
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
1587515908
SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
1587615909
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
1587715910
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
1587815911
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15879
-SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15912
+SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, u64*);
1588015913
SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
1588115914
SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
1588215915
1588315916
/* Functions used to truncate the database file. */
1588415917
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
@@ -18629,10 +18662,11 @@
1862918662
unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
1863018663
unsigned isResized:1; /* True if resizeIndexObject() has been called */
1863118664
unsigned isCovering:1; /* True if this is a covering index */
1863218665
unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
1863318666
unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
18667
+ unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
1863418668
unsigned bNoQuery:1; /* Do not use this index to optimize queries */
1863518669
unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
1863618670
unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
1863718671
unsigned bHasExpr:1; /* Index contains an expression, either a literal
1863818672
** expression, or a reference to a VIRTUAL column */
@@ -24028,11 +24062,11 @@
2402824062
/* no break */ deliberate_fall_through
2402924063
case SQLITE_DBSTATUS_CACHE_HIT:
2403024064
case SQLITE_DBSTATUS_CACHE_MISS:
2403124065
case SQLITE_DBSTATUS_CACHE_WRITE:{
2403224066
int i;
24033
- int nRet = 0;
24067
+ u64 nRet = 0;
2403424068
assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
2403524069
assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
2403624070
2403724071
for(i=0; i<db->nDb; i++){
2403824072
if( db->aDb[i].pBt ){
@@ -24041,11 +24075,11 @@
2404124075
}
2404224076
}
2404324077
*pHighwater = 0; /* IMP: R-42420-56072 */
2404424078
/* IMP: R-54100-20147 */
2404524079
/* IMP: R-29431-39229 */
24046
- *pCurrent = nRet;
24080
+ *pCurrent = (int)nRet & 0x7fffffff;
2404724081
break;
2404824082
}
2404924083
2405024084
/* Set *pCurrent to non-zero if there are unresolved deferred foreign
2405124085
** key constraints. Set *pCurrent to zero if all foreign key constraints
@@ -34677,11 +34711,11 @@
3467734711
** Load the sqlite3.iSysErrno field if that is an appropriate thing
3467834712
** to do based on the SQLite error code in rc.
3467934713
*/
3468034714
SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
3468134715
if( rc==SQLITE_IOERR_NOMEM ) return;
34682
-#ifdef SQLITE_USE_SEH
34716
+#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
3468334717
if( rc==SQLITE_IOERR_IN_PAGE ){
3468434718
int ii;
3468534719
int iErr;
3468634720
sqlite3BtreeEnterAll(db);
3468734721
for(ii=0; ii<db->nDb; ii++){
@@ -42365,13 +42399,19 @@
4236542399
struct flock f; /* The posix advisory locking structure */
4236642400
int rc = SQLITE_OK; /* Result code form fcntl() */
4236742401
4236842402
pShmNode = pFile->pInode->pShmNode;
4236942403
42370
- /* Assert that the correct mutex or mutexes are held. */
42371
- if( pShmNode->nRef==0 ){
42372
- assert( ofst==UNIX_SHM_DMS && n==1 && unixMutexHeld() );
42404
+ /* Assert that the parameters are within expected range and that the
42405
+ ** correct mutex or mutexes are held. */
42406
+ assert( pShmNode->nRef>=0 );
42407
+ assert( (ofst==UNIX_SHM_DMS && n==1)
42408
+ || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK))
42409
+ );
42410
+ if( ofst==UNIX_SHM_DMS ){
42411
+ assert( pShmNode->nRef>0 || unixMutexHeld() );
42412
+ assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
4237342413
}else{
4237442414
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
4237542415
int ii;
4237642416
for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){
4237742417
assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) );
@@ -57333,11 +57373,11 @@
5733357373
i64 journalSizeLimit; /* Size limit for persistent journal files */
5733457374
char *zFilename; /* Name of the database file */
5733557375
char *zJournal; /* Name of the journal file */
5733657376
int (*xBusyHandler)(void*); /* Function to call when busy */
5733757377
void *pBusyHandlerArg; /* Context argument for xBusyHandler */
57338
- int aStat[4]; /* Total cache hits, misses, writes, spills */
57378
+ u32 aStat[4]; /* Total cache hits, misses, writes, spills */
5733957379
#ifdef SQLITE_TEST
5734057380
int nRead; /* Database pages read */
5734157381
#endif
5734257382
void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
5734357383
int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
@@ -63477,15 +63517,15 @@
6347763517
a[1] = sqlite3PcachePagecount(pPager->pPCache);
6347863518
a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
6347963519
a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
6348063520
a[4] = pPager->eState;
6348163521
a[5] = pPager->errCode;
63482
- a[6] = pPager->aStat[PAGER_STAT_HIT];
63483
- a[7] = pPager->aStat[PAGER_STAT_MISS];
63522
+ a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff;
63523
+ a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff;
6348463524
a[8] = 0; /* Used to be pPager->nOvfl */
6348563525
a[9] = pPager->nRead;
63486
- a[10] = pPager->aStat[PAGER_STAT_WRITE];
63526
+ a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff;
6348763527
return a;
6348863528
}
6348963529
#endif
6349063530
6349163531
/*
@@ -63497,11 +63537,11 @@
6349763537
** Before returning, *pnVal is incremented by the
6349863538
** current cache hit or miss count, according to the value of eStat. If the
6349963539
** reset parameter is non-zero, the cache hit or miss count is zeroed before
6350063540
** returning.
6350163541
*/
63502
-SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
63542
+SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){
6350363543
6350463544
assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
6350563545
|| eStat==SQLITE_DBSTATUS_CACHE_MISS
6350663546
|| eStat==SQLITE_DBSTATUS_CACHE_WRITE
6350763547
|| eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
@@ -64437,11 +64477,11 @@
6443764477
assert( pPager->eState>=PAGER_READER );
6443864478
return sqlite3WalFramesize(pPager->pWal);
6443964479
}
6444064480
#endif
6444164481
64442
-#ifdef SQLITE_USE_SEH
64482
+#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
6444364483
SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){
6444464484
return sqlite3WalSystemErrno(pPager->pWal);
6444564485
}
6444664486
#endif
6444764487
@@ -106789,10 +106829,11 @@
106789106829
sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
106790106830
}
106791106831
sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
106792106832
pParse->checkSchema = 1;
106793106833
pTopNC->nNcErr++;
106834
+ eNewExprOp = TK_NULL;
106794106835
}
106795106836
assert( pFJMatch==0 );
106796106837
106797106838
/* Remove all substructure from pExpr */
106798106839
if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
@@ -110976,13 +111017,14 @@
110976111017
case TK_BLOB:
110977111018
return 0;
110978111019
case TK_COLUMN:
110979111020
assert( ExprUseYTab(p) );
110980111021
return ExprHasProperty(p, EP_CanBeNull) ||
110981
- p->y.pTab==0 || /* Reference to column of index on expression */
111022
+ NEVER(p->y.pTab==0) || /* Reference to column of index on expr */
110982111023
(p->iColumn>=0
110983111024
&& p->y.pTab->aCol!=0 /* Possible due to prior error */
111025
+ && ALWAYS(p->iColumn<p->y.pTab->nCol)
110984111026
&& p->y.pTab->aCol[p->iColumn].notNull==0);
110985111027
default:
110986111028
return 1;
110987111029
}
110988111030
}
@@ -113560,12 +113602,14 @@
113560113602
assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
113561113603
if( pParse->pVdbe==0 ) return;
113562113604
inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
113563113605
if( inReg!=target ){
113564113606
u8 op;
113565
- if( ALWAYS(pExpr)
113566
- && (ExprHasProperty(pExpr,EP_Subquery) || pExpr->op==TK_REGISTER)
113607
+ Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr);
113608
+ testcase( pX!=pExpr );
113609
+ if( ALWAYS(pX)
113610
+ && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER)
113567113611
){
113568113612
op = OP_Copy;
113569113613
}else{
113570113614
op = OP_SCopy;
113571113615
}
@@ -117825,13 +117869,13 @@
117825117869
** information.
117826117870
*/
117827117871
typedef struct StatAccum StatAccum;
117828117872
typedef struct StatSample StatSample;
117829117873
struct StatSample {
117830
- tRowcnt *anEq; /* sqlite_stat4.nEq */
117831117874
tRowcnt *anDLt; /* sqlite_stat4.nDLt */
117832117875
#ifdef SQLITE_ENABLE_STAT4
117876
+ tRowcnt *anEq; /* sqlite_stat4.nEq */
117833117877
tRowcnt *anLt; /* sqlite_stat4.nLt */
117834117878
union {
117835117879
i64 iRowid; /* Rowid in main table of the key */
117836117880
u8 *aRowid; /* Key for WITHOUT ROWID tables */
117837117881
} u;
@@ -117985,13 +118029,13 @@
117985118029
assert( nKeyCol<=nCol );
117986118030
assert( nKeyCol>0 );
117987118031
117988118032
/* Allocate the space required for the StatAccum object */
117989118033
n = sizeof(*p)
117990
- + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
117991
- + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
118034
+ + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
117992118035
#ifdef SQLITE_ENABLE_STAT4
118036
+ n += sizeof(tRowcnt)*nColUp; /* StatAccum.anEq */
117993118037
if( mxSample ){
117994118038
n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
117995118039
+ sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
117996118040
+ sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
117997118041
}
@@ -118008,13 +118052,13 @@
118008118052
p->nLimit = sqlite3_value_int64(argv[3]);
118009118053
p->nCol = nCol;
118010118054
p->nKeyCol = nKeyCol;
118011118055
p->nSkipAhead = 0;
118012118056
p->current.anDLt = (tRowcnt*)&p[1];
118013
- p->current.anEq = &p->current.anDLt[nColUp];
118014118057
118015118058
#ifdef SQLITE_ENABLE_STAT4
118059
+ p->current.anEq = &p->current.anDLt[nColUp];
118016118060
p->mxSample = p->nLimit==0 ? mxSample : 0;
118017118061
if( mxSample ){
118018118062
u8 *pSpace; /* Allocated space not yet assigned */
118019118063
int i; /* Used to iterate through p->aSample[] */
118020118064
@@ -118277,28 +118321,32 @@
118277118321
assert( p->nCol>0 );
118278118322
assert( iChng<p->nCol );
118279118323
118280118324
if( p->nRow==0 ){
118281118325
/* This is the first call to this function. Do initialization. */
118326
+#ifdef SQLITE_ENABLE_STAT4
118282118327
for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
118328
+#endif
118283118329
}else{
118284118330
/* Second and subsequent calls get processed here */
118285118331
#ifdef SQLITE_ENABLE_STAT4
118286118332
if( p->mxSample ) samplePushPrevious(p, iChng);
118287118333
#endif
118288118334
118289118335
/* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
118290118336
** to the current row of the index. */
118337
+#ifdef SQLITE_ENABLE_STAT4
118291118338
for(i=0; i<iChng; i++){
118292118339
p->current.anEq[i]++;
118293118340
}
118341
+#endif
118294118342
for(i=iChng; i<p->nCol; i++){
118295118343
p->current.anDLt[i]++;
118296118344
#ifdef SQLITE_ENABLE_STAT4
118297118345
if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
118298
-#endif
118299118346
p->current.anEq[i] = 1;
118347
+#endif
118300118348
}
118301118349
}
118302118350
118303118351
p->nRow++;
118304118352
#ifdef SQLITE_ENABLE_STAT4
@@ -118428,11 +118476,13 @@
118428118476
for(i=0; i<p->nKeyCol; i++){
118429118477
u64 nDistinct = p->current.anDLt[i] + 1;
118430118478
u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
118431118479
if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
118432118480
sqlite3_str_appendf(&sStat, " %llu", iVal);
118481
+#ifdef SQLITE_ENABLE_STAT4
118433118482
assert( p->current.anEq[i] );
118483
+#endif
118434118484
}
118435118485
sqlite3ResultStrAccum(context, &sStat);
118436118486
}
118437118487
#ifdef SQLITE_ENABLE_STAT4
118438118488
else if( eCall==STAT_GET_ROWID ){
@@ -119117,10 +119167,20 @@
119117119167
}
119118119168
#endif
119119119169
while( z[0]!=0 && z[0]!=' ' ) z++;
119120119170
while( z[0]==' ' ) z++;
119121119171
}
119172
+
119173
+ /* Set the bLowQual flag if the peak number of rows obtained
119174
+ ** from a full equality match is so large that a full table scan
119175
+ ** seems likely to be faster than using the index.
119176
+ */
119177
+ if( aLog[0] > 66 /* Index has more than 100 rows */
119178
+ && aLog[0] <= aLog[nOut-1] /* And only a single value seen */
119179
+ ){
119180
+ pIndex->bLowQual = 1;
119181
+ }
119122119182
}
119123119183
}
119124119184
119125119185
/*
119126119186
** This callback is invoked once for each index when reading the
@@ -148743,11 +148803,11 @@
148743148803
pSub->pPrior = 0;
148744148804
pSub->pNext = 0;
148745148805
pSub->selFlags |= SF_Aggregate;
148746148806
pSub->selFlags &= ~SF_Compound;
148747148807
pSub->nSelectRow = 0;
148748
- sqlite3ExprListDelete(db, pSub->pEList);
148808
+ sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
148749148809
pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
148750148810
pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
148751148811
pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
148752148812
sqlite3PExprAddSelect(pParse, pTerm, pSub);
148753148813
if( pExpr==0 ){
@@ -154295,11 +154355,10 @@
154295154355
assert( sqlite3BtreeHoldsAllMutexes(db) );
154296154356
assert( sqlite3_mutex_held(db->mutex) );
154297154357
154298154358
if( p ){
154299154359
db->pDisconnect = 0;
154300
- sqlite3ExpirePreparedStatements(db, 0);
154301154360
do {
154302154361
VTable *pNext = p->pNext;
154303154362
sqlite3VtabUnlock(p);
154304154363
p = pNext;
154305154364
}while( p );
@@ -155861,11 +155920,11 @@
155861155920
*/
155862155921
SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
155863155922
#ifdef WHERETRACE_ENABLED
155864155923
SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
155865155924
SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
155866
-SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
155925
+SQLITE_PRIVATE void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC);
155867155926
#endif
155868155927
SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
155869155928
WhereClause *pWC, /* The WHERE clause to be searched */
155870155929
int iCur, /* Cursor number of LHS */
155871155930
int iColumn, /* Column number of LHS */
@@ -162890,21 +162949,38 @@
162890162949
#endif
162891162950
162892162951
#ifdef WHERETRACE_ENABLED
162893162952
/*
162894162953
** Print a WhereLoop object for debugging purposes
162954
+**
162955
+** Format example:
162956
+**
162957
+** .--- Position in WHERE clause rSetup, rRun, nOut ---.
162958
+** | |
162959
+** | .--- selfMask nTerm ------. |
162960
+** | | | |
162961
+** | | .-- prereq Idx wsFlags----. | |
162962
+** | | | Name | | |
162963
+** | | | __|__ nEq ---. ___|__ | __|__
162964
+** | / \ / \ / \ | / \ / \ / \
162965
+** 1.002.001 t2.t2xy 2 f 010241 N 2 cost 0,56,31
162895162966
*/
162896
-SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
162897
- WhereInfo *pWInfo = pWC->pWInfo;
162898
- int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
162899
- SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
162900
- Table *pTab = pItem->pTab;
162901
- Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
162902
- sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
162903
- p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
162904
- sqlite3DebugPrintf(" %12s",
162905
- pItem->zAlias ? pItem->zAlias : pTab->zName);
162967
+SQLITE_PRIVATE void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC){
162968
+ if( pWC ){
162969
+ WhereInfo *pWInfo = pWC->pWInfo;
162970
+ int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
162971
+ SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
162972
+ Table *pTab = pItem->pTab;
162973
+ Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
162974
+ sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
162975
+ p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
162976
+ sqlite3DebugPrintf(" %12s",
162977
+ pItem->zAlias ? pItem->zAlias : pTab->zName);
162978
+ }else{
162979
+ sqlite3DebugPrintf("%c%2d.%03llx.%03llx %c%d",
162980
+ p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab);
162981
+ }
162906162982
if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
162907162983
const char *zName;
162908162984
if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
162909162985
if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
162910162986
int i = sqlite3Strlen30(zName) - 1;
@@ -162936,10 +163012,19 @@
162936163012
int i;
162937163013
for(i=0; i<p->nLTerm; i++){
162938163014
sqlite3WhereTermPrint(p->aLTerm[i], i);
162939163015
}
162940163016
}
163017
+}
163018
+SQLITE_PRIVATE void sqlite3ShowWhereLoop(const WhereLoop *p){
163019
+ if( p ) sqlite3WhereLoopPrint(p, 0);
163020
+}
163021
+SQLITE_PRIVATE void sqlite3ShowWhereLoopList(const WhereLoop *p){
163022
+ while( p ){
163023
+ sqlite3ShowWhereLoop(p);
163024
+ p = p->pNextLoop;
163025
+ }
162941163026
}
162942163027
#endif
162943163028
162944163029
/*
162945163030
** Convert bulk memory into a valid WhereLoop that can be passed
@@ -163049,50 +163134,64 @@
163049163134
}
163050163135
sqlite3DbNNFreeNN(db, pWInfo);
163051163136
}
163052163137
163053163138
/*
163054
-** Return TRUE if all of the following are true:
163055
-**
163056
-** (1) X has the same or lower cost, or returns the same or fewer rows,
163057
-** than Y.
163058
-** (2) X uses fewer WHERE clause terms than Y
163059
-** (3) Every WHERE clause term used by X is also used by Y
163060
-** (4) X skips at least as many columns as Y
163061
-** (5) If X is a covering index, than Y is too
163062
-**
163063
-** Conditions (2) and (3) mean that X is a "proper subset" of Y.
163064
-** If X is a proper subset of Y then Y is a better choice and ought
163065
-** to have a lower cost. This routine returns TRUE when that cost
163066
-** relationship is inverted and needs to be adjusted. Constraint (4)
163067
-** was added because if X uses skip-scan less than Y it still might
163068
-** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
163069
-** was added because a covering index probably deserves to have a lower cost
163070
-** than a non-covering index even if it is a proper subset.
163139
+** Return TRUE if X is a proper subset of Y but is of equal or less cost.
163140
+** In other words, return true if all constraints of X are also part of Y
163141
+** and Y has additional constraints that might speed the search that X lacks
163142
+** but the cost of running X is not more than the cost of running Y.
163143
+**
163144
+** In other words, return true if the cost relationwship between X and Y
163145
+** is inverted and needs to be adjusted.
163146
+**
163147
+** Case 1:
163148
+**
163149
+** (1a) X and Y use the same index.
163150
+** (1b) X has fewer == terms than Y
163151
+** (1c) Neither X nor Y use skip-scan
163152
+** (1d) X does not have a a greater cost than Y
163153
+**
163154
+** Case 2:
163155
+**
163156
+** (2a) X has the same or lower cost, or returns the same or fewer rows,
163157
+** than Y.
163158
+** (2b) X uses fewer WHERE clause terms than Y
163159
+** (2c) Every WHERE clause term used by X is also used by Y
163160
+** (2d) X skips at least as many columns as Y
163161
+** (2e) If X is a covering index, than Y is too
163071163162
*/
163072163163
static int whereLoopCheaperProperSubset(
163073163164
const WhereLoop *pX, /* First WhereLoop to compare */
163074163165
const WhereLoop *pY /* Compare against this WhereLoop */
163075163166
){
163076163167
int i, j;
163168
+ if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */
163169
+ assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 );
163170
+ assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 );
163171
+ if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */
163172
+ && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */
163173
+ && pX->nSkip==0 && pY->nSkip==0 /* (1c) */
163174
+ ){
163175
+ return 1; /* Case 1 is true */
163176
+ }
163077163177
if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
163078
- return 0; /* X is not a subset of Y */
163178
+ return 0; /* (2b) */
163079163179
}
163080
- if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
163081
- if( pY->nSkip > pX->nSkip ) return 0;
163180
+ if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */
163082163181
for(i=pX->nLTerm-1; i>=0; i--){
163083163182
if( pX->aLTerm[i]==0 ) continue;
163084163183
for(j=pY->nLTerm-1; j>=0; j--){
163085163184
if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
163086163185
}
163087
- if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
163186
+ if( j<0 ) return 0; /* (2c) */
163088163187
}
163089163188
if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
163090163189
&& (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
163091
- return 0; /* Constraint (5) */
163190
+ return 0; /* (2e) */
163092163191
}
163093
- return 1; /* All conditions meet */
163192
+ return 1; /* Case 2 is true */
163094163193
}
163095163194
163096163195
/*
163097163196
** Try to adjust the cost and number of output rows of WhereLoop pTemplate
163098163197
** upwards or downwards so that:
@@ -163578,11 +163677,14 @@
163578163677
opMask = WO_LT|WO_LE;
163579163678
}else{
163580163679
assert( pNew->u.btree.nBtm==0 );
163581163680
opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
163582163681
}
163583
- if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
163682
+ if( pProbe->bUnordered || pProbe->bLowQual ){
163683
+ if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
163684
+ if( pProbe->bLowQual ) opMask &= ~(WO_EQ|WO_IN|WO_IS);
163685
+ }
163584163686
163585163687
assert( pNew->u.btree.nEq<pProbe->nColumn );
163586163688
assert( pNew->u.btree.nEq<pProbe->nKeyCol
163587163689
|| pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
163588163690
@@ -166683,11 +166785,14 @@
166683166785
** struct, the contents of WhereInfo.a[], the WhereClause structure
166684166786
** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
166685166787
** field (type Bitmask) it must be aligned on an 8-byte boundary on
166686166788
** some architectures. Hence the ROUND8() below.
166687166789
*/
166688
- nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
166790
+ nByteWInfo = ROUND8P(sizeof(WhereInfo));
166791
+ if( nTabList>1 ){
166792
+ nByteWInfo = ROUND8P(nByteWInfo + (nTabList-1)*sizeof(WhereLevel));
166793
+ }
166689166794
pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
166690166795
if( db->mallocFailed ){
166691166796
sqlite3DbFree(db, pWInfo);
166692166797
pWInfo = 0;
166693166798
goto whereBeginError;
@@ -167245,10 +167350,15 @@
167245167350
whereBeginError:
167246167351
if( pWInfo ){
167247167352
pParse->nQueryLoop = pWInfo->savedNQueryLoop;
167248167353
whereInfoFree(db, pWInfo);
167249167354
}
167355
+#ifdef WHERETRACE_ENABLED
167356
+ /* Prevent harmless compiler warnings about debugging routines
167357
+ ** being declared but never used */
167358
+ sqlite3ShowWhereLoopList(0);
167359
+#endif /* WHERETRACE_ENABLED */
167250167360
return 0;
167251167361
}
167252167362
167253167363
/*
167254167364
** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
@@ -203027,13 +203137,13 @@
203027203137
** The original design stored all JSON as pure text, canonical RFC-8259.
203028203138
** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16).
203029203139
** All generated JSON text still conforms strictly to RFC-8259, but text
203030203140
** with JSON-5 extensions is accepted as input.
203031203141
**
203032
-** Beginning with version 3.45.0 (pending), these routines also accept
203033
-** BLOB values that have JSON encoded using a binary representation we
203034
-** call JSONB. The name JSONB comes from PostgreSQL, however the on-disk
203142
+** Beginning with version 3.45.0 (circa 2024-01-01), these routines also
203143
+** accept BLOB values that have JSON encoded using a binary representation
203144
+** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk
203035203145
** format SQLite JSONB is completely different and incompatible with
203036203146
** PostgreSQL JSONB.
203037203147
**
203038203148
** Decoding and interpreting JSONB is still O(N) where N is the size of
203039203149
** the input, the same as text JSON. However, the constant of proportionality
@@ -203120,10 +203230,13 @@
203120203230
** code is between 0 and 12 and that the total size of the element
203121203231
** (header plus payload) is the same as the size of the BLOB. If those
203122203232
** checks are true, the BLOB is assumed to be JSONB and processing continues.
203123203233
** Errors are only raised if some other miscoding is discovered during
203124203234
** processing.
203235
+**
203236
+** Additional information can be found in the doc/jsonb.md file of the
203237
+** canonical SQLite source tree.
203125203238
*/
203126203239
#ifndef SQLITE_OMIT_JSON
203127203240
/* #include "sqliteInt.h" */
203128203241
203129203242
/* JSONB element types
@@ -203218,18 +203331,26 @@
203218203331
** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
203219203332
*/
203220203333
#define JSON_CACHE_ID (-429938) /* Cache entry */
203221203334
#define JSON_CACHE_SIZE 4 /* Max number of cache entries */
203222203335
203336
+/*
203337
+** jsonUnescapeOneChar() returns this invalid code point if it encounters
203338
+** a syntax error.
203339
+*/
203340
+#define JSON_INVALID_CHAR 0x99999
203341
+
203223203342
/* A cache mapping JSON text into JSONB blobs.
203224203343
**
203225203344
** Each cache entry is a JsonParse object with the following restrictions:
203226203345
**
203227203346
** * The bReadOnly flag must be set
203228203347
**
203229203348
** * The aBlob[] array must be owned by the JsonParse object. In other
203230203349
** words, nBlobAlloc must be non-zero.
203350
+**
203351
+** * eEdit and delta must be zero.
203231203352
**
203232203353
** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
203233203354
*/
203234203355
struct JsonCache {
203235203356
sqlite3 *db; /* Database connection */
@@ -203286,27 +203407,27 @@
203286203407
**
203287203408
** 3. Zero or more changes are made to aBlob[] (via json_remove() or
203288203409
** json_replace() or json_patch() or similar).
203289203410
**
203290203411
** 4. New JSON text is generated from the aBlob[] for output. This step
203291
-** is skipped the function is one of the jsonb_* functions that returns
203292
-** JSONB instead of text JSON.
203412
+** is skipped if the function is one of the jsonb_* functions that
203413
+** returns JSONB instead of text JSON.
203293203414
*/
203294203415
struct JsonParse {
203295203416
u8 *aBlob; /* JSONB representation of JSON value */
203296203417
u32 nBlob; /* Bytes of aBlob[] actually used */
203297203418
u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
203298203419
char *zJson; /* Json text used for parsing */
203299203420
int nJson; /* Length of the zJson string in bytes */
203421
+ u32 nJPRef; /* Number of references to this object */
203422
+ u32 iErr; /* Error location in zJson[] */
203300203423
u16 iDepth; /* Nesting depth */
203301203424
u8 nErr; /* Number of errors seen */
203302203425
u8 oom; /* Set to true if out of memory */
203303203426
u8 bJsonIsRCStr; /* True if zJson is an RCStr */
203304203427
u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
203305203428
u8 bReadOnly; /* Do not modify. */
203306
- u32 nJPRef; /* Number of references to this object */
203307
- u32 iErr; /* Error location in zJson[] */
203308203429
/* Search and edit information. See jsonLookupStep() */
203309203430
u8 eEdit; /* Edit operation to apply */
203310203431
int delta; /* Size change due to the edit */
203311203432
u32 nIns; /* Number of bytes to insert */
203312203433
u32 iLabel; /* Location of label if search landed on an object value */
@@ -203341,11 +203462,11 @@
203341203462
/**************************************************************************
203342203463
** Forward references
203343203464
**************************************************************************/
203344203465
static void jsonReturnStringAsBlob(JsonString*);
203345203466
static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
203346
-static u32 jsonXlateBlobToText(const JsonParse*,u32,JsonString*);
203467
+static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
203347203468
static void jsonReturnParse(sqlite3_context*,JsonParse*);
203348203469
static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
203349203470
static void jsonParseFree(JsonParse*);
203350203471
static u32 jsonbPayloadSize(const JsonParse*, u32, u32*);
203351203472
static u32 jsonUnescapeOneChar(const char*, u32, u32*);
@@ -203381,10 +203502,11 @@
203381203502
){
203382203503
JsonCache *p;
203383203504
203384203505
assert( pParse->zJson!=0 );
203385203506
assert( pParse->bJsonIsRCStr );
203507
+ assert( pParse->delta==0 );
203386203508
p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID);
203387203509
if( p==0 ){
203388203510
sqlite3 *db = sqlite3_context_db_handle(ctx);
203389203511
p = sqlite3DbMallocZero(db, sizeof(*p));
203390203512
if( p==0 ) return SQLITE_NOMEM;
@@ -203453,10 +203575,11 @@
203453203575
JsonParse *tmp = p->a[i];
203454203576
memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp));
203455203577
p->a[p->nUsed-1] = tmp;
203456203578
i = p->nUsed - 1;
203457203579
}
203580
+ assert( p->a[i]->delta==0 );
203458203581
return p->a[i];
203459203582
}else{
203460203583
return 0;
203461203584
}
203462203585
}
@@ -203621,12 +203744,37 @@
203621203744
if( z==0 ) return;
203622203745
if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
203623203746
p->zBuf[p->nUsed++] = '"';
203624203747
while( 1 /*exit-by-break*/ ){
203625203748
k = 0;
203626
- while( k+1<N && jsonIsOk[z[k]] && jsonIsOk[z[k+1]] ){ k += 2; } /* <--, */
203627
- while( k<N && jsonIsOk[z[k]] ){ k++; } /* <-- loop unwound for speed */
203749
+ /* The following while() is the 4-way unwound equivalent of
203750
+ **
203751
+ ** while( k<N && jsonIsOk[z[k]] ){ k++; }
203752
+ */
203753
+ while( 1 /* Exit by break */ ){
203754
+ if( k+3>=N ){
203755
+ while( k<N && jsonIsOk[z[k]] ){ k++; }
203756
+ break;
203757
+ }
203758
+ if( !jsonIsOk[z[k]] ){
203759
+ break;
203760
+ }
203761
+ if( !jsonIsOk[z[k+1]] ){
203762
+ k += 1;
203763
+ break;
203764
+ }
203765
+ if( !jsonIsOk[z[k+2]] ){
203766
+ k += 2;
203767
+ break;
203768
+ }
203769
+ if( !jsonIsOk[z[k+3]] ){
203770
+ k += 3;
203771
+ break;
203772
+ }else{
203773
+ k += 4;
203774
+ }
203775
+ }
203628203776
if( k>=N ){
203629203777
if( k>0 ){
203630203778
memcpy(&p->zBuf[p->nUsed], z, k);
203631203779
p->nUsed += k;
203632203780
}
@@ -203714,11 +203862,11 @@
203714203862
if( jsonFuncArgMightBeBinary(pValue) ){
203715203863
JsonParse px;
203716203864
memset(&px, 0, sizeof(px));
203717203865
px.aBlob = (u8*)sqlite3_value_blob(pValue);
203718203866
px.nBlob = sqlite3_value_bytes(pValue);
203719
- jsonXlateBlobToText(&px, 0, p);
203867
+ jsonTranslateBlobToText(&px, 0, p);
203720203868
}else if( p->eErr==0 ){
203721203869
sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
203722203870
p->eErr = JSTRING_ERR;
203723203871
jsonStringReset(p);
203724203872
}
@@ -204231,18 +204379,14 @@
204231204379
** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
204232204380
** any changes to *pOp and return false.
204233204381
*/
204234204382
static int jsonIs4HexB(const char *z, int *pOp){
204235204383
if( z[0]!='u' ) return 0;
204236
- if( !sqlite3Isxdigit(z[1]) ) return 0;
204237
- if( !sqlite3Isxdigit(z[2]) ) return 0;
204238
- if( !sqlite3Isxdigit(z[3]) ) return 0;
204239
- if( !sqlite3Isxdigit(z[4]) ) return 0;
204384
+ if( !jsonIs4Hex(&z[1]) ) return 0;
204240204385
*pOp = JSONB_TEXTJ;
204241204386
return 1;
204242204387
}
204243
-
204244204388
204245204389
/*
204246204390
** Check a single element of the JSONB in pParse for validity.
204247204391
**
204248204392
** The element to be checked starts at offset i and must end at on the
@@ -204384,11 +204528,11 @@
204384204528
}else if( x!=JSONB_TEXT5 ){
204385204529
return j+1;
204386204530
}else{
204387204531
u32 c = 0;
204388204532
u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
204389
- if( c==0xfffd ) return j+1;
204533
+ if( c==JSON_INVALID_CHAR ) return j+1;
204390204534
j += szC - 1;
204391204535
}
204392204536
}
204393204537
j++;
204394204538
}
@@ -204456,11 +204600,11 @@
204456204600
** -2 '}' seen \
204457204601
** -3 ']' seen \___ For these returns, pParse->iErr is set to
204458204602
** -4 ',' seen / the index in zJson[] of the seen character
204459204603
** -5 ':' seen /
204460204604
*/
204461
-static int jsonXlateTextToBlob(JsonParse *pParse, u32 i){
204605
+static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
204462204606
char c;
204463204607
u32 j;
204464204608
u32 iThis, iStart;
204465204609
int x;
204466204610
u8 t;
@@ -204476,11 +204620,11 @@
204476204620
return -1;
204477204621
}
204478204622
iStart = pParse->nBlob;
204479204623
for(j=i+1;;j++){
204480204624
u32 iBlob = pParse->nBlob;
204481
- x = jsonXlateTextToBlob(pParse, j);
204625
+ x = jsonTranslateTextToBlob(pParse, j);
204482204626
if( x<=0 ){
204483204627
int op;
204484204628
if( x==(-2) ){
204485204629
j = pParse->iErr;
204486204630
if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
@@ -204522,19 +204666,19 @@
204522204666
if( z[j]==':' ){
204523204667
j++;
204524204668
goto parse_object_value;
204525204669
}
204526204670
}
204527
- x = jsonXlateTextToBlob(pParse, j);
204671
+ x = jsonTranslateTextToBlob(pParse, j);
204528204672
if( x!=(-5) ){
204529204673
if( x!=(-1) ) pParse->iErr = j;
204530204674
return -1;
204531204675
}
204532204676
j = pParse->iErr+1;
204533204677
}
204534204678
parse_object_value:
204535
- x = jsonXlateTextToBlob(pParse, j);
204679
+ x = jsonTranslateTextToBlob(pParse, j);
204536204680
if( x<=0 ){
204537204681
if( x!=(-1) ) pParse->iErr = j;
204538204682
return -1;
204539204683
}
204540204684
j = x;
@@ -204549,11 +204693,11 @@
204549204693
continue;
204550204694
}else if( z[j]=='}' ){
204551204695
break;
204552204696
}
204553204697
}
204554
- x = jsonXlateTextToBlob(pParse, j);
204698
+ x = jsonTranslateTextToBlob(pParse, j);
204555204699
if( x==(-4) ){
204556204700
j = pParse->iErr;
204557204701
continue;
204558204702
}
204559204703
if( x==(-2) ){
@@ -204577,11 +204721,11 @@
204577204721
if( ++pParse->iDepth > JSON_MAX_DEPTH ){
204578204722
pParse->iErr = i;
204579204723
return -1;
204580204724
}
204581204725
for(j=i+1;;j++){
204582
- x = jsonXlateTextToBlob(pParse, j);
204726
+ x = jsonTranslateTextToBlob(pParse, j);
204583204727
if( x<=0 ){
204584204728
if( x==(-3) ){
204585204729
j = pParse->iErr;
204586204730
if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
204587204731
break;
@@ -204601,11 +204745,11 @@
204601204745
continue;
204602204746
}else if( z[j]==']' ){
204603204747
break;
204604204748
}
204605204749
}
204606
- x = jsonXlateTextToBlob(pParse, j);
204750
+ x = jsonTranslateTextToBlob(pParse, j);
204607204751
if( x==(-4) ){
204608204752
j = pParse->iErr;
204609204753
continue;
204610204754
}
204611204755
if( x==(-3) ){
@@ -204924,11 +205068,11 @@
204924205068
JsonParse *pParse, /* Initialize and fill this JsonParse object */
204925205069
sqlite3_context *pCtx /* Report errors here */
204926205070
){
204927205071
int i;
204928205072
const char *zJson = pParse->zJson;
204929
- i = jsonXlateTextToBlob(pParse, 0);
205073
+ i = jsonTranslateTextToBlob(pParse, 0);
204930205074
if( pParse->oom ) i = -1;
204931205075
if( i>0 ){
204932205076
#ifdef SQLITE_DEBUG
204933205077
assert( pParse->iDepth==0 );
204934205078
if( sqlite3Config.bJsonSelfcheck ){
@@ -204969,11 +205113,11 @@
204969205113
JsonParse px;
204970205114
memset(&px, 0, sizeof(px));
204971205115
jsonStringTerminate(pStr);
204972205116
px.zJson = pStr->zBuf;
204973205117
px.nJson = pStr->nUsed;
204974
- (void)jsonXlateTextToBlob(&px, 0);
205118
+ (void)jsonTranslateTextToBlob(&px, 0);
204975205119
if( px.oom ){
204976205120
sqlite3_free(px.aBlob);
204977205121
sqlite3_result_error_nomem(pStr->pCtx);
204978205122
}else{
204979205123
assert( px.nBlobAlloc>0 );
@@ -205057,11 +205201,11 @@
205057205201
** are detected. So a malformed JSONB input might either result
205058205202
** in an error, or in incorrect JSON.
205059205203
**
205060205204
** The pOut->eErr JSTRING_OOM flag is set on a OOM.
205061205205
*/
205062
-static u32 jsonXlateBlobToText(
205206
+static u32 jsonTranslateBlobToText(
205063205207
const JsonParse *pParse, /* the complete parse of the JSON */
205064205208
u32 i, /* Start rendering at this index */
205065205209
JsonString *pOut /* Write JSON here */
205066205210
){
205067205211
u32 sz, n, j, iEnd;
@@ -205228,11 +205372,11 @@
205228205372
case JSONB_ARRAY: {
205229205373
jsonAppendChar(pOut, '[');
205230205374
j = i+n;
205231205375
iEnd = j+sz;
205232205376
while( j<iEnd ){
205233
- j = jsonXlateBlobToText(pParse, j, pOut);
205377
+ j = jsonTranslateBlobToText(pParse, j, pOut);
205234205378
jsonAppendChar(pOut, ',');
205235205379
}
205236205380
if( sz>0 ) pOut->nUsed--;
205237205381
jsonAppendChar(pOut, ']');
205238205382
break;
@@ -205241,11 +205385,11 @@
205241205385
int x = 0;
205242205386
jsonAppendChar(pOut, '{');
205243205387
j = i+n;
205244205388
iEnd = j+sz;
205245205389
while( j<iEnd ){
205246
- j = jsonXlateBlobToText(pParse, j, pOut);
205390
+ j = jsonTranslateBlobToText(pParse, j, pOut);
205247205391
jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
205248205392
}
205249205393
if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED;
205250205394
if( sz>0 ) pOut->nUsed--;
205251205395
jsonAppendChar(pOut, '}');
@@ -205394,23 +205538,27 @@
205394205538
205395205539
/*
205396205540
** Input z[0..n] defines JSON escape sequence including the leading '\\'.
205397205541
** Decode that escape sequence into a single character. Write that
205398205542
** character into *piOut. Return the number of bytes in the escape sequence.
205543
+**
205544
+** If there is a syntax error of some kind (for example too few characters
205545
+** after the '\\' to complete the encoding) then *piOut is set to
205546
+** JSON_INVALID_CHAR.
205399205547
*/
205400205548
static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
205401205549
assert( n>0 );
205402205550
assert( z[0]=='\\' );
205403205551
if( n<2 ){
205404
- *piOut = 0xFFFD;
205552
+ *piOut = JSON_INVALID_CHAR;
205405205553
return n;
205406205554
}
205407205555
switch( (u8)z[1] ){
205408205556
case 'u': {
205409205557
u32 v, vlo;
205410205558
if( n<6 ){
205411
- *piOut = 0xFFFD;
205559
+ *piOut = JSON_INVALID_CHAR;
205412205560
return n;
205413205561
}
205414205562
v = jsonHexToInt4(&z[2]);
205415205563
if( (v & 0xfc00)==0xd800
205416205564
&& n>=12
@@ -205436,11 +205584,11 @@
205436205584
case '"':
205437205585
case '/':
205438205586
case '\\':{ *piOut = z[1]; return 2; }
205439205587
case 'x': {
205440205588
if( n<4 ){
205441
- *piOut = 0xFFFD;
205589
+ *piOut = JSON_INVALID_CHAR;
205442205590
return n;
205443205591
}
205444205592
*piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
205445205593
return 4;
205446205594
}
@@ -205447,11 +205595,11 @@
205447205595
case 0xe2:
205448205596
case '\r':
205449205597
case '\n': {
205450205598
u32 nSkip = jsonBytesToBypass(z, n);
205451205599
if( nSkip==0 ){
205452
- *piOut = 0xFFFD;
205600
+ *piOut = JSON_INVALID_CHAR;
205453205601
return n;
205454205602
}else if( nSkip==n ){
205455205603
*piOut = 0;
205456205604
return n;
205457205605
}else if( z[nSkip]=='\\' ){
@@ -205460,11 +205608,11 @@
205460205608
int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
205461205609
return nSkip + sz;
205462205610
}
205463205611
}
205464205612
default: {
205465
- *piOut = 0xFFFD;
205613
+ *piOut = JSON_INVALID_CHAR;
205466205614
return 2;
205467205615
}
205468205616
}
205469205617
}
205470205618
@@ -205823,11 +205971,11 @@
205823205971
if( NEVER(aBlob==0) ) return;
205824205972
memset(&x, 0, sizeof(x));
205825205973
x.aBlob = (u8*)aBlob;
205826205974
x.nBlob = nBlob;
205827205975
jsonStringInit(&s, ctx);
205828
- jsonXlateBlobToText(&x, 0, &s);
205976
+ jsonTranslateBlobToText(&x, 0, &s);
205829205977
jsonReturnString(&s, 0, 0);
205830205978
}
205831205979
205832205980
205833205981
/*
@@ -205934,21 +206082,21 @@
205934206082
if( c=='\\' ){
205935206083
u32 v;
205936206084
u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
205937206085
if( v<=0x7f ){
205938206086
zOut[iOut++] = (char)v;
205939
- }else if( v==0xfffd ){
205940
- /* Silently ignore illegal unicode */
205941206087
}else if( v<=0x7ff ){
205942206088
assert( szEscape>=2 );
205943206089
zOut[iOut++] = (char)(0xc0 | (v>>6));
205944206090
zOut[iOut++] = 0x80 | (v&0x3f);
205945206091
}else if( v<0x10000 ){
205946206092
assert( szEscape>=3 );
205947206093
zOut[iOut++] = 0xe0 | (v>>12);
205948206094
zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
205949206095
zOut[iOut++] = 0x80 | (v&0x3f);
206096
+ }else if( v==JSON_INVALID_CHAR ){
206097
+ /* Silently ignore illegal unicode */
205950206098
}else{
205951206099
assert( szEscape>=4 );
205952206100
zOut[iOut++] = 0xf0 | (v>>18);
205953206101
zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
205954206102
zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
@@ -206046,17 +206194,33 @@
206046206194
}else{
206047206195
jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson);
206048206196
}
206049206197
break;
206050206198
}
206051
- case SQLITE_FLOAT:
206199
+ case SQLITE_FLOAT: {
206200
+ double r = sqlite3_value_double(pArg);
206201
+ if( NEVER(sqlite3IsNaN(r)) ){
206202
+ jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0);
206203
+ }else{
206204
+ int n = sqlite3_value_bytes(pArg);
206205
+ const char *z = (const char*)sqlite3_value_text(pArg);
206206
+ if( z==0 ) return 1;
206207
+ if( z[0]=='I' ){
206208
+ jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
206209
+ }else if( z[0]=='-' && z[1]=='I' ){
206210
+ jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
206211
+ }else{
206212
+ jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z);
206213
+ }
206214
+ }
206215
+ break;
206216
+ }
206052206217
case SQLITE_INTEGER: {
206053206218
int n = sqlite3_value_bytes(pArg);
206054206219
const char *z = (const char*)sqlite3_value_text(pArg);
206055
- int e = eType==SQLITE_INTEGER ? JSONB_INT : JSONB_FLOAT;
206056206220
if( z==0 ) return 1;
206057
- jsonBlobAppendNode(pParse, e, n, z);
206221
+ jsonBlobAppendNode(pParse, JSONB_INT, n, z);
206058206222
break;
206059206223
}
206060206224
}
206061206225
if( pParse->oom ){
206062206226
sqlite3_result_error_nomem(ctx);
@@ -206153,15 +206317,10 @@
206153206317
}else{
206154206318
jsonBadPathError(ctx, zPath);
206155206319
}
206156206320
return;
206157206321
}
206158
-
206159
-/*
206160
-** Make a copy of a JsonParse object. The copy will be editable.
206161
-*/
206162
-
206163206322
206164206323
/*
206165206324
** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
206166206325
** from the SQL function argument pArg. Return a pointer to the new
206167206326
** JsonParse object.
@@ -206313,11 +206472,12 @@
206313206472
sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT);
206314206473
}
206315206474
}else{
206316206475
JsonString s;
206317206476
jsonStringInit(&s, ctx);
206318
- jsonXlateBlobToText(p, 0, &s);
206477
+ p->delta = 0;
206478
+ jsonTranslateBlobToText(p, 0, &s);
206319206479
jsonReturnString(&s, p, ctx);
206320206480
sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206321206481
}
206322206482
}
206323206483
@@ -206333,29 +206493,32 @@
206333206493
*/
206334206494
static void jsonDebugPrintBlob(
206335206495
JsonParse *pParse, /* JSON content */
206336206496
u32 iStart, /* Start rendering here */
206337206497
u32 iEnd, /* Do not render this byte or any byte after this one */
206338
- int nIndent /* Indent by this many spaces */
206498
+ int nIndent, /* Indent by this many spaces */
206499
+ sqlite3_str *pOut /* Generate output into this sqlite3_str object */
206339206500
){
206340206501
while( iStart<iEnd ){
206341206502
u32 i, n, nn, sz = 0;
206342206503
int showContent = 1;
206343206504
u8 x = pParse->aBlob[iStart] & 0x0f;
206344206505
u32 savedNBlob = pParse->nBlob;
206345
- printf("%5d:%*s", iStart, nIndent, "");
206506
+ sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
206346206507
if( pParse->nBlobAlloc>pParse->nBlob ){
206347206508
pParse->nBlob = pParse->nBlobAlloc;
206348206509
}
206349206510
nn = n = jsonbPayloadSize(pParse, iStart, &sz);
206350206511
if( nn==0 ) nn = 1;
206351206512
if( sz>0 && x<JSONB_ARRAY ){
206352206513
nn += sz;
206353206514
}
206354
- for(i=0; i<nn; i++) printf(" %02x", pParse->aBlob[iStart+i]);
206515
+ for(i=0; i<nn; i++){
206516
+ sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
206517
+ }
206355206518
if( n==0 ){
206356
- printf(" ERROR invalid node size\n");
206519
+ sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
206357206520
iStart = n==0 ? iStart+1 : iEnd;
206358206521
continue;
206359206522
}
206360206523
pParse->nBlob = savedNBlob;
206361206524
if( iStart+n+sz>iEnd ){
@@ -206366,59 +206529,61 @@
206366206529
}else{
206367206530
iEnd = pParse->nBlob;
206368206531
}
206369206532
}
206370206533
}
206371
- printf(" <-- ");
206534
+ sqlite3_str_appendall(pOut," <-- ");
206372206535
switch( x ){
206373
- case JSONB_NULL: printf("null"); break;
206374
- case JSONB_TRUE: printf("true"); break;
206375
- case JSONB_FALSE: printf("false"); break;
206376
- case JSONB_INT: printf("int"); break;
206377
- case JSONB_INT5: printf("int5"); break;
206378
- case JSONB_FLOAT: printf("float"); break;
206379
- case JSONB_FLOAT5: printf("float5"); break;
206380
- case JSONB_TEXT: printf("text"); break;
206381
- case JSONB_TEXTJ: printf("textj"); break;
206382
- case JSONB_TEXT5: printf("text5"); break;
206383
- case JSONB_TEXTRAW: printf("textraw"); break;
206536
+ case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break;
206537
+ case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break;
206538
+ case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break;
206539
+ case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break;
206540
+ case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break;
206541
+ case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break;
206542
+ case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break;
206543
+ case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break;
206544
+ case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break;
206545
+ case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break;
206546
+ case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break;
206384206547
case JSONB_ARRAY: {
206385
- printf("array, %u bytes\n", sz);
206386
- jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2);
206548
+ sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
206549
+ jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
206387206550
showContent = 0;
206388206551
break;
206389206552
}
206390206553
case JSONB_OBJECT: {
206391
- printf("object, %u bytes\n", sz);
206392
- jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2);
206554
+ sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
206555
+ jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
206393206556
showContent = 0;
206394206557
break;
206395206558
}
206396206559
default: {
206397
- printf("ERROR: unknown node type\n");
206560
+ sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
206398206561
showContent = 0;
206399206562
break;
206400206563
}
206401206564
}
206402206565
if( showContent ){
206403206566
if( sz==0 && x<=JSONB_FALSE ){
206404
- printf("\n");
206567
+ sqlite3_str_append(pOut, "\n", 1);
206405206568
}else{
206406206569
u32 i;
206407
- printf(": \"");
206570
+ sqlite3_str_appendall(pOut, ": \"");
206408206571
for(i=iStart+n; i<iStart+n+sz; i++){
206409206572
u8 c = pParse->aBlob[i];
206410206573
if( c<0x20 || c>=0x7f ) c = '.';
206411
- putchar(c);
206574
+ sqlite3_str_append(pOut, (char*)&c, 1);
206412206575
}
206413
- printf("\"\n");
206576
+ sqlite3_str_append(pOut, "\"\n", 2);
206414206577
}
206415206578
}
206416206579
iStart += n + sz;
206417206580
}
206418206581
}
206419206582
static void jsonShowParse(JsonParse *pParse){
206583
+ sqlite3_str out;
206584
+ char zBuf[1000];
206420206585
if( pParse==0 ){
206421206586
printf("NULL pointer\n");
206422206587
return;
206423206588
}else{
206424206589
printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
@@ -206425,31 +206590,42 @@
206425206590
printf("nBlob = %u\n", pParse->nBlob);
206426206591
printf("delta = %d\n", pParse->delta);
206427206592
if( pParse->nBlob==0 ) return;
206428206593
printf("content (bytes 0..%u):\n", pParse->nBlob-1);
206429206594
}
206430
- jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0);
206595
+ sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
206596
+ jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
206597
+ printf("%s", sqlite3_str_value(&out));
206598
+ sqlite3_str_reset(&out);
206431206599
}
206432206600
#endif /* SQLITE_DEBUG */
206433206601
206434206602
#ifdef SQLITE_DEBUG
206435206603
/*
206436206604
** SQL function: json_parse(JSON)
206437206605
**
206438
-** Parse JSON using jsonParseFuncArg(). Then print a dump of that
206439
-** parse on standard output.
206606
+** Parse JSON using jsonParseFuncArg(). Return text that is a
206607
+** human-readable dump of the binary JSONB for the input parameter.
206440206608
*/
206441206609
static void jsonParseFunc(
206442206610
sqlite3_context *ctx,
206443206611
int argc,
206444206612
sqlite3_value **argv
206445206613
){
206446206614
JsonParse *p; /* The parse */
206615
+ sqlite3_str out;
206447206616
206448
- assert( argc==1 );
206617
+ assert( argc>=1 );
206618
+ sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
206449206619
p = jsonParseFuncArg(ctx, argv[0], 0);
206450
- jsonShowParse(p);
206620
+ if( p==0 ) return;
206621
+ if( argc==1 ){
206622
+ jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
206623
+ sqlite3_result_text64(ctx, out.zText, out.nChar, sqlite3_free, SQLITE_UTF8);
206624
+ }else{
206625
+ jsonShowParse(p);
206626
+ }
206451206627
jsonParseFree(p);
206452206628
}
206453206629
#endif /* SQLITE_DEBUG */
206454206630
206455206631
/****************************************************************************
@@ -206641,11 +206817,11 @@
206641206817
}
206642206818
if( j<p->nBlob ){
206643206819
if( argc==2 ){
206644206820
if( flags & JSON_JSON ){
206645206821
jsonStringInit(&jx, ctx);
206646
- jsonXlateBlobToText(p, j, &jx);
206822
+ jsonTranslateBlobToText(p, j, &jx);
206647206823
jsonReturnString(&jx, 0, 0);
206648206824
jsonStringReset(&jx);
206649206825
assert( (flags & JSON_BLOB)==0 );
206650206826
sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206651206827
}else{
@@ -206656,11 +206832,11 @@
206656206832
sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206657206833
}
206658206834
}
206659206835
}else{
206660206836
jsonAppendSeparator(&jx);
206661
- jsonXlateBlobToText(p, j, &jx);
206837
+ jsonTranslateBlobToText(p, j, &jx);
206662206838
}
206663206839
}else if( j==JSON_LOOKUP_NOTFOUND ){
206664206840
if( argc==2 ){
206665206841
goto json_extract_error; /* Return NULL if not found */
206666206842
}else{
@@ -229267,23 +229443,28 @@
229267229443
**
229268229444
** This function may be quite inefficient if used with an FTS5 table
229269229445
** created with the "columnsize=0" option.
229270229446
**
229271229447
** xColumnText:
229272
-** This function attempts to retrieve the text of column iCol of the
229273
-** current document. If successful, (*pz) is set to point to a buffer
229448
+** If parameter iCol is less than zero, or greater than or equal to the
229449
+** number of columns in the table, SQLITE_RANGE is returned.
229450
+**
229451
+** Otherwise, this function attempts to retrieve the text of column iCol of
229452
+** the current document. If successful, (*pz) is set to point to a buffer
229274229453
** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
229275229454
** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
229276229455
** if an error occurs, an SQLite error code is returned and the final values
229277229456
** of (*pz) and (*pn) are undefined.
229278229457
**
229279229458
** xPhraseCount:
229280229459
** Returns the number of phrases in the current query expression.
229281229460
**
229282229461
** xPhraseSize:
229283
-** Returns the number of tokens in phrase iPhrase of the query. Phrases
229284
-** are numbered starting from zero.
229462
+** If parameter iCol is less than zero, or greater than or equal to the
229463
+** number of phrases in the current query, as returned by xPhraseCount,
229464
+** 0 is returned. Otherwise, this function returns the number of tokens in
229465
+** phrase iPhrase of the query. Phrases are numbered starting from zero.
229285229466
**
229286229467
** xInstCount:
229287229468
** Set *pnInst to the total number of occurrences of all phrases within
229288229469
** the query within the current row. Return SQLITE_OK if successful, or
229289229470
** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -229295,16 +229476,17 @@
229295229476
**
229296229477
** xInst:
229297229478
** Query for the details of phrase match iIdx within the current row.
229298229479
** Phrase matches are numbered starting from zero, so the iIdx argument
229299229480
** should be greater than or equal to zero and smaller than the value
229300
-** output by xInstCount().
229481
+** output by xInstCount(). If iIdx is less than zero or greater than
229482
+** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
229301229483
**
229302
-** Usually, output parameter *piPhrase is set to the phrase number, *piCol
229484
+** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
229303229485
** to the column in which it occurs and *piOff the token offset of the
229304
-** first token of the phrase. Returns SQLITE_OK if successful, or an error
229305
-** code (i.e. SQLITE_NOMEM) if an error occurs.
229486
+** first token of the phrase. SQLITE_OK is returned if successful, or an
229487
+** error code (i.e. SQLITE_NOMEM) if an error occurs.
229306229488
**
229307229489
** This API can be quite slow if used with an FTS5 table created with the
229308229490
** "detail=none" or "detail=column" option.
229309229491
**
229310229492
** xRowid:
@@ -229325,10 +229507,14 @@
229325229507
** row visited, the callback function passed as the fourth argument
229326229508
** is invoked. The context and API objects passed to the callback
229327229509
** function may be used to access the properties of each matched row.
229328229510
** Invoking Api.xUserData() returns a copy of the pointer passed as
229329229511
** the third argument to pUserData.
229512
+**
229513
+** If parameter iPhrase is less than zero, or greater than or equal to
229514
+** the number of phrases in the query, as returned by xPhraseCount(),
229515
+** this function returns SQLITE_RANGE.
229330229516
**
229331229517
** If the callback function returns any value other than SQLITE_OK, the
229332229518
** query is abandoned and the xQueryPhrase function returns immediately.
229333229519
** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
229334229520
** Otherwise, the error code is propagated upwards.
@@ -229446,22 +229632,30 @@
229446229632
** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
229447229633
** This is used to access token iToken of phrase iPhrase of the current
229448229634
** query. Before returning, output parameter *ppToken is set to point
229449229635
** to a buffer containing the requested token, and *pnToken to the
229450229636
** size of this buffer in bytes.
229637
+**
229638
+** If iPhrase or iToken are less than zero, or if iPhrase is greater than
229639
+** or equal to the number of phrases in the query as reported by
229640
+** xPhraseCount(), or if iToken is equal to or greater than the number of
229641
+** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
229642
+ are both zeroed.
229451229643
**
229452229644
** The output text is not a copy of the query text that specified the
229453229645
** token. It is the output of the tokenizer module. For tokendata=1
229454229646
** tables, this includes any embedded 0x00 and trailing data.
229455229647
**
229456229648
** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
229457229649
** This is used to access token iToken of phrase hit iIdx within the
229458
-** current row. Output variable (*ppToken) is set to point to a buffer
229459
-** containing the matching document token, and (*pnToken) to the size
229460
-** of that buffer in bytes. This API is not available if the specified
229461
-** token matches a prefix query term. In that case both output variables
229462
-** are always set to 0.
229650
+** current row. If iIdx is less than zero or greater than or equal to the
229651
+** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
229652
+** output variable (*ppToken) is set to point to a buffer containing the
229653
+** matching document token, and (*pnToken) to the size of that buffer in
229654
+** bytes. This API is not available if the specified token matches a
229655
+** prefix query term. In that case both output variables are always set
229656
+** to 0.
229463229657
**
229464229658
** The output text is not a copy of the document text that was tokenized.
229465229659
** It is the output of the tokenizer module. For tokendata=1 tables, this
229466229660
** includes any embedded 0x00 and trailing data.
229467229661
**
@@ -232433,12 +232627,14 @@
232433232627
memset(&ctx, 0, sizeof(HighlightContext));
232434232628
ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
232435232629
ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
232436232630
ctx.iRangeEnd = -1;
232437232631
rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
232438
-
232439
- if( ctx.zIn ){
232632
+ if( rc==SQLITE_RANGE ){
232633
+ sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
232634
+ rc = SQLITE_OK;
232635
+ }else if( ctx.zIn ){
232440232636
if( rc==SQLITE_OK ){
232441232637
rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
232442232638
}
232443232639
232444232640
if( rc==SQLITE_OK ){
@@ -236273,15 +236469,19 @@
236273236469
Fts5Expr *pExpr,
236274236470
int iPhrase,
236275236471
Fts5Expr **ppNew
236276236472
){
236277236473
int rc = SQLITE_OK; /* Return code */
236278
- Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
236474
+ Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */
236279236475
Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
236280236476
TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
236281
- pOrig = pExpr->apExprPhrase[iPhrase];
236282
- pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
236477
+ if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
236478
+ rc = SQLITE_RANGE;
236479
+ }else{
236480
+ pOrig = pExpr->apExprPhrase[iPhrase];
236481
+ pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
236482
+ }
236283236483
if( rc==SQLITE_OK ){
236284236484
pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
236285236485
sizeof(Fts5ExprPhrase*));
236286236486
}
236287236487
if( rc==SQLITE_OK ){
@@ -236290,11 +236490,11 @@
236290236490
}
236291236491
if( rc==SQLITE_OK ){
236292236492
pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
236293236493
sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
236294236494
}
236295
- if( rc==SQLITE_OK ){
236495
+ if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){
236296236496
Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
236297236497
if( pColsetOrig ){
236298236498
sqlite3_int64 nByte;
236299236499
Fts5Colset *pColset;
236300236500
nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
@@ -236304,29 +236504,31 @@
236304236504
}
236305236505
pNew->pRoot->pNear->pColset = pColset;
236306236506
}
236307236507
}
236308236508
236309
- if( pOrig->nTerm ){
236310
- int i; /* Used to iterate through phrase terms */
236311
- sCtx.pConfig = pExpr->pConfig;
236312
- for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
236313
- int tflags = 0;
236314
- Fts5ExprTerm *p;
236315
- for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
236316
- rc = fts5ParseTokenize((void*)&sCtx, tflags, p->pTerm,p->nFullTerm,0,0);
236317
- tflags = FTS5_TOKEN_COLOCATED;
236318
- }
236319
- if( rc==SQLITE_OK ){
236320
- sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
236321
- sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
236322
- }
236323
- }
236324
- }else{
236325
- /* This happens when parsing a token or quoted phrase that contains
236326
- ** no token characters at all. (e.g ... MATCH '""'). */
236327
- sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
236509
+ if( rc==SQLITE_OK ){
236510
+ if( pOrig->nTerm ){
236511
+ int i; /* Used to iterate through phrase terms */
236512
+ sCtx.pConfig = pExpr->pConfig;
236513
+ for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
236514
+ int tflags = 0;
236515
+ Fts5ExprTerm *p;
236516
+ for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
236517
+ rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0);
236518
+ tflags = FTS5_TOKEN_COLOCATED;
236519
+ }
236520
+ if( rc==SQLITE_OK ){
236521
+ sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
236522
+ sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
236523
+ }
236524
+ }
236525
+ }else{
236526
+ /* This happens when parsing a token or quoted phrase that contains
236527
+ ** no token characters at all. (e.g ... MATCH '""'). */
236528
+ sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
236529
+ }
236328236530
}
236329236531
236330236532
if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
236331236533
/* All the allocations succeeded. Put the expression object together. */
236332236534
pNew->pIndex = pExpr->pIndex;
@@ -239776,13 +239978,13 @@
239776239978
for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
239777239979
if( pData->p[iOff] ) break;
239778239980
}
239779239981
239780239982
if( iOff<pData->nn ){
239781
- i64 iVal;
239983
+ u64 iVal;
239782239984
pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
239783
- iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
239985
+ iOff += fts5GetVarint(&pData->p[iOff], &iVal);
239784239986
pLvl->iRowid += iVal;
239785239987
pLvl->iOff = iOff;
239786239988
}else{
239787239989
pLvl->bEof = 1;
239788239990
}
@@ -246173,20 +246375,20 @@
246173246375
fts5DataRelease(pLeaf);
246174246376
}
246175246377
}
246176246378
246177246379
static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
246178
- int iTermOff = 0;
246380
+ i64 iTermOff = 0;
246179246381
int ii;
246180246382
246181246383
Fts5Buffer buf1 = {0,0,0};
246182246384
Fts5Buffer buf2 = {0,0,0};
246183246385
246184246386
ii = pLeaf->szLeaf;
246185246387
while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
246186246388
int res;
246187
- int iOff;
246389
+ i64 iOff;
246188246390
int nIncr;
246189246391
246190246392
ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
246191246393
iTermOff += nIncr;
246192246394
iOff = iTermOff;
@@ -249207,11 +249409,14 @@
249207249409
const char **pz,
249208249410
int *pn
249209249411
){
249210249412
int rc = SQLITE_OK;
249211249413
Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
249212
- if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
249414
+ Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
249415
+ if( iCol<0 || iCol>=pTab->pConfig->nCol ){
249416
+ rc = SQLITE_RANGE;
249417
+ }else if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
249213249418
|| pCsr->ePlan==FTS5_PLAN_SPECIAL
249214249419
){
249215249420
*pz = 0;
249216249421
*pn = 0;
249217249422
}else{
@@ -249232,12 +249437,13 @@
249232249437
){
249233249438
Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
249234249439
int rc = SQLITE_OK;
249235249440
int bLive = (pCsr->pSorter==0);
249236249441
249237
- if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
249238
-
249442
+ if( iPhrase<0 || iPhrase>=sqlite3Fts5ExprPhraseCount(pCsr->pExpr) ){
249443
+ rc = SQLITE_RANGE;
249444
+ }else if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
249239249445
if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
249240249446
Fts5PoslistPopulator *aPopulator;
249241249447
int i;
249242249448
aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
249243249449
if( aPopulator==0 ) rc = SQLITE_NOMEM;
@@ -249257,18 +249463,24 @@
249257249463
}
249258249464
}
249259249465
CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
249260249466
}
249261249467
249262
- if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
249263
- Fts5Sorter *pSorter = pCsr->pSorter;
249264
- int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
249265
- *pn = pSorter->aIdx[iPhrase] - i1;
249266
- *pa = &pSorter->aPoslist[i1];
249468
+ if( rc==SQLITE_OK ){
249469
+ if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
249470
+ Fts5Sorter *pSorter = pCsr->pSorter;
249471
+ int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
249472
+ *pn = pSorter->aIdx[iPhrase] - i1;
249473
+ *pa = &pSorter->aPoslist[i1];
249474
+ }else{
249475
+ *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
249476
+ }
249267249477
}else{
249268
- *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
249478
+ *pa = 0;
249479
+ *pn = 0;
249269249480
}
249481
+
249270249482
249271249483
return rc;
249272249484
}
249273249485
249274249486
/*
@@ -250223,11 +250435,11 @@
250223250435
int nArg, /* Number of args */
250224250436
sqlite3_value **apUnused /* Function arguments */
250225250437
){
250226250438
assert( nArg==0 );
250227250439
UNUSED_PARAM2(nArg, apUnused);
250228
- sqlite3_result_text(pCtx, "fts5: 2023-12-14 16:34:47 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9", -1, SQLITE_TRANSIENT);
250440
+ sqlite3_result_text(pCtx, "fts5: 2023-12-29 19:03:01 4b70b94616ef37bac969051eee3ea6913a28f30520cdd4fc3a19e848f2cf12b7", -1, SQLITE_TRANSIENT);
250229250441
}
250230250442
250231250443
/*
250232250444
** Return true if zName is the extension on one of the shadow tables used
250233250445
** by this module.
250234250446
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** 27d4a89a5ff96b7b7fc5dc9650e1269f7c7e.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.45.0"
463 #define SQLITE_VERSION_NUMBER 3045000
464 #define SQLITE_SOURCE_ID "2023-12-14 16:34:47 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -8350,13 +8350,15 @@
8350 ** can enter.)^ If the same thread tries to enter any mutex other
8351 ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8352 **
8353 ** ^(Some systems (for example, Windows 95) do not support the operation
8354 ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8355 ** will always return SQLITE_BUSY. The SQLite core only ever uses
8356 ** sqlite3_mutex_try() as an optimization so this is acceptable
8357 ** behavior.)^
 
 
8358 **
8359 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
8360 ** previously entered by the same thread. The behavior
8361 ** is undefined if the mutex is not currently entered by the
8362 ** calling thread or is not currently allocated.
@@ -13125,23 +13127,28 @@
13125 **
13126 ** This function may be quite inefficient if used with an FTS5 table
13127 ** created with the "columnsize=0" option.
13128 **
13129 ** xColumnText:
13130 ** This function attempts to retrieve the text of column iCol of the
13131 ** current document. If successful, (*pz) is set to point to a buffer
 
 
 
13132 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13133 ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
13134 ** if an error occurs, an SQLite error code is returned and the final values
13135 ** of (*pz) and (*pn) are undefined.
13136 **
13137 ** xPhraseCount:
13138 ** Returns the number of phrases in the current query expression.
13139 **
13140 ** xPhraseSize:
13141 ** Returns the number of tokens in phrase iPhrase of the query. Phrases
13142 ** are numbered starting from zero.
 
 
13143 **
13144 ** xInstCount:
13145 ** Set *pnInst to the total number of occurrences of all phrases within
13146 ** the query within the current row. Return SQLITE_OK if successful, or
13147 ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -13153,16 +13160,17 @@
13153 **
13154 ** xInst:
13155 ** Query for the details of phrase match iIdx within the current row.
13156 ** Phrase matches are numbered starting from zero, so the iIdx argument
13157 ** should be greater than or equal to zero and smaller than the value
13158 ** output by xInstCount().
 
13159 **
13160 ** Usually, output parameter *piPhrase is set to the phrase number, *piCol
13161 ** to the column in which it occurs and *piOff the token offset of the
13162 ** first token of the phrase. Returns SQLITE_OK if successful, or an error
13163 ** code (i.e. SQLITE_NOMEM) if an error occurs.
13164 **
13165 ** This API can be quite slow if used with an FTS5 table created with the
13166 ** "detail=none" or "detail=column" option.
13167 **
13168 ** xRowid:
@@ -13183,10 +13191,14 @@
13183 ** row visited, the callback function passed as the fourth argument
13184 ** is invoked. The context and API objects passed to the callback
13185 ** function may be used to access the properties of each matched row.
13186 ** Invoking Api.xUserData() returns a copy of the pointer passed as
13187 ** the third argument to pUserData.
 
 
 
 
13188 **
13189 ** If the callback function returns any value other than SQLITE_OK, the
13190 ** query is abandoned and the xQueryPhrase function returns immediately.
13191 ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
13192 ** Otherwise, the error code is propagated upwards.
@@ -13304,22 +13316,30 @@
13304 ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13305 ** This is used to access token iToken of phrase iPhrase of the current
13306 ** query. Before returning, output parameter *ppToken is set to point
13307 ** to a buffer containing the requested token, and *pnToken to the
13308 ** size of this buffer in bytes.
 
 
 
 
 
 
13309 **
13310 ** The output text is not a copy of the query text that specified the
13311 ** token. It is the output of the tokenizer module. For tokendata=1
13312 ** tables, this includes any embedded 0x00 and trailing data.
13313 **
13314 ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13315 ** This is used to access token iToken of phrase hit iIdx within the
13316 ** current row. Output variable (*ppToken) is set to point to a buffer
13317 ** containing the matching document token, and (*pnToken) to the size
13318 ** of that buffer in bytes. This API is not available if the specified
13319 ** token matches a prefix query term. In that case both output variables
13320 ** are always set to 0.
 
 
13321 **
13322 ** The output text is not a copy of the document text that was tokenized.
13323 ** It is the output of the tokenizer module. For tokendata=1 tables, this
13324 ** includes any embedded 0x00 and trailing data.
13325 **
@@ -13991,10 +14011,23 @@
13991 #if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
13992 # define SQLITE_USE_SEH 1
13993 #else
13994 # undef SQLITE_USE_SEH
13995 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
13996
13997 /*
13998 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13999 ** 0 means mutexes are permanently disable and the library is never
14000 ** threadsafe. 1 means the library is serialized which is the highest
@@ -15874,11 +15907,11 @@
15874 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
15875 SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15876 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15877 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15878 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15879 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15880 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15881 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15882
15883 /* Functions used to truncate the database file. */
15884 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
@@ -18629,10 +18662,11 @@
18629 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
18630 unsigned isResized:1; /* True if resizeIndexObject() has been called */
18631 unsigned isCovering:1; /* True if this is a covering index */
18632 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18633 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
 
18634 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
18635 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
18636 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
18637 unsigned bHasExpr:1; /* Index contains an expression, either a literal
18638 ** expression, or a reference to a VIRTUAL column */
@@ -24028,11 +24062,11 @@
24028 /* no break */ deliberate_fall_through
24029 case SQLITE_DBSTATUS_CACHE_HIT:
24030 case SQLITE_DBSTATUS_CACHE_MISS:
24031 case SQLITE_DBSTATUS_CACHE_WRITE:{
24032 int i;
24033 int nRet = 0;
24034 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
24035 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
24036
24037 for(i=0; i<db->nDb; i++){
24038 if( db->aDb[i].pBt ){
@@ -24041,11 +24075,11 @@
24041 }
24042 }
24043 *pHighwater = 0; /* IMP: R-42420-56072 */
24044 /* IMP: R-54100-20147 */
24045 /* IMP: R-29431-39229 */
24046 *pCurrent = nRet;
24047 break;
24048 }
24049
24050 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
24051 ** key constraints. Set *pCurrent to zero if all foreign key constraints
@@ -34677,11 +34711,11 @@
34677 ** Load the sqlite3.iSysErrno field if that is an appropriate thing
34678 ** to do based on the SQLite error code in rc.
34679 */
34680 SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
34681 if( rc==SQLITE_IOERR_NOMEM ) return;
34682 #ifdef SQLITE_USE_SEH
34683 if( rc==SQLITE_IOERR_IN_PAGE ){
34684 int ii;
34685 int iErr;
34686 sqlite3BtreeEnterAll(db);
34687 for(ii=0; ii<db->nDb; ii++){
@@ -42365,13 +42399,19 @@
42365 struct flock f; /* The posix advisory locking structure */
42366 int rc = SQLITE_OK; /* Result code form fcntl() */
42367
42368 pShmNode = pFile->pInode->pShmNode;
42369
42370 /* Assert that the correct mutex or mutexes are held. */
42371 if( pShmNode->nRef==0 ){
42372 assert( ofst==UNIX_SHM_DMS && n==1 && unixMutexHeld() );
 
 
 
 
 
 
42373 }else{
42374 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42375 int ii;
42376 for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){
42377 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) );
@@ -57333,11 +57373,11 @@
57333 i64 journalSizeLimit; /* Size limit for persistent journal files */
57334 char *zFilename; /* Name of the database file */
57335 char *zJournal; /* Name of the journal file */
57336 int (*xBusyHandler)(void*); /* Function to call when busy */
57337 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
57338 int aStat[4]; /* Total cache hits, misses, writes, spills */
57339 #ifdef SQLITE_TEST
57340 int nRead; /* Database pages read */
57341 #endif
57342 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
57343 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
@@ -63477,15 +63517,15 @@
63477 a[1] = sqlite3PcachePagecount(pPager->pPCache);
63478 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
63479 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63480 a[4] = pPager->eState;
63481 a[5] = pPager->errCode;
63482 a[6] = pPager->aStat[PAGER_STAT_HIT];
63483 a[7] = pPager->aStat[PAGER_STAT_MISS];
63484 a[8] = 0; /* Used to be pPager->nOvfl */
63485 a[9] = pPager->nRead;
63486 a[10] = pPager->aStat[PAGER_STAT_WRITE];
63487 return a;
63488 }
63489 #endif
63490
63491 /*
@@ -63497,11 +63537,11 @@
63497 ** Before returning, *pnVal is incremented by the
63498 ** current cache hit or miss count, according to the value of eStat. If the
63499 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
63500 ** returning.
63501 */
63502 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
63503
63504 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
63505 || eStat==SQLITE_DBSTATUS_CACHE_MISS
63506 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
63507 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
@@ -64437,11 +64477,11 @@
64437 assert( pPager->eState>=PAGER_READER );
64438 return sqlite3WalFramesize(pPager->pWal);
64439 }
64440 #endif
64441
64442 #ifdef SQLITE_USE_SEH
64443 SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){
64444 return sqlite3WalSystemErrno(pPager->pWal);
64445 }
64446 #endif
64447
@@ -106789,10 +106829,11 @@
106789 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
106790 }
106791 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
106792 pParse->checkSchema = 1;
106793 pTopNC->nNcErr++;
 
106794 }
106795 assert( pFJMatch==0 );
106796
106797 /* Remove all substructure from pExpr */
106798 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
@@ -110976,13 +111017,14 @@
110976 case TK_BLOB:
110977 return 0;
110978 case TK_COLUMN:
110979 assert( ExprUseYTab(p) );
110980 return ExprHasProperty(p, EP_CanBeNull) ||
110981 p->y.pTab==0 || /* Reference to column of index on expression */
110982 (p->iColumn>=0
110983 && p->y.pTab->aCol!=0 /* Possible due to prior error */
 
110984 && p->y.pTab->aCol[p->iColumn].notNull==0);
110985 default:
110986 return 1;
110987 }
110988 }
@@ -113560,12 +113602,14 @@
113560 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
113561 if( pParse->pVdbe==0 ) return;
113562 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
113563 if( inReg!=target ){
113564 u8 op;
113565 if( ALWAYS(pExpr)
113566 && (ExprHasProperty(pExpr,EP_Subquery) || pExpr->op==TK_REGISTER)
 
 
113567 ){
113568 op = OP_Copy;
113569 }else{
113570 op = OP_SCopy;
113571 }
@@ -117825,13 +117869,13 @@
117825 ** information.
117826 */
117827 typedef struct StatAccum StatAccum;
117828 typedef struct StatSample StatSample;
117829 struct StatSample {
117830 tRowcnt *anEq; /* sqlite_stat4.nEq */
117831 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
117832 #ifdef SQLITE_ENABLE_STAT4
 
117833 tRowcnt *anLt; /* sqlite_stat4.nLt */
117834 union {
117835 i64 iRowid; /* Rowid in main table of the key */
117836 u8 *aRowid; /* Key for WITHOUT ROWID tables */
117837 } u;
@@ -117985,13 +118029,13 @@
117985 assert( nKeyCol<=nCol );
117986 assert( nKeyCol>0 );
117987
117988 /* Allocate the space required for the StatAccum object */
117989 n = sizeof(*p)
117990 + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
117991 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
117992 #ifdef SQLITE_ENABLE_STAT4
 
117993 if( mxSample ){
117994 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
117995 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
117996 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
117997 }
@@ -118008,13 +118052,13 @@
118008 p->nLimit = sqlite3_value_int64(argv[3]);
118009 p->nCol = nCol;
118010 p->nKeyCol = nKeyCol;
118011 p->nSkipAhead = 0;
118012 p->current.anDLt = (tRowcnt*)&p[1];
118013 p->current.anEq = &p->current.anDLt[nColUp];
118014
118015 #ifdef SQLITE_ENABLE_STAT4
 
118016 p->mxSample = p->nLimit==0 ? mxSample : 0;
118017 if( mxSample ){
118018 u8 *pSpace; /* Allocated space not yet assigned */
118019 int i; /* Used to iterate through p->aSample[] */
118020
@@ -118277,28 +118321,32 @@
118277 assert( p->nCol>0 );
118278 assert( iChng<p->nCol );
118279
118280 if( p->nRow==0 ){
118281 /* This is the first call to this function. Do initialization. */
 
118282 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
 
118283 }else{
118284 /* Second and subsequent calls get processed here */
118285 #ifdef SQLITE_ENABLE_STAT4
118286 if( p->mxSample ) samplePushPrevious(p, iChng);
118287 #endif
118288
118289 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
118290 ** to the current row of the index. */
 
118291 for(i=0; i<iChng; i++){
118292 p->current.anEq[i]++;
118293 }
 
118294 for(i=iChng; i<p->nCol; i++){
118295 p->current.anDLt[i]++;
118296 #ifdef SQLITE_ENABLE_STAT4
118297 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
118298 #endif
118299 p->current.anEq[i] = 1;
 
118300 }
118301 }
118302
118303 p->nRow++;
118304 #ifdef SQLITE_ENABLE_STAT4
@@ -118428,11 +118476,13 @@
118428 for(i=0; i<p->nKeyCol; i++){
118429 u64 nDistinct = p->current.anDLt[i] + 1;
118430 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
118431 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
118432 sqlite3_str_appendf(&sStat, " %llu", iVal);
 
118433 assert( p->current.anEq[i] );
 
118434 }
118435 sqlite3ResultStrAccum(context, &sStat);
118436 }
118437 #ifdef SQLITE_ENABLE_STAT4
118438 else if( eCall==STAT_GET_ROWID ){
@@ -119117,10 +119167,20 @@
119117 }
119118 #endif
119119 while( z[0]!=0 && z[0]!=' ' ) z++;
119120 while( z[0]==' ' ) z++;
119121 }
 
 
 
 
 
 
 
 
 
 
119122 }
119123 }
119124
119125 /*
119126 ** This callback is invoked once for each index when reading the
@@ -148743,11 +148803,11 @@
148743 pSub->pPrior = 0;
148744 pSub->pNext = 0;
148745 pSub->selFlags |= SF_Aggregate;
148746 pSub->selFlags &= ~SF_Compound;
148747 pSub->nSelectRow = 0;
148748 sqlite3ExprListDelete(db, pSub->pEList);
148749 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
148750 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
148751 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
148752 sqlite3PExprAddSelect(pParse, pTerm, pSub);
148753 if( pExpr==0 ){
@@ -154295,11 +154355,10 @@
154295 assert( sqlite3BtreeHoldsAllMutexes(db) );
154296 assert( sqlite3_mutex_held(db->mutex) );
154297
154298 if( p ){
154299 db->pDisconnect = 0;
154300 sqlite3ExpirePreparedStatements(db, 0);
154301 do {
154302 VTable *pNext = p->pNext;
154303 sqlite3VtabUnlock(p);
154304 p = pNext;
154305 }while( p );
@@ -155861,11 +155920,11 @@
155861 */
155862 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
155863 #ifdef WHERETRACE_ENABLED
155864 SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
155865 SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
155866 SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
155867 #endif
155868 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
155869 WhereClause *pWC, /* The WHERE clause to be searched */
155870 int iCur, /* Cursor number of LHS */
155871 int iColumn, /* Column number of LHS */
@@ -162890,21 +162949,38 @@
162890 #endif
162891
162892 #ifdef WHERETRACE_ENABLED
162893 /*
162894 ** Print a WhereLoop object for debugging purposes
 
 
 
 
 
 
 
 
 
 
 
 
162895 */
162896 SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
162897 WhereInfo *pWInfo = pWC->pWInfo;
162898 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
162899 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
162900 Table *pTab = pItem->pTab;
162901 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
162902 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
162903 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
162904 sqlite3DebugPrintf(" %12s",
162905 pItem->zAlias ? pItem->zAlias : pTab->zName);
 
 
 
 
 
162906 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
162907 const char *zName;
162908 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
162909 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
162910 int i = sqlite3Strlen30(zName) - 1;
@@ -162936,10 +163012,19 @@
162936 int i;
162937 for(i=0; i<p->nLTerm; i++){
162938 sqlite3WhereTermPrint(p->aLTerm[i], i);
162939 }
162940 }
 
 
 
 
 
 
 
 
 
162941 }
162942 #endif
162943
162944 /*
162945 ** Convert bulk memory into a valid WhereLoop that can be passed
@@ -163049,50 +163134,64 @@
163049 }
163050 sqlite3DbNNFreeNN(db, pWInfo);
163051 }
163052
163053 /*
163054 ** Return TRUE if all of the following are true:
163055 **
163056 ** (1) X has the same or lower cost, or returns the same or fewer rows,
163057 ** than Y.
163058 ** (2) X uses fewer WHERE clause terms than Y
163059 ** (3) Every WHERE clause term used by X is also used by Y
163060 ** (4) X skips at least as many columns as Y
163061 ** (5) If X is a covering index, than Y is too
163062 **
163063 ** Conditions (2) and (3) mean that X is a "proper subset" of Y.
163064 ** If X is a proper subset of Y then Y is a better choice and ought
163065 ** to have a lower cost. This routine returns TRUE when that cost
163066 ** relationship is inverted and needs to be adjusted. Constraint (4)
163067 ** was added because if X uses skip-scan less than Y it still might
163068 ** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
163069 ** was added because a covering index probably deserves to have a lower cost
163070 ** than a non-covering index even if it is a proper subset.
 
 
 
 
 
 
163071 */
163072 static int whereLoopCheaperProperSubset(
163073 const WhereLoop *pX, /* First WhereLoop to compare */
163074 const WhereLoop *pY /* Compare against this WhereLoop */
163075 ){
163076 int i, j;
 
 
 
 
 
 
 
 
 
163077 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
163078 return 0; /* X is not a subset of Y */
163079 }
163080 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
163081 if( pY->nSkip > pX->nSkip ) return 0;
163082 for(i=pX->nLTerm-1; i>=0; i--){
163083 if( pX->aLTerm[i]==0 ) continue;
163084 for(j=pY->nLTerm-1; j>=0; j--){
163085 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
163086 }
163087 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
163088 }
163089 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
163090 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
163091 return 0; /* Constraint (5) */
163092 }
163093 return 1; /* All conditions meet */
163094 }
163095
163096 /*
163097 ** Try to adjust the cost and number of output rows of WhereLoop pTemplate
163098 ** upwards or downwards so that:
@@ -163578,11 +163677,14 @@
163578 opMask = WO_LT|WO_LE;
163579 }else{
163580 assert( pNew->u.btree.nBtm==0 );
163581 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
163582 }
163583 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
 
 
 
163584
163585 assert( pNew->u.btree.nEq<pProbe->nColumn );
163586 assert( pNew->u.btree.nEq<pProbe->nKeyCol
163587 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
163588
@@ -166683,11 +166785,14 @@
166683 ** struct, the contents of WhereInfo.a[], the WhereClause structure
166684 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
166685 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
166686 ** some architectures. Hence the ROUND8() below.
166687 */
166688 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
 
 
 
166689 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
166690 if( db->mallocFailed ){
166691 sqlite3DbFree(db, pWInfo);
166692 pWInfo = 0;
166693 goto whereBeginError;
@@ -167245,10 +167350,15 @@
167245 whereBeginError:
167246 if( pWInfo ){
167247 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
167248 whereInfoFree(db, pWInfo);
167249 }
 
 
 
 
 
167250 return 0;
167251 }
167252
167253 /*
167254 ** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
@@ -203027,13 +203137,13 @@
203027 ** The original design stored all JSON as pure text, canonical RFC-8259.
203028 ** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16).
203029 ** All generated JSON text still conforms strictly to RFC-8259, but text
203030 ** with JSON-5 extensions is accepted as input.
203031 **
203032 ** Beginning with version 3.45.0 (pending), these routines also accept
203033 ** BLOB values that have JSON encoded using a binary representation we
203034 ** call JSONB. The name JSONB comes from PostgreSQL, however the on-disk
203035 ** format SQLite JSONB is completely different and incompatible with
203036 ** PostgreSQL JSONB.
203037 **
203038 ** Decoding and interpreting JSONB is still O(N) where N is the size of
203039 ** the input, the same as text JSON. However, the constant of proportionality
@@ -203120,10 +203230,13 @@
203120 ** code is between 0 and 12 and that the total size of the element
203121 ** (header plus payload) is the same as the size of the BLOB. If those
203122 ** checks are true, the BLOB is assumed to be JSONB and processing continues.
203123 ** Errors are only raised if some other miscoding is discovered during
203124 ** processing.
 
 
 
203125 */
203126 #ifndef SQLITE_OMIT_JSON
203127 /* #include "sqliteInt.h" */
203128
203129 /* JSONB element types
@@ -203218,18 +203331,26 @@
203218 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
203219 */
203220 #define JSON_CACHE_ID (-429938) /* Cache entry */
203221 #define JSON_CACHE_SIZE 4 /* Max number of cache entries */
203222
 
 
 
 
 
 
203223 /* A cache mapping JSON text into JSONB blobs.
203224 **
203225 ** Each cache entry is a JsonParse object with the following restrictions:
203226 **
203227 ** * The bReadOnly flag must be set
203228 **
203229 ** * The aBlob[] array must be owned by the JsonParse object. In other
203230 ** words, nBlobAlloc must be non-zero.
 
 
203231 **
203232 ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
203233 */
203234 struct JsonCache {
203235 sqlite3 *db; /* Database connection */
@@ -203286,27 +203407,27 @@
203286 **
203287 ** 3. Zero or more changes are made to aBlob[] (via json_remove() or
203288 ** json_replace() or json_patch() or similar).
203289 **
203290 ** 4. New JSON text is generated from the aBlob[] for output. This step
203291 ** is skipped the function is one of the jsonb_* functions that returns
203292 ** JSONB instead of text JSON.
203293 */
203294 struct JsonParse {
203295 u8 *aBlob; /* JSONB representation of JSON value */
203296 u32 nBlob; /* Bytes of aBlob[] actually used */
203297 u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
203298 char *zJson; /* Json text used for parsing */
203299 int nJson; /* Length of the zJson string in bytes */
 
 
203300 u16 iDepth; /* Nesting depth */
203301 u8 nErr; /* Number of errors seen */
203302 u8 oom; /* Set to true if out of memory */
203303 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
203304 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
203305 u8 bReadOnly; /* Do not modify. */
203306 u32 nJPRef; /* Number of references to this object */
203307 u32 iErr; /* Error location in zJson[] */
203308 /* Search and edit information. See jsonLookupStep() */
203309 u8 eEdit; /* Edit operation to apply */
203310 int delta; /* Size change due to the edit */
203311 u32 nIns; /* Number of bytes to insert */
203312 u32 iLabel; /* Location of label if search landed on an object value */
@@ -203341,11 +203462,11 @@
203341 /**************************************************************************
203342 ** Forward references
203343 **************************************************************************/
203344 static void jsonReturnStringAsBlob(JsonString*);
203345 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
203346 static u32 jsonXlateBlobToText(const JsonParse*,u32,JsonString*);
203347 static void jsonReturnParse(sqlite3_context*,JsonParse*);
203348 static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
203349 static void jsonParseFree(JsonParse*);
203350 static u32 jsonbPayloadSize(const JsonParse*, u32, u32*);
203351 static u32 jsonUnescapeOneChar(const char*, u32, u32*);
@@ -203381,10 +203502,11 @@
203381 ){
203382 JsonCache *p;
203383
203384 assert( pParse->zJson!=0 );
203385 assert( pParse->bJsonIsRCStr );
 
203386 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID);
203387 if( p==0 ){
203388 sqlite3 *db = sqlite3_context_db_handle(ctx);
203389 p = sqlite3DbMallocZero(db, sizeof(*p));
203390 if( p==0 ) return SQLITE_NOMEM;
@@ -203453,10 +203575,11 @@
203453 JsonParse *tmp = p->a[i];
203454 memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp));
203455 p->a[p->nUsed-1] = tmp;
203456 i = p->nUsed - 1;
203457 }
 
203458 return p->a[i];
203459 }else{
203460 return 0;
203461 }
203462 }
@@ -203621,12 +203744,37 @@
203621 if( z==0 ) return;
203622 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
203623 p->zBuf[p->nUsed++] = '"';
203624 while( 1 /*exit-by-break*/ ){
203625 k = 0;
203626 while( k+1<N && jsonIsOk[z[k]] && jsonIsOk[z[k+1]] ){ k += 2; } /* <--, */
203627 while( k<N && jsonIsOk[z[k]] ){ k++; } /* <-- loop unwound for speed */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203628 if( k>=N ){
203629 if( k>0 ){
203630 memcpy(&p->zBuf[p->nUsed], z, k);
203631 p->nUsed += k;
203632 }
@@ -203714,11 +203862,11 @@
203714 if( jsonFuncArgMightBeBinary(pValue) ){
203715 JsonParse px;
203716 memset(&px, 0, sizeof(px));
203717 px.aBlob = (u8*)sqlite3_value_blob(pValue);
203718 px.nBlob = sqlite3_value_bytes(pValue);
203719 jsonXlateBlobToText(&px, 0, p);
203720 }else if( p->eErr==0 ){
203721 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
203722 p->eErr = JSTRING_ERR;
203723 jsonStringReset(p);
203724 }
@@ -204231,18 +204379,14 @@
204231 ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
204232 ** any changes to *pOp and return false.
204233 */
204234 static int jsonIs4HexB(const char *z, int *pOp){
204235 if( z[0]!='u' ) return 0;
204236 if( !sqlite3Isxdigit(z[1]) ) return 0;
204237 if( !sqlite3Isxdigit(z[2]) ) return 0;
204238 if( !sqlite3Isxdigit(z[3]) ) return 0;
204239 if( !sqlite3Isxdigit(z[4]) ) return 0;
204240 *pOp = JSONB_TEXTJ;
204241 return 1;
204242 }
204243
204244
204245 /*
204246 ** Check a single element of the JSONB in pParse for validity.
204247 **
204248 ** The element to be checked starts at offset i and must end at on the
@@ -204384,11 +204528,11 @@
204384 }else if( x!=JSONB_TEXT5 ){
204385 return j+1;
204386 }else{
204387 u32 c = 0;
204388 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
204389 if( c==0xfffd ) return j+1;
204390 j += szC - 1;
204391 }
204392 }
204393 j++;
204394 }
@@ -204456,11 +204600,11 @@
204456 ** -2 '}' seen \
204457 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
204458 ** -4 ',' seen / the index in zJson[] of the seen character
204459 ** -5 ':' seen /
204460 */
204461 static int jsonXlateTextToBlob(JsonParse *pParse, u32 i){
204462 char c;
204463 u32 j;
204464 u32 iThis, iStart;
204465 int x;
204466 u8 t;
@@ -204476,11 +204620,11 @@
204476 return -1;
204477 }
204478 iStart = pParse->nBlob;
204479 for(j=i+1;;j++){
204480 u32 iBlob = pParse->nBlob;
204481 x = jsonXlateTextToBlob(pParse, j);
204482 if( x<=0 ){
204483 int op;
204484 if( x==(-2) ){
204485 j = pParse->iErr;
204486 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
@@ -204522,19 +204666,19 @@
204522 if( z[j]==':' ){
204523 j++;
204524 goto parse_object_value;
204525 }
204526 }
204527 x = jsonXlateTextToBlob(pParse, j);
204528 if( x!=(-5) ){
204529 if( x!=(-1) ) pParse->iErr = j;
204530 return -1;
204531 }
204532 j = pParse->iErr+1;
204533 }
204534 parse_object_value:
204535 x = jsonXlateTextToBlob(pParse, j);
204536 if( x<=0 ){
204537 if( x!=(-1) ) pParse->iErr = j;
204538 return -1;
204539 }
204540 j = x;
@@ -204549,11 +204693,11 @@
204549 continue;
204550 }else if( z[j]=='}' ){
204551 break;
204552 }
204553 }
204554 x = jsonXlateTextToBlob(pParse, j);
204555 if( x==(-4) ){
204556 j = pParse->iErr;
204557 continue;
204558 }
204559 if( x==(-2) ){
@@ -204577,11 +204721,11 @@
204577 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
204578 pParse->iErr = i;
204579 return -1;
204580 }
204581 for(j=i+1;;j++){
204582 x = jsonXlateTextToBlob(pParse, j);
204583 if( x<=0 ){
204584 if( x==(-3) ){
204585 j = pParse->iErr;
204586 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
204587 break;
@@ -204601,11 +204745,11 @@
204601 continue;
204602 }else if( z[j]==']' ){
204603 break;
204604 }
204605 }
204606 x = jsonXlateTextToBlob(pParse, j);
204607 if( x==(-4) ){
204608 j = pParse->iErr;
204609 continue;
204610 }
204611 if( x==(-3) ){
@@ -204924,11 +205068,11 @@
204924 JsonParse *pParse, /* Initialize and fill this JsonParse object */
204925 sqlite3_context *pCtx /* Report errors here */
204926 ){
204927 int i;
204928 const char *zJson = pParse->zJson;
204929 i = jsonXlateTextToBlob(pParse, 0);
204930 if( pParse->oom ) i = -1;
204931 if( i>0 ){
204932 #ifdef SQLITE_DEBUG
204933 assert( pParse->iDepth==0 );
204934 if( sqlite3Config.bJsonSelfcheck ){
@@ -204969,11 +205113,11 @@
204969 JsonParse px;
204970 memset(&px, 0, sizeof(px));
204971 jsonStringTerminate(pStr);
204972 px.zJson = pStr->zBuf;
204973 px.nJson = pStr->nUsed;
204974 (void)jsonXlateTextToBlob(&px, 0);
204975 if( px.oom ){
204976 sqlite3_free(px.aBlob);
204977 sqlite3_result_error_nomem(pStr->pCtx);
204978 }else{
204979 assert( px.nBlobAlloc>0 );
@@ -205057,11 +205201,11 @@
205057 ** are detected. So a malformed JSONB input might either result
205058 ** in an error, or in incorrect JSON.
205059 **
205060 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
205061 */
205062 static u32 jsonXlateBlobToText(
205063 const JsonParse *pParse, /* the complete parse of the JSON */
205064 u32 i, /* Start rendering at this index */
205065 JsonString *pOut /* Write JSON here */
205066 ){
205067 u32 sz, n, j, iEnd;
@@ -205228,11 +205372,11 @@
205228 case JSONB_ARRAY: {
205229 jsonAppendChar(pOut, '[');
205230 j = i+n;
205231 iEnd = j+sz;
205232 while( j<iEnd ){
205233 j = jsonXlateBlobToText(pParse, j, pOut);
205234 jsonAppendChar(pOut, ',');
205235 }
205236 if( sz>0 ) pOut->nUsed--;
205237 jsonAppendChar(pOut, ']');
205238 break;
@@ -205241,11 +205385,11 @@
205241 int x = 0;
205242 jsonAppendChar(pOut, '{');
205243 j = i+n;
205244 iEnd = j+sz;
205245 while( j<iEnd ){
205246 j = jsonXlateBlobToText(pParse, j, pOut);
205247 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
205248 }
205249 if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED;
205250 if( sz>0 ) pOut->nUsed--;
205251 jsonAppendChar(pOut, '}');
@@ -205394,23 +205538,27 @@
205394
205395 /*
205396 ** Input z[0..n] defines JSON escape sequence including the leading '\\'.
205397 ** Decode that escape sequence into a single character. Write that
205398 ** character into *piOut. Return the number of bytes in the escape sequence.
 
 
 
 
205399 */
205400 static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
205401 assert( n>0 );
205402 assert( z[0]=='\\' );
205403 if( n<2 ){
205404 *piOut = 0xFFFD;
205405 return n;
205406 }
205407 switch( (u8)z[1] ){
205408 case 'u': {
205409 u32 v, vlo;
205410 if( n<6 ){
205411 *piOut = 0xFFFD;
205412 return n;
205413 }
205414 v = jsonHexToInt4(&z[2]);
205415 if( (v & 0xfc00)==0xd800
205416 && n>=12
@@ -205436,11 +205584,11 @@
205436 case '"':
205437 case '/':
205438 case '\\':{ *piOut = z[1]; return 2; }
205439 case 'x': {
205440 if( n<4 ){
205441 *piOut = 0xFFFD;
205442 return n;
205443 }
205444 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
205445 return 4;
205446 }
@@ -205447,11 +205595,11 @@
205447 case 0xe2:
205448 case '\r':
205449 case '\n': {
205450 u32 nSkip = jsonBytesToBypass(z, n);
205451 if( nSkip==0 ){
205452 *piOut = 0xFFFD;
205453 return n;
205454 }else if( nSkip==n ){
205455 *piOut = 0;
205456 return n;
205457 }else if( z[nSkip]=='\\' ){
@@ -205460,11 +205608,11 @@
205460 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
205461 return nSkip + sz;
205462 }
205463 }
205464 default: {
205465 *piOut = 0xFFFD;
205466 return 2;
205467 }
205468 }
205469 }
205470
@@ -205823,11 +205971,11 @@
205823 if( NEVER(aBlob==0) ) return;
205824 memset(&x, 0, sizeof(x));
205825 x.aBlob = (u8*)aBlob;
205826 x.nBlob = nBlob;
205827 jsonStringInit(&s, ctx);
205828 jsonXlateBlobToText(&x, 0, &s);
205829 jsonReturnString(&s, 0, 0);
205830 }
205831
205832
205833 /*
@@ -205934,21 +206082,21 @@
205934 if( c=='\\' ){
205935 u32 v;
205936 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
205937 if( v<=0x7f ){
205938 zOut[iOut++] = (char)v;
205939 }else if( v==0xfffd ){
205940 /* Silently ignore illegal unicode */
205941 }else if( v<=0x7ff ){
205942 assert( szEscape>=2 );
205943 zOut[iOut++] = (char)(0xc0 | (v>>6));
205944 zOut[iOut++] = 0x80 | (v&0x3f);
205945 }else if( v<0x10000 ){
205946 assert( szEscape>=3 );
205947 zOut[iOut++] = 0xe0 | (v>>12);
205948 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
205949 zOut[iOut++] = 0x80 | (v&0x3f);
 
 
205950 }else{
205951 assert( szEscape>=4 );
205952 zOut[iOut++] = 0xf0 | (v>>18);
205953 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
205954 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
@@ -206046,17 +206194,33 @@
206046 }else{
206047 jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson);
206048 }
206049 break;
206050 }
206051 case SQLITE_FLOAT:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206052 case SQLITE_INTEGER: {
206053 int n = sqlite3_value_bytes(pArg);
206054 const char *z = (const char*)sqlite3_value_text(pArg);
206055 int e = eType==SQLITE_INTEGER ? JSONB_INT : JSONB_FLOAT;
206056 if( z==0 ) return 1;
206057 jsonBlobAppendNode(pParse, e, n, z);
206058 break;
206059 }
206060 }
206061 if( pParse->oom ){
206062 sqlite3_result_error_nomem(ctx);
@@ -206153,15 +206317,10 @@
206153 }else{
206154 jsonBadPathError(ctx, zPath);
206155 }
206156 return;
206157 }
206158
206159 /*
206160 ** Make a copy of a JsonParse object. The copy will be editable.
206161 */
206162
206163
206164 /*
206165 ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
206166 ** from the SQL function argument pArg. Return a pointer to the new
206167 ** JsonParse object.
@@ -206313,11 +206472,12 @@
206313 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT);
206314 }
206315 }else{
206316 JsonString s;
206317 jsonStringInit(&s, ctx);
206318 jsonXlateBlobToText(p, 0, &s);
 
206319 jsonReturnString(&s, p, ctx);
206320 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206321 }
206322 }
206323
@@ -206333,29 +206493,32 @@
206333 */
206334 static void jsonDebugPrintBlob(
206335 JsonParse *pParse, /* JSON content */
206336 u32 iStart, /* Start rendering here */
206337 u32 iEnd, /* Do not render this byte or any byte after this one */
206338 int nIndent /* Indent by this many spaces */
 
206339 ){
206340 while( iStart<iEnd ){
206341 u32 i, n, nn, sz = 0;
206342 int showContent = 1;
206343 u8 x = pParse->aBlob[iStart] & 0x0f;
206344 u32 savedNBlob = pParse->nBlob;
206345 printf("%5d:%*s", iStart, nIndent, "");
206346 if( pParse->nBlobAlloc>pParse->nBlob ){
206347 pParse->nBlob = pParse->nBlobAlloc;
206348 }
206349 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
206350 if( nn==0 ) nn = 1;
206351 if( sz>0 && x<JSONB_ARRAY ){
206352 nn += sz;
206353 }
206354 for(i=0; i<nn; i++) printf(" %02x", pParse->aBlob[iStart+i]);
 
 
206355 if( n==0 ){
206356 printf(" ERROR invalid node size\n");
206357 iStart = n==0 ? iStart+1 : iEnd;
206358 continue;
206359 }
206360 pParse->nBlob = savedNBlob;
206361 if( iStart+n+sz>iEnd ){
@@ -206366,59 +206529,61 @@
206366 }else{
206367 iEnd = pParse->nBlob;
206368 }
206369 }
206370 }
206371 printf(" <-- ");
206372 switch( x ){
206373 case JSONB_NULL: printf("null"); break;
206374 case JSONB_TRUE: printf("true"); break;
206375 case JSONB_FALSE: printf("false"); break;
206376 case JSONB_INT: printf("int"); break;
206377 case JSONB_INT5: printf("int5"); break;
206378 case JSONB_FLOAT: printf("float"); break;
206379 case JSONB_FLOAT5: printf("float5"); break;
206380 case JSONB_TEXT: printf("text"); break;
206381 case JSONB_TEXTJ: printf("textj"); break;
206382 case JSONB_TEXT5: printf("text5"); break;
206383 case JSONB_TEXTRAW: printf("textraw"); break;
206384 case JSONB_ARRAY: {
206385 printf("array, %u bytes\n", sz);
206386 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2);
206387 showContent = 0;
206388 break;
206389 }
206390 case JSONB_OBJECT: {
206391 printf("object, %u bytes\n", sz);
206392 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2);
206393 showContent = 0;
206394 break;
206395 }
206396 default: {
206397 printf("ERROR: unknown node type\n");
206398 showContent = 0;
206399 break;
206400 }
206401 }
206402 if( showContent ){
206403 if( sz==0 && x<=JSONB_FALSE ){
206404 printf("\n");
206405 }else{
206406 u32 i;
206407 printf(": \"");
206408 for(i=iStart+n; i<iStart+n+sz; i++){
206409 u8 c = pParse->aBlob[i];
206410 if( c<0x20 || c>=0x7f ) c = '.';
206411 putchar(c);
206412 }
206413 printf("\"\n");
206414 }
206415 }
206416 iStart += n + sz;
206417 }
206418 }
206419 static void jsonShowParse(JsonParse *pParse){
 
 
206420 if( pParse==0 ){
206421 printf("NULL pointer\n");
206422 return;
206423 }else{
206424 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
@@ -206425,31 +206590,42 @@
206425 printf("nBlob = %u\n", pParse->nBlob);
206426 printf("delta = %d\n", pParse->delta);
206427 if( pParse->nBlob==0 ) return;
206428 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
206429 }
206430 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0);
 
 
 
206431 }
206432 #endif /* SQLITE_DEBUG */
206433
206434 #ifdef SQLITE_DEBUG
206435 /*
206436 ** SQL function: json_parse(JSON)
206437 **
206438 ** Parse JSON using jsonParseFuncArg(). Then print a dump of that
206439 ** parse on standard output.
206440 */
206441 static void jsonParseFunc(
206442 sqlite3_context *ctx,
206443 int argc,
206444 sqlite3_value **argv
206445 ){
206446 JsonParse *p; /* The parse */
 
206447
206448 assert( argc==1 );
 
206449 p = jsonParseFuncArg(ctx, argv[0], 0);
206450 jsonShowParse(p);
 
 
 
 
 
 
206451 jsonParseFree(p);
206452 }
206453 #endif /* SQLITE_DEBUG */
206454
206455 /****************************************************************************
@@ -206641,11 +206817,11 @@
206641 }
206642 if( j<p->nBlob ){
206643 if( argc==2 ){
206644 if( flags & JSON_JSON ){
206645 jsonStringInit(&jx, ctx);
206646 jsonXlateBlobToText(p, j, &jx);
206647 jsonReturnString(&jx, 0, 0);
206648 jsonStringReset(&jx);
206649 assert( (flags & JSON_BLOB)==0 );
206650 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206651 }else{
@@ -206656,11 +206832,11 @@
206656 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206657 }
206658 }
206659 }else{
206660 jsonAppendSeparator(&jx);
206661 jsonXlateBlobToText(p, j, &jx);
206662 }
206663 }else if( j==JSON_LOOKUP_NOTFOUND ){
206664 if( argc==2 ){
206665 goto json_extract_error; /* Return NULL if not found */
206666 }else{
@@ -229267,23 +229443,28 @@
229267 **
229268 ** This function may be quite inefficient if used with an FTS5 table
229269 ** created with the "columnsize=0" option.
229270 **
229271 ** xColumnText:
229272 ** This function attempts to retrieve the text of column iCol of the
229273 ** current document. If successful, (*pz) is set to point to a buffer
 
 
 
229274 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
229275 ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
229276 ** if an error occurs, an SQLite error code is returned and the final values
229277 ** of (*pz) and (*pn) are undefined.
229278 **
229279 ** xPhraseCount:
229280 ** Returns the number of phrases in the current query expression.
229281 **
229282 ** xPhraseSize:
229283 ** Returns the number of tokens in phrase iPhrase of the query. Phrases
229284 ** are numbered starting from zero.
 
 
229285 **
229286 ** xInstCount:
229287 ** Set *pnInst to the total number of occurrences of all phrases within
229288 ** the query within the current row. Return SQLITE_OK if successful, or
229289 ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -229295,16 +229476,17 @@
229295 **
229296 ** xInst:
229297 ** Query for the details of phrase match iIdx within the current row.
229298 ** Phrase matches are numbered starting from zero, so the iIdx argument
229299 ** should be greater than or equal to zero and smaller than the value
229300 ** output by xInstCount().
 
229301 **
229302 ** Usually, output parameter *piPhrase is set to the phrase number, *piCol
229303 ** to the column in which it occurs and *piOff the token offset of the
229304 ** first token of the phrase. Returns SQLITE_OK if successful, or an error
229305 ** code (i.e. SQLITE_NOMEM) if an error occurs.
229306 **
229307 ** This API can be quite slow if used with an FTS5 table created with the
229308 ** "detail=none" or "detail=column" option.
229309 **
229310 ** xRowid:
@@ -229325,10 +229507,14 @@
229325 ** row visited, the callback function passed as the fourth argument
229326 ** is invoked. The context and API objects passed to the callback
229327 ** function may be used to access the properties of each matched row.
229328 ** Invoking Api.xUserData() returns a copy of the pointer passed as
229329 ** the third argument to pUserData.
 
 
 
 
229330 **
229331 ** If the callback function returns any value other than SQLITE_OK, the
229332 ** query is abandoned and the xQueryPhrase function returns immediately.
229333 ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
229334 ** Otherwise, the error code is propagated upwards.
@@ -229446,22 +229632,30 @@
229446 ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
229447 ** This is used to access token iToken of phrase iPhrase of the current
229448 ** query. Before returning, output parameter *ppToken is set to point
229449 ** to a buffer containing the requested token, and *pnToken to the
229450 ** size of this buffer in bytes.
 
 
 
 
 
 
229451 **
229452 ** The output text is not a copy of the query text that specified the
229453 ** token. It is the output of the tokenizer module. For tokendata=1
229454 ** tables, this includes any embedded 0x00 and trailing data.
229455 **
229456 ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
229457 ** This is used to access token iToken of phrase hit iIdx within the
229458 ** current row. Output variable (*ppToken) is set to point to a buffer
229459 ** containing the matching document token, and (*pnToken) to the size
229460 ** of that buffer in bytes. This API is not available if the specified
229461 ** token matches a prefix query term. In that case both output variables
229462 ** are always set to 0.
 
 
229463 **
229464 ** The output text is not a copy of the document text that was tokenized.
229465 ** It is the output of the tokenizer module. For tokendata=1 tables, this
229466 ** includes any embedded 0x00 and trailing data.
229467 **
@@ -232433,12 +232627,14 @@
232433 memset(&ctx, 0, sizeof(HighlightContext));
232434 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
232435 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
232436 ctx.iRangeEnd = -1;
232437 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
232438
232439 if( ctx.zIn ){
 
 
232440 if( rc==SQLITE_OK ){
232441 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
232442 }
232443
232444 if( rc==SQLITE_OK ){
@@ -236273,15 +236469,19 @@
236273 Fts5Expr *pExpr,
236274 int iPhrase,
236275 Fts5Expr **ppNew
236276 ){
236277 int rc = SQLITE_OK; /* Return code */
236278 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
236279 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
236280 TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
236281 pOrig = pExpr->apExprPhrase[iPhrase];
236282 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
 
 
 
 
236283 if( rc==SQLITE_OK ){
236284 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
236285 sizeof(Fts5ExprPhrase*));
236286 }
236287 if( rc==SQLITE_OK ){
@@ -236290,11 +236490,11 @@
236290 }
236291 if( rc==SQLITE_OK ){
236292 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
236293 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
236294 }
236295 if( rc==SQLITE_OK ){
236296 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
236297 if( pColsetOrig ){
236298 sqlite3_int64 nByte;
236299 Fts5Colset *pColset;
236300 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
@@ -236304,29 +236504,31 @@
236304 }
236305 pNew->pRoot->pNear->pColset = pColset;
236306 }
236307 }
236308
236309 if( pOrig->nTerm ){
236310 int i; /* Used to iterate through phrase terms */
236311 sCtx.pConfig = pExpr->pConfig;
236312 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
236313 int tflags = 0;
236314 Fts5ExprTerm *p;
236315 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
236316 rc = fts5ParseTokenize((void*)&sCtx, tflags, p->pTerm,p->nFullTerm,0,0);
236317 tflags = FTS5_TOKEN_COLOCATED;
236318 }
236319 if( rc==SQLITE_OK ){
236320 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
236321 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
236322 }
236323 }
236324 }else{
236325 /* This happens when parsing a token or quoted phrase that contains
236326 ** no token characters at all. (e.g ... MATCH '""'). */
236327 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
 
 
236328 }
236329
236330 if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
236331 /* All the allocations succeeded. Put the expression object together. */
236332 pNew->pIndex = pExpr->pIndex;
@@ -239776,13 +239978,13 @@
239776 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
239777 if( pData->p[iOff] ) break;
239778 }
239779
239780 if( iOff<pData->nn ){
239781 i64 iVal;
239782 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
239783 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
239784 pLvl->iRowid += iVal;
239785 pLvl->iOff = iOff;
239786 }else{
239787 pLvl->bEof = 1;
239788 }
@@ -246173,20 +246375,20 @@
246173 fts5DataRelease(pLeaf);
246174 }
246175 }
246176
246177 static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
246178 int iTermOff = 0;
246179 int ii;
246180
246181 Fts5Buffer buf1 = {0,0,0};
246182 Fts5Buffer buf2 = {0,0,0};
246183
246184 ii = pLeaf->szLeaf;
246185 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
246186 int res;
246187 int iOff;
246188 int nIncr;
246189
246190 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
246191 iTermOff += nIncr;
246192 iOff = iTermOff;
@@ -249207,11 +249409,14 @@
249207 const char **pz,
249208 int *pn
249209 ){
249210 int rc = SQLITE_OK;
249211 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
249212 if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
 
 
 
249213 || pCsr->ePlan==FTS5_PLAN_SPECIAL
249214 ){
249215 *pz = 0;
249216 *pn = 0;
249217 }else{
@@ -249232,12 +249437,13 @@
249232 ){
249233 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
249234 int rc = SQLITE_OK;
249235 int bLive = (pCsr->pSorter==0);
249236
249237 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
249238
 
249239 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
249240 Fts5PoslistPopulator *aPopulator;
249241 int i;
249242 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
249243 if( aPopulator==0 ) rc = SQLITE_NOMEM;
@@ -249257,18 +249463,24 @@
249257 }
249258 }
249259 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
249260 }
249261
249262 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
249263 Fts5Sorter *pSorter = pCsr->pSorter;
249264 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
249265 *pn = pSorter->aIdx[iPhrase] - i1;
249266 *pa = &pSorter->aPoslist[i1];
 
 
 
 
249267 }else{
249268 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
 
249269 }
 
249270
249271 return rc;
249272 }
249273
249274 /*
@@ -250223,11 +250435,11 @@
250223 int nArg, /* Number of args */
250224 sqlite3_value **apUnused /* Function arguments */
250225 ){
250226 assert( nArg==0 );
250227 UNUSED_PARAM2(nArg, apUnused);
250228 sqlite3_result_text(pCtx, "fts5: 2023-12-14 16:34:47 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9", -1, SQLITE_TRANSIENT);
250229 }
250230
250231 /*
250232 ** Return true if zName is the extension on one of the shadow tables used
250233 ** by this module.
250234
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** bcac937526d9a6ef914a74b4d6757fa91cd7.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.45.0"
463 #define SQLITE_VERSION_NUMBER 3045000
464 #define SQLITE_SOURCE_ID "2024-01-01 19:20:00 bcac937526d9a6ef914a74b4d6757fa91cd74edab871bcd934fde4a2f9b6debd"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -8350,13 +8350,15 @@
8350 ** can enter.)^ If the same thread tries to enter any mutex other
8351 ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8352 **
8353 ** ^(Some systems (for example, Windows 95) do not support the operation
8354 ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8355 ** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8356 ** sqlite3_mutex_try() as an optimization, so this is acceptable
8357 ** behavior. The exceptions are unix builds that set the
8358 ** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8359 ** sqlite3_mutex_try() is required.)^
8360 **
8361 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
8362 ** previously entered by the same thread. The behavior
8363 ** is undefined if the mutex is not currently entered by the
8364 ** calling thread or is not currently allocated.
@@ -13125,23 +13127,28 @@
13127 **
13128 ** This function may be quite inefficient if used with an FTS5 table
13129 ** created with the "columnsize=0" option.
13130 **
13131 ** xColumnText:
13132 ** If parameter iCol is less than zero, or greater than or equal to the
13133 ** number of columns in the table, SQLITE_RANGE is returned.
13134 **
13135 ** Otherwise, this function attempts to retrieve the text of column iCol of
13136 ** the current document. If successful, (*pz) is set to point to a buffer
13137 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13138 ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
13139 ** if an error occurs, an SQLite error code is returned and the final values
13140 ** of (*pz) and (*pn) are undefined.
13141 **
13142 ** xPhraseCount:
13143 ** Returns the number of phrases in the current query expression.
13144 **
13145 ** xPhraseSize:
13146 ** If parameter iCol is less than zero, or greater than or equal to the
13147 ** number of phrases in the current query, as returned by xPhraseCount,
13148 ** 0 is returned. Otherwise, this function returns the number of tokens in
13149 ** phrase iPhrase of the query. Phrases are numbered starting from zero.
13150 **
13151 ** xInstCount:
13152 ** Set *pnInst to the total number of occurrences of all phrases within
13153 ** the query within the current row. Return SQLITE_OK if successful, or
13154 ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -13153,16 +13160,17 @@
13160 **
13161 ** xInst:
13162 ** Query for the details of phrase match iIdx within the current row.
13163 ** Phrase matches are numbered starting from zero, so the iIdx argument
13164 ** should be greater than or equal to zero and smaller than the value
13165 ** output by xInstCount(). If iIdx is less than zero or greater than
13166 ** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
13167 **
13168 ** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
13169 ** to the column in which it occurs and *piOff the token offset of the
13170 ** first token of the phrase. SQLITE_OK is returned if successful, or an
13171 ** error code (i.e. SQLITE_NOMEM) if an error occurs.
13172 **
13173 ** This API can be quite slow if used with an FTS5 table created with the
13174 ** "detail=none" or "detail=column" option.
13175 **
13176 ** xRowid:
@@ -13183,10 +13191,14 @@
13191 ** row visited, the callback function passed as the fourth argument
13192 ** is invoked. The context and API objects passed to the callback
13193 ** function may be used to access the properties of each matched row.
13194 ** Invoking Api.xUserData() returns a copy of the pointer passed as
13195 ** the third argument to pUserData.
13196 **
13197 ** If parameter iPhrase is less than zero, or greater than or equal to
13198 ** the number of phrases in the query, as returned by xPhraseCount(),
13199 ** this function returns SQLITE_RANGE.
13200 **
13201 ** If the callback function returns any value other than SQLITE_OK, the
13202 ** query is abandoned and the xQueryPhrase function returns immediately.
13203 ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
13204 ** Otherwise, the error code is propagated upwards.
@@ -13304,22 +13316,30 @@
13316 ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13317 ** This is used to access token iToken of phrase iPhrase of the current
13318 ** query. Before returning, output parameter *ppToken is set to point
13319 ** to a buffer containing the requested token, and *pnToken to the
13320 ** size of this buffer in bytes.
13321 **
13322 ** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13323 ** or equal to the number of phrases in the query as reported by
13324 ** xPhraseCount(), or if iToken is equal to or greater than the number of
13325 ** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13326 are both zeroed.
13327 **
13328 ** The output text is not a copy of the query text that specified the
13329 ** token. It is the output of the tokenizer module. For tokendata=1
13330 ** tables, this includes any embedded 0x00 and trailing data.
13331 **
13332 ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13333 ** This is used to access token iToken of phrase hit iIdx within the
13334 ** current row. If iIdx is less than zero or greater than or equal to the
13335 ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13336 ** output variable (*ppToken) is set to point to a buffer containing the
13337 ** matching document token, and (*pnToken) to the size of that buffer in
13338 ** bytes. This API is not available if the specified token matches a
13339 ** prefix query term. In that case both output variables are always set
13340 ** to 0.
13341 **
13342 ** The output text is not a copy of the document text that was tokenized.
13343 ** It is the output of the tokenizer module. For tokendata=1 tables, this
13344 ** includes any embedded 0x00 and trailing data.
13345 **
@@ -13991,10 +14011,23 @@
14011 #if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
14012 # define SQLITE_USE_SEH 1
14013 #else
14014 # undef SQLITE_USE_SEH
14015 #endif
14016
14017 /*
14018 ** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly
14019 ** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14020 */
14021 #if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1
14022 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14023 # undef SQLITE_DIRECT_OVERFLOW_READ
14024 #else
14025 /* In all other cases, enable */
14026 # define SQLITE_DIRECT_OVERFLOW_READ 1
14027 #endif
14028
14029
14030 /*
14031 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
14032 ** 0 means mutexes are permanently disable and the library is never
14033 ** threadsafe. 1 means the library is serialized which is the highest
@@ -15874,11 +15907,11 @@
15907 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
15908 SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15909 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15910 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15911 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15912 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, u64*);
15913 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15914 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15915
15916 /* Functions used to truncate the database file. */
15917 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
@@ -18629,10 +18662,11 @@
18662 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
18663 unsigned isResized:1; /* True if resizeIndexObject() has been called */
18664 unsigned isCovering:1; /* True if this is a covering index */
18665 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18666 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
18667 unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
18668 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
18669 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
18670 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
18671 unsigned bHasExpr:1; /* Index contains an expression, either a literal
18672 ** expression, or a reference to a VIRTUAL column */
@@ -24028,11 +24062,11 @@
24062 /* no break */ deliberate_fall_through
24063 case SQLITE_DBSTATUS_CACHE_HIT:
24064 case SQLITE_DBSTATUS_CACHE_MISS:
24065 case SQLITE_DBSTATUS_CACHE_WRITE:{
24066 int i;
24067 u64 nRet = 0;
24068 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
24069 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
24070
24071 for(i=0; i<db->nDb; i++){
24072 if( db->aDb[i].pBt ){
@@ -24041,11 +24075,11 @@
24075 }
24076 }
24077 *pHighwater = 0; /* IMP: R-42420-56072 */
24078 /* IMP: R-54100-20147 */
24079 /* IMP: R-29431-39229 */
24080 *pCurrent = (int)nRet & 0x7fffffff;
24081 break;
24082 }
24083
24084 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
24085 ** key constraints. Set *pCurrent to zero if all foreign key constraints
@@ -34677,11 +34711,11 @@
34711 ** Load the sqlite3.iSysErrno field if that is an appropriate thing
34712 ** to do based on the SQLite error code in rc.
34713 */
34714 SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
34715 if( rc==SQLITE_IOERR_NOMEM ) return;
34716 #if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
34717 if( rc==SQLITE_IOERR_IN_PAGE ){
34718 int ii;
34719 int iErr;
34720 sqlite3BtreeEnterAll(db);
34721 for(ii=0; ii<db->nDb; ii++){
@@ -42365,13 +42399,19 @@
42399 struct flock f; /* The posix advisory locking structure */
42400 int rc = SQLITE_OK; /* Result code form fcntl() */
42401
42402 pShmNode = pFile->pInode->pShmNode;
42403
42404 /* Assert that the parameters are within expected range and that the
42405 ** correct mutex or mutexes are held. */
42406 assert( pShmNode->nRef>=0 );
42407 assert( (ofst==UNIX_SHM_DMS && n==1)
42408 || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK))
42409 );
42410 if( ofst==UNIX_SHM_DMS ){
42411 assert( pShmNode->nRef>0 || unixMutexHeld() );
42412 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
42413 }else{
42414 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42415 int ii;
42416 for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){
42417 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) );
@@ -57333,11 +57373,11 @@
57373 i64 journalSizeLimit; /* Size limit for persistent journal files */
57374 char *zFilename; /* Name of the database file */
57375 char *zJournal; /* Name of the journal file */
57376 int (*xBusyHandler)(void*); /* Function to call when busy */
57377 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
57378 u32 aStat[4]; /* Total cache hits, misses, writes, spills */
57379 #ifdef SQLITE_TEST
57380 int nRead; /* Database pages read */
57381 #endif
57382 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
57383 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
@@ -63477,15 +63517,15 @@
63517 a[1] = sqlite3PcachePagecount(pPager->pPCache);
63518 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
63519 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63520 a[4] = pPager->eState;
63521 a[5] = pPager->errCode;
63522 a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff;
63523 a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff;
63524 a[8] = 0; /* Used to be pPager->nOvfl */
63525 a[9] = pPager->nRead;
63526 a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff;
63527 return a;
63528 }
63529 #endif
63530
63531 /*
@@ -63497,11 +63537,11 @@
63537 ** Before returning, *pnVal is incremented by the
63538 ** current cache hit or miss count, according to the value of eStat. If the
63539 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
63540 ** returning.
63541 */
63542 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){
63543
63544 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
63545 || eStat==SQLITE_DBSTATUS_CACHE_MISS
63546 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
63547 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
@@ -64437,11 +64477,11 @@
64477 assert( pPager->eState>=PAGER_READER );
64478 return sqlite3WalFramesize(pPager->pWal);
64479 }
64480 #endif
64481
64482 #if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
64483 SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){
64484 return sqlite3WalSystemErrno(pPager->pWal);
64485 }
64486 #endif
64487
@@ -106789,10 +106829,11 @@
106829 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
106830 }
106831 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
106832 pParse->checkSchema = 1;
106833 pTopNC->nNcErr++;
106834 eNewExprOp = TK_NULL;
106835 }
106836 assert( pFJMatch==0 );
106837
106838 /* Remove all substructure from pExpr */
106839 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
@@ -110976,13 +111017,14 @@
111017 case TK_BLOB:
111018 return 0;
111019 case TK_COLUMN:
111020 assert( ExprUseYTab(p) );
111021 return ExprHasProperty(p, EP_CanBeNull) ||
111022 NEVER(p->y.pTab==0) || /* Reference to column of index on expr */
111023 (p->iColumn>=0
111024 && p->y.pTab->aCol!=0 /* Possible due to prior error */
111025 && ALWAYS(p->iColumn<p->y.pTab->nCol)
111026 && p->y.pTab->aCol[p->iColumn].notNull==0);
111027 default:
111028 return 1;
111029 }
111030 }
@@ -113560,12 +113602,14 @@
113602 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
113603 if( pParse->pVdbe==0 ) return;
113604 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
113605 if( inReg!=target ){
113606 u8 op;
113607 Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr);
113608 testcase( pX!=pExpr );
113609 if( ALWAYS(pX)
113610 && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER)
113611 ){
113612 op = OP_Copy;
113613 }else{
113614 op = OP_SCopy;
113615 }
@@ -117825,13 +117869,13 @@
117869 ** information.
117870 */
117871 typedef struct StatAccum StatAccum;
117872 typedef struct StatSample StatSample;
117873 struct StatSample {
 
117874 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
117875 #ifdef SQLITE_ENABLE_STAT4
117876 tRowcnt *anEq; /* sqlite_stat4.nEq */
117877 tRowcnt *anLt; /* sqlite_stat4.nLt */
117878 union {
117879 i64 iRowid; /* Rowid in main table of the key */
117880 u8 *aRowid; /* Key for WITHOUT ROWID tables */
117881 } u;
@@ -117985,13 +118029,13 @@
118029 assert( nKeyCol<=nCol );
118030 assert( nKeyCol>0 );
118031
118032 /* Allocate the space required for the StatAccum object */
118033 n = sizeof(*p)
118034 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
 
118035 #ifdef SQLITE_ENABLE_STAT4
118036 n += sizeof(tRowcnt)*nColUp; /* StatAccum.anEq */
118037 if( mxSample ){
118038 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
118039 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
118040 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
118041 }
@@ -118008,13 +118052,13 @@
118052 p->nLimit = sqlite3_value_int64(argv[3]);
118053 p->nCol = nCol;
118054 p->nKeyCol = nKeyCol;
118055 p->nSkipAhead = 0;
118056 p->current.anDLt = (tRowcnt*)&p[1];
 
118057
118058 #ifdef SQLITE_ENABLE_STAT4
118059 p->current.anEq = &p->current.anDLt[nColUp];
118060 p->mxSample = p->nLimit==0 ? mxSample : 0;
118061 if( mxSample ){
118062 u8 *pSpace; /* Allocated space not yet assigned */
118063 int i; /* Used to iterate through p->aSample[] */
118064
@@ -118277,28 +118321,32 @@
118321 assert( p->nCol>0 );
118322 assert( iChng<p->nCol );
118323
118324 if( p->nRow==0 ){
118325 /* This is the first call to this function. Do initialization. */
118326 #ifdef SQLITE_ENABLE_STAT4
118327 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
118328 #endif
118329 }else{
118330 /* Second and subsequent calls get processed here */
118331 #ifdef SQLITE_ENABLE_STAT4
118332 if( p->mxSample ) samplePushPrevious(p, iChng);
118333 #endif
118334
118335 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
118336 ** to the current row of the index. */
118337 #ifdef SQLITE_ENABLE_STAT4
118338 for(i=0; i<iChng; i++){
118339 p->current.anEq[i]++;
118340 }
118341 #endif
118342 for(i=iChng; i<p->nCol; i++){
118343 p->current.anDLt[i]++;
118344 #ifdef SQLITE_ENABLE_STAT4
118345 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
 
118346 p->current.anEq[i] = 1;
118347 #endif
118348 }
118349 }
118350
118351 p->nRow++;
118352 #ifdef SQLITE_ENABLE_STAT4
@@ -118428,11 +118476,13 @@
118476 for(i=0; i<p->nKeyCol; i++){
118477 u64 nDistinct = p->current.anDLt[i] + 1;
118478 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
118479 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
118480 sqlite3_str_appendf(&sStat, " %llu", iVal);
118481 #ifdef SQLITE_ENABLE_STAT4
118482 assert( p->current.anEq[i] );
118483 #endif
118484 }
118485 sqlite3ResultStrAccum(context, &sStat);
118486 }
118487 #ifdef SQLITE_ENABLE_STAT4
118488 else if( eCall==STAT_GET_ROWID ){
@@ -119117,10 +119167,20 @@
119167 }
119168 #endif
119169 while( z[0]!=0 && z[0]!=' ' ) z++;
119170 while( z[0]==' ' ) z++;
119171 }
119172
119173 /* Set the bLowQual flag if the peak number of rows obtained
119174 ** from a full equality match is so large that a full table scan
119175 ** seems likely to be faster than using the index.
119176 */
119177 if( aLog[0] > 66 /* Index has more than 100 rows */
119178 && aLog[0] <= aLog[nOut-1] /* And only a single value seen */
119179 ){
119180 pIndex->bLowQual = 1;
119181 }
119182 }
119183 }
119184
119185 /*
119186 ** This callback is invoked once for each index when reading the
@@ -148743,11 +148803,11 @@
148803 pSub->pPrior = 0;
148804 pSub->pNext = 0;
148805 pSub->selFlags |= SF_Aggregate;
148806 pSub->selFlags &= ~SF_Compound;
148807 pSub->nSelectRow = 0;
148808 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
148809 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
148810 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
148811 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
148812 sqlite3PExprAddSelect(pParse, pTerm, pSub);
148813 if( pExpr==0 ){
@@ -154295,11 +154355,10 @@
154355 assert( sqlite3BtreeHoldsAllMutexes(db) );
154356 assert( sqlite3_mutex_held(db->mutex) );
154357
154358 if( p ){
154359 db->pDisconnect = 0;
 
154360 do {
154361 VTable *pNext = p->pNext;
154362 sqlite3VtabUnlock(p);
154363 p = pNext;
154364 }while( p );
@@ -155861,11 +155920,11 @@
155920 */
155921 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
155922 #ifdef WHERETRACE_ENABLED
155923 SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
155924 SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
155925 SQLITE_PRIVATE void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC);
155926 #endif
155927 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
155928 WhereClause *pWC, /* The WHERE clause to be searched */
155929 int iCur, /* Cursor number of LHS */
155930 int iColumn, /* Column number of LHS */
@@ -162890,21 +162949,38 @@
162949 #endif
162950
162951 #ifdef WHERETRACE_ENABLED
162952 /*
162953 ** Print a WhereLoop object for debugging purposes
162954 **
162955 ** Format example:
162956 **
162957 ** .--- Position in WHERE clause rSetup, rRun, nOut ---.
162958 ** | |
162959 ** | .--- selfMask nTerm ------. |
162960 ** | | | |
162961 ** | | .-- prereq Idx wsFlags----. | |
162962 ** | | | Name | | |
162963 ** | | | __|__ nEq ---. ___|__ | __|__
162964 ** | / \ / \ / \ | / \ / \ / \
162965 ** 1.002.001 t2.t2xy 2 f 010241 N 2 cost 0,56,31
162966 */
162967 SQLITE_PRIVATE void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC){
162968 if( pWC ){
162969 WhereInfo *pWInfo = pWC->pWInfo;
162970 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
162971 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
162972 Table *pTab = pItem->pTab;
162973 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
162974 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
162975 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
162976 sqlite3DebugPrintf(" %12s",
162977 pItem->zAlias ? pItem->zAlias : pTab->zName);
162978 }else{
162979 sqlite3DebugPrintf("%c%2d.%03llx.%03llx %c%d",
162980 p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab);
162981 }
162982 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
162983 const char *zName;
162984 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
162985 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
162986 int i = sqlite3Strlen30(zName) - 1;
@@ -162936,10 +163012,19 @@
163012 int i;
163013 for(i=0; i<p->nLTerm; i++){
163014 sqlite3WhereTermPrint(p->aLTerm[i], i);
163015 }
163016 }
163017 }
163018 SQLITE_PRIVATE void sqlite3ShowWhereLoop(const WhereLoop *p){
163019 if( p ) sqlite3WhereLoopPrint(p, 0);
163020 }
163021 SQLITE_PRIVATE void sqlite3ShowWhereLoopList(const WhereLoop *p){
163022 while( p ){
163023 sqlite3ShowWhereLoop(p);
163024 p = p->pNextLoop;
163025 }
163026 }
163027 #endif
163028
163029 /*
163030 ** Convert bulk memory into a valid WhereLoop that can be passed
@@ -163049,50 +163134,64 @@
163134 }
163135 sqlite3DbNNFreeNN(db, pWInfo);
163136 }
163137
163138 /*
163139 ** Return TRUE if X is a proper subset of Y but is of equal or less cost.
163140 ** In other words, return true if all constraints of X are also part of Y
163141 ** and Y has additional constraints that might speed the search that X lacks
163142 ** but the cost of running X is not more than the cost of running Y.
163143 **
163144 ** In other words, return true if the cost relationwship between X and Y
163145 ** is inverted and needs to be adjusted.
163146 **
163147 ** Case 1:
163148 **
163149 ** (1a) X and Y use the same index.
163150 ** (1b) X has fewer == terms than Y
163151 ** (1c) Neither X nor Y use skip-scan
163152 ** (1d) X does not have a a greater cost than Y
163153 **
163154 ** Case 2:
163155 **
163156 ** (2a) X has the same or lower cost, or returns the same or fewer rows,
163157 ** than Y.
163158 ** (2b) X uses fewer WHERE clause terms than Y
163159 ** (2c) Every WHERE clause term used by X is also used by Y
163160 ** (2d) X skips at least as many columns as Y
163161 ** (2e) If X is a covering index, than Y is too
163162 */
163163 static int whereLoopCheaperProperSubset(
163164 const WhereLoop *pX, /* First WhereLoop to compare */
163165 const WhereLoop *pY /* Compare against this WhereLoop */
163166 ){
163167 int i, j;
163168 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */
163169 assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 );
163170 assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 );
163171 if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */
163172 && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */
163173 && pX->nSkip==0 && pY->nSkip==0 /* (1c) */
163174 ){
163175 return 1; /* Case 1 is true */
163176 }
163177 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
163178 return 0; /* (2b) */
163179 }
163180 if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */
 
163181 for(i=pX->nLTerm-1; i>=0; i--){
163182 if( pX->aLTerm[i]==0 ) continue;
163183 for(j=pY->nLTerm-1; j>=0; j--){
163184 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
163185 }
163186 if( j<0 ) return 0; /* (2c) */
163187 }
163188 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
163189 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
163190 return 0; /* (2e) */
163191 }
163192 return 1; /* Case 2 is true */
163193 }
163194
163195 /*
163196 ** Try to adjust the cost and number of output rows of WhereLoop pTemplate
163197 ** upwards or downwards so that:
@@ -163578,11 +163677,14 @@
163677 opMask = WO_LT|WO_LE;
163678 }else{
163679 assert( pNew->u.btree.nBtm==0 );
163680 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
163681 }
163682 if( pProbe->bUnordered || pProbe->bLowQual ){
163683 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
163684 if( pProbe->bLowQual ) opMask &= ~(WO_EQ|WO_IN|WO_IS);
163685 }
163686
163687 assert( pNew->u.btree.nEq<pProbe->nColumn );
163688 assert( pNew->u.btree.nEq<pProbe->nKeyCol
163689 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
163690
@@ -166683,11 +166785,14 @@
166785 ** struct, the contents of WhereInfo.a[], the WhereClause structure
166786 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
166787 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
166788 ** some architectures. Hence the ROUND8() below.
166789 */
166790 nByteWInfo = ROUND8P(sizeof(WhereInfo));
166791 if( nTabList>1 ){
166792 nByteWInfo = ROUND8P(nByteWInfo + (nTabList-1)*sizeof(WhereLevel));
166793 }
166794 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
166795 if( db->mallocFailed ){
166796 sqlite3DbFree(db, pWInfo);
166797 pWInfo = 0;
166798 goto whereBeginError;
@@ -167245,10 +167350,15 @@
167350 whereBeginError:
167351 if( pWInfo ){
167352 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
167353 whereInfoFree(db, pWInfo);
167354 }
167355 #ifdef WHERETRACE_ENABLED
167356 /* Prevent harmless compiler warnings about debugging routines
167357 ** being declared but never used */
167358 sqlite3ShowWhereLoopList(0);
167359 #endif /* WHERETRACE_ENABLED */
167360 return 0;
167361 }
167362
167363 /*
167364 ** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
@@ -203027,13 +203137,13 @@
203137 ** The original design stored all JSON as pure text, canonical RFC-8259.
203138 ** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16).
203139 ** All generated JSON text still conforms strictly to RFC-8259, but text
203140 ** with JSON-5 extensions is accepted as input.
203141 **
203142 ** Beginning with version 3.45.0 (circa 2024-01-01), these routines also
203143 ** accept BLOB values that have JSON encoded using a binary representation
203144 ** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk
203145 ** format SQLite JSONB is completely different and incompatible with
203146 ** PostgreSQL JSONB.
203147 **
203148 ** Decoding and interpreting JSONB is still O(N) where N is the size of
203149 ** the input, the same as text JSON. However, the constant of proportionality
@@ -203120,10 +203230,13 @@
203230 ** code is between 0 and 12 and that the total size of the element
203231 ** (header plus payload) is the same as the size of the BLOB. If those
203232 ** checks are true, the BLOB is assumed to be JSONB and processing continues.
203233 ** Errors are only raised if some other miscoding is discovered during
203234 ** processing.
203235 **
203236 ** Additional information can be found in the doc/jsonb.md file of the
203237 ** canonical SQLite source tree.
203238 */
203239 #ifndef SQLITE_OMIT_JSON
203240 /* #include "sqliteInt.h" */
203241
203242 /* JSONB element types
@@ -203218,18 +203331,26 @@
203331 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
203332 */
203333 #define JSON_CACHE_ID (-429938) /* Cache entry */
203334 #define JSON_CACHE_SIZE 4 /* Max number of cache entries */
203335
203336 /*
203337 ** jsonUnescapeOneChar() returns this invalid code point if it encounters
203338 ** a syntax error.
203339 */
203340 #define JSON_INVALID_CHAR 0x99999
203341
203342 /* A cache mapping JSON text into JSONB blobs.
203343 **
203344 ** Each cache entry is a JsonParse object with the following restrictions:
203345 **
203346 ** * The bReadOnly flag must be set
203347 **
203348 ** * The aBlob[] array must be owned by the JsonParse object. In other
203349 ** words, nBlobAlloc must be non-zero.
203350 **
203351 ** * eEdit and delta must be zero.
203352 **
203353 ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
203354 */
203355 struct JsonCache {
203356 sqlite3 *db; /* Database connection */
@@ -203286,27 +203407,27 @@
203407 **
203408 ** 3. Zero or more changes are made to aBlob[] (via json_remove() or
203409 ** json_replace() or json_patch() or similar).
203410 **
203411 ** 4. New JSON text is generated from the aBlob[] for output. This step
203412 ** is skipped if the function is one of the jsonb_* functions that
203413 ** returns JSONB instead of text JSON.
203414 */
203415 struct JsonParse {
203416 u8 *aBlob; /* JSONB representation of JSON value */
203417 u32 nBlob; /* Bytes of aBlob[] actually used */
203418 u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
203419 char *zJson; /* Json text used for parsing */
203420 int nJson; /* Length of the zJson string in bytes */
203421 u32 nJPRef; /* Number of references to this object */
203422 u32 iErr; /* Error location in zJson[] */
203423 u16 iDepth; /* Nesting depth */
203424 u8 nErr; /* Number of errors seen */
203425 u8 oom; /* Set to true if out of memory */
203426 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
203427 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
203428 u8 bReadOnly; /* Do not modify. */
 
 
203429 /* Search and edit information. See jsonLookupStep() */
203430 u8 eEdit; /* Edit operation to apply */
203431 int delta; /* Size change due to the edit */
203432 u32 nIns; /* Number of bytes to insert */
203433 u32 iLabel; /* Location of label if search landed on an object value */
@@ -203341,11 +203462,11 @@
203462 /**************************************************************************
203463 ** Forward references
203464 **************************************************************************/
203465 static void jsonReturnStringAsBlob(JsonString*);
203466 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
203467 static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
203468 static void jsonReturnParse(sqlite3_context*,JsonParse*);
203469 static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
203470 static void jsonParseFree(JsonParse*);
203471 static u32 jsonbPayloadSize(const JsonParse*, u32, u32*);
203472 static u32 jsonUnescapeOneChar(const char*, u32, u32*);
@@ -203381,10 +203502,11 @@
203502 ){
203503 JsonCache *p;
203504
203505 assert( pParse->zJson!=0 );
203506 assert( pParse->bJsonIsRCStr );
203507 assert( pParse->delta==0 );
203508 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID);
203509 if( p==0 ){
203510 sqlite3 *db = sqlite3_context_db_handle(ctx);
203511 p = sqlite3DbMallocZero(db, sizeof(*p));
203512 if( p==0 ) return SQLITE_NOMEM;
@@ -203453,10 +203575,11 @@
203575 JsonParse *tmp = p->a[i];
203576 memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp));
203577 p->a[p->nUsed-1] = tmp;
203578 i = p->nUsed - 1;
203579 }
203580 assert( p->a[i]->delta==0 );
203581 return p->a[i];
203582 }else{
203583 return 0;
203584 }
203585 }
@@ -203621,12 +203744,37 @@
203744 if( z==0 ) return;
203745 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
203746 p->zBuf[p->nUsed++] = '"';
203747 while( 1 /*exit-by-break*/ ){
203748 k = 0;
203749 /* The following while() is the 4-way unwound equivalent of
203750 **
203751 ** while( k<N && jsonIsOk[z[k]] ){ k++; }
203752 */
203753 while( 1 /* Exit by break */ ){
203754 if( k+3>=N ){
203755 while( k<N && jsonIsOk[z[k]] ){ k++; }
203756 break;
203757 }
203758 if( !jsonIsOk[z[k]] ){
203759 break;
203760 }
203761 if( !jsonIsOk[z[k+1]] ){
203762 k += 1;
203763 break;
203764 }
203765 if( !jsonIsOk[z[k+2]] ){
203766 k += 2;
203767 break;
203768 }
203769 if( !jsonIsOk[z[k+3]] ){
203770 k += 3;
203771 break;
203772 }else{
203773 k += 4;
203774 }
203775 }
203776 if( k>=N ){
203777 if( k>0 ){
203778 memcpy(&p->zBuf[p->nUsed], z, k);
203779 p->nUsed += k;
203780 }
@@ -203714,11 +203862,11 @@
203862 if( jsonFuncArgMightBeBinary(pValue) ){
203863 JsonParse px;
203864 memset(&px, 0, sizeof(px));
203865 px.aBlob = (u8*)sqlite3_value_blob(pValue);
203866 px.nBlob = sqlite3_value_bytes(pValue);
203867 jsonTranslateBlobToText(&px, 0, p);
203868 }else if( p->eErr==0 ){
203869 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
203870 p->eErr = JSTRING_ERR;
203871 jsonStringReset(p);
203872 }
@@ -204231,18 +204379,14 @@
204379 ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
204380 ** any changes to *pOp and return false.
204381 */
204382 static int jsonIs4HexB(const char *z, int *pOp){
204383 if( z[0]!='u' ) return 0;
204384 if( !jsonIs4Hex(&z[1]) ) return 0;
 
 
 
204385 *pOp = JSONB_TEXTJ;
204386 return 1;
204387 }
 
204388
204389 /*
204390 ** Check a single element of the JSONB in pParse for validity.
204391 **
204392 ** The element to be checked starts at offset i and must end at on the
@@ -204384,11 +204528,11 @@
204528 }else if( x!=JSONB_TEXT5 ){
204529 return j+1;
204530 }else{
204531 u32 c = 0;
204532 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
204533 if( c==JSON_INVALID_CHAR ) return j+1;
204534 j += szC - 1;
204535 }
204536 }
204537 j++;
204538 }
@@ -204456,11 +204600,11 @@
204600 ** -2 '}' seen \
204601 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
204602 ** -4 ',' seen / the index in zJson[] of the seen character
204603 ** -5 ':' seen /
204604 */
204605 static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
204606 char c;
204607 u32 j;
204608 u32 iThis, iStart;
204609 int x;
204610 u8 t;
@@ -204476,11 +204620,11 @@
204620 return -1;
204621 }
204622 iStart = pParse->nBlob;
204623 for(j=i+1;;j++){
204624 u32 iBlob = pParse->nBlob;
204625 x = jsonTranslateTextToBlob(pParse, j);
204626 if( x<=0 ){
204627 int op;
204628 if( x==(-2) ){
204629 j = pParse->iErr;
204630 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
@@ -204522,19 +204666,19 @@
204666 if( z[j]==':' ){
204667 j++;
204668 goto parse_object_value;
204669 }
204670 }
204671 x = jsonTranslateTextToBlob(pParse, j);
204672 if( x!=(-5) ){
204673 if( x!=(-1) ) pParse->iErr = j;
204674 return -1;
204675 }
204676 j = pParse->iErr+1;
204677 }
204678 parse_object_value:
204679 x = jsonTranslateTextToBlob(pParse, j);
204680 if( x<=0 ){
204681 if( x!=(-1) ) pParse->iErr = j;
204682 return -1;
204683 }
204684 j = x;
@@ -204549,11 +204693,11 @@
204693 continue;
204694 }else if( z[j]=='}' ){
204695 break;
204696 }
204697 }
204698 x = jsonTranslateTextToBlob(pParse, j);
204699 if( x==(-4) ){
204700 j = pParse->iErr;
204701 continue;
204702 }
204703 if( x==(-2) ){
@@ -204577,11 +204721,11 @@
204721 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
204722 pParse->iErr = i;
204723 return -1;
204724 }
204725 for(j=i+1;;j++){
204726 x = jsonTranslateTextToBlob(pParse, j);
204727 if( x<=0 ){
204728 if( x==(-3) ){
204729 j = pParse->iErr;
204730 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
204731 break;
@@ -204601,11 +204745,11 @@
204745 continue;
204746 }else if( z[j]==']' ){
204747 break;
204748 }
204749 }
204750 x = jsonTranslateTextToBlob(pParse, j);
204751 if( x==(-4) ){
204752 j = pParse->iErr;
204753 continue;
204754 }
204755 if( x==(-3) ){
@@ -204924,11 +205068,11 @@
205068 JsonParse *pParse, /* Initialize and fill this JsonParse object */
205069 sqlite3_context *pCtx /* Report errors here */
205070 ){
205071 int i;
205072 const char *zJson = pParse->zJson;
205073 i = jsonTranslateTextToBlob(pParse, 0);
205074 if( pParse->oom ) i = -1;
205075 if( i>0 ){
205076 #ifdef SQLITE_DEBUG
205077 assert( pParse->iDepth==0 );
205078 if( sqlite3Config.bJsonSelfcheck ){
@@ -204969,11 +205113,11 @@
205113 JsonParse px;
205114 memset(&px, 0, sizeof(px));
205115 jsonStringTerminate(pStr);
205116 px.zJson = pStr->zBuf;
205117 px.nJson = pStr->nUsed;
205118 (void)jsonTranslateTextToBlob(&px, 0);
205119 if( px.oom ){
205120 sqlite3_free(px.aBlob);
205121 sqlite3_result_error_nomem(pStr->pCtx);
205122 }else{
205123 assert( px.nBlobAlloc>0 );
@@ -205057,11 +205201,11 @@
205201 ** are detected. So a malformed JSONB input might either result
205202 ** in an error, or in incorrect JSON.
205203 **
205204 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
205205 */
205206 static u32 jsonTranslateBlobToText(
205207 const JsonParse *pParse, /* the complete parse of the JSON */
205208 u32 i, /* Start rendering at this index */
205209 JsonString *pOut /* Write JSON here */
205210 ){
205211 u32 sz, n, j, iEnd;
@@ -205228,11 +205372,11 @@
205372 case JSONB_ARRAY: {
205373 jsonAppendChar(pOut, '[');
205374 j = i+n;
205375 iEnd = j+sz;
205376 while( j<iEnd ){
205377 j = jsonTranslateBlobToText(pParse, j, pOut);
205378 jsonAppendChar(pOut, ',');
205379 }
205380 if( sz>0 ) pOut->nUsed--;
205381 jsonAppendChar(pOut, ']');
205382 break;
@@ -205241,11 +205385,11 @@
205385 int x = 0;
205386 jsonAppendChar(pOut, '{');
205387 j = i+n;
205388 iEnd = j+sz;
205389 while( j<iEnd ){
205390 j = jsonTranslateBlobToText(pParse, j, pOut);
205391 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
205392 }
205393 if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED;
205394 if( sz>0 ) pOut->nUsed--;
205395 jsonAppendChar(pOut, '}');
@@ -205394,23 +205538,27 @@
205538
205539 /*
205540 ** Input z[0..n] defines JSON escape sequence including the leading '\\'.
205541 ** Decode that escape sequence into a single character. Write that
205542 ** character into *piOut. Return the number of bytes in the escape sequence.
205543 **
205544 ** If there is a syntax error of some kind (for example too few characters
205545 ** after the '\\' to complete the encoding) then *piOut is set to
205546 ** JSON_INVALID_CHAR.
205547 */
205548 static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
205549 assert( n>0 );
205550 assert( z[0]=='\\' );
205551 if( n<2 ){
205552 *piOut = JSON_INVALID_CHAR;
205553 return n;
205554 }
205555 switch( (u8)z[1] ){
205556 case 'u': {
205557 u32 v, vlo;
205558 if( n<6 ){
205559 *piOut = JSON_INVALID_CHAR;
205560 return n;
205561 }
205562 v = jsonHexToInt4(&z[2]);
205563 if( (v & 0xfc00)==0xd800
205564 && n>=12
@@ -205436,11 +205584,11 @@
205584 case '"':
205585 case '/':
205586 case '\\':{ *piOut = z[1]; return 2; }
205587 case 'x': {
205588 if( n<4 ){
205589 *piOut = JSON_INVALID_CHAR;
205590 return n;
205591 }
205592 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
205593 return 4;
205594 }
@@ -205447,11 +205595,11 @@
205595 case 0xe2:
205596 case '\r':
205597 case '\n': {
205598 u32 nSkip = jsonBytesToBypass(z, n);
205599 if( nSkip==0 ){
205600 *piOut = JSON_INVALID_CHAR;
205601 return n;
205602 }else if( nSkip==n ){
205603 *piOut = 0;
205604 return n;
205605 }else if( z[nSkip]=='\\' ){
@@ -205460,11 +205608,11 @@
205608 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
205609 return nSkip + sz;
205610 }
205611 }
205612 default: {
205613 *piOut = JSON_INVALID_CHAR;
205614 return 2;
205615 }
205616 }
205617 }
205618
@@ -205823,11 +205971,11 @@
205971 if( NEVER(aBlob==0) ) return;
205972 memset(&x, 0, sizeof(x));
205973 x.aBlob = (u8*)aBlob;
205974 x.nBlob = nBlob;
205975 jsonStringInit(&s, ctx);
205976 jsonTranslateBlobToText(&x, 0, &s);
205977 jsonReturnString(&s, 0, 0);
205978 }
205979
205980
205981 /*
@@ -205934,21 +206082,21 @@
206082 if( c=='\\' ){
206083 u32 v;
206084 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
206085 if( v<=0x7f ){
206086 zOut[iOut++] = (char)v;
 
 
206087 }else if( v<=0x7ff ){
206088 assert( szEscape>=2 );
206089 zOut[iOut++] = (char)(0xc0 | (v>>6));
206090 zOut[iOut++] = 0x80 | (v&0x3f);
206091 }else if( v<0x10000 ){
206092 assert( szEscape>=3 );
206093 zOut[iOut++] = 0xe0 | (v>>12);
206094 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
206095 zOut[iOut++] = 0x80 | (v&0x3f);
206096 }else if( v==JSON_INVALID_CHAR ){
206097 /* Silently ignore illegal unicode */
206098 }else{
206099 assert( szEscape>=4 );
206100 zOut[iOut++] = 0xf0 | (v>>18);
206101 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
206102 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
@@ -206046,17 +206194,33 @@
206194 }else{
206195 jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson);
206196 }
206197 break;
206198 }
206199 case SQLITE_FLOAT: {
206200 double r = sqlite3_value_double(pArg);
206201 if( NEVER(sqlite3IsNaN(r)) ){
206202 jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0);
206203 }else{
206204 int n = sqlite3_value_bytes(pArg);
206205 const char *z = (const char*)sqlite3_value_text(pArg);
206206 if( z==0 ) return 1;
206207 if( z[0]=='I' ){
206208 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
206209 }else if( z[0]=='-' && z[1]=='I' ){
206210 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
206211 }else{
206212 jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z);
206213 }
206214 }
206215 break;
206216 }
206217 case SQLITE_INTEGER: {
206218 int n = sqlite3_value_bytes(pArg);
206219 const char *z = (const char*)sqlite3_value_text(pArg);
 
206220 if( z==0 ) return 1;
206221 jsonBlobAppendNode(pParse, JSONB_INT, n, z);
206222 break;
206223 }
206224 }
206225 if( pParse->oom ){
206226 sqlite3_result_error_nomem(ctx);
@@ -206153,15 +206317,10 @@
206317 }else{
206318 jsonBadPathError(ctx, zPath);
206319 }
206320 return;
206321 }
 
 
 
 
 
206322
206323 /*
206324 ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
206325 ** from the SQL function argument pArg. Return a pointer to the new
206326 ** JsonParse object.
@@ -206313,11 +206472,12 @@
206472 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT);
206473 }
206474 }else{
206475 JsonString s;
206476 jsonStringInit(&s, ctx);
206477 p->delta = 0;
206478 jsonTranslateBlobToText(p, 0, &s);
206479 jsonReturnString(&s, p, ctx);
206480 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206481 }
206482 }
206483
@@ -206333,29 +206493,32 @@
206493 */
206494 static void jsonDebugPrintBlob(
206495 JsonParse *pParse, /* JSON content */
206496 u32 iStart, /* Start rendering here */
206497 u32 iEnd, /* Do not render this byte or any byte after this one */
206498 int nIndent, /* Indent by this many spaces */
206499 sqlite3_str *pOut /* Generate output into this sqlite3_str object */
206500 ){
206501 while( iStart<iEnd ){
206502 u32 i, n, nn, sz = 0;
206503 int showContent = 1;
206504 u8 x = pParse->aBlob[iStart] & 0x0f;
206505 u32 savedNBlob = pParse->nBlob;
206506 sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
206507 if( pParse->nBlobAlloc>pParse->nBlob ){
206508 pParse->nBlob = pParse->nBlobAlloc;
206509 }
206510 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
206511 if( nn==0 ) nn = 1;
206512 if( sz>0 && x<JSONB_ARRAY ){
206513 nn += sz;
206514 }
206515 for(i=0; i<nn; i++){
206516 sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
206517 }
206518 if( n==0 ){
206519 sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
206520 iStart = n==0 ? iStart+1 : iEnd;
206521 continue;
206522 }
206523 pParse->nBlob = savedNBlob;
206524 if( iStart+n+sz>iEnd ){
@@ -206366,59 +206529,61 @@
206529 }else{
206530 iEnd = pParse->nBlob;
206531 }
206532 }
206533 }
206534 sqlite3_str_appendall(pOut," <-- ");
206535 switch( x ){
206536 case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break;
206537 case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break;
206538 case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break;
206539 case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break;
206540 case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break;
206541 case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break;
206542 case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break;
206543 case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break;
206544 case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break;
206545 case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break;
206546 case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break;
206547 case JSONB_ARRAY: {
206548 sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
206549 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
206550 showContent = 0;
206551 break;
206552 }
206553 case JSONB_OBJECT: {
206554 sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
206555 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
206556 showContent = 0;
206557 break;
206558 }
206559 default: {
206560 sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
206561 showContent = 0;
206562 break;
206563 }
206564 }
206565 if( showContent ){
206566 if( sz==0 && x<=JSONB_FALSE ){
206567 sqlite3_str_append(pOut, "\n", 1);
206568 }else{
206569 u32 i;
206570 sqlite3_str_appendall(pOut, ": \"");
206571 for(i=iStart+n; i<iStart+n+sz; i++){
206572 u8 c = pParse->aBlob[i];
206573 if( c<0x20 || c>=0x7f ) c = '.';
206574 sqlite3_str_append(pOut, (char*)&c, 1);
206575 }
206576 sqlite3_str_append(pOut, "\"\n", 2);
206577 }
206578 }
206579 iStart += n + sz;
206580 }
206581 }
206582 static void jsonShowParse(JsonParse *pParse){
206583 sqlite3_str out;
206584 char zBuf[1000];
206585 if( pParse==0 ){
206586 printf("NULL pointer\n");
206587 return;
206588 }else{
206589 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
@@ -206425,31 +206590,42 @@
206590 printf("nBlob = %u\n", pParse->nBlob);
206591 printf("delta = %d\n", pParse->delta);
206592 if( pParse->nBlob==0 ) return;
206593 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
206594 }
206595 sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
206596 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
206597 printf("%s", sqlite3_str_value(&out));
206598 sqlite3_str_reset(&out);
206599 }
206600 #endif /* SQLITE_DEBUG */
206601
206602 #ifdef SQLITE_DEBUG
206603 /*
206604 ** SQL function: json_parse(JSON)
206605 **
206606 ** Parse JSON using jsonParseFuncArg(). Return text that is a
206607 ** human-readable dump of the binary JSONB for the input parameter.
206608 */
206609 static void jsonParseFunc(
206610 sqlite3_context *ctx,
206611 int argc,
206612 sqlite3_value **argv
206613 ){
206614 JsonParse *p; /* The parse */
206615 sqlite3_str out;
206616
206617 assert( argc>=1 );
206618 sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
206619 p = jsonParseFuncArg(ctx, argv[0], 0);
206620 if( p==0 ) return;
206621 if( argc==1 ){
206622 jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
206623 sqlite3_result_text64(ctx, out.zText, out.nChar, sqlite3_free, SQLITE_UTF8);
206624 }else{
206625 jsonShowParse(p);
206626 }
206627 jsonParseFree(p);
206628 }
206629 #endif /* SQLITE_DEBUG */
206630
206631 /****************************************************************************
@@ -206641,11 +206817,11 @@
206817 }
206818 if( j<p->nBlob ){
206819 if( argc==2 ){
206820 if( flags & JSON_JSON ){
206821 jsonStringInit(&jx, ctx);
206822 jsonTranslateBlobToText(p, j, &jx);
206823 jsonReturnString(&jx, 0, 0);
206824 jsonStringReset(&jx);
206825 assert( (flags & JSON_BLOB)==0 );
206826 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206827 }else{
@@ -206656,11 +206832,11 @@
206832 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
206833 }
206834 }
206835 }else{
206836 jsonAppendSeparator(&jx);
206837 jsonTranslateBlobToText(p, j, &jx);
206838 }
206839 }else if( j==JSON_LOOKUP_NOTFOUND ){
206840 if( argc==2 ){
206841 goto json_extract_error; /* Return NULL if not found */
206842 }else{
@@ -229267,23 +229443,28 @@
229443 **
229444 ** This function may be quite inefficient if used with an FTS5 table
229445 ** created with the "columnsize=0" option.
229446 **
229447 ** xColumnText:
229448 ** If parameter iCol is less than zero, or greater than or equal to the
229449 ** number of columns in the table, SQLITE_RANGE is returned.
229450 **
229451 ** Otherwise, this function attempts to retrieve the text of column iCol of
229452 ** the current document. If successful, (*pz) is set to point to a buffer
229453 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
229454 ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
229455 ** if an error occurs, an SQLite error code is returned and the final values
229456 ** of (*pz) and (*pn) are undefined.
229457 **
229458 ** xPhraseCount:
229459 ** Returns the number of phrases in the current query expression.
229460 **
229461 ** xPhraseSize:
229462 ** If parameter iCol is less than zero, or greater than or equal to the
229463 ** number of phrases in the current query, as returned by xPhraseCount,
229464 ** 0 is returned. Otherwise, this function returns the number of tokens in
229465 ** phrase iPhrase of the query. Phrases are numbered starting from zero.
229466 **
229467 ** xInstCount:
229468 ** Set *pnInst to the total number of occurrences of all phrases within
229469 ** the query within the current row. Return SQLITE_OK if successful, or
229470 ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -229295,16 +229476,17 @@
229476 **
229477 ** xInst:
229478 ** Query for the details of phrase match iIdx within the current row.
229479 ** Phrase matches are numbered starting from zero, so the iIdx argument
229480 ** should be greater than or equal to zero and smaller than the value
229481 ** output by xInstCount(). If iIdx is less than zero or greater than
229482 ** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
229483 **
229484 ** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
229485 ** to the column in which it occurs and *piOff the token offset of the
229486 ** first token of the phrase. SQLITE_OK is returned if successful, or an
229487 ** error code (i.e. SQLITE_NOMEM) if an error occurs.
229488 **
229489 ** This API can be quite slow if used with an FTS5 table created with the
229490 ** "detail=none" or "detail=column" option.
229491 **
229492 ** xRowid:
@@ -229325,10 +229507,14 @@
229507 ** row visited, the callback function passed as the fourth argument
229508 ** is invoked. The context and API objects passed to the callback
229509 ** function may be used to access the properties of each matched row.
229510 ** Invoking Api.xUserData() returns a copy of the pointer passed as
229511 ** the third argument to pUserData.
229512 **
229513 ** If parameter iPhrase is less than zero, or greater than or equal to
229514 ** the number of phrases in the query, as returned by xPhraseCount(),
229515 ** this function returns SQLITE_RANGE.
229516 **
229517 ** If the callback function returns any value other than SQLITE_OK, the
229518 ** query is abandoned and the xQueryPhrase function returns immediately.
229519 ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
229520 ** Otherwise, the error code is propagated upwards.
@@ -229446,22 +229632,30 @@
229632 ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
229633 ** This is used to access token iToken of phrase iPhrase of the current
229634 ** query. Before returning, output parameter *ppToken is set to point
229635 ** to a buffer containing the requested token, and *pnToken to the
229636 ** size of this buffer in bytes.
229637 **
229638 ** If iPhrase or iToken are less than zero, or if iPhrase is greater than
229639 ** or equal to the number of phrases in the query as reported by
229640 ** xPhraseCount(), or if iToken is equal to or greater than the number of
229641 ** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
229642 are both zeroed.
229643 **
229644 ** The output text is not a copy of the query text that specified the
229645 ** token. It is the output of the tokenizer module. For tokendata=1
229646 ** tables, this includes any embedded 0x00 and trailing data.
229647 **
229648 ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
229649 ** This is used to access token iToken of phrase hit iIdx within the
229650 ** current row. If iIdx is less than zero or greater than or equal to the
229651 ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
229652 ** output variable (*ppToken) is set to point to a buffer containing the
229653 ** matching document token, and (*pnToken) to the size of that buffer in
229654 ** bytes. This API is not available if the specified token matches a
229655 ** prefix query term. In that case both output variables are always set
229656 ** to 0.
229657 **
229658 ** The output text is not a copy of the document text that was tokenized.
229659 ** It is the output of the tokenizer module. For tokendata=1 tables, this
229660 ** includes any embedded 0x00 and trailing data.
229661 **
@@ -232433,12 +232627,14 @@
232627 memset(&ctx, 0, sizeof(HighlightContext));
232628 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
232629 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
232630 ctx.iRangeEnd = -1;
232631 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
232632 if( rc==SQLITE_RANGE ){
232633 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
232634 rc = SQLITE_OK;
232635 }else if( ctx.zIn ){
232636 if( rc==SQLITE_OK ){
232637 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
232638 }
232639
232640 if( rc==SQLITE_OK ){
@@ -236273,15 +236469,19 @@
236469 Fts5Expr *pExpr,
236470 int iPhrase,
236471 Fts5Expr **ppNew
236472 ){
236473 int rc = SQLITE_OK; /* Return code */
236474 Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */
236475 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
236476 TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
236477 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
236478 rc = SQLITE_RANGE;
236479 }else{
236480 pOrig = pExpr->apExprPhrase[iPhrase];
236481 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
236482 }
236483 if( rc==SQLITE_OK ){
236484 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
236485 sizeof(Fts5ExprPhrase*));
236486 }
236487 if( rc==SQLITE_OK ){
@@ -236290,11 +236490,11 @@
236490 }
236491 if( rc==SQLITE_OK ){
236492 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
236493 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
236494 }
236495 if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){
236496 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
236497 if( pColsetOrig ){
236498 sqlite3_int64 nByte;
236499 Fts5Colset *pColset;
236500 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
@@ -236304,29 +236504,31 @@
236504 }
236505 pNew->pRoot->pNear->pColset = pColset;
236506 }
236507 }
236508
236509 if( rc==SQLITE_OK ){
236510 if( pOrig->nTerm ){
236511 int i; /* Used to iterate through phrase terms */
236512 sCtx.pConfig = pExpr->pConfig;
236513 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
236514 int tflags = 0;
236515 Fts5ExprTerm *p;
236516 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
236517 rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0);
236518 tflags = FTS5_TOKEN_COLOCATED;
236519 }
236520 if( rc==SQLITE_OK ){
236521 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
236522 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
236523 }
236524 }
236525 }else{
236526 /* This happens when parsing a token or quoted phrase that contains
236527 ** no token characters at all. (e.g ... MATCH '""'). */
236528 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
236529 }
236530 }
236531
236532 if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
236533 /* All the allocations succeeded. Put the expression object together. */
236534 pNew->pIndex = pExpr->pIndex;
@@ -239776,13 +239978,13 @@
239978 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
239979 if( pData->p[iOff] ) break;
239980 }
239981
239982 if( iOff<pData->nn ){
239983 u64 iVal;
239984 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
239985 iOff += fts5GetVarint(&pData->p[iOff], &iVal);
239986 pLvl->iRowid += iVal;
239987 pLvl->iOff = iOff;
239988 }else{
239989 pLvl->bEof = 1;
239990 }
@@ -246173,20 +246375,20 @@
246375 fts5DataRelease(pLeaf);
246376 }
246377 }
246378
246379 static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
246380 i64 iTermOff = 0;
246381 int ii;
246382
246383 Fts5Buffer buf1 = {0,0,0};
246384 Fts5Buffer buf2 = {0,0,0};
246385
246386 ii = pLeaf->szLeaf;
246387 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
246388 int res;
246389 i64 iOff;
246390 int nIncr;
246391
246392 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
246393 iTermOff += nIncr;
246394 iOff = iTermOff;
@@ -249207,11 +249409,14 @@
249409 const char **pz,
249410 int *pn
249411 ){
249412 int rc = SQLITE_OK;
249413 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
249414 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
249415 if( iCol<0 || iCol>=pTab->pConfig->nCol ){
249416 rc = SQLITE_RANGE;
249417 }else if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
249418 || pCsr->ePlan==FTS5_PLAN_SPECIAL
249419 ){
249420 *pz = 0;
249421 *pn = 0;
249422 }else{
@@ -249232,12 +249437,13 @@
249437 ){
249438 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
249439 int rc = SQLITE_OK;
249440 int bLive = (pCsr->pSorter==0);
249441
249442 if( iPhrase<0 || iPhrase>=sqlite3Fts5ExprPhraseCount(pCsr->pExpr) ){
249443 rc = SQLITE_RANGE;
249444 }else if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
249445 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
249446 Fts5PoslistPopulator *aPopulator;
249447 int i;
249448 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
249449 if( aPopulator==0 ) rc = SQLITE_NOMEM;
@@ -249257,18 +249463,24 @@
249463 }
249464 }
249465 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
249466 }
249467
249468 if( rc==SQLITE_OK ){
249469 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
249470 Fts5Sorter *pSorter = pCsr->pSorter;
249471 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
249472 *pn = pSorter->aIdx[iPhrase] - i1;
249473 *pa = &pSorter->aPoslist[i1];
249474 }else{
249475 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
249476 }
249477 }else{
249478 *pa = 0;
249479 *pn = 0;
249480 }
249481
249482
249483 return rc;
249484 }
249485
249486 /*
@@ -250223,11 +250435,11 @@
250435 int nArg, /* Number of args */
250436 sqlite3_value **apUnused /* Function arguments */
250437 ){
250438 assert( nArg==0 );
250439 UNUSED_PARAM2(nArg, apUnused);
250440 sqlite3_result_text(pCtx, "fts5: 2023-12-29 19:03:01 4b70b94616ef37bac969051eee3ea6913a28f30520cdd4fc3a19e848f2cf12b7", -1, SQLITE_TRANSIENT);
250441 }
250442
250443 /*
250444 ** Return true if zName is the extension on one of the shadow tables used
250445 ** by this module.
250446
+37 -17
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.45.0"
150150
#define SQLITE_VERSION_NUMBER 3045000
151
-#define SQLITE_SOURCE_ID "2023-12-14 16:34:47 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9"
151
+#define SQLITE_SOURCE_ID "2024-01-01 19:20:00 bcac937526d9a6ef914a74b4d6757fa91cd74edab871bcd934fde4a2f9b6debd"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -8037,13 +8037,15 @@
80378037
** can enter.)^ If the same thread tries to enter any mutex other
80388038
** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
80398039
**
80408040
** ^(Some systems (for example, Windows 95) do not support the operation
80418041
** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8042
-** will always return SQLITE_BUSY. The SQLite core only ever uses
8043
-** sqlite3_mutex_try() as an optimization so this is acceptable
8044
-** behavior.)^
8042
+** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8043
+** sqlite3_mutex_try() as an optimization, so this is acceptable
8044
+** behavior. The exceptions are unix builds that set the
8045
+** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8046
+** sqlite3_mutex_try() is required.)^
80458047
**
80468048
** ^The sqlite3_mutex_leave() routine exits a mutex that was
80478049
** previously entered by the same thread. The behavior
80488050
** is undefined if the mutex is not currently entered by the
80498051
** calling thread or is not currently allocated.
@@ -12812,23 +12814,28 @@
1281212814
**
1281312815
** This function may be quite inefficient if used with an FTS5 table
1281412816
** created with the "columnsize=0" option.
1281512817
**
1281612818
** xColumnText:
12817
-** This function attempts to retrieve the text of column iCol of the
12818
-** current document. If successful, (*pz) is set to point to a buffer
12819
+** If parameter iCol is less than zero, or greater than or equal to the
12820
+** number of columns in the table, SQLITE_RANGE is returned.
12821
+**
12822
+** Otherwise, this function attempts to retrieve the text of column iCol of
12823
+** the current document. If successful, (*pz) is set to point to a buffer
1281912824
** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
1282012825
** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
1282112826
** if an error occurs, an SQLite error code is returned and the final values
1282212827
** of (*pz) and (*pn) are undefined.
1282312828
**
1282412829
** xPhraseCount:
1282512830
** Returns the number of phrases in the current query expression.
1282612831
**
1282712832
** xPhraseSize:
12828
-** Returns the number of tokens in phrase iPhrase of the query. Phrases
12829
-** are numbered starting from zero.
12833
+** If parameter iCol is less than zero, or greater than or equal to the
12834
+** number of phrases in the current query, as returned by xPhraseCount,
12835
+** 0 is returned. Otherwise, this function returns the number of tokens in
12836
+** phrase iPhrase of the query. Phrases are numbered starting from zero.
1283012837
**
1283112838
** xInstCount:
1283212839
** Set *pnInst to the total number of occurrences of all phrases within
1283312840
** the query within the current row. Return SQLITE_OK if successful, or
1283412841
** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -12840,16 +12847,17 @@
1284012847
**
1284112848
** xInst:
1284212849
** Query for the details of phrase match iIdx within the current row.
1284312850
** Phrase matches are numbered starting from zero, so the iIdx argument
1284412851
** should be greater than or equal to zero and smaller than the value
12845
-** output by xInstCount().
12852
+** output by xInstCount(). If iIdx is less than zero or greater than
12853
+** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
1284612854
**
12847
-** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12855
+** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
1284812856
** to the column in which it occurs and *piOff the token offset of the
12849
-** first token of the phrase. Returns SQLITE_OK if successful, or an error
12850
-** code (i.e. SQLITE_NOMEM) if an error occurs.
12857
+** first token of the phrase. SQLITE_OK is returned if successful, or an
12858
+** error code (i.e. SQLITE_NOMEM) if an error occurs.
1285112859
**
1285212860
** This API can be quite slow if used with an FTS5 table created with the
1285312861
** "detail=none" or "detail=column" option.
1285412862
**
1285512863
** xRowid:
@@ -12870,10 +12878,14 @@
1287012878
** row visited, the callback function passed as the fourth argument
1287112879
** is invoked. The context and API objects passed to the callback
1287212880
** function may be used to access the properties of each matched row.
1287312881
** Invoking Api.xUserData() returns a copy of the pointer passed as
1287412882
** the third argument to pUserData.
12883
+**
12884
+** If parameter iPhrase is less than zero, or greater than or equal to
12885
+** the number of phrases in the query, as returned by xPhraseCount(),
12886
+** this function returns SQLITE_RANGE.
1287512887
**
1287612888
** If the callback function returns any value other than SQLITE_OK, the
1287712889
** query is abandoned and the xQueryPhrase function returns immediately.
1287812890
** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
1287912891
** Otherwise, the error code is propagated upwards.
@@ -12991,22 +13003,30 @@
1299113003
** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
1299213004
** This is used to access token iToken of phrase iPhrase of the current
1299313005
** query. Before returning, output parameter *ppToken is set to point
1299413006
** to a buffer containing the requested token, and *pnToken to the
1299513007
** size of this buffer in bytes.
13008
+**
13009
+** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13010
+** or equal to the number of phrases in the query as reported by
13011
+** xPhraseCount(), or if iToken is equal to or greater than the number of
13012
+** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13013
+ are both zeroed.
1299613014
**
1299713015
** The output text is not a copy of the query text that specified the
1299813016
** token. It is the output of the tokenizer module. For tokendata=1
1299913017
** tables, this includes any embedded 0x00 and trailing data.
1300013018
**
1300113019
** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
1300213020
** This is used to access token iToken of phrase hit iIdx within the
13003
-** current row. Output variable (*ppToken) is set to point to a buffer
13004
-** containing the matching document token, and (*pnToken) to the size
13005
-** of that buffer in bytes. This API is not available if the specified
13006
-** token matches a prefix query term. In that case both output variables
13007
-** are always set to 0.
13021
+** current row. If iIdx is less than zero or greater than or equal to the
13022
+** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13023
+** output variable (*ppToken) is set to point to a buffer containing the
13024
+** matching document token, and (*pnToken) to the size of that buffer in
13025
+** bytes. This API is not available if the specified token matches a
13026
+** prefix query term. In that case both output variables are always set
13027
+** to 0.
1300813028
**
1300913029
** The output text is not a copy of the document text that was tokenized.
1301013030
** It is the output of the tokenizer module. For tokendata=1 tables, this
1301113031
** includes any embedded 0x00 and trailing data.
1301213032
**
1301313033
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.45.0"
150 #define SQLITE_VERSION_NUMBER 3045000
151 #define SQLITE_SOURCE_ID "2023-12-14 16:34:47 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -8037,13 +8037,15 @@
8037 ** can enter.)^ If the same thread tries to enter any mutex other
8038 ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8039 **
8040 ** ^(Some systems (for example, Windows 95) do not support the operation
8041 ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8042 ** will always return SQLITE_BUSY. The SQLite core only ever uses
8043 ** sqlite3_mutex_try() as an optimization so this is acceptable
8044 ** behavior.)^
 
 
8045 **
8046 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
8047 ** previously entered by the same thread. The behavior
8048 ** is undefined if the mutex is not currently entered by the
8049 ** calling thread or is not currently allocated.
@@ -12812,23 +12814,28 @@
12812 **
12813 ** This function may be quite inefficient if used with an FTS5 table
12814 ** created with the "columnsize=0" option.
12815 **
12816 ** xColumnText:
12817 ** This function attempts to retrieve the text of column iCol of the
12818 ** current document. If successful, (*pz) is set to point to a buffer
 
 
 
12819 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12820 ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12821 ** if an error occurs, an SQLite error code is returned and the final values
12822 ** of (*pz) and (*pn) are undefined.
12823 **
12824 ** xPhraseCount:
12825 ** Returns the number of phrases in the current query expression.
12826 **
12827 ** xPhraseSize:
12828 ** Returns the number of tokens in phrase iPhrase of the query. Phrases
12829 ** are numbered starting from zero.
 
 
12830 **
12831 ** xInstCount:
12832 ** Set *pnInst to the total number of occurrences of all phrases within
12833 ** the query within the current row. Return SQLITE_OK if successful, or
12834 ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -12840,16 +12847,17 @@
12840 **
12841 ** xInst:
12842 ** Query for the details of phrase match iIdx within the current row.
12843 ** Phrase matches are numbered starting from zero, so the iIdx argument
12844 ** should be greater than or equal to zero and smaller than the value
12845 ** output by xInstCount().
 
12846 **
12847 ** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12848 ** to the column in which it occurs and *piOff the token offset of the
12849 ** first token of the phrase. Returns SQLITE_OK if successful, or an error
12850 ** code (i.e. SQLITE_NOMEM) if an error occurs.
12851 **
12852 ** This API can be quite slow if used with an FTS5 table created with the
12853 ** "detail=none" or "detail=column" option.
12854 **
12855 ** xRowid:
@@ -12870,10 +12878,14 @@
12870 ** row visited, the callback function passed as the fourth argument
12871 ** is invoked. The context and API objects passed to the callback
12872 ** function may be used to access the properties of each matched row.
12873 ** Invoking Api.xUserData() returns a copy of the pointer passed as
12874 ** the third argument to pUserData.
 
 
 
 
12875 **
12876 ** If the callback function returns any value other than SQLITE_OK, the
12877 ** query is abandoned and the xQueryPhrase function returns immediately.
12878 ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12879 ** Otherwise, the error code is propagated upwards.
@@ -12991,22 +13003,30 @@
12991 ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
12992 ** This is used to access token iToken of phrase iPhrase of the current
12993 ** query. Before returning, output parameter *ppToken is set to point
12994 ** to a buffer containing the requested token, and *pnToken to the
12995 ** size of this buffer in bytes.
 
 
 
 
 
 
12996 **
12997 ** The output text is not a copy of the query text that specified the
12998 ** token. It is the output of the tokenizer module. For tokendata=1
12999 ** tables, this includes any embedded 0x00 and trailing data.
13000 **
13001 ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13002 ** This is used to access token iToken of phrase hit iIdx within the
13003 ** current row. Output variable (*ppToken) is set to point to a buffer
13004 ** containing the matching document token, and (*pnToken) to the size
13005 ** of that buffer in bytes. This API is not available if the specified
13006 ** token matches a prefix query term. In that case both output variables
13007 ** are always set to 0.
 
 
13008 **
13009 ** The output text is not a copy of the document text that was tokenized.
13010 ** It is the output of the tokenizer module. For tokendata=1 tables, this
13011 ** includes any embedded 0x00 and trailing data.
13012 **
13013
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.45.0"
150 #define SQLITE_VERSION_NUMBER 3045000
151 #define SQLITE_SOURCE_ID "2024-01-01 19:20:00 bcac937526d9a6ef914a74b4d6757fa91cd74edab871bcd934fde4a2f9b6debd"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -8037,13 +8037,15 @@
8037 ** can enter.)^ If the same thread tries to enter any mutex other
8038 ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8039 **
8040 ** ^(Some systems (for example, Windows 95) do not support the operation
8041 ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8042 ** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8043 ** sqlite3_mutex_try() as an optimization, so this is acceptable
8044 ** behavior. The exceptions are unix builds that set the
8045 ** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8046 ** sqlite3_mutex_try() is required.)^
8047 **
8048 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
8049 ** previously entered by the same thread. The behavior
8050 ** is undefined if the mutex is not currently entered by the
8051 ** calling thread or is not currently allocated.
@@ -12812,23 +12814,28 @@
12814 **
12815 ** This function may be quite inefficient if used with an FTS5 table
12816 ** created with the "columnsize=0" option.
12817 **
12818 ** xColumnText:
12819 ** If parameter iCol is less than zero, or greater than or equal to the
12820 ** number of columns in the table, SQLITE_RANGE is returned.
12821 **
12822 ** Otherwise, this function attempts to retrieve the text of column iCol of
12823 ** the current document. If successful, (*pz) is set to point to a buffer
12824 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12825 ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12826 ** if an error occurs, an SQLite error code is returned and the final values
12827 ** of (*pz) and (*pn) are undefined.
12828 **
12829 ** xPhraseCount:
12830 ** Returns the number of phrases in the current query expression.
12831 **
12832 ** xPhraseSize:
12833 ** If parameter iCol is less than zero, or greater than or equal to the
12834 ** number of phrases in the current query, as returned by xPhraseCount,
12835 ** 0 is returned. Otherwise, this function returns the number of tokens in
12836 ** phrase iPhrase of the query. Phrases are numbered starting from zero.
12837 **
12838 ** xInstCount:
12839 ** Set *pnInst to the total number of occurrences of all phrases within
12840 ** the query within the current row. Return SQLITE_OK if successful, or
12841 ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
@@ -12840,16 +12847,17 @@
12847 **
12848 ** xInst:
12849 ** Query for the details of phrase match iIdx within the current row.
12850 ** Phrase matches are numbered starting from zero, so the iIdx argument
12851 ** should be greater than or equal to zero and smaller than the value
12852 ** output by xInstCount(). If iIdx is less than zero or greater than
12853 ** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
12854 **
12855 ** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
12856 ** to the column in which it occurs and *piOff the token offset of the
12857 ** first token of the phrase. SQLITE_OK is returned if successful, or an
12858 ** error code (i.e. SQLITE_NOMEM) if an error occurs.
12859 **
12860 ** This API can be quite slow if used with an FTS5 table created with the
12861 ** "detail=none" or "detail=column" option.
12862 **
12863 ** xRowid:
@@ -12870,10 +12878,14 @@
12878 ** row visited, the callback function passed as the fourth argument
12879 ** is invoked. The context and API objects passed to the callback
12880 ** function may be used to access the properties of each matched row.
12881 ** Invoking Api.xUserData() returns a copy of the pointer passed as
12882 ** the third argument to pUserData.
12883 **
12884 ** If parameter iPhrase is less than zero, or greater than or equal to
12885 ** the number of phrases in the query, as returned by xPhraseCount(),
12886 ** this function returns SQLITE_RANGE.
12887 **
12888 ** If the callback function returns any value other than SQLITE_OK, the
12889 ** query is abandoned and the xQueryPhrase function returns immediately.
12890 ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12891 ** Otherwise, the error code is propagated upwards.
@@ -12991,22 +13003,30 @@
13003 ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13004 ** This is used to access token iToken of phrase iPhrase of the current
13005 ** query. Before returning, output parameter *ppToken is set to point
13006 ** to a buffer containing the requested token, and *pnToken to the
13007 ** size of this buffer in bytes.
13008 **
13009 ** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13010 ** or equal to the number of phrases in the query as reported by
13011 ** xPhraseCount(), or if iToken is equal to or greater than the number of
13012 ** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13013 are both zeroed.
13014 **
13015 ** The output text is not a copy of the query text that specified the
13016 ** token. It is the output of the tokenizer module. For tokendata=1
13017 ** tables, this includes any embedded 0x00 and trailing data.
13018 **
13019 ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13020 ** This is used to access token iToken of phrase hit iIdx within the
13021 ** current row. If iIdx is less than zero or greater than or equal to the
13022 ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13023 ** output variable (*ppToken) is set to point to a buffer containing the
13024 ** matching document token, and (*pnToken) to the size of that buffer in
13025 ** bytes. This API is not available if the specified token matches a
13026 ** prefix query term. In that case both output variables are always set
13027 ** to 0.
13028 **
13029 ** The output text is not a copy of the document text that was tokenized.
13030 ** It is the output of the tokenizer module. For tokendata=1 tables, this
13031 ** includes any embedded 0x00 and trailing data.
13032 **
13033

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button