Fossil SCM

Update the built-in SQLite to the latest trunk version, in order to beta-test the latest SQLite changes.

drh 2026-01-26 11:52 trunk
Commit 13c221af37242a59468605eb511f461ad31e18dcb418e4fd06ee9f31e5c5999a
3 files changed +35 -27 +606 -528 +67 -12
+35 -27
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -13103,11 +13103,11 @@
1310313103
nFile = (int)strlen(zFile)+1;
1310413104
}
1310513105
1310613106
rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
1310713107
if( rc==SQLITE_OK ){
13108
- pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
13108
+ pNew = (ZipfileTab*)sqlite3_malloc64((i64)nByte+nFile);
1310913109
if( pNew==0 ) return SQLITE_NOMEM;
1311013110
memset(pNew, 0, nByte+nFile);
1311113111
pNew->db = db;
1311213112
pNew->aBuffer = (u8*)&pNew[1];
1311313113
if( zFile ){
@@ -13249,18 +13249,19 @@
1324913249
** sqlite3_free().
1325013250
*/
1325113251
static int zipfileReadData(
1325213252
FILE *pFile, /* Read from this file */
1325313253
u8 *aRead, /* Read into this buffer */
13254
- int nRead, /* Number of bytes to read */
13254
+ i64 nRead, /* Number of bytes to read */
1325513255
i64 iOff, /* Offset to read from */
1325613256
char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
1325713257
){
1325813258
size_t n;
1325913259
fseek(pFile, (long)iOff, SEEK_SET);
13260
- n = fread(aRead, 1, nRead, pFile);
13261
- if( (int)n!=nRead ){
13260
+ n = fread(aRead, 1, (long)nRead, pFile);
13261
+ if( n!=(size_t)nRead ){
13262
+ sqlite3_free(*pzErrmsg);
1326213263
*pzErrmsg = sqlite3_mprintf("error in fread()");
1326313264
return SQLITE_ERROR;
1326413265
}
1326513266
return SQLITE_OK;
1326613267
}
@@ -13273,11 +13274,11 @@
1327313274
if( nWrite>0 ){
1327413275
size_t n = nWrite;
1327513276
fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
1327613277
n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
1327713278
if( (int)n!=nWrite ){
13278
- pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
13279
+ zipfileTableErr(pTab,"error in fwrite()");
1327913280
return SQLITE_ERROR;
1328013281
}
1328113282
pTab->szCurrent += nWrite;
1328213283
}
1328313284
return SQLITE_OK;
@@ -13520,10 +13521,11 @@
1352013521
/*
1352113522
** Set (*pzErr) to point to a buffer from sqlite3_malloc() containing a
1352213523
** generic corruption message and return SQLITE_CORRUPT;
1352313524
*/
1352413525
static int zipfileCorrupt(char **pzErr){
13526
+ sqlite3_free(*pzErr);
1352513527
*pzErr = sqlite3_mprintf("zip archive is corrupt");
1352613528
return SQLITE_CORRUPT;
1352713529
}
1352813530
1352913531
/*
@@ -13538,11 +13540,11 @@
1353813540
** final value of (*ppEntry) undefined.
1353913541
*/
1354013542
static int zipfileGetEntry(
1354113543
ZipfileTab *pTab, /* Store any error message here */
1354213544
const u8 *aBlob, /* Pointer to in-memory file image */
13543
- int nBlob, /* Size of aBlob[] in bytes */
13545
+ i64 nBlob, /* Size of aBlob[] in bytes */
1354413546
FILE *pFile, /* If aBlob==0, read from this file */
1354513547
i64 iOff, /* Offset of CDS record */
1354613548
ZipfileEntry **ppEntry /* OUT: Pointer to new object */
1354713549
){
1354813550
u8 *aRead;
@@ -13578,11 +13580,11 @@
1357813580
rc = SQLITE_NOMEM;
1357913581
}else{
1358013582
memset(pNew, 0, sizeof(ZipfileEntry));
1358113583
rc = zipfileReadCDS(aRead, &pNew->cds);
1358213584
if( rc!=SQLITE_OK ){
13583
- *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
13585
+ zipfileTableErr(pTab, "failed to read CDS at offset %lld", iOff);
1358413586
}else if( aBlob==0 ){
1358513587
rc = zipfileReadData(
1358613588
pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
1358713589
);
1358813590
}else{
@@ -13610,19 +13612,19 @@
1361013612
ZipfileLFH lfh;
1361113613
if( pFile ){
1361213614
rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
1361313615
}else{
1361413616
aRead = (u8*)&aBlob[pNew->cds.iOffset];
13615
- if( (pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>(unsigned)nBlob ){
13617
+ if( ((i64)pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>nBlob ){
1361613618
rc = zipfileCorrupt(pzErr);
1361713619
}
1361813620
}
1361913621
1362013622
memset(&lfh, 0, sizeof(lfh));
1362113623
if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
1362213624
if( rc==SQLITE_OK ){
13623
- pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
13625
+ pNew->iDataOff = (i64)pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
1362413626
pNew->iDataOff += lfh.nFile + lfh.nExtra;
1362513627
if( aBlob && pNew->cds.szCompressed ){
1362613628
if( pNew->iDataOff + pNew->cds.szCompressed > nBlob ){
1362713629
rc = zipfileCorrupt(pzErr);
1362813630
}else{
@@ -13629,11 +13631,11 @@
1362913631
pNew->aData = &pNew->aExtra[nExtra];
1363013632
memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
1363113633
}
1363213634
}
1363313635
}else{
13634
- *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
13636
+ zipfileTableErr(pTab, "failed to read LFH at offset %d",
1363513637
(int)pNew->cds.iOffset
1363613638
);
1363713639
}
1363813640
}
1363913641
@@ -13653,11 +13655,11 @@
1365313655
static int zipfileNext(sqlite3_vtab_cursor *cur){
1365413656
ZipfileCsr *pCsr = (ZipfileCsr*)cur;
1365513657
int rc = SQLITE_OK;
1365613658
1365713659
if( pCsr->pFile ){
13658
- i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
13660
+ i64 iEof = (i64)pCsr->eocd.iOffset + (i64)pCsr->eocd.nSize;
1365913661
zipfileEntryFree(pCsr->pCurrent);
1366013662
pCsr->pCurrent = 0;
1366113663
if( pCsr->iNextOff>=iEof ){
1366213664
pCsr->bEof = 1;
1366313665
}else{
@@ -13891,16 +13893,16 @@
1389113893
** an English language error message may be left in virtual-table pTab.
1389213894
*/
1389313895
static int zipfileReadEOCD(
1389413896
ZipfileTab *pTab, /* Return errors here */
1389513897
const u8 *aBlob, /* Pointer to in-memory file image */
13896
- int nBlob, /* Size of aBlob[] in bytes */
13898
+ i64 nBlob, /* Size of aBlob[] in bytes */
1389713899
FILE *pFile, /* Read from this file if aBlob==0 */
1389813900
ZipfileEOCD *pEOCD /* Object to populate */
1389913901
){
1390013902
u8 *aRead = pTab->aBuffer; /* Temporary buffer */
13901
- int nRead; /* Bytes to read from file */
13903
+ i64 nRead; /* Bytes to read from file */
1390213904
int rc = SQLITE_OK;
1390313905
1390413906
memset(pEOCD, 0, sizeof(ZipfileEOCD));
1390513907
if( aBlob==0 ){
1390613908
i64 iOff; /* Offset to read from */
@@ -13917,11 +13919,11 @@
1391713919
nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
1391813920
aRead = (u8*)&aBlob[nBlob-nRead];
1391913921
}
1392013922
1392113923
if( rc==SQLITE_OK ){
13922
- int i;
13924
+ i64 i;
1392313925
1392413926
/* Scan backwards looking for the signature bytes */
1392513927
for(i=nRead-20; i>=0; i--){
1392613928
if( aRead[i]==0x50 && aRead[i+1]==0x4b
1392713929
&& aRead[i+2]==0x05 && aRead[i+3]==0x06
@@ -13928,13 +13930,11 @@
1392813930
){
1392913931
break;
1393013932
}
1393113933
}
1393213934
if( i<0 ){
13933
- pTab->base.zErrMsg = sqlite3_mprintf(
13934
- "cannot find end of central directory record"
13935
- );
13935
+ zipfileTableErr(pTab, "cannot find end of central directory record");
1393613936
return SQLITE_ERROR;
1393713937
}
1393813938
1393913939
aRead += i+4;
1394013940
pEOCD->iDisk = zipfileRead16(aRead);
@@ -13975,11 +13975,11 @@
1397513975
pNew->pNext = pBefore;
1397613976
*pp = pNew;
1397713977
}
1397813978
}
1397913979
13980
-static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
13980
+static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, i64 nBlob){
1398113981
ZipfileEOCD eocd;
1398213982
int rc;
1398313983
int i;
1398413984
i64 iOff;
1398513985
@@ -14023,11 +14023,11 @@
1402314023
zipfileCursorErr(pCsr, "zipfile() function requires an argument");
1402414024
return SQLITE_ERROR;
1402514025
}else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
1402614026
static const u8 aEmptyBlob = 0;
1402714027
const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
14028
- int nBlob = sqlite3_value_bytes(argv[0]);
14028
+ i64 nBlob = sqlite3_value_bytes(argv[0]);
1402914029
assert( pTab->pFirstEntry==0 );
1403014030
if( aBlob==0 ){
1403114031
aBlob = &aEmptyBlob;
1403214032
nBlob = 0;
1403314033
}
@@ -14221,23 +14221,23 @@
1422114221
ZipfileTab *pTab = (ZipfileTab*)pVtab;
1422214222
int rc = SQLITE_OK;
1422314223
1422414224
assert( pTab->pWriteFd==0 );
1422514225
if( pTab->zFile==0 || pTab->zFile[0]==0 ){
14226
- pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
14226
+ zipfileTableErr(pTab, "zipfile: missing filename");
1422714227
return SQLITE_ERROR;
1422814228
}
1422914229
1423014230
/* Open a write fd on the file. Also load the entire central directory
1423114231
** structure into memory. During the transaction any new file data is
1423214232
** appended to the archive file, but the central directory is accumulated
1423314233
** in main-memory until the transaction is committed. */
1423414234
pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
1423514235
if( pTab->pWriteFd==0 ){
14236
- pTab->base.zErrMsg = sqlite3_mprintf(
14237
- "zipfile: failed to open file %s for writing", pTab->zFile
14238
- );
14236
+ zipfileTableErr(pTab,
14237
+ "zipfile: failed to open file %s for writing", pTab->zFile
14238
+ );
1423914239
rc = SQLITE_ERROR;
1424014240
}else{
1424114241
fseek(pTab->pWriteFd, 0, SEEK_END);
1424214242
pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
1424314243
rc = zipfileLoadDirectory(pTab, 0, 0);
@@ -14698,11 +14698,11 @@
1469814698
int nEntry;
1469914699
ZipfileBuffer body;
1470014700
ZipfileBuffer cds;
1470114701
};
1470214702
14703
-static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
14703
+static int zipfileBufferGrow(ZipfileBuffer *pBuf, i64 nByte){
1470414704
if( pBuf->n+nByte>pBuf->nAlloc ){
1470514705
u8 *aNew;
1470614706
sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
1470714707
int nReq = pBuf->n + nByte;
1470814708
@@ -14747,11 +14747,11 @@
1474714747
u32 iCrc32 = 0; /* crc32 of uncompressed data */
1474814748
1474914749
char *zName = 0; /* Path (name) of new entry */
1475014750
int nName = 0; /* Size of zName in bytes */
1475114751
char *zFree = 0; /* Free this before returning */
14752
- int nByte;
14752
+ i64 nByte;
1475314753
1475414754
memset(&e, 0, sizeof(e));
1475514755
p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
1475614756
if( p==0 ) return;
1475714757
@@ -26213,12 +26213,18 @@
2621326213
if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
2621426214
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
2621526215
}
2621626216
sqlite3_reset(pStmt);
2621726217
spec.eStyle = QRF_STYLE_Auto;
26218
- sqlite3_stmt_explain(pStmt, 2-(pArg->mode.autoEQP>=AUTOEQP_full));
26218
+ sqlite3_stmt_explain(pStmt, 2);
2621926219
sqlite3_format_query_result(pStmt, &spec, 0);
26220
+ if( pArg->mode.autoEQP>=AUTOEQP_full ){
26221
+ sqlite3_reset(pStmt);
26222
+ sqlite3_stmt_explain(pStmt, 1);
26223
+ sqlite3_format_query_result(pStmt, &spec, 0);
26224
+ }
26225
+
2622026226
if( pArg->mode.autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
2622126227
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
2622226228
}
2622326229
sqlite3_reset(pStmt);
2622426230
sqlite3_stmt_explain(pStmt, 0);
@@ -27378,11 +27384,13 @@
2737827384
*/
2737927385
static int isScriptFile(const char *z, int bLeaveUninit){
2738027386
sqlite3_int64 sz = fileSize(z);
2738127387
if( sz<=0 ) return 0;
2738227388
if( (sz%512)==0 ){
27383
- int rc = isDatabaseFile(z, SQLITE_OPEN_READONLY);
27389
+ int rc;
27390
+ sqlite3_initialize();
27391
+ rc = isDatabaseFile(z, SQLITE_OPEN_READONLY);
2738427392
if( bLeaveUninit ){
2738527393
sqlite3_shutdown();
2738627394
}
2738727395
if( rc ) return 0; /* Is a database */
2738827396
}
2738927397
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -13103,11 +13103,11 @@
13103 nFile = (int)strlen(zFile)+1;
13104 }
13105
13106 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
13107 if( rc==SQLITE_OK ){
13108 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
13109 if( pNew==0 ) return SQLITE_NOMEM;
13110 memset(pNew, 0, nByte+nFile);
13111 pNew->db = db;
13112 pNew->aBuffer = (u8*)&pNew[1];
13113 if( zFile ){
@@ -13249,18 +13249,19 @@
13249 ** sqlite3_free().
13250 */
13251 static int zipfileReadData(
13252 FILE *pFile, /* Read from this file */
13253 u8 *aRead, /* Read into this buffer */
13254 int nRead, /* Number of bytes to read */
13255 i64 iOff, /* Offset to read from */
13256 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
13257 ){
13258 size_t n;
13259 fseek(pFile, (long)iOff, SEEK_SET);
13260 n = fread(aRead, 1, nRead, pFile);
13261 if( (int)n!=nRead ){
 
13262 *pzErrmsg = sqlite3_mprintf("error in fread()");
13263 return SQLITE_ERROR;
13264 }
13265 return SQLITE_OK;
13266 }
@@ -13273,11 +13274,11 @@
13273 if( nWrite>0 ){
13274 size_t n = nWrite;
13275 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
13276 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
13277 if( (int)n!=nWrite ){
13278 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
13279 return SQLITE_ERROR;
13280 }
13281 pTab->szCurrent += nWrite;
13282 }
13283 return SQLITE_OK;
@@ -13520,10 +13521,11 @@
13520 /*
13521 ** Set (*pzErr) to point to a buffer from sqlite3_malloc() containing a
13522 ** generic corruption message and return SQLITE_CORRUPT;
13523 */
13524 static int zipfileCorrupt(char **pzErr){
 
13525 *pzErr = sqlite3_mprintf("zip archive is corrupt");
13526 return SQLITE_CORRUPT;
13527 }
13528
13529 /*
@@ -13538,11 +13540,11 @@
13538 ** final value of (*ppEntry) undefined.
13539 */
13540 static int zipfileGetEntry(
13541 ZipfileTab *pTab, /* Store any error message here */
13542 const u8 *aBlob, /* Pointer to in-memory file image */
13543 int nBlob, /* Size of aBlob[] in bytes */
13544 FILE *pFile, /* If aBlob==0, read from this file */
13545 i64 iOff, /* Offset of CDS record */
13546 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
13547 ){
13548 u8 *aRead;
@@ -13578,11 +13580,11 @@
13578 rc = SQLITE_NOMEM;
13579 }else{
13580 memset(pNew, 0, sizeof(ZipfileEntry));
13581 rc = zipfileReadCDS(aRead, &pNew->cds);
13582 if( rc!=SQLITE_OK ){
13583 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
13584 }else if( aBlob==0 ){
13585 rc = zipfileReadData(
13586 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
13587 );
13588 }else{
@@ -13610,19 +13612,19 @@
13610 ZipfileLFH lfh;
13611 if( pFile ){
13612 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
13613 }else{
13614 aRead = (u8*)&aBlob[pNew->cds.iOffset];
13615 if( (pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>(unsigned)nBlob ){
13616 rc = zipfileCorrupt(pzErr);
13617 }
13618 }
13619
13620 memset(&lfh, 0, sizeof(lfh));
13621 if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
13622 if( rc==SQLITE_OK ){
13623 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
13624 pNew->iDataOff += lfh.nFile + lfh.nExtra;
13625 if( aBlob && pNew->cds.szCompressed ){
13626 if( pNew->iDataOff + pNew->cds.szCompressed > nBlob ){
13627 rc = zipfileCorrupt(pzErr);
13628 }else{
@@ -13629,11 +13631,11 @@
13629 pNew->aData = &pNew->aExtra[nExtra];
13630 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
13631 }
13632 }
13633 }else{
13634 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
13635 (int)pNew->cds.iOffset
13636 );
13637 }
13638 }
13639
@@ -13653,11 +13655,11 @@
13653 static int zipfileNext(sqlite3_vtab_cursor *cur){
13654 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
13655 int rc = SQLITE_OK;
13656
13657 if( pCsr->pFile ){
13658 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
13659 zipfileEntryFree(pCsr->pCurrent);
13660 pCsr->pCurrent = 0;
13661 if( pCsr->iNextOff>=iEof ){
13662 pCsr->bEof = 1;
13663 }else{
@@ -13891,16 +13893,16 @@
13891 ** an English language error message may be left in virtual-table pTab.
13892 */
13893 static int zipfileReadEOCD(
13894 ZipfileTab *pTab, /* Return errors here */
13895 const u8 *aBlob, /* Pointer to in-memory file image */
13896 int nBlob, /* Size of aBlob[] in bytes */
13897 FILE *pFile, /* Read from this file if aBlob==0 */
13898 ZipfileEOCD *pEOCD /* Object to populate */
13899 ){
13900 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
13901 int nRead; /* Bytes to read from file */
13902 int rc = SQLITE_OK;
13903
13904 memset(pEOCD, 0, sizeof(ZipfileEOCD));
13905 if( aBlob==0 ){
13906 i64 iOff; /* Offset to read from */
@@ -13917,11 +13919,11 @@
13917 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
13918 aRead = (u8*)&aBlob[nBlob-nRead];
13919 }
13920
13921 if( rc==SQLITE_OK ){
13922 int i;
13923
13924 /* Scan backwards looking for the signature bytes */
13925 for(i=nRead-20; i>=0; i--){
13926 if( aRead[i]==0x50 && aRead[i+1]==0x4b
13927 && aRead[i+2]==0x05 && aRead[i+3]==0x06
@@ -13928,13 +13930,11 @@
13928 ){
13929 break;
13930 }
13931 }
13932 if( i<0 ){
13933 pTab->base.zErrMsg = sqlite3_mprintf(
13934 "cannot find end of central directory record"
13935 );
13936 return SQLITE_ERROR;
13937 }
13938
13939 aRead += i+4;
13940 pEOCD->iDisk = zipfileRead16(aRead);
@@ -13975,11 +13975,11 @@
13975 pNew->pNext = pBefore;
13976 *pp = pNew;
13977 }
13978 }
13979
13980 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
13981 ZipfileEOCD eocd;
13982 int rc;
13983 int i;
13984 i64 iOff;
13985
@@ -14023,11 +14023,11 @@
14023 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
14024 return SQLITE_ERROR;
14025 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
14026 static const u8 aEmptyBlob = 0;
14027 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
14028 int nBlob = sqlite3_value_bytes(argv[0]);
14029 assert( pTab->pFirstEntry==0 );
14030 if( aBlob==0 ){
14031 aBlob = &aEmptyBlob;
14032 nBlob = 0;
14033 }
@@ -14221,23 +14221,23 @@
14221 ZipfileTab *pTab = (ZipfileTab*)pVtab;
14222 int rc = SQLITE_OK;
14223
14224 assert( pTab->pWriteFd==0 );
14225 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
14226 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
14227 return SQLITE_ERROR;
14228 }
14229
14230 /* Open a write fd on the file. Also load the entire central directory
14231 ** structure into memory. During the transaction any new file data is
14232 ** appended to the archive file, but the central directory is accumulated
14233 ** in main-memory until the transaction is committed. */
14234 pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
14235 if( pTab->pWriteFd==0 ){
14236 pTab->base.zErrMsg = sqlite3_mprintf(
14237 "zipfile: failed to open file %s for writing", pTab->zFile
14238 );
14239 rc = SQLITE_ERROR;
14240 }else{
14241 fseek(pTab->pWriteFd, 0, SEEK_END);
14242 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
14243 rc = zipfileLoadDirectory(pTab, 0, 0);
@@ -14698,11 +14698,11 @@
14698 int nEntry;
14699 ZipfileBuffer body;
14700 ZipfileBuffer cds;
14701 };
14702
14703 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
14704 if( pBuf->n+nByte>pBuf->nAlloc ){
14705 u8 *aNew;
14706 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
14707 int nReq = pBuf->n + nByte;
14708
@@ -14747,11 +14747,11 @@
14747 u32 iCrc32 = 0; /* crc32 of uncompressed data */
14748
14749 char *zName = 0; /* Path (name) of new entry */
14750 int nName = 0; /* Size of zName in bytes */
14751 char *zFree = 0; /* Free this before returning */
14752 int nByte;
14753
14754 memset(&e, 0, sizeof(e));
14755 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
14756 if( p==0 ) return;
14757
@@ -26213,12 +26213,18 @@
26213 if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
26214 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
26215 }
26216 sqlite3_reset(pStmt);
26217 spec.eStyle = QRF_STYLE_Auto;
26218 sqlite3_stmt_explain(pStmt, 2-(pArg->mode.autoEQP>=AUTOEQP_full));
26219 sqlite3_format_query_result(pStmt, &spec, 0);
 
 
 
 
 
 
26220 if( pArg->mode.autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
26221 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
26222 }
26223 sqlite3_reset(pStmt);
26224 sqlite3_stmt_explain(pStmt, 0);
@@ -27378,11 +27384,13 @@
27378 */
27379 static int isScriptFile(const char *z, int bLeaveUninit){
27380 sqlite3_int64 sz = fileSize(z);
27381 if( sz<=0 ) return 0;
27382 if( (sz%512)==0 ){
27383 int rc = isDatabaseFile(z, SQLITE_OPEN_READONLY);
 
 
27384 if( bLeaveUninit ){
27385 sqlite3_shutdown();
27386 }
27387 if( rc ) return 0; /* Is a database */
27388 }
27389
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -13103,11 +13103,11 @@
13103 nFile = (int)strlen(zFile)+1;
13104 }
13105
13106 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
13107 if( rc==SQLITE_OK ){
13108 pNew = (ZipfileTab*)sqlite3_malloc64((i64)nByte+nFile);
13109 if( pNew==0 ) return SQLITE_NOMEM;
13110 memset(pNew, 0, nByte+nFile);
13111 pNew->db = db;
13112 pNew->aBuffer = (u8*)&pNew[1];
13113 if( zFile ){
@@ -13249,18 +13249,19 @@
13249 ** sqlite3_free().
13250 */
13251 static int zipfileReadData(
13252 FILE *pFile, /* Read from this file */
13253 u8 *aRead, /* Read into this buffer */
13254 i64 nRead, /* Number of bytes to read */
13255 i64 iOff, /* Offset to read from */
13256 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
13257 ){
13258 size_t n;
13259 fseek(pFile, (long)iOff, SEEK_SET);
13260 n = fread(aRead, 1, (long)nRead, pFile);
13261 if( n!=(size_t)nRead ){
13262 sqlite3_free(*pzErrmsg);
13263 *pzErrmsg = sqlite3_mprintf("error in fread()");
13264 return SQLITE_ERROR;
13265 }
13266 return SQLITE_OK;
13267 }
@@ -13273,11 +13274,11 @@
13274 if( nWrite>0 ){
13275 size_t n = nWrite;
13276 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
13277 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
13278 if( (int)n!=nWrite ){
13279 zipfileTableErr(pTab,"error in fwrite()");
13280 return SQLITE_ERROR;
13281 }
13282 pTab->szCurrent += nWrite;
13283 }
13284 return SQLITE_OK;
@@ -13520,10 +13521,11 @@
13521 /*
13522 ** Set (*pzErr) to point to a buffer from sqlite3_malloc() containing a
13523 ** generic corruption message and return SQLITE_CORRUPT;
13524 */
13525 static int zipfileCorrupt(char **pzErr){
13526 sqlite3_free(*pzErr);
13527 *pzErr = sqlite3_mprintf("zip archive is corrupt");
13528 return SQLITE_CORRUPT;
13529 }
13530
13531 /*
@@ -13538,11 +13540,11 @@
13540 ** final value of (*ppEntry) undefined.
13541 */
13542 static int zipfileGetEntry(
13543 ZipfileTab *pTab, /* Store any error message here */
13544 const u8 *aBlob, /* Pointer to in-memory file image */
13545 i64 nBlob, /* Size of aBlob[] in bytes */
13546 FILE *pFile, /* If aBlob==0, read from this file */
13547 i64 iOff, /* Offset of CDS record */
13548 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
13549 ){
13550 u8 *aRead;
@@ -13578,11 +13580,11 @@
13580 rc = SQLITE_NOMEM;
13581 }else{
13582 memset(pNew, 0, sizeof(ZipfileEntry));
13583 rc = zipfileReadCDS(aRead, &pNew->cds);
13584 if( rc!=SQLITE_OK ){
13585 zipfileTableErr(pTab, "failed to read CDS at offset %lld", iOff);
13586 }else if( aBlob==0 ){
13587 rc = zipfileReadData(
13588 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
13589 );
13590 }else{
@@ -13610,19 +13612,19 @@
13612 ZipfileLFH lfh;
13613 if( pFile ){
13614 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
13615 }else{
13616 aRead = (u8*)&aBlob[pNew->cds.iOffset];
13617 if( ((i64)pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>nBlob ){
13618 rc = zipfileCorrupt(pzErr);
13619 }
13620 }
13621
13622 memset(&lfh, 0, sizeof(lfh));
13623 if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
13624 if( rc==SQLITE_OK ){
13625 pNew->iDataOff = (i64)pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
13626 pNew->iDataOff += lfh.nFile + lfh.nExtra;
13627 if( aBlob && pNew->cds.szCompressed ){
13628 if( pNew->iDataOff + pNew->cds.szCompressed > nBlob ){
13629 rc = zipfileCorrupt(pzErr);
13630 }else{
@@ -13629,11 +13631,11 @@
13631 pNew->aData = &pNew->aExtra[nExtra];
13632 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
13633 }
13634 }
13635 }else{
13636 zipfileTableErr(pTab, "failed to read LFH at offset %d",
13637 (int)pNew->cds.iOffset
13638 );
13639 }
13640 }
13641
@@ -13653,11 +13655,11 @@
13655 static int zipfileNext(sqlite3_vtab_cursor *cur){
13656 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
13657 int rc = SQLITE_OK;
13658
13659 if( pCsr->pFile ){
13660 i64 iEof = (i64)pCsr->eocd.iOffset + (i64)pCsr->eocd.nSize;
13661 zipfileEntryFree(pCsr->pCurrent);
13662 pCsr->pCurrent = 0;
13663 if( pCsr->iNextOff>=iEof ){
13664 pCsr->bEof = 1;
13665 }else{
@@ -13891,16 +13893,16 @@
13893 ** an English language error message may be left in virtual-table pTab.
13894 */
13895 static int zipfileReadEOCD(
13896 ZipfileTab *pTab, /* Return errors here */
13897 const u8 *aBlob, /* Pointer to in-memory file image */
13898 i64 nBlob, /* Size of aBlob[] in bytes */
13899 FILE *pFile, /* Read from this file if aBlob==0 */
13900 ZipfileEOCD *pEOCD /* Object to populate */
13901 ){
13902 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
13903 i64 nRead; /* Bytes to read from file */
13904 int rc = SQLITE_OK;
13905
13906 memset(pEOCD, 0, sizeof(ZipfileEOCD));
13907 if( aBlob==0 ){
13908 i64 iOff; /* Offset to read from */
@@ -13917,11 +13919,11 @@
13919 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
13920 aRead = (u8*)&aBlob[nBlob-nRead];
13921 }
13922
13923 if( rc==SQLITE_OK ){
13924 i64 i;
13925
13926 /* Scan backwards looking for the signature bytes */
13927 for(i=nRead-20; i>=0; i--){
13928 if( aRead[i]==0x50 && aRead[i+1]==0x4b
13929 && aRead[i+2]==0x05 && aRead[i+3]==0x06
@@ -13928,13 +13930,11 @@
13930 ){
13931 break;
13932 }
13933 }
13934 if( i<0 ){
13935 zipfileTableErr(pTab, "cannot find end of central directory record");
 
 
13936 return SQLITE_ERROR;
13937 }
13938
13939 aRead += i+4;
13940 pEOCD->iDisk = zipfileRead16(aRead);
@@ -13975,11 +13975,11 @@
13975 pNew->pNext = pBefore;
13976 *pp = pNew;
13977 }
13978 }
13979
13980 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, i64 nBlob){
13981 ZipfileEOCD eocd;
13982 int rc;
13983 int i;
13984 i64 iOff;
13985
@@ -14023,11 +14023,11 @@
14023 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
14024 return SQLITE_ERROR;
14025 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
14026 static const u8 aEmptyBlob = 0;
14027 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
14028 i64 nBlob = sqlite3_value_bytes(argv[0]);
14029 assert( pTab->pFirstEntry==0 );
14030 if( aBlob==0 ){
14031 aBlob = &aEmptyBlob;
14032 nBlob = 0;
14033 }
@@ -14221,23 +14221,23 @@
14221 ZipfileTab *pTab = (ZipfileTab*)pVtab;
14222 int rc = SQLITE_OK;
14223
14224 assert( pTab->pWriteFd==0 );
14225 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
14226 zipfileTableErr(pTab, "zipfile: missing filename");
14227 return SQLITE_ERROR;
14228 }
14229
14230 /* Open a write fd on the file. Also load the entire central directory
14231 ** structure into memory. During the transaction any new file data is
14232 ** appended to the archive file, but the central directory is accumulated
14233 ** in main-memory until the transaction is committed. */
14234 pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
14235 if( pTab->pWriteFd==0 ){
14236 zipfileTableErr(pTab,
14237 "zipfile: failed to open file %s for writing", pTab->zFile
14238 );
14239 rc = SQLITE_ERROR;
14240 }else{
14241 fseek(pTab->pWriteFd, 0, SEEK_END);
14242 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
14243 rc = zipfileLoadDirectory(pTab, 0, 0);
@@ -14698,11 +14698,11 @@
14698 int nEntry;
14699 ZipfileBuffer body;
14700 ZipfileBuffer cds;
14701 };
14702
14703 static int zipfileBufferGrow(ZipfileBuffer *pBuf, i64 nByte){
14704 if( pBuf->n+nByte>pBuf->nAlloc ){
14705 u8 *aNew;
14706 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
14707 int nReq = pBuf->n + nByte;
14708
@@ -14747,11 +14747,11 @@
14747 u32 iCrc32 = 0; /* crc32 of uncompressed data */
14748
14749 char *zName = 0; /* Path (name) of new entry */
14750 int nName = 0; /* Size of zName in bytes */
14751 char *zFree = 0; /* Free this before returning */
14752 i64 nByte;
14753
14754 memset(&e, 0, sizeof(e));
14755 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
14756 if( p==0 ) return;
14757
@@ -26213,12 +26213,18 @@
26213 if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
26214 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
26215 }
26216 sqlite3_reset(pStmt);
26217 spec.eStyle = QRF_STYLE_Auto;
26218 sqlite3_stmt_explain(pStmt, 2);
26219 sqlite3_format_query_result(pStmt, &spec, 0);
26220 if( pArg->mode.autoEQP>=AUTOEQP_full ){
26221 sqlite3_reset(pStmt);
26222 sqlite3_stmt_explain(pStmt, 1);
26223 sqlite3_format_query_result(pStmt, &spec, 0);
26224 }
26225
26226 if( pArg->mode.autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
26227 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
26228 }
26229 sqlite3_reset(pStmt);
26230 sqlite3_stmt_explain(pStmt, 0);
@@ -27378,11 +27384,13 @@
27384 */
27385 static int isScriptFile(const char *z, int bLeaveUninit){
27386 sqlite3_int64 sz = fileSize(z);
27387 if( sz<=0 ) return 0;
27388 if( (sz%512)==0 ){
27389 int rc;
27390 sqlite3_initialize();
27391 rc = isDatabaseFile(z, SQLITE_OPEN_READONLY);
27392 if( bLeaveUninit ){
27393 sqlite3_shutdown();
27394 }
27395 if( rc ) return 0; /* Is a database */
27396 }
27397
+606 -528
--- 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
-** 9adab8b2bef4130abd358d53384cb5f4dd69 with changes in files:
21
+** 4733d351ec2376291f093ba8d2ba71d82c6f with changes in files:
2222
**
2323
**
2424
*/
2525
#ifndef SQLITE_AMALGAMATION
2626
#define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467467
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468468
** [sqlite_version()] and [sqlite_source_id()].
469469
*/
470470
#define SQLITE_VERSION "3.52.0"
471471
#define SQLITE_VERSION_NUMBER 3052000
472
-#define SQLITE_SOURCE_ID "2026-01-09 00:41:35 9adab8b2bef4130abd358d53384cb5f4dd691b808336bb7102793b0165b1c516"
472
+#define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
473473
#define SQLITE_SCM_BRANCH "trunk"
474474
#define SQLITE_SCM_TAGS ""
475
-#define SQLITE_SCM_DATETIME "2026-01-09T00:41:35.433Z"
475
+#define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
476476
477477
/*
478478
** CAPI3REF: Run-Time Library Version Numbers
479479
** KEYWORDS: sqlite3_version sqlite3_sourceid
480480
**
@@ -5194,12 +5194,12 @@
51945194
** it should be a pointer to well-formed UTF8 text.
51955195
** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
51965196
** it should be a pointer to well-formed UTF16 text.
51975197
** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
51985198
** it should be a pointer to a well-formed unicode string that is
5199
-** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
5200
-** otherwise.
5199
+** either UTF8 if the sixth parameter is SQLITE_UTF8 or SQLITE_UTF8_ZT,
5200
+** or UTF16 otherwise.
52015201
**
52025202
** [[byte-order determination rules]] ^The byte-order of
52035203
** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
52045204
** found in the first character, which is removed, or in the absence of a BOM
52055205
** the byte order is the native byte order of the host
@@ -5241,14 +5241,19 @@
52415241
** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
52425242
** object is to be copied prior to the return from sqlite3_bind_*(). ^The
52435243
** object and pointer to it must remain valid until then. ^SQLite will then
52445244
** manage the lifetime of its private copy.
52455245
**
5246
-** ^The sixth argument to sqlite3_bind_text64() must be one of
5247
-** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5248
-** to specify the encoding of the text in the third parameter. If
5249
-** the sixth argument to sqlite3_bind_text64() is not one of the
5246
+** ^The sixth argument (the E argument)
5247
+** to sqlite3_bind_text64(S,K,Z,N,D,E) must be one of
5248
+** [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
5249
+** or [SQLITE_UTF16LE] to specify the encoding of the text in the
5250
+** third parameter, Z. The special value [SQLITE_UTF8_ZT] means that the
5251
+** string argument is both UTF-8 encoded and is zero-terminated. In other
5252
+** words, SQLITE_UTF8_ZT means that the Z array is allocated to hold at
5253
+** least N+1 bytes and that the Z&#91;N&#93; byte is zero. If
5254
+** the E argument to sqlite3_bind_text64(S,K,Z,N,D,E) is not one of the
52505255
** allowed values shown above, or if the text encoding is different
52515256
** from the encoding specified by the sixth parameter, then the behavior
52525257
** is undefined.
52535258
**
52545259
** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -6111,17 +6116,63 @@
61116116
/*
61126117
** CAPI3REF: Text Encodings
61136118
**
61146119
** These constants define integer codes that represent the various
61156120
** text encodings supported by SQLite.
6121
+**
6122
+** <dl>
6123
+** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
6124
+**
6125
+** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
6126
+** with each code point being expressed "little endian" - the least significant
6127
+** byte first. This is the usual encoding, for example on Windows.</dd>
6128
+**
6129
+** [[SQLITE_UTF16BE]] <dt>SQLITE_UTF16BE</dt><dd>Text is encoding as UTF-16
6130
+** with each code point being expressed "big endian" - the most significant
6131
+** byte first. This encoding is less common, but is still sometimes seen,
6132
+** specially on older systems.
6133
+**
6134
+** [[SQLITE_UTF16]] <dt>SQLITE_UTF16</dt><dd>Text is encoding as UTF-16
6135
+** with each code point being expressed either little endian or as big
6136
+** endian, according to the native endianness of the host computer.
6137
+**
6138
+** [[SQLITE_ANY]] <dt>SQLITE_ANY</dt><dd>This encoding value may only be used
6139
+** to declare the preferred text for [application-defined SQL functions]
6140
+** created using [sqlite3_create_function()] and similar. If the preferred
6141
+** encoding (the 4th parameter to sqlite3_create_function() - the eTextRep
6142
+** parameter) is SQLITE_ANY, that indicates that the function does not have
6143
+** a preference regarding the text encoding of its parameters and can take
6144
+** any text encoding that the SQLite core find convenient to supply. This
6145
+** option is deprecated. Please do not use it in new applications.
6146
+**
6147
+** [[SQLITE_UTF16_ALIGNED]] <dt>SQLITE_UTF16_ALIGNED</dt><dd>This encoding
6148
+** value may be used as the 3rd parameter (the eTextRep parameter) to
6149
+** [sqlite3_create_collation()] and similar. This encoding value means
6150
+** that the application-defined collating sequence created expects its
6151
+** input strings to be in UTF16 in native byte order, and that the start
6152
+** of the strings must be aligned to a 2-byte boundary.
6153
+**
6154
+** [[SQLITE_UTF8_ZT]] <dt>SQLITE_UTF8_ZT</dt><dd>This option can only be
6155
+** used to specify the text encoding to strings input to [sqlite3_result_text64()]
6156
+** and [sqlite3_bind_text64()]. It means that the input string (call it "z")
6157
+** is UTF-8 encoded and that it is zero-terminated. If the length parameter
6158
+** (call it "n") is non-negative, this encoding option means that the caller
6159
+** guarantees that z array contains at least n+1 bytes and that the z&#91;n&#93;
6160
+** byte has a value of zero.
6161
+** This option gives the same output as SQLITE_UTF8, but can be more efficient
6162
+** by avoiding the need to make a copy of the input string, in some cases.
6163
+** However, if z is allocated to hold fewer than n+1 bytes or if the
6164
+** z&#91;n&#93; byte is not zero, undefined behavior may result.
6165
+** </dl>
61166166
*/
61176167
#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
61186168
#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
61196169
#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
61206170
#define SQLITE_UTF16 4 /* Use native byte order */
61216171
#define SQLITE_ANY 5 /* Deprecated */
61226172
#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
6173
+#define SQLITE_UTF8_ZT 16 /* Zero-terminated UTF8 */
61236174
61246175
/*
61256176
** CAPI3REF: Function Flags
61266177
**
61276178
** These constants may be ORed together with the
@@ -6738,14 +6789,18 @@
67386789
** ^The sqlite3_result_text(), sqlite3_result_text16(),
67396790
** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
67406791
** set the return value of the application-defined function to be
67416792
** a text string which is represented as UTF-8, UTF-16 native byte order,
67426793
** UTF-16 little endian, or UTF-16 big endian, respectively.
6743
-** ^The sqlite3_result_text64() interface sets the return value of an
6794
+** ^The sqlite3_result_text64(C,Z,N,D,E) interface sets the return value of an
67446795
** application-defined function to be a text string in an encoding
6745
-** specified by the fifth (and last) parameter, which must be one
6746
-** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6796
+** specified the E parameter, which must be one
6797
+** of [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
6798
+** or [SQLITE_UTF16LE]. ^The special value [SQLITE_UTF8_ZT] means that
6799
+** the result text is both UTF-8 and zero-terminated. In other words,
6800
+** SQLITE_UTF8_ZT means that the Z array holds at least N+1 byes and that
6801
+** the Z&#91;N&#93; is zero.
67476802
** ^SQLite takes the text result from the application from
67486803
** the 2nd parameter of the sqlite3_result_text* interfaces.
67496804
** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
67506805
** other than sqlite3_result_text64() is negative, then SQLite computes
67516806
** the string length itself by searching the 2nd parameter for the first
@@ -6828,11 +6883,11 @@
68286883
SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
68296884
SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
68306885
SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
68316886
SQLITE_API void sqlite3_result_null(sqlite3_context*);
68326887
SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6833
-SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6888
+SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char *z, sqlite3_uint64 n,
68346889
void(*)(void*), unsigned char encoding);
68356890
SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
68366891
SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
68376892
SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
68386893
SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
@@ -14373,10 +14428,31 @@
1437314428
#ifndef SQLITE_MAX_LENGTH
1437414429
# define SQLITE_MAX_LENGTH 1000000000
1437514430
#endif
1437614431
#define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */
1437714432
14433
+/*
14434
+** Maximum size of any single memory allocation.
14435
+**
14436
+** This is not a limit on the total amount of memory used. This is
14437
+** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
14438
+**
14439
+** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
14440
+** This provides a 256-byte safety margin for defense against 32-bit
14441
+** signed integer overflow bugs when computing memory allocation sizes.
14442
+** Paranoid applications might want to reduce the maximum allocation size
14443
+** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
14444
+** or even smaller would be reasonable upper bounds on the size of a memory
14445
+** allocations for most applications.
14446
+*/
14447
+#ifndef SQLITE_MAX_ALLOCATION_SIZE
14448
+# define SQLITE_MAX_ALLOCATION_SIZE 2147483391
14449
+#endif
14450
+#if SQLITE_MAX_ALLOCATION_SIZE>2147483391
14451
+# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
14452
+#endif
14453
+
1437814454
/*
1437914455
** This is the maximum number of
1438014456
**
1438114457
** * Columns in a table
1438214458
** * Columns in an index
@@ -20223,31 +20299,17 @@
2022320299
};
2022420300
2022520301
/*
2022620302
** An instance of the following structure contains all information
2022720303
** needed to generate code for a single SELECT statement.
20228
-**
20229
-** See the header comment on the computeLimitRegisters() routine for a
20230
-** detailed description of the meaning of the iLimit and iOffset fields.
20231
-**
20232
-** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
20233
-** These addresses must be stored so that we can go back and fill in
20234
-** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
20235
-** the number of columns in P2 can be computed at the same time
20236
-** as the OP_OpenEphm instruction is coded because not
20237
-** enough information about the compound query is known at that point.
20238
-** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
20239
-** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
20240
-** sequences for the ORDER BY clause.
2024120304
*/
2024220305
struct Select {
2024320306
u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
2024420307
LogEst nSelectRow; /* Estimated number of result rows */
2024520308
u32 selFlags; /* Various SF_* values */
2024620309
int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
2024720310
u32 selId; /* Unique identifier number for this SELECT */
20248
- int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
2024920311
ExprList *pEList; /* The fields of the result */
2025020312
SrcList *pSrc; /* The FROM clause */
2025120313
Expr *pWhere; /* The WHERE clause */
2025220314
ExprList *pGroupBy; /* The GROUP BY clause */
2025320315
Expr *pHaving; /* The HAVING clause */
@@ -20275,28 +20337,28 @@
2027520337
#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
2027620338
#define SF_All 0x0000002 /* Includes the ALL keyword */
2027720339
#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
2027820340
#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
2027920341
#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
20280
-#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
20342
+/* 0x0000020 // available for reuse */
2028120343
#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
2028220344
#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
2028320345
#define SF_Compound 0x0000100 /* Part of a compound query */
2028420346
#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
2028520347
#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
2028620348
#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
2028720349
#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
2028820350
#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
2028920351
#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
20290
-#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
20352
+/* 0x0008000 // available for reuse */
2029120353
#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
2029220354
#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
2029320355
#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
2029420356
#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
2029520357
#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
2029620358
#define SF_View 0x0200000 /* SELECT statement is a view */
20297
-#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
20359
+/* 0x0400000 // available for reuse */
2029820360
#define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
2029920361
#define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
2030020362
#define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
2030120363
#define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
2030220364
#define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
@@ -20312,15 +20374,10 @@
2031220374
/*
2031320375
** The results of a SELECT can be distributed in several ways, as defined
2031420376
** by one of the following macros. The "SRT" prefix means "SELECT Result
2031520377
** Type".
2031620378
**
20317
-** SRT_Union Store results as a key in a temporary index
20318
-** identified by pDest->iSDParm.
20319
-**
20320
-** SRT_Except Remove results from the temporary index pDest->iSDParm.
20321
-**
2032220379
** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
2032320380
** set is not empty.
2032420381
**
2032520382
** SRT_Discard Throw the results away. This is used by SELECT
2032620383
** statements within triggers whose only purpose is
@@ -20380,34 +20437,32 @@
2038020437
** column returned by the SELECT is used as the integer
2038120438
** key. If (pDest->iSDParm>0), then the table is an index
2038220439
** table. (pDest->iSDParm) is the number of key columns in
2038320440
** each index record in this case.
2038420441
*/
20385
-#define SRT_Union 1 /* Store result as keys in an index */
20386
-#define SRT_Except 2 /* Remove result from a UNION index */
20387
-#define SRT_Exists 3 /* Store 1 if the result is not empty */
20388
-#define SRT_Discard 4 /* Do not save the results anywhere */
20389
-#define SRT_DistFifo 5 /* Like SRT_Fifo, but unique results only */
20390
-#define SRT_DistQueue 6 /* Like SRT_Queue, but unique results only */
20442
+#define SRT_Exists 1 /* Store 1 if the result is not empty */
20443
+#define SRT_Discard 2 /* Do not save the results anywhere */
20444
+#define SRT_DistFifo 3 /* Like SRT_Fifo, but unique results only */
20445
+#define SRT_DistQueue 4 /* Like SRT_Queue, but unique results only */
2039120446
2039220447
/* The DISTINCT clause is ignored for all of the above. Not that
2039320448
** IgnorableDistinct() implies IgnorableOrderby() */
2039420449
#define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
2039520450
20396
-#define SRT_Queue 7 /* Store result in an queue */
20397
-#define SRT_Fifo 8 /* Store result as data with an automatic rowid */
20451
+#define SRT_Queue 5 /* Store result in an queue */
20452
+#define SRT_Fifo 6 /* Store result as data with an automatic rowid */
2039820453
2039920454
/* The ORDER BY clause is ignored for all of the above */
2040020455
#define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
2040120456
20402
-#define SRT_Output 9 /* Output each row of result */
20403
-#define SRT_Mem 10 /* Store result in a memory cell */
20404
-#define SRT_Set 11 /* Store results as keys in an index */
20405
-#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
20406
-#define SRT_Coroutine 13 /* Generate a single row of result */
20407
-#define SRT_Table 14 /* Store result as data with an automatic rowid */
20408
-#define SRT_Upfrom 15 /* Store result as data with rowid */
20457
+#define SRT_Output 7 /* Output each row of result */
20458
+#define SRT_Mem 8 /* Store result in a memory cell */
20459
+#define SRT_Set 9 /* Store results as keys in an index */
20460
+#define SRT_EphemTab 10 /* Create transient tab and store like SRT_Table */
20461
+#define SRT_Coroutine 11 /* Generate a single row of result */
20462
+#define SRT_Table 12 /* Store result as data with an automatic rowid */
20463
+#define SRT_Upfrom 13 /* Store result as data with rowid */
2040920464
2041020465
/*
2041120466
** An instance of this object describes where to put of the results of
2041220467
** a SELECT statement.
2041320468
*/
@@ -24510,10 +24565,11 @@
2451024565
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
2451124566
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
2451224567
SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
2451324568
SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
2451424569
SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
24570
+SQLITE_PRIVATE int sqlite3VdbeMemSetText(Mem*, const char*, i64, void(*)(void*));
2451524571
SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
2451624572
#ifdef SQLITE_OMIT_FLOATING_POINT
2451724573
# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
2451824574
#else
2451924575
SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
@@ -31356,31 +31412,10 @@
3135631412
sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
3135731413
}
3135831414
*pp = p;
3135931415
}
3136031416
31361
-/*
31362
-** Maximum size of any single memory allocation.
31363
-**
31364
-** This is not a limit on the total amount of memory used. This is
31365
-** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
31366
-**
31367
-** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
31368
-** This provides a 256-byte safety margin for defense against 32-bit
31369
-** signed integer overflow bugs when computing memory allocation sizes.
31370
-** Paranoid applications might want to reduce the maximum allocation size
31371
-** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
31372
-** or even smaller would be reasonable upper bounds on the size of a memory
31373
-** allocations for most applications.
31374
-*/
31375
-#ifndef SQLITE_MAX_ALLOCATION_SIZE
31376
-# define SQLITE_MAX_ALLOCATION_SIZE 2147483391
31377
-#endif
31378
-#if SQLITE_MAX_ALLOCATION_SIZE>2147483391
31379
-# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
31380
-#endif
31381
-
3138231417
/*
3138331418
** Allocate memory. This routine is like sqlite3_malloc() except that it
3138431419
** assumes the memory subsystem has already been initialized.
3138531420
*/
3138631421
SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
@@ -31600,12 +31635,11 @@
3160031635
}
3160131636
if( nBytes==0 ){
3160231637
sqlite3_free(pOld); /* IMP: R-26507-47431 */
3160331638
return 0;
3160431639
}
31605
- if( nBytes>=0x7fffff00 ){
31606
- /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
31640
+ if( nBytes>SQLITE_MAX_ALLOCATION_SIZE ){
3160731641
return 0;
3160831642
}
3160931643
nOld = sqlite3MallocSize(pOld);
3161031644
/* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
3161131645
** argument to xRealloc is always a value returned by a prior call to
@@ -77429,11 +77463,11 @@
7742977463
}
7743077464
assert( cursorHoldsMutex(pCur) );
7743177465
7743277466
getCellInfo(pCur);
7743377467
aPayload = pCur->info.pPayload;
77434
- assert( offset+amt <= pCur->info.nPayload );
77468
+ assert( (u64)offset+(u64)amt <= (u64)pCur->info.nPayload );
7743577469
7743677470
assert( aPayload > pPage->aData );
7743777471
if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
7743877472
/* Trying to read or write past the end of the data is an error. The
7743977473
** conditional above is really:
@@ -77986,11 +78020,11 @@
7798678020
SQLITE_PRIVATE int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes){
7798778021
int rc;
7798878022
7798978023
assert( cursorOwnsBtShared(pCur) );
7799078024
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77991
- if( pCur->eState==CURSOR_VALID ){
78025
+ if( NEVER(pCur->eState==CURSOR_VALID) ){
7799278026
*pRes = 0;
7799378027
return SQLITE_OK;
7799478028
}
7799578029
rc = moveToRoot(pCur);
7799678030
if( rc==SQLITE_EMPTY ){
@@ -85879,10 +85913,88 @@
8587985913
#endif
8588085914
8588185915
8588285916
return SQLITE_OK;
8588385917
}
85918
+
85919
+/* Like sqlite3VdbeMemSetStr() except:
85920
+**
85921
+** enc is always SQLITE_UTF8
85922
+** pMem->db is always non-NULL
85923
+*/
85924
+SQLITE_PRIVATE int sqlite3VdbeMemSetText(
85925
+ Mem *pMem, /* Memory cell to set to string value */
85926
+ const char *z, /* String pointer */
85927
+ i64 n, /* Bytes in string, or negative */
85928
+ void (*xDel)(void*) /* Destructor function */
85929
+){
85930
+ i64 nByte = n; /* New value for pMem->n */
85931
+ u16 flags;
85932
+
85933
+ assert( pMem!=0 );
85934
+ assert( pMem->db!=0 );
85935
+ assert( sqlite3_mutex_held(pMem->db->mutex) );
85936
+ assert( !sqlite3VdbeMemIsRowSet(pMem) );
85937
+
85938
+ /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
85939
+ if( !z ){
85940
+ sqlite3VdbeMemSetNull(pMem);
85941
+ return SQLITE_OK;
85942
+ }
85943
+
85944
+ if( nByte<0 ){
85945
+ nByte = strlen(z);
85946
+ flags = MEM_Str|MEM_Term;
85947
+ }else{
85948
+ flags = MEM_Str;
85949
+ }
85950
+ if( nByte>(i64)pMem->db->aLimit[SQLITE_LIMIT_LENGTH] ){
85951
+ if( xDel && xDel!=SQLITE_TRANSIENT ){
85952
+ if( xDel==SQLITE_DYNAMIC ){
85953
+ sqlite3DbFree(pMem->db, (void*)z);
85954
+ }else{
85955
+ xDel((void*)z);
85956
+ }
85957
+ }
85958
+ sqlite3VdbeMemSetNull(pMem);
85959
+ return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
85960
+ }
85961
+
85962
+ /* The following block sets the new values of Mem.z and Mem.xDel. It
85963
+ ** also sets a flag in local variable "flags" to indicate the memory
85964
+ ** management (one of MEM_Dyn or MEM_Static).
85965
+ */
85966
+ if( xDel==SQLITE_TRANSIENT ){
85967
+ i64 nAlloc = nByte + 1;
85968
+ testcase( nAlloc==31 );
85969
+ testcase( nAlloc==32 );
85970
+ if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
85971
+ return SQLITE_NOMEM_BKPT;
85972
+ }
85973
+ assert( pMem->z!=0 );
85974
+ memcpy(pMem->z, z, nByte);
85975
+ pMem->z[nByte] = 0;
85976
+ }else{
85977
+ sqlite3VdbeMemRelease(pMem);
85978
+ pMem->z = (char *)z;
85979
+ if( xDel==SQLITE_DYNAMIC ){
85980
+ pMem->zMalloc = pMem->z;
85981
+ pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
85982
+ pMem->xDel = 0;
85983
+ }else if( xDel==SQLITE_STATIC ){
85984
+ pMem->xDel = xDel;
85985
+ flags |= MEM_Static;
85986
+ }else{
85987
+ pMem->xDel = xDel;
85988
+ flags |= MEM_Dyn;
85989
+ }
85990
+ }
85991
+ pMem->flags = flags;
85992
+ pMem->n = (int)(nByte & 0x7fffffff);
85993
+ pMem->enc = SQLITE_UTF8;
85994
+ return SQLITE_OK;
85995
+}
8588485996
8588585997
/*
8588685998
** Move data out of a btree key or data field and into a Mem structure.
8588785999
** The data is payload from the entry that pCur is currently pointing
8588886000
** to. offset and amt determine what portion of the data or key to retrieve.
@@ -85903,11 +86015,16 @@
8590386015
u32 amt, /* Number of bytes to return. */
8590486016
Mem *pMem /* OUT: Return data in this Mem structure. */
8590586017
){
8590686018
int rc;
8590786019
pMem->flags = MEM_Null;
85908
- if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
86020
+ testcase( amt==SQLITE_MAX_ALLOCATION_SIZE-1 );
86021
+ testcase( amt==SQLITE_MAX_ALLOCATION_SIZE );
86022
+ if( amt>=SQLITE_MAX_ALLOCATION_SIZE ){
86023
+ return SQLITE_NOMEM_BKPT;
86024
+ }
86025
+ if( (u64)amt + (u64)offset > (u64)sqlite3BtreeMaxRecordSize(pCur) ){
8590986026
return SQLITE_CORRUPT_BKPT;
8591086027
}
8591186028
if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
8591286029
rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
8591386030
if( rc==SQLITE_OK ){
@@ -89587,11 +89704,11 @@
8958789704
assert( !zName || xDel!=SQLITE_DYNAMIC );
8958889705
return SQLITE_NOMEM_BKPT;
8958989706
}
8959089707
assert( p->aColName!=0 );
8959189708
pColName = &(p->aColName[idx+var*p->nResAlloc]);
89592
- rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
89709
+ rc = sqlite3VdbeMemSetText(pColName, zName, -1, xDel);
8959389710
assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
8959489711
return rc;
8959589712
}
8959689713
8959789714
/*
@@ -92665,11 +92782,27 @@
9266592782
int n, /* Bytes in string, or negative */
9266692783
u8 enc, /* Encoding of z. 0 for BLOBs */
9266792784
void (*xDel)(void*) /* Destructor function */
9266892785
){
9266992786
Mem *pOut = pCtx->pOut;
92670
- int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
92787
+ int rc;
92788
+ if( enc==SQLITE_UTF8 ){
92789
+ rc = sqlite3VdbeMemSetText(pOut, z, n, xDel);
92790
+ }else if( enc==SQLITE_UTF8_ZT ){
92791
+ /* It is usually considered improper to assert() on an input. However,
92792
+ ** the following assert() is checking for inputs that are documented
92793
+ ** to result in undefined behavior. */
92794
+ assert( z==0
92795
+ || n<0
92796
+ || n>pOut->db->aLimit[SQLITE_LIMIT_LENGTH]
92797
+ || z[n]==0
92798
+ );
92799
+ rc = sqlite3VdbeMemSetText(pOut, z, n, xDel);
92800
+ pOut->flags |= MEM_Term;
92801
+ }else{
92802
+ rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
92803
+ }
9267192804
if( rc ){
9267292805
if( rc==SQLITE_TOOBIG ){
9267392806
sqlite3_result_error_toobig(pCtx);
9267492807
}else{
9267592808
/* The only errors possible from sqlite3VdbeMemSetStr are
@@ -92858,11 +92991,11 @@
9285892991
return;
9285992992
}
9286092993
#endif
9286192994
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
9286292995
assert( xDel!=SQLITE_DYNAMIC );
92863
- if( enc!=SQLITE_UTF8 ){
92996
+ if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF8_ZT ){
9286492997
if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
9286592998
n &= ~(u64)1;
9286692999
}
9286793000
if( n>0x7fffffff ){
9286893001
(void)invokeValueDestructor(z, xDel, pCtx);
@@ -93318,11 +93451,11 @@
9331893451
if( rc==SQLITE_OK ){
9331993452
u32 sz; /* Size of current row in bytes */
9332093453
Mem sMem; /* Raw content of current row */
9332193454
memset(&sMem, 0, sizeof(sMem));
9332293455
sz = sqlite3BtreePayloadSize(pRhs->pCsr);
93323
- rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
93456
+ rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,sz,&sMem);
9332493457
if( rc==SQLITE_OK ){
9332593458
u8 *zBuf = (u8*)sMem.z;
9332693459
u32 iSerial;
9332793460
sqlite3_value *pOut = pRhs->pOut;
9332893461
int iOff = 1 + getVarint32(&zBuf[1], iSerial);
@@ -93967,17 +94100,29 @@
9396794100
rc = vdbeUnbind(p, (u32)(i-1));
9396894101
if( rc==SQLITE_OK ){
9396994102
assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
9397094103
if( zData!=0 ){
9397194104
pVar = &p->aVar[i-1];
93972
- rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
93973
- if( rc==SQLITE_OK ){
93974
- if( encoding==0 ){
93975
- pVar->enc = ENC(p->db);
93976
- }else{
93977
- rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
93978
- }
94105
+ if( encoding==SQLITE_UTF8 ){
94106
+ rc = sqlite3VdbeMemSetText(pVar, zData, nData, xDel);
94107
+ }else if( encoding==SQLITE_UTF8_ZT ){
94108
+ /* It is usually consider improper to assert() on an input.
94109
+ ** However, the following assert() is checking for inputs
94110
+ ** that are documented to result in undefined behavior. */
94111
+ assert( zData==0
94112
+ || nData<0
94113
+ || nData>pVar->db->aLimit[SQLITE_LIMIT_LENGTH]
94114
+ || ((u8*)zData)[nData]==0
94115
+ );
94116
+ rc = sqlite3VdbeMemSetText(pVar, zData, nData, xDel);
94117
+ pVar->flags |= MEM_Term;
94118
+ }else{
94119
+ rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
94120
+ if( encoding==0 ) pVar->enc = ENC(p->db);
94121
+ }
94122
+ if( rc==SQLITE_OK && encoding!=0 ){
94123
+ rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
9397994124
}
9398094125
if( rc ){
9398194126
sqlite3Error(p->db, rc);
9398294127
rc = sqlite3ApiExit(p->db, rc);
9398394128
}
@@ -94085,11 +94230,11 @@
9408594230
sqlite3_uint64 nData,
9408694231
void (*xDel)(void*),
9408794232
unsigned char enc
9408894233
){
9408994234
assert( xDel!=SQLITE_DYNAMIC );
94090
- if( enc!=SQLITE_UTF8 ){
94235
+ if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF8_ZT ){
9409194236
if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
9409294237
nData &= ~(u64)1;
9409394238
}
9409494239
return bindText(pStmt, i, zData, nData, xDel, enc);
9409594240
}
@@ -101783,24 +101928,19 @@
101783101928
rc = sqlite3VdbeSorterWrite(pC, pIn2);
101784101929
if( rc) goto abort_due_to_error;
101785101930
break;
101786101931
}
101787101932
101788
-/* Opcode: IdxDelete P1 P2 P3 * P5
101933
+/* Opcode: IdxDelete P1 P2 P3 * *
101789101934
** Synopsis: key=r[P2@P3]
101790101935
**
101791101936
** The content of P3 registers starting at register P2 form
101792101937
** an unpacked index key. This opcode removes that entry from the
101793101938
** index opened by cursor P1.
101794101939
**
101795
-** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
101796
-** if no matching index entry is found. This happens when running
101797
-** an UPDATE or DELETE statement and the index entry to be updated
101798
-** or deleted is not found. For some uses of IdxDelete
101799
-** (example: the EXCEPT operator) it does not matter that no matching
101800
-** entry is found. For those cases, P5 is zero. Also, do not raise
101801
-** this (self-correcting and non-critical) error if in writable_schema mode.
101940
+** Raise an SQLITE_CORRUPT_INDEX error if no matching index entry is found
101941
+** and not in writable_schema mode.
101802101942
*/
101803101943
case OP_IdxDelete: {
101804101944
VdbeCursor *pC;
101805101945
BtCursor *pCrsr;
101806101946
int res;
@@ -101822,11 +101962,11 @@
101822101962
rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
101823101963
if( rc ) goto abort_due_to_error;
101824101964
if( res==0 ){
101825101965
rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
101826101966
if( rc ) goto abort_due_to_error;
101827
- }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
101967
+ }else if( !sqlite3WritableSchema(db) ){
101828101968
rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
101829101969
goto abort_due_to_error;
101830101970
}
101831101971
assert( pC->deferredMoveto==0 );
101832101972
pC->cacheStatus = CACHE_STALE;
@@ -113272,13 +113412,11 @@
113272113412
pNew->pNext = pNext;
113273113413
pNew->pPrior = 0;
113274113414
pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
113275113415
pNew->iLimit = 0;
113276113416
pNew->iOffset = 0;
113277
- pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral;
113278
- pNew->addrOpenEphm[0] = -1;
113279
- pNew->addrOpenEphm[1] = -1;
113417
+ pNew->selFlags = p->selFlags;
113280113418
pNew->nSelectRow = p->nSelectRow;
113281113419
pNew->pWith = sqlite3WithDup(db, p->pWith);
113282113420
#ifndef SQLITE_OMIT_WINDOWFUNC
113283113421
pNew->pWin = 0;
113284113422
pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
@@ -115496,12 +115634,13 @@
115496115634
if( destIfFalse==destIfNull ){
115497115635
/* Combine Step 3 and Step 5 into a single opcode */
115498115636
if( ExprHasProperty(pExpr, EP_Subrtn) ){
115499115637
const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
115500115638
assert( pOp->opcode==OP_Once || pParse->nErr );
115501
- if( pOp->opcode==OP_Once && pOp->p3>0 ){ /* tag-202407032019 */
115502
- assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) );
115639
+ if( pOp->p3>0 ){ /* tag-202407032019 */
115640
+ assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter)
115641
+ || pParse->nErr );
115503115642
sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse,
115504115643
rLhs, nVector); VdbeCoverage(v);
115505115644
}
115506115645
}
115507115646
sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
@@ -132240,11 +132379,10 @@
132240132379
VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
132241132380
r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
132242132381
&iPartIdxLabel, pPrior, r1);
132243132382
sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
132244132383
pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
132245
- sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */
132246132384
sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
132247132385
pPrior = pIdx;
132248132386
}
132249132387
}
132250132388
@@ -133587,11 +133725,11 @@
133587133725
}else{
133588133726
goto unistr_error;
133589133727
}
133590133728
}
133591133729
zOut[j] = 0;
133592
- sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8);
133730
+ sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8_ZT);
133593133731
return;
133594133732
133595133733
unistr_error:
133596133734
sqlite3_free(zOut);
133597133735
sqlite3_result_error(context, "invalid Unicode escape", -1);
@@ -133680,11 +133818,11 @@
133680133818
*zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
133681133819
*zOut++ = 0x80 + (u8)(c & 0x3F);
133682133820
} \
133683133821
}
133684133822
*zOut = 0;
133685
- sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
133823
+ sqlite3_result_text64(context, (char*)z, zOut-z,sqlite3_free,SQLITE_UTF8_ZT);
133686133824
}
133687133825
133688133826
/*
133689133827
** The hex() function. Interpret the argument as a blob. Return
133690133828
** a hexadecimal rendering as text.
@@ -133709,11 +133847,11 @@
133709133847
*(z++) = hexdigits[(c>>4)&0xf];
133710133848
*(z++) = hexdigits[c&0xf];
133711133849
}
133712133850
*z = 0;
133713133851
sqlite3_result_text64(context, zHex, (u64)(z-zHex),
133714
- sqlite3_free, SQLITE_UTF8);
133852
+ sqlite3_free, SQLITE_UTF8_ZT);
133715133853
}
133716133854
}
133717133855
133718133856
/*
133719133857
** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
@@ -134047,11 +134185,11 @@
134047134185
}
134048134186
}
134049134187
}
134050134188
z[j] = 0;
134051134189
assert( j<=n );
134052
- sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8);
134190
+ sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8_ZT);
134053134191
}
134054134192
134055134193
/*
134056134194
** The CONCAT(...) function. Generate a string result that is the
134057134195
** concatentation of all non-null arguments.
@@ -141235,10 +141373,11 @@
141235141373
int (*set_errmsg)(sqlite3*,int,const char*);
141236141374
int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
141237141375
/* Version 3.52.0 and later */
141238141376
void (*str_truncate)(sqlite3_str*,int);
141239141377
void (*str_free)(sqlite3_str*);
141378
+ int (*carray_bind)(sqlite3_stmt*,int,void*,int,int,void(*)(void*));
141240141379
};
141241141380
141242141381
/*
141243141382
** This is the function signature used for all extension entry points. It
141244141383
** is also defined in the file "loadext.c".
@@ -141576,10 +141715,11 @@
141576141715
#define sqlite3_set_errmsg sqlite3_api->set_errmsg
141577141716
#define sqlite3_db_status64 sqlite3_api->db_status64
141578141717
/* Version 3.52.0 and later */
141579141718
#define sqlite3_str_truncate sqlite3_api->str_truncate
141580141719
#define sqlite3_str_free sqlite3_api->str_free
141720
+#define sqlite3_carray_bind sqlite3_api->carray_bind
141581141721
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
141582141722
141583141723
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
141584141724
/* This case when the file really is being compiled as a loadable
141585141725
** extension */
@@ -142105,11 +142245,16 @@
142105142245
/* Version 3.51.0 and later */
142106142246
sqlite3_set_errmsg,
142107142247
sqlite3_db_status64,
142108142248
/* Version 3.52.0 and later */
142109142249
sqlite3_str_truncate,
142110
- sqlite3_str_free
142250
+ sqlite3_str_free,
142251
+#ifdef SQLITE_ENABLE_CARRAY
142252
+ sqlite3_carray_bind
142253
+#else
142254
+ 0
142255
+#endif
142111142256
};
142112142257
142113142258
/* True if x is the directory separator character
142114142259
*/
142115142260
#if SQLITE_OS_WIN
@@ -147528,12 +147673,10 @@
147528147673
pNew->op = TK_SELECT;
147529147674
pNew->selFlags = selFlags;
147530147675
pNew->iLimit = 0;
147531147676
pNew->iOffset = 0;
147532147677
pNew->selId = ++pParse->nSelect;
147533
- pNew->addrOpenEphm[0] = -1;
147534
- pNew->addrOpenEphm[1] = -1;
147535147678
pNew->nSelectRow = 0;
147536147679
if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1);
147537147680
pNew->pSrc = pSrc;
147538147681
pNew->pWhere = pWhere;
147539147682
pNew->pGroupBy = pGroupBy;
@@ -148677,33 +148820,10 @@
148677148820
codeOffset(v, p->iOffset, iContinue);
148678148821
}
148679148822
}
148680148823
148681148824
switch( eDest ){
148682
- /* In this mode, write each query result to the key of the temporary
148683
- ** table iParm.
148684
- */
148685
-#ifndef SQLITE_OMIT_COMPOUND_SELECT
148686
- case SRT_Union: {
148687
- int r1;
148688
- r1 = sqlite3GetTempReg(pParse);
148689
- sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
148690
- sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
148691
- sqlite3ReleaseTempReg(pParse, r1);
148692
- break;
148693
- }
148694
-
148695
- /* Construct a record from the query result, but instead of
148696
- ** saving that record, use it as a key to delete elements from
148697
- ** the temporary table iParm.
148698
- */
148699
- case SRT_Except: {
148700
- sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
148701
- break;
148702
- }
148703
-#endif /* SQLITE_OMIT_COMPOUND_SELECT */
148704
-
148705148825
/* Store the result as data using a unique key.
148706148826
*/
148707148827
case SRT_Fifo:
148708148828
case SRT_DistFifo:
148709148829
case SRT_Table:
@@ -149986,11 +150106,11 @@
149986150106
**
149987150107
** Space to hold the KeyInfo structure is obtained from malloc. The calling
149988150108
** function is responsible for ensuring that this structure is eventually
149989150109
** freed.
149990150110
*/
149991
-static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
150111
+static KeyInfo *multiSelectByMergeKeyInfo(Parse *pParse, Select *p, int nExtra){
149992150112
ExprList *pOrderBy = p->pOrderBy;
149993150113
int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
149994150114
sqlite3 *db = pParse->db;
149995150115
KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
149996150116
if( pRet ){
@@ -150121,21 +150241,41 @@
150121150241
150122150242
/* Allocate cursors for Current, Queue, and Distinct. */
150123150243
regCurrent = ++pParse->nMem;
150124150244
sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
150125150245
if( pOrderBy ){
150126
- KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
150246
+ KeyInfo *pKeyInfo = multiSelectByMergeKeyInfo(pParse, p, 1);
150127150247
sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
150128150248
(char*)pKeyInfo, P4_KEYINFO);
150129150249
destQueue.pOrderBy = pOrderBy;
150130150250
}else{
150131150251
sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
150132150252
}
150133150253
VdbeComment((v, "Queue table"));
150134150254
if( iDistinct ){
150135
- p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
150136
- p->selFlags |= SF_UsesEphemeral;
150255
+ /* Generate an ephemeral table used to enforce distinctness on the
150256
+ ** output of the recursive part of the CTE.
150257
+ */
150258
+ KeyInfo *pKeyInfo; /* Collating sequence for the result set */
150259
+ CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
150260
+
150261
+ assert( p->pNext==0 );
150262
+ assert( p->pEList!=0 );
150263
+ nCol = p->pEList->nExpr;
150264
+ pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nCol, 1);
150265
+ if( pKeyInfo ){
150266
+ for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
150267
+ *apColl = multiSelectCollSeq(pParse, p, i);
150268
+ if( 0==*apColl ){
150269
+ *apColl = pParse->db->pDfltColl;
150270
+ }
150271
+ }
150272
+ sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iDistinct, nCol, 0,
150273
+ (void*)pKeyInfo, P4_KEYINFO);
150274
+ }else{
150275
+ assert( pParse->nErr>0 );
150276
+ }
150137150277
}
150138150278
150139150279
/* Detach the ORDER BY clause from the compound SELECT */
150140150280
p->pOrderBy = 0;
150141150281
@@ -150206,11 +150346,11 @@
150206150346
return;
150207150347
}
150208150348
#endif /* SQLITE_OMIT_CTE */
150209150349
150210150350
/* Forward references */
150211
-static int multiSelectOrderBy(
150351
+static int multiSelectByMerge(
150212150352
Parse *pParse, /* Parsing context */
150213150353
Select *p, /* The right-most of SELECTs to be coded */
150214150354
SelectDest *pDest /* What to do with query results */
150215150355
);
150216150356
@@ -150355,317 +150495,80 @@
150355150495
#ifndef SQLITE_OMIT_CTE
150356150496
if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
150357150497
generateWithRecursiveQuery(pParse, p, &dest);
150358150498
}else
150359150499
#endif
150360
-
150361
- /* Compound SELECTs that have an ORDER BY clause are handled separately.
150362
- */
150363150500
if( p->pOrderBy ){
150364
- return multiSelectOrderBy(pParse, p, pDest);
150501
+ /* If the compound has an ORDER BY clause, then always use the merge
150502
+ ** algorithm. */
150503
+ return multiSelectByMerge(pParse, p, pDest);
150504
+ }else if( p->op!=TK_ALL ){
150505
+ /* If the compound is EXCEPT, INTERSECT, or UNION (anything other than
150506
+ ** UNION ALL) then also always use the merge algorithm. However, the
150507
+ ** multiSelectByMerge() routine requires that the compound have an
150508
+ ** ORDER BY clause, and it doesn't right now. So invent one first. */
150509
+ Expr *pOne = sqlite3ExprInt32(db, 1);
150510
+ p->pOrderBy = sqlite3ExprListAppend(pParse, 0, pOne);
150511
+ if( pParse->nErr ) goto multi_select_end;
150512
+ assert( p->pOrderBy!=0 );
150513
+ p->pOrderBy->a[0].u.x.iOrderByCol = 1;
150514
+ return multiSelectByMerge(pParse, p, pDest);
150365150515
}else{
150516
+ /* For a UNION ALL compound without ORDER BY, simply run the left
150517
+ ** query, then run the right query */
150518
+ int addr = 0;
150519
+ int nLimit = 0; /* Initialize to suppress harmless compiler warning */
150366150520
150367150521
#ifndef SQLITE_OMIT_EXPLAIN
150368150522
if( pPrior->pPrior==0 ){
150369150523
ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
150370150524
ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
150371150525
}
150372150526
#endif
150373
-
150374
- /* Generate code for the left and right SELECT statements.
150375
- */
150376
- switch( p->op ){
150377
- case TK_ALL: {
150378
- int addr = 0;
150379
- int nLimit = 0; /* Initialize to suppress harmless compiler warning */
150380
- assert( !pPrior->pLimit );
150381
- pPrior->iLimit = p->iLimit;
150382
- pPrior->iOffset = p->iOffset;
150383
- pPrior->pLimit = p->pLimit;
150384
- TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
150385
- rc = sqlite3Select(pParse, pPrior, &dest);
150386
- pPrior->pLimit = 0;
150387
- if( rc ){
150388
- goto multi_select_end;
150389
- }
150390
- p->pPrior = 0;
150391
- p->iLimit = pPrior->iLimit;
150392
- p->iOffset = pPrior->iOffset;
150393
- if( p->iLimit ){
150394
- addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
150395
- VdbeComment((v, "Jump ahead if LIMIT reached"));
150396
- if( p->iOffset ){
150397
- sqlite3VdbeAddOp3(v, OP_OffsetLimit,
150398
- p->iLimit, p->iOffset+1, p->iOffset);
150399
- }
150400
- }
150401
- ExplainQueryPlan((pParse, 1, "UNION ALL"));
150402
- TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
150403
- rc = sqlite3Select(pParse, p, &dest);
150404
- testcase( rc!=SQLITE_OK );
150405
- pDelete = p->pPrior;
150406
- p->pPrior = pPrior;
150407
- p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
150408
- if( p->pLimit
150409
- && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
150410
- && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
150411
- ){
150412
- p->nSelectRow = sqlite3LogEst((u64)nLimit);
150413
- }
150414
- if( addr ){
150415
- sqlite3VdbeJumpHere(v, addr);
150416
- }
150417
- break;
150418
- }
150419
- case TK_EXCEPT:
150420
- case TK_UNION: {
150421
- int unionTab; /* Cursor number of the temp table holding result */
150422
- u8 op = 0; /* One of the SRT_ operations to apply to self */
150423
- int priorOp; /* The SRT_ operation to apply to prior selects */
150424
- Expr *pLimit; /* Saved values of p->nLimit */
150425
- int addr;
150426
- int emptyBypass = 0; /* IfEmpty opcode to bypass RHS */
150427
- SelectDest uniondest;
150428
-
150429
-
150430
- testcase( p->op==TK_EXCEPT );
150431
- testcase( p->op==TK_UNION );
150432
- priorOp = SRT_Union;
150433
- if( dest.eDest==priorOp ){
150434
- /* We can reuse a temporary table generated by a SELECT to our
150435
- ** right.
150436
- */
150437
- assert( p->pLimit==0 ); /* Not allowed on leftward elements */
150438
- unionTab = dest.iSDParm;
150439
- }else{
150440
- /* We will need to create our own temporary table to hold the
150441
- ** intermediate results.
150442
- */
150443
- unionTab = pParse->nTab++;
150444
- assert( p->pOrderBy==0 );
150445
- addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
150446
- assert( p->addrOpenEphm[0] == -1 );
150447
- p->addrOpenEphm[0] = addr;
150448
- findRightmost(p)->selFlags |= SF_UsesEphemeral;
150449
- assert( p->pEList );
150450
- }
150451
-
150452
-
150453
- /* Code the SELECT statements to our left
150454
- */
150455
- assert( !pPrior->pOrderBy );
150456
- sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
150457
- TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
150458
- rc = sqlite3Select(pParse, pPrior, &uniondest);
150459
- if( rc ){
150460
- goto multi_select_end;
150461
- }
150462
-
150463
- /* Code the current SELECT statement
150464
- */
150465
- if( p->op==TK_EXCEPT ){
150466
- op = SRT_Except;
150467
- emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, unionTab);
150468
- VdbeCoverage(v);
150469
- }else{
150470
- assert( p->op==TK_UNION );
150471
- op = SRT_Union;
150472
- }
150473
- p->pPrior = 0;
150474
- pLimit = p->pLimit;
150475
- p->pLimit = 0;
150476
- uniondest.eDest = op;
150477
- ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
150478
- sqlite3SelectOpName(p->op)));
150479
- TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
150480
- rc = sqlite3Select(pParse, p, &uniondest);
150481
- testcase( rc!=SQLITE_OK );
150482
- assert( p->pOrderBy==0 );
150483
- pDelete = p->pPrior;
150484
- p->pPrior = pPrior;
150485
- p->pOrderBy = 0;
150486
- if( p->op==TK_UNION ){
150487
- p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
150488
- }
150489
- if( emptyBypass ) sqlite3VdbeJumpHere(v, emptyBypass);
150490
- sqlite3ExprDelete(db, p->pLimit);
150491
- p->pLimit = pLimit;
150492
- p->iLimit = 0;
150493
- p->iOffset = 0;
150494
-
150495
- /* Convert the data in the temporary table into whatever form
150496
- ** it is that we currently need.
150497
- */
150498
- assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
150499
- assert( p->pEList || db->mallocFailed );
150500
- if( dest.eDest!=priorOp && db->mallocFailed==0 ){
150501
- int iCont, iBreak, iStart;
150502
- iBreak = sqlite3VdbeMakeLabel(pParse);
150503
- iCont = sqlite3VdbeMakeLabel(pParse);
150504
- computeLimitRegisters(pParse, p, iBreak);
150505
- sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
150506
- iStart = sqlite3VdbeCurrentAddr(v);
150507
- selectInnerLoop(pParse, p, unionTab,
150508
- 0, 0, &dest, iCont, iBreak);
150509
- sqlite3VdbeResolveLabel(v, iCont);
150510
- sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
150511
- sqlite3VdbeResolveLabel(v, iBreak);
150512
- sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
150513
- }
150514
- break;
150515
- }
150516
- default: assert( p->op==TK_INTERSECT ); {
150517
- int tab1, tab2;
150518
- int iCont, iBreak, iStart;
150519
- Expr *pLimit;
150520
- int addr, iLimit, iOffset;
150521
- SelectDest intersectdest;
150522
- int r1;
150523
- int emptyBypass;
150524
-
150525
- /* INTERSECT is different from the others since it requires
150526
- ** two temporary tables. Hence it has its own case. Begin
150527
- ** by allocating the tables we will need.
150528
- */
150529
- tab1 = pParse->nTab++;
150530
- tab2 = pParse->nTab++;
150531
- assert( p->pOrderBy==0 );
150532
-
150533
- addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
150534
- assert( p->addrOpenEphm[0] == -1 );
150535
- p->addrOpenEphm[0] = addr;
150536
- findRightmost(p)->selFlags |= SF_UsesEphemeral;
150537
- assert( p->pEList );
150538
-
150539
- /* Code the SELECTs to our left into temporary table "tab1".
150540
- */
150541
- sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
150542
- TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
150543
- rc = sqlite3Select(pParse, pPrior, &intersectdest);
150544
- if( rc ){
150545
- goto multi_select_end;
150546
- }
150547
-
150548
- /* Initialize LIMIT counters before checking to see if the LHS
150549
- ** is empty, in case the jump is taken */
150550
- iBreak = sqlite3VdbeMakeLabel(pParse);
150551
- computeLimitRegisters(pParse, p, iBreak);
150552
- emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, tab1); VdbeCoverage(v);
150553
-
150554
- /* Code the current SELECT into temporary table "tab2"
150555
- */
150556
- addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
150557
- assert( p->addrOpenEphm[1] == -1 );
150558
- p->addrOpenEphm[1] = addr;
150559
-
150560
- /* Disable prior SELECTs and the LIMIT counters during the computation
150561
- ** of the RHS select */
150562
- pLimit = p->pLimit;
150563
- iLimit = p->iLimit;
150564
- iOffset = p->iOffset;
150565
- p->pPrior = 0;
150566
- p->pLimit = 0;
150567
- p->iLimit = 0;
150568
- p->iOffset = 0;
150569
-
150570
- intersectdest.iSDParm = tab2;
150571
- ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
150572
- sqlite3SelectOpName(p->op)));
150573
- TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
150574
- rc = sqlite3Select(pParse, p, &intersectdest);
150575
- testcase( rc!=SQLITE_OK );
150576
- pDelete = p->pPrior;
150577
- p->pPrior = pPrior;
150578
- if( p->nSelectRow>pPrior->nSelectRow ){
150579
- p->nSelectRow = pPrior->nSelectRow;
150580
- }
150581
- sqlite3ExprDelete(db, p->pLimit);
150582
-
150583
- /* Reinstate the LIMIT counters prior to running the final intersect */
150584
- p->pLimit = pLimit;
150585
- p->iLimit = iLimit;
150586
- p->iOffset = iOffset;
150587
-
150588
- /* Generate code to take the intersection of the two temporary
150589
- ** tables.
150590
- */
150591
- if( rc ) break;
150592
- assert( p->pEList );
150593
- sqlite3VdbeAddOp1(v, OP_Rewind, tab1);
150594
- r1 = sqlite3GetTempReg(pParse);
150595
- iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
150596
- iCont = sqlite3VdbeMakeLabel(pParse);
150597
- sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
150598
- VdbeCoverage(v);
150599
- sqlite3ReleaseTempReg(pParse, r1);
150600
- selectInnerLoop(pParse, p, tab1,
150601
- 0, 0, &dest, iCont, iBreak);
150602
- sqlite3VdbeResolveLabel(v, iCont);
150603
- sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
150604
- sqlite3VdbeResolveLabel(v, iBreak);
150605
- sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
150606
- sqlite3VdbeJumpHere(v, emptyBypass);
150607
- sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
150608
- break;
150609
- }
150610
- }
150611
-
150612
- #ifndef SQLITE_OMIT_EXPLAIN
150527
+ assert( !pPrior->pLimit );
150528
+ pPrior->iLimit = p->iLimit;
150529
+ pPrior->iOffset = p->iOffset;
150530
+ pPrior->pLimit = sqlite3ExprDup(db, p->pLimit, 0);
150531
+ TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
150532
+ rc = sqlite3Select(pParse, pPrior, &dest);
150533
+ sqlite3ExprDelete(db, pPrior->pLimit);
150534
+ pPrior->pLimit = 0;
150535
+ if( rc ){
150536
+ goto multi_select_end;
150537
+ }
150538
+ p->pPrior = 0;
150539
+ p->iLimit = pPrior->iLimit;
150540
+ p->iOffset = pPrior->iOffset;
150541
+ if( p->iLimit ){
150542
+ addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
150543
+ VdbeComment((v, "Jump ahead if LIMIT reached"));
150544
+ if( p->iOffset ){
150545
+ sqlite3VdbeAddOp3(v, OP_OffsetLimit,
150546
+ p->iLimit, p->iOffset+1, p->iOffset);
150547
+ }
150548
+ }
150549
+ ExplainQueryPlan((pParse, 1, "UNION ALL"));
150550
+ TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
150551
+ rc = sqlite3Select(pParse, p, &dest);
150552
+ testcase( rc!=SQLITE_OK );
150553
+ pDelete = p->pPrior;
150554
+ p->pPrior = pPrior;
150555
+ p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
150556
+ if( p->pLimit
150557
+ && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
150558
+ && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
150559
+ ){
150560
+ p->nSelectRow = sqlite3LogEst((u64)nLimit);
150561
+ }
150562
+ if( addr ){
150563
+ sqlite3VdbeJumpHere(v, addr);
150564
+ }
150565
+#ifndef SQLITE_OMIT_EXPLAIN
150613150566
if( p->pNext==0 ){
150614150567
ExplainQueryPlanPop(pParse);
150615150568
}
150616
- #endif
150617
- }
150618
- if( pParse->nErr ) goto multi_select_end;
150619
-
150620
- /* Compute collating sequences used by
150621
- ** temporary tables needed to implement the compound select.
150622
- ** Attach the KeyInfo structure to all temporary tables.
150623
- **
150624
- ** This section is run by the right-most SELECT statement only.
150625
- ** SELECT statements to the left always skip this part. The right-most
150626
- ** SELECT might also skip this part if it has no ORDER BY clause and
150627
- ** no temp tables are required.
150628
- */
150629
- if( p->selFlags & SF_UsesEphemeral ){
150630
- int i; /* Loop counter */
150631
- KeyInfo *pKeyInfo; /* Collating sequence for the result set */
150632
- Select *pLoop; /* For looping through SELECT statements */
150633
- CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
150634
- int nCol; /* Number of columns in result set */
150635
-
150636
- assert( p->pNext==0 );
150637
- assert( p->pEList!=0 );
150638
- nCol = p->pEList->nExpr;
150639
- pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
150640
- if( !pKeyInfo ){
150641
- rc = SQLITE_NOMEM_BKPT;
150642
- goto multi_select_end;
150643
- }
150644
- for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
150645
- *apColl = multiSelectCollSeq(pParse, p, i);
150646
- if( 0==*apColl ){
150647
- *apColl = db->pDfltColl;
150648
- }
150649
- }
150650
-
150651
- for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
150652
- for(i=0; i<2; i++){
150653
- int addr = pLoop->addrOpenEphm[i];
150654
- if( addr<0 ){
150655
- /* If [0] is unused then [1] is also unused. So we can
150656
- ** always safely abort as soon as the first unused slot is found */
150657
- assert( pLoop->addrOpenEphm[1]<0 );
150658
- break;
150659
- }
150660
- sqlite3VdbeChangeP2(v, addr, nCol);
150661
- sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
150662
- P4_KEYINFO);
150663
- pLoop->addrOpenEphm[i] = -1;
150664
- }
150665
- }
150666
- sqlite3KeyInfoUnref(pKeyInfo);
150569
+#endif
150667150570
}
150668150571
150669150572
multi_select_end:
150670150573
pDest->iSdst = dest.iSdst;
150671150574
pDest->nSdst = dest.nSdst;
@@ -150693,12 +150596,12 @@
150693150596
150694150597
/*
150695150598
** Code an output subroutine for a coroutine implementation of a
150696150599
** SELECT statement.
150697150600
**
150698
-** The data to be output is contained in pIn->iSdst. There are
150699
-** pIn->nSdst columns to be output. pDest is where the output should
150601
+** The data to be output is contained in an array of pIn->nSdst registers
150602
+** starting at register pIn->iSdst. pDest is where the output should
150700150603
** be sent.
150701150604
**
150702150605
** regReturn is the number of the register holding the subroutine
150703150606
** return address.
150704150607
**
@@ -150723,10 +150626,12 @@
150723150626
){
150724150627
Vdbe *v = pParse->pVdbe;
150725150628
int iContinue;
150726150629
int addr;
150727150630
150631
+ assert( pIn->eDest==SRT_Coroutine );
150632
+
150728150633
addr = sqlite3VdbeCurrentAddr(v);
150729150634
iContinue = sqlite3VdbeMakeLabel(pParse);
150730150635
150731150636
/* Suppress duplicates for UNION, EXCEPT, and INTERSECT
150732150637
*/
@@ -150744,26 +150649,63 @@
150744150649
150745150650
/* Suppress the first OFFSET entries if there is an OFFSET clause
150746150651
*/
150747150652
codeOffset(v, p->iOffset, iContinue);
150748150653
150749
- assert( pDest->eDest!=SRT_Exists );
150750
- assert( pDest->eDest!=SRT_Table );
150751150654
switch( pDest->eDest ){
150752150655
/* Store the result as data using a unique key.
150753150656
*/
150657
+ case SRT_Fifo:
150658
+ case SRT_DistFifo:
150659
+ case SRT_Table:
150754150660
case SRT_EphemTab: {
150755150661
int r1 = sqlite3GetTempReg(pParse);
150756150662
int r2 = sqlite3GetTempReg(pParse);
150663
+ int iParm = pDest->iSDParm;
150664
+ testcase( pDest->eDest==SRT_Table );
150665
+ testcase( pDest->eDest==SRT_EphemTab );
150666
+ testcase( pDest->eDest==SRT_Fifo );
150667
+ testcase( pDest->eDest==SRT_DistFifo );
150757150668
sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
150758
- sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
150759
- sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
150669
+#if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG)
150670
+ /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
150671
+ ** that this is an "UPDATE ... FROM" on a virtual table or view. In this
150672
+ ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC.
150673
+ ** This does not affect operation in any way - it just allows MakeRecord
150674
+ ** to process OPFLAG_NOCHANGE values without an assert() failing. */
150675
+ if( pDest->eDest==SRT_Table && pDest->iSDParm2 ){
150676
+ sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
150677
+ }
150678
+#endif
150679
+#ifndef SQLITE_OMIT_CTE
150680
+ if( pDest->eDest==SRT_DistFifo ){
150681
+ /* If the destination is DistFifo, then cursor (iParm+1) is open
150682
+ ** on an ephemeral index that is used to enforce uniqueness on the
150683
+ ** total result. At this point, we are processing the setup portion
150684
+ ** of the recursive CTE using the merge algorithm, so the results are
150685
+ ** guaranteed to be unique anyhow. But we still need to populate the
150686
+ ** (iParm+1) cursor for use by the subsequent recursive phase.
150687
+ */
150688
+ sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,
150689
+ pIn->iSdst, pIn->nSdst);
150690
+ }
150691
+#endif
150692
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
150693
+ sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
150760150694
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
150761150695
sqlite3ReleaseTempReg(pParse, r2);
150762150696
sqlite3ReleaseTempReg(pParse, r1);
150763150697
break;
150764150698
}
150699
+
150700
+ /* If any row exist in the result set, record that fact and abort.
150701
+ */
150702
+ case SRT_Exists: {
150703
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
150704
+ /* The LIMIT clause will terminate the loop for us */
150705
+ break;
150706
+ }
150765150707
150766150708
#ifndef SQLITE_OMIT_SUBQUERY
150767150709
/* If we are creating a set for an "expr IN (SELECT ...)".
150768150710
*/
150769150711
case SRT_Set: {
@@ -150806,14 +150748,74 @@
150806150748
}
150807150749
sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
150808150750
sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
150809150751
break;
150810150752
}
150753
+
150754
+#ifndef SQLITE_OMIT_CTE
150755
+ /* Write the results into a priority queue that is order according to
150756
+ ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
150757
+ ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
150758
+ ** pSO->nExpr columns, then make sure all keys are unique by adding a
150759
+ ** final OP_Sequence column. The last column is the record as a blob.
150760
+ */
150761
+ case SRT_DistQueue:
150762
+ case SRT_Queue: {
150763
+ int nKey;
150764
+ int r1, r2, r3, ii;
150765
+ ExprList *pSO;
150766
+ int iParm = pDest->iSDParm;
150767
+ pSO = pDest->pOrderBy;
150768
+ assert( pSO );
150769
+ nKey = pSO->nExpr;
150770
+ r1 = sqlite3GetTempReg(pParse);
150771
+ r2 = sqlite3GetTempRange(pParse, nKey+2);
150772
+ r3 = r2+nKey+1;
150773
+
150774
+#if 0 /* <-- Why the next block of code is commented out: (tag-20260125-a)
150775
+ **
150776
+ ** If the destination is DistQueue, then cursor (iParm+1) is open
150777
+ ** on a second ephemeral index that holds all values previously
150778
+ ** added to the queue. This code only runs during the setup phase
150779
+ ** using the merge algorithm, and so the values here are already
150780
+ ** guaranteed to be unique.
150781
+ */
150782
+ if( pDest->eDest==SRT_DistQueue ){
150783
+ addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
150784
+ pIn->iSdst, pIn->nSdst);
150785
+ VdbeCoverage(v);
150786
+ }
150787
+#endif
150788
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r3);
150789
+ if( pDest->eDest==SRT_DistQueue ){
150790
+ sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
150791
+ sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
150792
+ }
150793
+ for(ii=0; ii<nKey; ii++){
150794
+ sqlite3VdbeAddOp2(v, OP_SCopy,
150795
+ pIn->iSdst + pSO->a[ii].u.x.iOrderByCol - 1,
150796
+ r2+ii);
150797
+ }
150798
+ sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
150799
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
150800
+ sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
150801
+#if 0 /* tag-20260125-a */
150802
+ if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
150803
+#endif
150804
+ sqlite3ReleaseTempReg(pParse, r1);
150805
+ sqlite3ReleaseTempRange(pParse, r2, nKey+2);
150806
+ break;
150807
+ }
150808
+#endif /* SQLITE_OMIT_CTE */
150809
+
150810
+ /* Ignore the output */
150811
+ case SRT_Discard: {
150812
+ break;
150813
+ }
150811150814
150812150815
/* If none of the above, then the result destination must be
150813
- ** SRT_Output. This routine is never called with any other
150814
- ** destination other than the ones handled above or SRT_Output.
150816
+ ** SRT_Output.
150815150817
**
150816150818
** For SRT_Output, results are stored in a sequence of registers.
150817150819
** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
150818150820
** return the next row of result.
150819150821
*/
@@ -150837,12 +150839,13 @@
150837150839
150838150840
return addr;
150839150841
}
150840150842
150841150843
/*
150842
-** Alternative compound select code generator for cases when there
150843
-** is an ORDER BY clause.
150844
+** Generate code for a compound SELECT statement using a merge
150845
+** algorithm. The compound must have an ORDER BY clause for this
150846
+** to work.
150844150847
**
150845150848
** We assume a query of the following form:
150846150849
**
150847150850
** <selectA> <operator> <selectB> ORDER BY <orderbylist>
150848150851
**
@@ -150855,11 +150858,11 @@
150855150858
** outA: Move the output of the selectA coroutine into the output
150856150859
** of the compound query.
150857150860
**
150858150861
** outB: Move the output of the selectB coroutine into the output
150859150862
** of the compound query. (Only generated for UNION and
150860
-** UNION ALL. EXCEPT and INSERTSECT never output a row that
150863
+** UNION ALL. EXCEPT and INTERSECT never output a row that
150861150864
** appears only in B.)
150862150865
**
150863150866
** AltB: Called when there is data from both coroutines and A<B.
150864150867
**
150865150868
** AeqB: Called when there is data from both coroutines and A==B.
@@ -150919,14 +150922,14 @@
150919150922
** End: ...
150920150923
**
150921150924
** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
150922150925
** actually called using Gosub and they do not Return. EofA and EofB loop
150923150926
** until all data is exhausted then jump to the "end" label. AltB, AeqB,
150924
-** and AgtB jump to either L2 or to one of EofA or EofB.
150927
+** and AgtB jump to either Cmpr or to one of EofA or EofB.
150925150928
*/
150926150929
#ifndef SQLITE_OMIT_COMPOUND_SELECT
150927
-static int multiSelectOrderBy(
150930
+static int multiSelectByMerge(
150928150931
Parse *pParse, /* Parsing context */
150929150932
Select *p, /* The right-most of SELECTs to be coded */
150930150933
SelectDest *pDest /* What to do with query results */
150931150934
){
150932150935
int i, j; /* Loop counters */
@@ -151019,11 +151022,11 @@
151019151022
assert( pItem!=0 );
151020151023
assert( pItem->u.x.iOrderByCol>0 );
151021151024
assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
151022151025
aPermute[i] = pItem->u.x.iOrderByCol - 1;
151023151026
}
151024
- pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
151027
+ pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1);
151025151028
}else{
151026151029
pKeyMerge = 0;
151027151030
}
151028151031
151029151032
/* Allocate a range of temporary registers and the KeyInfo needed
@@ -152131,11 +152134,11 @@
152131152134
pItem->fg.jointype |= (jointype & JT_LTORJ);
152132152135
memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
152133152136
}
152134152137
pSubitem->fg.jointype |= jointype;
152135152138
152136
- /* Now begin substituting subquery result set expressions for
152139
+ /* Begin substituting subquery result set expressions for
152137152140
** references to the iParent in the outer query.
152138152141
**
152139152142
** Example:
152140152143
**
152141152144
** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
@@ -152143,21 +152146,21 @@
152143152146
** \_____________________ outer query ______________________________/
152144152147
**
152145152148
** We look at every expression in the outer query and every place we see
152146152149
** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
152147152150
*/
152148
- if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
152151
+ if( pSub->pOrderBy ){
152149152152
/* At this point, any non-zero iOrderByCol values indicate that the
152150152153
** ORDER BY column expression is identical to the iOrderByCol'th
152151152154
** expression returned by SELECT statement pSub. Since these values
152152152155
** do not necessarily correspond to columns in SELECT statement pParent,
152153152156
** zero them before transferring the ORDER BY clause.
152154152157
**
152155152158
** Not doing this may cause an error if a subsequent call to this
152156
- ** function attempts to flatten a compound sub-query into pParent
152157
- ** (the only way this can happen is if the compound sub-query is
152158
- ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
152159
+ ** function attempts to flatten a compound sub-query into pParent.
152160
+ ** See ticket [d11a6e908f].
152161
+ */
152159152162
ExprList *pOrderBy = pSub->pOrderBy;
152160152163
for(i=0; i<pOrderBy->nExpr; i++){
152161152164
pOrderBy->a[i].u.x.iOrderByCol = 0;
152162152165
}
152163152166
assert( pParent->pOrderBy==0 );
@@ -153005,18 +153008,18 @@
153005153008
** These are rewritten as a subquery:
153006153009
**
153007153010
** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
153008153011
** ORDER BY ... COLLATE ...
153009153012
**
153010
-** This transformation is necessary because the multiSelectOrderBy() routine
153013
+** This transformation is necessary because the multiSelectByMerge() routine
153011153014
** above that generates the code for a compound SELECT with an ORDER BY clause
153012153015
** uses a merge algorithm that requires the same collating sequence on the
153013153016
** result columns as on the ORDER BY clause. See ticket
153014153017
** http://sqlite.org/src/info/6709574d2a
153015153018
**
153016153019
** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
153017
-** The UNION ALL operator works fine with multiSelectOrderBy() even when
153020
+** The UNION ALL operator works fine with multiSelectByMerge() even when
153018153021
** there are COLLATE terms in the ORDER BY.
153019153022
*/
153020153023
static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
153021153024
int i;
153022153025
Select *pNew;
@@ -154862,11 +154865,10 @@
154862154865
pWhere->op = TK_INTEGER;
154863154866
pWhere->u.iValue = 1;
154864154867
ExprSetProperty(pWhere, EP_IntValue);
154865154868
assert( p->pWhere!=0 );
154866154869
pSub->pSrc->a[0].fg.fromExists = 1;
154867
- pSub->pSrc->a[0].fg.jointype |= JT_CROSS;
154868154870
p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc);
154869154871
if( pSubWhere ){
154870154872
p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSubWhere);
154871154873
pSub->pWhere = 0;
154872154874
}
@@ -155111,12 +155113,11 @@
155111155113
assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
155112155114
assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
155113155115
assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
155114155116
assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
155115155117
if( IgnorableDistinct(pDest) ){
155116
- assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
155117
- pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
155118
+ assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Discard ||
155118155119
pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
155119155120
/* All of these destinations are also able to ignore the ORDER BY clause */
155120155121
if( p->pOrderBy ){
155121155122
#if TREETRACE_ENABLED
155122155123
TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
@@ -155128,11 +155129,10 @@
155128155129
p->pOrderBy);
155129155130
testcase( pParse->earlyCleanup );
155130155131
p->pOrderBy = 0;
155131155132
}
155132155133
p->selFlags &= ~(u32)SF_Distinct;
155133
- p->selFlags |= SF_NoopOrderBy;
155134155134
}
155135155135
sqlite3SelectPrep(pParse, p, 0);
155136155136
if( pParse->nErr ){
155137155137
goto select_end;
155138155138
}
@@ -165904,20 +165904,26 @@
165904165904
u16 eOp = pOne->eOperator | pTwo->eOperator;
165905165905
sqlite3 *db; /* Database connection (for malloc) */
165906165906
Expr *pNew; /* New virtual expression */
165907165907
int op; /* Operator for the combined expression */
165908165908
int idxNew; /* Index in pWC of the next virtual term */
165909
+ Expr *pA, *pB; /* Expressions associated with pOne and pTwo */
165909165910
165910165911
if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
165911165912
if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
165912165913
if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
165913165914
if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
165914165915
&& (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
165915
- assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
165916
- assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
165917
- if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
165918
- if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
165916
+ pA = pOne->pExpr;
165917
+ pB = pTwo->pExpr;
165918
+ assert( pA->pLeft!=0 && pA->pRight!=0 );
165919
+ assert( pB->pLeft!=0 && pB->pRight!=0 );
165920
+ if( sqlite3ExprCompare(0,pA->pLeft, pB->pLeft, -1) ) return;
165921
+ if( sqlite3ExprCompare(0,pA->pRight, pB->pRight,-1) ) return;
165922
+ if( ExprHasProperty(pA,EP_Commuted)!=ExprHasProperty(pB,EP_Commuted) ){
165923
+ return;
165924
+ }
165919165925
/* If we reach this point, it means the two subterms can be combined */
165920165926
if( (eOp & (eOp-1))!=0 ){
165921165927
if( eOp & (WO_LT|WO_LE) ){
165922165928
eOp = WO_LE;
165923165929
}else{
@@ -165924,11 +165930,11 @@
165924165930
assert( eOp & (WO_GT|WO_GE) );
165925165931
eOp = WO_GE;
165926165932
}
165927165933
}
165928165934
db = pWC->pWInfo->pParse->db;
165929
- pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
165935
+ pNew = sqlite3ExprDup(db, pA, 0);
165930165936
if( pNew==0 ) return;
165931165937
for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
165932165938
pNew->op = op;
165933165939
idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
165934165940
exprAnalyze(pSrc, pWC, idxNew);
@@ -170766,10 +170772,11 @@
170766170772
170767170773
nOutUnadjusted = pNew->nOut;
170768170774
pNew->rRun += nInMul + nIn;
170769170775
pNew->nOut += nInMul + nIn;
170770170776
whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
170777
+ if( pSrc->fg.fromExists ) pNew->nOut = 0;
170771170778
rc = whereLoopInsert(pBuilder, pNew);
170772170779
170773170780
if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
170774170781
pNew->nOut = saved_nOut;
170775170782
}else{
@@ -171362,10 +171369,12 @@
171362171369
ApplyCostMultiplier(pNew->rRun, pTab->costMult);
171363171370
whereLoopOutputAdjust(pWC, pNew, rSize);
171364171371
if( pSrc->fg.isSubquery ){
171365171372
if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE;
171366171373
pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy;
171374
+ }else if( pSrc->fg.fromExists ){
171375
+ pNew->nOut = 0;
171367171376
}
171368171377
rc = whereLoopInsert(pBuilder, pNew);
171369171378
pNew->nOut = rSize;
171370171379
if( rc ) break;
171371171380
}else{
@@ -171464,10 +171473,11 @@
171464171473
/* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
171465171474
** because the cursor used to access the index might not be
171466171475
** positioned to the correct row during the right-join no-match
171467171476
** loop. */
171468171477
}else{
171478
+ if( pSrc->fg.fromExists ) pNew->nOut = 0;
171469171479
rc = whereLoopInsert(pBuilder, pNew);
171470171480
}
171471171481
pNew->nOut = rSize;
171472171482
if( rc ) break;
171473171483
}
@@ -172161,10 +172171,21 @@
172161172171
** is itself on the left side of a RIGHT JOIN.
172162172172
*/
172163172173
if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
172164172174
mPrereq |= mPrior;
172165172175
bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
172176
+ }else if( pItem->fg.fromExists ){
172177
+ /* joins that result from the EXISTS-to-JOIN optimization should not
172178
+ ** be moved to the left of any of their dependencies */
172179
+ WhereClause *pWC = &pWInfo->sWC;
172180
+ WhereTerm *pTerm;
172181
+ int i;
172182
+ for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
172183
+ if( (pNew->maskSelf & pTerm->prereqAll)!=0 ){
172184
+ mPrereq |= (pTerm->prereqAll & (pNew->maskSelf-1));
172185
+ }
172186
+ }
172166172187
}else if( !hasRightJoin ){
172167172188
mPrereq = 0;
172168172189
}
172169172190
#ifndef SQLITE_OMIT_VIRTUALTABLE
172170172191
if( IsVirtual(pItem->pSTab) ){
@@ -174713,26 +174734,31 @@
174713174734
VdbeCoverageIf(v, op==OP_SeekGT);
174714174735
sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
174715174736
}
174716174737
#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
174717174738
}
174718
- if( pTabList->a[pLevel->iFrom].fg.fromExists && i==pWInfo->nLevel-1 ){
174719
- /* If the EXISTS-to-JOIN optimization was applied, then the EXISTS
174720
- ** loop(s) will be the inner-most loops of the join. There might be
174721
- ** multiple EXISTS loops, but they will all be nested, and the join
174722
- ** order will not have been changed by the query planner. If the
174723
- ** inner-most EXISTS loop sees a single successful row, it should
174724
- ** break out of *all* EXISTS loops. But only the inner-most of the
174725
- ** nested EXISTS loops should do this breakout. */
174739
+ if( pTabList->a[pLevel->iFrom].fg.fromExists
174740
+ && (i==pWInfo->nLevel-1
174741
+ || pTabList->a[pWInfo->a[i+1].iFrom].fg.fromExists==0)
174742
+ ){
174743
+ /* This is an EXISTS-to-JOIN optimization which is either the
174744
+ ** inner-most loop, or the inner-most of a group of nested
174745
+ ** EXISTS-to-JOIN optimization loops. If this loop sees a successful
174746
+ ** row, it should break out of itself as well as other EXISTS-to-JOIN
174747
+ ** loops in which is is directly nested. */
174726174748
int nOuter = 0; /* Nr of outer EXISTS that this one is nested within */
174727174749
while( nOuter<i ){
174728174750
if( !pTabList->a[pLevel[-nOuter-1].iFrom].fg.fromExists ) break;
174729174751
nOuter++;
174730174752
}
174731174753
testcase( nOuter>0 );
174732174754
sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel[-nOuter].addrBrk);
174733
- VdbeComment((v, "EXISTS break"));
174755
+ if( nOuter ){
174756
+ VdbeComment((v, "EXISTS break %d..%d", i-nOuter, i));
174757
+ }else{
174758
+ VdbeComment((v, "EXISTS break %d", i));
174759
+ }
174734174760
}
174735174761
sqlite3VdbeResolveLabel(v, pLevel->addrCont);
174736174762
if( pLevel->op!=OP_Noop ){
174737174763
sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
174738174764
sqlite3VdbeChangeP5(v, pLevel->p5);
@@ -189501,10 +189527,16 @@
189501189527
/*
189502189528
** Find existing client data.
189503189529
*/
189504189530
SQLITE_API void *sqlite3_get_clientdata(sqlite3 *db, const char *zName){
189505189531
DbClientData *p;
189532
+#ifdef SQLITE_ENABLE_API_ARMOR
189533
+ if( !zName || !sqlite3SafetyCheckOk(db) ){
189534
+ (void)SQLITE_MISUSE_BKPT;
189535
+ return 0;
189536
+ }
189537
+#endif
189506189538
sqlite3_mutex_enter(db->mutex);
189507189539
for(p=db->pDbData; p; p=p->pNext){
189508189540
if( strcmp(p->zName, zName)==0 ){
189509189541
void *pResult = p->pData;
189510189542
sqlite3_mutex_leave(db->mutex);
@@ -211254,11 +211286,14 @@
211254211286
*/
211255211287
#define JSON_JSON 0x01 /* Result is always JSON */
211256211288
#define JSON_SQL 0x02 /* Result is always SQL */
211257211289
#define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
211258211290
#define JSON_ISSET 0x04 /* json_set(), not json_insert() */
211259
-#define JSON_BLOB 0x08 /* Use the BLOB output format */
211291
+#define JSON_AINS 0x08 /* json_array_insert(), not json_insert() */
211292
+#define JSON_BLOB 0x10 /* Use the BLOB output format */
211293
+
211294
+#define JSON_INSERT_TYPE(X) (((X)&0xC)>>2)
211260211295
211261211296
211262211297
/* A parsed JSON value. Lifecycle:
211263211298
**
211264211299
** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
@@ -211300,10 +211335,11 @@
211300211335
/* Allowed values for JsonParse.eEdit */
211301211336
#define JEDIT_DEL 1 /* Delete if exists */
211302211337
#define JEDIT_REPL 2 /* Overwrite if exists */
211303211338
#define JEDIT_INS 3 /* Insert if not exists */
211304211339
#define JEDIT_SET 4 /* Insert or overwrite */
211340
+#define JEDIT_AINS 5 /* array_insert() */
211305211341
211306211342
/*
211307211343
** Maximum nesting depth of JSON for this implementation.
211308211344
**
211309211345
** This limit is needed to avoid a stack overflow in the recursive
@@ -213796,11 +213832,12 @@
213796213832
/*
213797213833
** Error returns from jsonLookupStep()
213798213834
*/
213799213835
#define JSON_LOOKUP_ERROR 0xffffffff
213800213836
#define JSON_LOOKUP_NOTFOUND 0xfffffffe
213801
-#define JSON_LOOKUP_PATHERROR 0xfffffffd
213837
+#define JSON_LOOKUP_NOTARRAY 0xfffffffd
213838
+#define JSON_LOOKUP_PATHERROR 0xfffffffc
213802213839
#define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR)
213803213840
213804213841
/* Forward declaration */
213805213842
static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
213806213843
@@ -213825,11 +213862,11 @@
213825213862
** using the substructure.
213826213863
*/
213827213864
static u32 jsonCreateEditSubstructure(
213828213865
JsonParse *pParse, /* The original JSONB that is being edited */
213829213866
JsonParse *pIns, /* Populate this with the blob data to insert */
213830
- const char *zTail /* Tail of the path that determins substructure */
213867
+ const char *zTail /* Tail of the path that determines substructure */
213831213868
){
213832213869
static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
213833213870
int rc;
213834213871
memset(pIns, 0, sizeof(*pIns));
213835213872
pIns->db = pParse->db;
@@ -213860,13 +213897,13 @@
213860213897
** label, before returning.
213861213898
**
213862213899
** Return one of the JSON_LOOKUP error codes if problems are seen.
213863213900
**
213864213901
** This routine will also modify the blob. If pParse->eEdit is one of
213865
-** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
213866
-** made to the selected value. If an edit is performed, then the return
213867
-** value does not necessarily point to the select element. If an edit
213902
+** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, JEDIT_SET, or JEDIT_AINS, then changes
213903
+** might be made to the selected value. If an edit is performed, then the
213904
+** return value does not necessarily point to the select element. If an edit
213868213905
** is performed, the return value is only useful for detecting error
213869213906
** conditions.
213870213907
*/
213871213908
static u32 jsonLookupStep(
213872213909
JsonParse *pParse, /* The JSON to search */
@@ -213888,10 +213925,17 @@
213888213925
iRoot = iLabel;
213889213926
}
213890213927
jsonBlobEdit(pParse, iRoot, sz, 0, 0);
213891213928
}else if( pParse->eEdit==JEDIT_INS ){
213892213929
/* Already exists, so json_insert() is a no-op */
213930
+ }else if( pParse->eEdit==JEDIT_AINS ){
213931
+ /* json_array_insert() */
213932
+ if( zPath[-1]!=']' ){
213933
+ return JSON_LOOKUP_NOTARRAY;
213934
+ }else{
213935
+ jsonBlobEdit(pParse, iRoot, 0, pParse->aIns, pParse->nIns);
213936
+ }
213893213937
}else{
213894213938
/* json_set() or json_replace() */
213895213939
jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
213896213940
}
213897213941
}
@@ -213959,10 +214003,14 @@
213959214003
u32 nIns; /* Total bytes to insert (label+value) */
213960214004
JsonParse v; /* BLOB encoding of the value to be inserted */
213961214005
JsonParse ix; /* Header of the label to be inserted */
213962214006
testcase( pParse->eEdit==JEDIT_INS );
213963214007
testcase( pParse->eEdit==JEDIT_SET );
214008
+ testcase( pParse->eEdit==JEDIT_AINS );
214009
+ if( pParse->eEdit==JEDIT_AINS && sqlite3_strglob("*]",&zPath[i])!=0 ){
214010
+ return JSON_LOOKUP_NOTARRAY;
214011
+ }
213964214012
memset(&ix, 0, sizeof(ix));
213965214013
ix.db = pParse->db;
213966214014
jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0);
213967214015
pParse->oom |= ix.oom;
213968214016
rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
@@ -214034,10 +214082,11 @@
214034214082
if( j>iEnd ) return JSON_LOOKUP_ERROR;
214035214083
if( k>0 ) return JSON_LOOKUP_NOTFOUND;
214036214084
if( pParse->eEdit>=JEDIT_INS ){
214037214085
JsonParse v;
214038214086
testcase( pParse->eEdit==JEDIT_INS );
214087
+ testcase( pParse->eEdit==JEDIT_AINS );
214039214088
testcase( pParse->eEdit==JEDIT_SET );
214040214089
rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
214041214090
if( !JSON_LOOKUP_ISERROR(rc)
214042214091
&& jsonBlobMakeEditable(pParse, v.nBlob)
214043214092
){
@@ -214358,13 +214407,19 @@
214358214407
** If ctx is not NULL then push the error message into ctx and return NULL.
214359214408
** If ctx is NULL, then return the text of the error message.
214360214409
*/
214361214410
static char *jsonBadPathError(
214362214411
sqlite3_context *ctx, /* The function call containing the error */
214363
- const char *zPath /* The path with the problem */
214412
+ const char *zPath, /* The path with the problem */
214413
+ int rc /* Maybe JSON_LOOKUP_NOTARRAY */
214364214414
){
214365
- char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
214415
+ char *zMsg;
214416
+ if( rc==(int)JSON_LOOKUP_NOTARRAY ){
214417
+ zMsg = sqlite3_mprintf("not an array element: %Q", zPath);
214418
+ }else{
214419
+ zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
214420
+ }
214366214421
if( ctx==0 ) return zMsg;
214367214422
if( zMsg ){
214368214423
sqlite3_result_error(ctx, zMsg, -1);
214369214424
sqlite3_free(zMsg);
214370214425
}else{
@@ -214377,17 +214432,17 @@
214377214432
** arguments come in pairs where each pair contains a JSON path and
214378214433
** content to insert or set at that patch. Do the updates
214379214434
** and return the result.
214380214435
**
214381214436
** The specific operation is determined by eEdit, which can be one
214382
-** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
214437
+** of JEDIT_INS, JEDIT_REPL, JEDIT_SET, or JEDIT_AINS.
214383214438
*/
214384214439
static void jsonInsertIntoBlob(
214385214440
sqlite3_context *ctx,
214386214441
int argc,
214387214442
sqlite3_value **argv,
214388
- int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
214443
+ int eEdit /* JEDIT_INS, JEDIT_REPL, JEDIT_SET, JEDIT_AINS */
214389214444
){
214390214445
int i;
214391214446
u32 rc = 0;
214392214447
const char *zPath = 0;
214393214448
int flgs;
@@ -214435,11 +214490,11 @@
214435214490
jsonInsertIntoBlob_patherror:
214436214491
jsonParseFree(p);
214437214492
if( rc==JSON_LOOKUP_ERROR ){
214438214493
sqlite3_result_error(ctx, "malformed JSON", -1);
214439214494
}else{
214440
- jsonBadPathError(ctx, zPath);
214495
+ jsonBadPathError(ctx, zPath, rc);
214441214496
}
214442214497
return;
214443214498
}
214444214499
214445214500
/*
@@ -214877,11 +214932,11 @@
214877214932
i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
214878214933
if( JSON_LOOKUP_ISERROR(i) ){
214879214934
if( i==JSON_LOOKUP_NOTFOUND ){
214880214935
/* no-op */
214881214936
}else if( i==JSON_LOOKUP_PATHERROR ){
214882
- jsonBadPathError(ctx, zPath);
214937
+ jsonBadPathError(ctx, zPath, 0);
214883214938
}else{
214884214939
sqlite3_result_error(ctx, "malformed JSON", -1);
214885214940
}
214886214941
eErr = 1;
214887214942
i = 0;
@@ -214982,11 +215037,11 @@
214982215037
}
214983215038
jsonStringTerminate(&jx);
214984215039
j = jsonLookupStep(p, 0, jx.zBuf, 0);
214985215040
jsonStringReset(&jx);
214986215041
}else{
214987
- jsonBadPathError(ctx, zPath);
215042
+ jsonBadPathError(ctx, zPath, 0);
214988215043
goto json_extract_error;
214989215044
}
214990215045
if( j<p->nBlob ){
214991215046
if( argc==2 ){
214992215047
if( flags & JSON_JSON ){
@@ -215017,11 +215072,11 @@
215017215072
}
215018215073
}else if( j==JSON_LOOKUP_ERROR ){
215019215074
sqlite3_result_error(ctx, "malformed JSON", -1);
215020215075
goto json_extract_error;
215021215076
}else{
215022
- jsonBadPathError(ctx, zPath);
215077
+ jsonBadPathError(ctx, zPath, 0);
215023215078
goto json_extract_error;
215024215079
}
215025215080
}
215026215081
if( argc>2 ){
215027215082
jsonAppendChar(&jx, ']');
@@ -215346,11 +215401,11 @@
215346215401
rc = jsonLookupStep(p, 0, zPath+1, 0);
215347215402
if( JSON_LOOKUP_ISERROR(rc) ){
215348215403
if( rc==JSON_LOOKUP_NOTFOUND ){
215349215404
continue; /* No-op */
215350215405
}else if( rc==JSON_LOOKUP_PATHERROR ){
215351
- jsonBadPathError(ctx, zPath);
215406
+ jsonBadPathError(ctx, zPath, rc);
215352215407
}else{
215353215408
sqlite3_result_error(ctx, "malformed JSON", -1);
215354215409
}
215355215410
goto json_remove_done;
215356215411
}
@@ -215358,11 +215413,11 @@
215358215413
jsonReturnParse(ctx, p);
215359215414
jsonParseFree(p);
215360215415
return;
215361215416
215362215417
json_remove_patherror:
215363
- jsonBadPathError(ctx, zPath);
215418
+ jsonBadPathError(ctx, zPath, 0);
215364215419
215365215420
json_remove_done:
215366215421
jsonParseFree(p);
215367215422
return;
215368215423
}
@@ -215402,20 +215457,22 @@
215402215457
static void jsonSetFunc(
215403215458
sqlite3_context *ctx,
215404215459
int argc,
215405215460
sqlite3_value **argv
215406215461
){
215407
-
215408215462
int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215409
- int bIsSet = (flags&JSON_ISSET)!=0;
215463
+ int eInsType = JSON_INSERT_TYPE(flags);
215464
+ static const char *azInsType[] = { "insert", "set", "array_insert" };
215465
+ static const u8 aEditType[] = { JEDIT_INS, JEDIT_SET, JEDIT_AINS };
215410215466
215411215467
if( argc<1 ) return;
215468
+ assert( eInsType>=0 && eInsType<=2 );
215412215469
if( (argc&1)==0 ) {
215413
- jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
215470
+ jsonWrongNumArgs(ctx, azInsType[eInsType]);
215414215471
return;
215415215472
}
215416
- jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS);
215473
+ jsonInsertIntoBlob(ctx, argc, argv, aEditType[eInsType]);
215417215474
}
215418215475
215419215476
/*
215420215477
** json_type(JSON)
215421215478
** json_type(JSON, PATH)
@@ -215436,19 +215493,19 @@
215436215493
if( p==0 ) return;
215437215494
if( argc==2 ){
215438215495
zPath = (const char*)sqlite3_value_text(argv[1]);
215439215496
if( zPath==0 ) goto json_type_done;
215440215497
if( zPath[0]!='$' ){
215441
- jsonBadPathError(ctx, zPath);
215498
+ jsonBadPathError(ctx, zPath, 0);
215442215499
goto json_type_done;
215443215500
}
215444215501
i = jsonLookupStep(p, 0, zPath+1, 0);
215445215502
if( JSON_LOOKUP_ISERROR(i) ){
215446215503
if( i==JSON_LOOKUP_NOTFOUND ){
215447215504
/* no-op */
215448215505
}else if( i==JSON_LOOKUP_PATHERROR ){
215449
- jsonBadPathError(ctx, zPath);
215506
+ jsonBadPathError(ctx, zPath, 0);
215450215507
}else{
215451215508
sqlite3_result_error(ctx, "malformed JSON", -1);
215452215509
}
215453215510
goto json_type_done;
215454215511
}
@@ -215700,16 +215757,15 @@
215700215757
jsonAppendSqlValue(pStr, argv[0]);
215701215758
}
215702215759
}
215703215760
static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
215704215761
JsonString *pStr;
215762
+ int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215705215763
pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
215706215764
if( pStr ){
215707
- int flags;
215708215765
pStr->pCtx = ctx;
215709215766
jsonAppendChar(pStr, ']');
215710
- flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215711215767
if( pStr->eErr ){
215712215768
jsonReturnString(pStr, 0, 0);
215713215769
return;
215714215770
}else if( flags & JSON_BLOB ){
215715215771
jsonReturnStringAsBlob(pStr);
@@ -215726,10 +215782,13 @@
215726215782
pStr->bStatic = 1;
215727215783
}else{
215728215784
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
215729215785
jsonStringTrimOneChar(pStr);
215730215786
}
215787
+ }else if( flags & JSON_BLOB ){
215788
+ static const u8 emptyArray = 0x0b;
215789
+ sqlite3_result_blob(ctx, &emptyArray, 1, SQLITE_STATIC);
215731215790
}else{
215732215791
sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
215733215792
}
215734215793
sqlite3_result_subtype(ctx, JSON_SUBTYPE);
215735215794
}
@@ -215822,16 +215881,15 @@
215822215881
}
215823215882
}
215824215883
}
215825215884
static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
215826215885
JsonString *pStr;
215886
+ int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215827215887
pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
215828215888
if( pStr ){
215829
- int flags;
215830215889
jsonAppendChar(pStr, '}');
215831215890
pStr->pCtx = ctx;
215832
- flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215833215891
if( pStr->eErr ){
215834215892
jsonReturnString(pStr, 0, 0);
215835215893
return;
215836215894
}else if( flags & JSON_BLOB ){
215837215895
jsonReturnStringAsBlob(pStr);
@@ -215848,10 +215906,13 @@
215848215906
pStr->bStatic = 1;
215849215907
}else{
215850215908
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
215851215909
jsonStringTrimOneChar(pStr);
215852215910
}
215911
+ }else if( flags & JSON_BLOB ){
215912
+ static const unsigned char emptyObject = 0x0c;
215913
+ sqlite3_result_blob(ctx, &emptyObject, 1, SQLITE_STATIC);
215853215914
}else{
215854215915
sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
215855215916
}
215856215917
sqlite3_result_subtype(ctx, JSON_SUBTYPE);
215857215918
}
@@ -216348,11 +216409,11 @@
216348216409
if( idxNum==3 ){
216349216410
zRoot = (const char*)sqlite3_value_text(argv[1]);
216350216411
if( zRoot==0 ) return SQLITE_OK;
216351216412
if( zRoot[0]!='$' ){
216352216413
sqlite3_free(cur->pVtab->zErrMsg);
216353
- cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
216414
+ cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot, 0);
216354216415
jsonEachCursorReset(p);
216355216416
return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
216356216417
}
216357216418
p->nRoot = sqlite3Strlen30(zRoot);
216358216419
if( zRoot[1]==0 ){
@@ -216366,11 +216427,11 @@
216366216427
p->eType = 0;
216367216428
p->iEnd = 0;
216368216429
return SQLITE_OK;
216369216430
}
216370216431
sqlite3_free(cur->pVtab->zErrMsg);
216371
- cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
216432
+ cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot, 0);
216372216433
jsonEachCursorReset(p);
216373216434
return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
216374216435
}
216375216436
if( p->sParse.iLabel ){
216376216437
p->i = p->sParse.iLabel;
@@ -216456,10 +216517,12 @@
216456216517
/* | | | | | | */
216457216518
JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc),
216458216519
JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc),
216459216520
JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc),
216460216521
JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc),
216522
+ JFUNCTION(json_array_insert, -1,1,1, 1,0,JSON_AINS, jsonSetFunc),
216523
+ JFUNCTION(jsonb_array_insert,-1,1,0, 1,1,JSON_AINS, jsonSetFunc),
216461216524
JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc),
216462216525
JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc),
216463216526
JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc),
216464216527
JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc),
216465216528
JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc),
@@ -232389,31 +232452,31 @@
232389232452
for(i=0; i<pTab->nCol; i++){
232390232453
int eType = *a;
232391232454
int isPK = pTab->abPK[i];
232392232455
if( bPkOnly && isPK==0 ) continue;
232393232456
232394
- /* It is not possible for eType to be SQLITE_NULL here. The session
232395
- ** module does not record changes for rows with NULL values stored in
232396
- ** primary key columns. */
232397232457
assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
232398232458
|| eType==SQLITE_TEXT || eType==SQLITE_BLOB
232399232459
|| eType==SQLITE_NULL || eType==0
232400232460
);
232401
- assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
232402232461
232403232462
if( isPK ){
232404232463
a++;
232405232464
h = sessionHashAppendType(h, eType);
232406232465
if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
232407232466
h = sessionHashAppendI64(h, sessionGetI64(a));
232408232467
a += 8;
232409
- }else{
232468
+ }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
232410232469
int n;
232411232470
a += sessionVarintGet(a, &n);
232412232471
h = sessionHashAppendBlob(h, n, a);
232413232472
a += n;
232414232473
}
232474
+ /* It should not be possible for eType to be SQLITE_NULL or 0x00 here,
232475
+ ** as the session module does not record changes for rows with NULL
232476
+ ** values stored in primary key columns. But a corrupt changesets
232477
+ ** may contain such a value. */
232415232478
}else{
232416232479
a += sessionSerialLen(a);
232417232480
}
232418232481
}
232419232482
return (h % nBucket);
@@ -234818,14 +234881,17 @@
234818234881
*pnChangeset = 0;
234819234882
*ppChangeset = 0;
234820234883
}
234821234884
234822234885
if( pSession->rc ) return pSession->rc;
234823
- rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
234824
- if( rc!=SQLITE_OK ) return rc;
234825234886
234826234887
sqlite3_mutex_enter(sqlite3_db_mutex(db));
234888
+ rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
234889
+ if( rc!=SQLITE_OK ){
234890
+ sqlite3_mutex_leave(sqlite3_db_mutex(db));
234891
+ return rc;
234892
+ }
234827234893
234828234894
for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
234829234895
if( pTab->nEntry ){
234830234896
const char *zName = pTab->zName;
234831234897
int i; /* Used to iterate through hash buckets */
@@ -235377,12 +235443,19 @@
235377235443
235378235444
while( rc==SQLITE_OK ){
235379235445
while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
235380235446
nRead++;
235381235447
}
235448
+
235449
+ /* Break out of the loop if if the nul-terminator byte has been found.
235450
+ ** Otherwise, read some more input data and keep seeking. If there is
235451
+ ** no more input data, consider the changeset corrupt. */
235382235452
if( (pIn->iNext + nRead)<pIn->nData ) break;
235383235453
rc = sessionInputBuffer(pIn, nRead + 100);
235454
+ if( rc==SQLITE_OK && (pIn->iNext + nRead)>=pIn->nData ){
235455
+ rc = SQLITE_CORRUPT_BKPT;
235456
+ }
235384235457
}
235385235458
*pnByte = nRead+1;
235386235459
return rc;
235387235460
}
235388235461
@@ -253281,11 +253354,11 @@
253281253354
){
253282253355
const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
253283253356
int iSegid = pSeg->pSeg->iSegid;
253284253357
u8 *aPg = pSeg->pLeaf->p;
253285253358
int nPg = pSeg->pLeaf->nn;
253286
- int iPgIdx = pSeg->pLeaf->szLeaf;
253359
+ int iPgIdx = pSeg->pLeaf->szLeaf; /* Offset of page footer */
253287253360
253288253361
u64 iDelta = 0;
253289253362
int iNextOff = 0;
253290253363
int iOff = 0;
253291253364
int nIdx = 0;
@@ -253360,11 +253433,11 @@
253360253433
iStart = iSOP + (nPos/2);
253361253434
iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
253362253435
iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
253363253436
}
253364253437
assert_nc( iSOP==pSeg->iLeafOffset );
253365
- iNextOff = pSeg->iLeafOffset + pSeg->nPos;
253438
+ iNextOff = iSOP + pSeg->nPos;
253366253439
}
253367253440
}
253368253441
253369253442
iOff = iStart;
253370253443
@@ -253440,35 +253513,35 @@
253440253513
if( iNextOff!=iPgIdx ){
253441253514
/* This is the only position-list associated with the term, and there
253442253515
** is another term following it on this page. So the subsequent term
253443253516
** needs to be moved to replace the term associated with the entry
253444253517
** being removed. */
253445
- int nPrefix = 0;
253446
- int nSuffix = 0;
253447
- int nPrefix2 = 0;
253448
- int nSuffix2 = 0;
253518
+ u64 nPrefix = 0;
253519
+ u64 nSuffix = 0;
253520
+ u64 nPrefix2 = 0;
253521
+ u64 nSuffix2 = 0;
253449253522
253450253523
iDelKeyOff = iNextOff;
253451
- iNextOff += fts5GetVarint32(&aPg[iNextOff], nPrefix2);
253452
- iNextOff += fts5GetVarint32(&aPg[iNextOff], nSuffix2);
253524
+ iNextOff += fts5GetVarint(&aPg[iNextOff], &nPrefix2);
253525
+ iNextOff += fts5GetVarint(&aPg[iNextOff], &nSuffix2);
253453253526
253454253527
if( iKey!=1 ){
253455
- iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nPrefix);
253528
+ iKeyOff += fts5GetVarint(&aPg[iKeyOff], &nPrefix);
253456253529
}
253457
- iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nSuffix);
253530
+ iKeyOff += fts5GetVarint(&aPg[iKeyOff], &nSuffix);
253458253531
253459253532
nPrefix = MIN(nPrefix, nPrefix2);
253460253533
nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
253461253534
253462
- if( (iKeyOff+nSuffix)>iPgIdx || (iNextOff+nSuffix2)>iPgIdx ){
253535
+ if( (iKeyOff+nSuffix)>(u64)iPgIdx || (iNextOff+nSuffix2)>(u64)iPgIdx ){
253463253536
FTS5_CORRUPT_IDX(p);
253464253537
}else{
253465253538
if( iKey!=1 ){
253466253539
iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix);
253467253540
}
253468253541
iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix);
253469
- if( nPrefix2>pSeg->term.n ){
253542
+ if( nPrefix2>(u64)pSeg->term.n ){
253470253543
FTS5_CORRUPT_IDX(p);
253471253544
}else if( nPrefix2>nPrefix ){
253472253545
memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix);
253473253546
iOff += (nPrefix2-nPrefix);
253474253547
}
@@ -253495,11 +253568,11 @@
253495253568
Fts5Data *pTerm = fts5DataRead(p, iId);
253496253569
if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
253497253570
u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
253498253571
int nTermIdx = pTerm->nn - pTerm->szLeaf;
253499253572
int iTermIdx = 0;
253500
- int iTermOff = 0;
253573
+ i64 iTermOff = 0;
253501253574
253502253575
while( 1 ){
253503253576
u32 iVal = 0;
253504253577
int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal);
253505253578
iTermOff += iVal;
@@ -253506,16 +253579,19 @@
253506253579
if( (iTermIdx+nByte)>=nTermIdx ) break;
253507253580
iTermIdx += nByte;
253508253581
}
253509253582
nTermIdx = iTermIdx;
253510253583
253511
- memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
253512
- fts5PutU16(&pTerm->p[2], iTermOff);
253513
-
253514
- fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
253515
- if( nTermIdx==0 ){
253516
- fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
253584
+ if( iTermOff>pTerm->szLeaf ){
253585
+ FTS5_CORRUPT_IDX(p);
253586
+ }else{
253587
+ memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
253588
+ fts5PutU16(&pTerm->p[2], iTermOff);
253589
+ fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
253590
+ if( nTermIdx==0 ){
253591
+ fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
253592
+ }
253517253593
}
253518253594
}
253519253595
fts5DataRelease(pTerm);
253520253596
}
253521253597
}
@@ -253534,11 +253610,13 @@
253534253610
int nShift = iNextOff - iOff; /* Distance to move them */
253535253611
253536253612
int iPrevKeyOut = 0;
253537253613
int iKeyIn = 0;
253538253614
253539
- memmove(&aPg[iOff], &aPg[iNextOff], nMove);
253615
+ if( nMove>0 ){
253616
+ memmove(&aPg[iOff], &aPg[iNextOff], nMove);
253617
+ }
253540253618
iPgIdx -= nShift;
253541253619
nPg = iPgIdx;
253542253620
fts5PutU16(&aPg[2], iPgIdx);
253543253621
253544253622
for(iIdx=0; iIdx<nIdx; /* no-op */){
@@ -261180,11 +261258,11 @@
261180261258
int nArg, /* Number of args */
261181261259
sqlite3_value **apUnused /* Function arguments */
261182261260
){
261183261261
assert( nArg==0 );
261184261262
UNUSED_PARAM2(nArg, apUnused);
261185
- sqlite3_result_text(pCtx, "fts5: 2026-01-09 00:41:35 9adab8b2bef4130abd358d53384cb5f4dd691b808336bb7102793b0165b1c516", -1, SQLITE_TRANSIENT);
261263
+ sqlite3_result_text(pCtx, "fts5: 2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71", -1, SQLITE_TRANSIENT);
261186261264
}
261187261265
261188261266
/*
261189261267
** Implementation of fts5_locale(LOCALE, TEXT) function.
261190261268
**
261191261269
--- 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 ** 9adab8b2bef4130abd358d53384cb5f4dd69 with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 #define SQLITE_VERSION "3.52.0"
471 #define SQLITE_VERSION_NUMBER 3052000
472 #define SQLITE_SOURCE_ID "2026-01-09 00:41:35 9adab8b2bef4130abd358d53384cb5f4dd691b808336bb7102793b0165b1c516"
473 #define SQLITE_SCM_BRANCH "trunk"
474 #define SQLITE_SCM_TAGS ""
475 #define SQLITE_SCM_DATETIME "2026-01-09T00:41:35.433Z"
476
477 /*
478 ** CAPI3REF: Run-Time Library Version Numbers
479 ** KEYWORDS: sqlite3_version sqlite3_sourceid
480 **
@@ -5194,12 +5194,12 @@
5194 ** it should be a pointer to well-formed UTF8 text.
5195 ** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
5196 ** it should be a pointer to well-formed UTF16 text.
5197 ** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
5198 ** it should be a pointer to a well-formed unicode string that is
5199 ** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
5200 ** otherwise.
5201 **
5202 ** [[byte-order determination rules]] ^The byte-order of
5203 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5204 ** found in the first character, which is removed, or in the absence of a BOM
5205 ** the byte order is the native byte order of the host
@@ -5241,14 +5241,19 @@
5241 ** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
5242 ** object is to be copied prior to the return from sqlite3_bind_*(). ^The
5243 ** object and pointer to it must remain valid until then. ^SQLite will then
5244 ** manage the lifetime of its private copy.
5245 **
5246 ** ^The sixth argument to sqlite3_bind_text64() must be one of
5247 ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5248 ** to specify the encoding of the text in the third parameter. If
5249 ** the sixth argument to sqlite3_bind_text64() is not one of the
 
 
 
 
 
5250 ** allowed values shown above, or if the text encoding is different
5251 ** from the encoding specified by the sixth parameter, then the behavior
5252 ** is undefined.
5253 **
5254 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -6111,17 +6116,63 @@
6111 /*
6112 ** CAPI3REF: Text Encodings
6113 **
6114 ** These constants define integer codes that represent the various
6115 ** text encodings supported by SQLite.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6116 */
6117 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
6118 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
6119 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
6120 #define SQLITE_UTF16 4 /* Use native byte order */
6121 #define SQLITE_ANY 5 /* Deprecated */
6122 #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
 
6123
6124 /*
6125 ** CAPI3REF: Function Flags
6126 **
6127 ** These constants may be ORed together with the
@@ -6738,14 +6789,18 @@
6738 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
6739 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6740 ** set the return value of the application-defined function to be
6741 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6742 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6743 ** ^The sqlite3_result_text64() interface sets the return value of an
6744 ** application-defined function to be a text string in an encoding
6745 ** specified by the fifth (and last) parameter, which must be one
6746 ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
 
 
 
 
6747 ** ^SQLite takes the text result from the application from
6748 ** the 2nd parameter of the sqlite3_result_text* interfaces.
6749 ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6750 ** other than sqlite3_result_text64() is negative, then SQLite computes
6751 ** the string length itself by searching the 2nd parameter for the first
@@ -6828,11 +6883,11 @@
6828 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6829 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6830 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6831 SQLITE_API void sqlite3_result_null(sqlite3_context*);
6832 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6833 SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6834 void(*)(void*), unsigned char encoding);
6835 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6836 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6837 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6838 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
@@ -14373,10 +14428,31 @@
14373 #ifndef SQLITE_MAX_LENGTH
14374 # define SQLITE_MAX_LENGTH 1000000000
14375 #endif
14376 #define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */
14377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14378 /*
14379 ** This is the maximum number of
14380 **
14381 ** * Columns in a table
14382 ** * Columns in an index
@@ -20223,31 +20299,17 @@
20223 };
20224
20225 /*
20226 ** An instance of the following structure contains all information
20227 ** needed to generate code for a single SELECT statement.
20228 **
20229 ** See the header comment on the computeLimitRegisters() routine for a
20230 ** detailed description of the meaning of the iLimit and iOffset fields.
20231 **
20232 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
20233 ** These addresses must be stored so that we can go back and fill in
20234 ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
20235 ** the number of columns in P2 can be computed at the same time
20236 ** as the OP_OpenEphm instruction is coded because not
20237 ** enough information about the compound query is known at that point.
20238 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
20239 ** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
20240 ** sequences for the ORDER BY clause.
20241 */
20242 struct Select {
20243 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
20244 LogEst nSelectRow; /* Estimated number of result rows */
20245 u32 selFlags; /* Various SF_* values */
20246 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
20247 u32 selId; /* Unique identifier number for this SELECT */
20248 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
20249 ExprList *pEList; /* The fields of the result */
20250 SrcList *pSrc; /* The FROM clause */
20251 Expr *pWhere; /* The WHERE clause */
20252 ExprList *pGroupBy; /* The GROUP BY clause */
20253 Expr *pHaving; /* The HAVING clause */
@@ -20275,28 +20337,28 @@
20275 #define SF_Distinct 0x0000001 /* Output should be DISTINCT */
20276 #define SF_All 0x0000002 /* Includes the ALL keyword */
20277 #define SF_Resolved 0x0000004 /* Identifiers have been resolved */
20278 #define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
20279 #define SF_HasAgg 0x0000010 /* Contains aggregate functions */
20280 #define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
20281 #define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
20282 #define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
20283 #define SF_Compound 0x0000100 /* Part of a compound query */
20284 #define SF_Values 0x0000200 /* Synthesized from VALUES clause */
20285 #define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
20286 #define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
20287 #define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
20288 #define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
20289 #define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
20290 #define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
20291 #define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
20292 #define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
20293 #define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
20294 #define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
20295 #define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
20296 #define SF_View 0x0200000 /* SELECT statement is a view */
20297 #define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
20298 #define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
20299 #define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
20300 #define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
20301 #define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
20302 #define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
@@ -20312,15 +20374,10 @@
20312 /*
20313 ** The results of a SELECT can be distributed in several ways, as defined
20314 ** by one of the following macros. The "SRT" prefix means "SELECT Result
20315 ** Type".
20316 **
20317 ** SRT_Union Store results as a key in a temporary index
20318 ** identified by pDest->iSDParm.
20319 **
20320 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
20321 **
20322 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
20323 ** set is not empty.
20324 **
20325 ** SRT_Discard Throw the results away. This is used by SELECT
20326 ** statements within triggers whose only purpose is
@@ -20380,34 +20437,32 @@
20380 ** column returned by the SELECT is used as the integer
20381 ** key. If (pDest->iSDParm>0), then the table is an index
20382 ** table. (pDest->iSDParm) is the number of key columns in
20383 ** each index record in this case.
20384 */
20385 #define SRT_Union 1 /* Store result as keys in an index */
20386 #define SRT_Except 2 /* Remove result from a UNION index */
20387 #define SRT_Exists 3 /* Store 1 if the result is not empty */
20388 #define SRT_Discard 4 /* Do not save the results anywhere */
20389 #define SRT_DistFifo 5 /* Like SRT_Fifo, but unique results only */
20390 #define SRT_DistQueue 6 /* Like SRT_Queue, but unique results only */
20391
20392 /* The DISTINCT clause is ignored for all of the above. Not that
20393 ** IgnorableDistinct() implies IgnorableOrderby() */
20394 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
20395
20396 #define SRT_Queue 7 /* Store result in an queue */
20397 #define SRT_Fifo 8 /* Store result as data with an automatic rowid */
20398
20399 /* The ORDER BY clause is ignored for all of the above */
20400 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
20401
20402 #define SRT_Output 9 /* Output each row of result */
20403 #define SRT_Mem 10 /* Store result in a memory cell */
20404 #define SRT_Set 11 /* Store results as keys in an index */
20405 #define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
20406 #define SRT_Coroutine 13 /* Generate a single row of result */
20407 #define SRT_Table 14 /* Store result as data with an automatic rowid */
20408 #define SRT_Upfrom 15 /* Store result as data with rowid */
20409
20410 /*
20411 ** An instance of this object describes where to put of the results of
20412 ** a SELECT statement.
20413 */
@@ -24510,10 +24565,11 @@
24510 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
24511 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
24512 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
24513 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
24514 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
 
24515 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
24516 #ifdef SQLITE_OMIT_FLOATING_POINT
24517 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
24518 #else
24519 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
@@ -31356,31 +31412,10 @@
31356 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
31357 }
31358 *pp = p;
31359 }
31360
31361 /*
31362 ** Maximum size of any single memory allocation.
31363 **
31364 ** This is not a limit on the total amount of memory used. This is
31365 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
31366 **
31367 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
31368 ** This provides a 256-byte safety margin for defense against 32-bit
31369 ** signed integer overflow bugs when computing memory allocation sizes.
31370 ** Paranoid applications might want to reduce the maximum allocation size
31371 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
31372 ** or even smaller would be reasonable upper bounds on the size of a memory
31373 ** allocations for most applications.
31374 */
31375 #ifndef SQLITE_MAX_ALLOCATION_SIZE
31376 # define SQLITE_MAX_ALLOCATION_SIZE 2147483391
31377 #endif
31378 #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
31379 # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
31380 #endif
31381
31382 /*
31383 ** Allocate memory. This routine is like sqlite3_malloc() except that it
31384 ** assumes the memory subsystem has already been initialized.
31385 */
31386 SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
@@ -31600,12 +31635,11 @@
31600 }
31601 if( nBytes==0 ){
31602 sqlite3_free(pOld); /* IMP: R-26507-47431 */
31603 return 0;
31604 }
31605 if( nBytes>=0x7fffff00 ){
31606 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
31607 return 0;
31608 }
31609 nOld = sqlite3MallocSize(pOld);
31610 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
31611 ** argument to xRealloc is always a value returned by a prior call to
@@ -77429,11 +77463,11 @@
77429 }
77430 assert( cursorHoldsMutex(pCur) );
77431
77432 getCellInfo(pCur);
77433 aPayload = pCur->info.pPayload;
77434 assert( offset+amt <= pCur->info.nPayload );
77435
77436 assert( aPayload > pPage->aData );
77437 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
77438 /* Trying to read or write past the end of the data is an error. The
77439 ** conditional above is really:
@@ -77986,11 +78020,11 @@
77986 SQLITE_PRIVATE int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes){
77987 int rc;
77988
77989 assert( cursorOwnsBtShared(pCur) );
77990 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77991 if( pCur->eState==CURSOR_VALID ){
77992 *pRes = 0;
77993 return SQLITE_OK;
77994 }
77995 rc = moveToRoot(pCur);
77996 if( rc==SQLITE_EMPTY ){
@@ -85879,10 +85913,88 @@
85879 #endif
85880
85881
85882 return SQLITE_OK;
85883 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85884
85885 /*
85886 ** Move data out of a btree key or data field and into a Mem structure.
85887 ** The data is payload from the entry that pCur is currently pointing
85888 ** to. offset and amt determine what portion of the data or key to retrieve.
@@ -85903,11 +86015,16 @@
85903 u32 amt, /* Number of bytes to return. */
85904 Mem *pMem /* OUT: Return data in this Mem structure. */
85905 ){
85906 int rc;
85907 pMem->flags = MEM_Null;
85908 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
 
 
 
 
 
85909 return SQLITE_CORRUPT_BKPT;
85910 }
85911 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
85912 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
85913 if( rc==SQLITE_OK ){
@@ -89587,11 +89704,11 @@
89587 assert( !zName || xDel!=SQLITE_DYNAMIC );
89588 return SQLITE_NOMEM_BKPT;
89589 }
89590 assert( p->aColName!=0 );
89591 pColName = &(p->aColName[idx+var*p->nResAlloc]);
89592 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
89593 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
89594 return rc;
89595 }
89596
89597 /*
@@ -92665,11 +92782,27 @@
92665 int n, /* Bytes in string, or negative */
92666 u8 enc, /* Encoding of z. 0 for BLOBs */
92667 void (*xDel)(void*) /* Destructor function */
92668 ){
92669 Mem *pOut = pCtx->pOut;
92670 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92671 if( rc ){
92672 if( rc==SQLITE_TOOBIG ){
92673 sqlite3_result_error_toobig(pCtx);
92674 }else{
92675 /* The only errors possible from sqlite3VdbeMemSetStr are
@@ -92858,11 +92991,11 @@
92858 return;
92859 }
92860 #endif
92861 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92862 assert( xDel!=SQLITE_DYNAMIC );
92863 if( enc!=SQLITE_UTF8 ){
92864 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
92865 n &= ~(u64)1;
92866 }
92867 if( n>0x7fffffff ){
92868 (void)invokeValueDestructor(z, xDel, pCtx);
@@ -93318,11 +93451,11 @@
93318 if( rc==SQLITE_OK ){
93319 u32 sz; /* Size of current row in bytes */
93320 Mem sMem; /* Raw content of current row */
93321 memset(&sMem, 0, sizeof(sMem));
93322 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
93323 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
93324 if( rc==SQLITE_OK ){
93325 u8 *zBuf = (u8*)sMem.z;
93326 u32 iSerial;
93327 sqlite3_value *pOut = pRhs->pOut;
93328 int iOff = 1 + getVarint32(&zBuf[1], iSerial);
@@ -93967,17 +94100,29 @@
93967 rc = vdbeUnbind(p, (u32)(i-1));
93968 if( rc==SQLITE_OK ){
93969 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93970 if( zData!=0 ){
93971 pVar = &p->aVar[i-1];
93972 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
93973 if( rc==SQLITE_OK ){
93974 if( encoding==0 ){
93975 pVar->enc = ENC(p->db);
93976 }else{
93977 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
93978 }
 
 
 
 
 
 
 
 
 
 
 
 
93979 }
93980 if( rc ){
93981 sqlite3Error(p->db, rc);
93982 rc = sqlite3ApiExit(p->db, rc);
93983 }
@@ -94085,11 +94230,11 @@
94085 sqlite3_uint64 nData,
94086 void (*xDel)(void*),
94087 unsigned char enc
94088 ){
94089 assert( xDel!=SQLITE_DYNAMIC );
94090 if( enc!=SQLITE_UTF8 ){
94091 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
94092 nData &= ~(u64)1;
94093 }
94094 return bindText(pStmt, i, zData, nData, xDel, enc);
94095 }
@@ -101783,24 +101928,19 @@
101783 rc = sqlite3VdbeSorterWrite(pC, pIn2);
101784 if( rc) goto abort_due_to_error;
101785 break;
101786 }
101787
101788 /* Opcode: IdxDelete P1 P2 P3 * P5
101789 ** Synopsis: key=r[P2@P3]
101790 **
101791 ** The content of P3 registers starting at register P2 form
101792 ** an unpacked index key. This opcode removes that entry from the
101793 ** index opened by cursor P1.
101794 **
101795 ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
101796 ** if no matching index entry is found. This happens when running
101797 ** an UPDATE or DELETE statement and the index entry to be updated
101798 ** or deleted is not found. For some uses of IdxDelete
101799 ** (example: the EXCEPT operator) it does not matter that no matching
101800 ** entry is found. For those cases, P5 is zero. Also, do not raise
101801 ** this (self-correcting and non-critical) error if in writable_schema mode.
101802 */
101803 case OP_IdxDelete: {
101804 VdbeCursor *pC;
101805 BtCursor *pCrsr;
101806 int res;
@@ -101822,11 +101962,11 @@
101822 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
101823 if( rc ) goto abort_due_to_error;
101824 if( res==0 ){
101825 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
101826 if( rc ) goto abort_due_to_error;
101827 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
101828 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
101829 goto abort_due_to_error;
101830 }
101831 assert( pC->deferredMoveto==0 );
101832 pC->cacheStatus = CACHE_STALE;
@@ -113272,13 +113412,11 @@
113272 pNew->pNext = pNext;
113273 pNew->pPrior = 0;
113274 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
113275 pNew->iLimit = 0;
113276 pNew->iOffset = 0;
113277 pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral;
113278 pNew->addrOpenEphm[0] = -1;
113279 pNew->addrOpenEphm[1] = -1;
113280 pNew->nSelectRow = p->nSelectRow;
113281 pNew->pWith = sqlite3WithDup(db, p->pWith);
113282 #ifndef SQLITE_OMIT_WINDOWFUNC
113283 pNew->pWin = 0;
113284 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
@@ -115496,12 +115634,13 @@
115496 if( destIfFalse==destIfNull ){
115497 /* Combine Step 3 and Step 5 into a single opcode */
115498 if( ExprHasProperty(pExpr, EP_Subrtn) ){
115499 const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
115500 assert( pOp->opcode==OP_Once || pParse->nErr );
115501 if( pOp->opcode==OP_Once && pOp->p3>0 ){ /* tag-202407032019 */
115502 assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) );
 
115503 sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse,
115504 rLhs, nVector); VdbeCoverage(v);
115505 }
115506 }
115507 sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
@@ -132240,11 +132379,10 @@
132240 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
132241 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
132242 &iPartIdxLabel, pPrior, r1);
132243 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
132244 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
132245 sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */
132246 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
132247 pPrior = pIdx;
132248 }
132249 }
132250
@@ -133587,11 +133725,11 @@
133587 }else{
133588 goto unistr_error;
133589 }
133590 }
133591 zOut[j] = 0;
133592 sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8);
133593 return;
133594
133595 unistr_error:
133596 sqlite3_free(zOut);
133597 sqlite3_result_error(context, "invalid Unicode escape", -1);
@@ -133680,11 +133818,11 @@
133680 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
133681 *zOut++ = 0x80 + (u8)(c & 0x3F);
133682 } \
133683 }
133684 *zOut = 0;
133685 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
133686 }
133687
133688 /*
133689 ** The hex() function. Interpret the argument as a blob. Return
133690 ** a hexadecimal rendering as text.
@@ -133709,11 +133847,11 @@
133709 *(z++) = hexdigits[(c>>4)&0xf];
133710 *(z++) = hexdigits[c&0xf];
133711 }
133712 *z = 0;
133713 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
133714 sqlite3_free, SQLITE_UTF8);
133715 }
133716 }
133717
133718 /*
133719 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
@@ -134047,11 +134185,11 @@
134047 }
134048 }
134049 }
134050 z[j] = 0;
134051 assert( j<=n );
134052 sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8);
134053 }
134054
134055 /*
134056 ** The CONCAT(...) function. Generate a string result that is the
134057 ** concatentation of all non-null arguments.
@@ -141235,10 +141373,11 @@
141235 int (*set_errmsg)(sqlite3*,int,const char*);
141236 int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
141237 /* Version 3.52.0 and later */
141238 void (*str_truncate)(sqlite3_str*,int);
141239 void (*str_free)(sqlite3_str*);
 
141240 };
141241
141242 /*
141243 ** This is the function signature used for all extension entry points. It
141244 ** is also defined in the file "loadext.c".
@@ -141576,10 +141715,11 @@
141576 #define sqlite3_set_errmsg sqlite3_api->set_errmsg
141577 #define sqlite3_db_status64 sqlite3_api->db_status64
141578 /* Version 3.52.0 and later */
141579 #define sqlite3_str_truncate sqlite3_api->str_truncate
141580 #define sqlite3_str_free sqlite3_api->str_free
 
141581 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
141582
141583 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
141584 /* This case when the file really is being compiled as a loadable
141585 ** extension */
@@ -142105,11 +142245,16 @@
142105 /* Version 3.51.0 and later */
142106 sqlite3_set_errmsg,
142107 sqlite3_db_status64,
142108 /* Version 3.52.0 and later */
142109 sqlite3_str_truncate,
142110 sqlite3_str_free
 
 
 
 
 
142111 };
142112
142113 /* True if x is the directory separator character
142114 */
142115 #if SQLITE_OS_WIN
@@ -147528,12 +147673,10 @@
147528 pNew->op = TK_SELECT;
147529 pNew->selFlags = selFlags;
147530 pNew->iLimit = 0;
147531 pNew->iOffset = 0;
147532 pNew->selId = ++pParse->nSelect;
147533 pNew->addrOpenEphm[0] = -1;
147534 pNew->addrOpenEphm[1] = -1;
147535 pNew->nSelectRow = 0;
147536 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1);
147537 pNew->pSrc = pSrc;
147538 pNew->pWhere = pWhere;
147539 pNew->pGroupBy = pGroupBy;
@@ -148677,33 +148820,10 @@
148677 codeOffset(v, p->iOffset, iContinue);
148678 }
148679 }
148680
148681 switch( eDest ){
148682 /* In this mode, write each query result to the key of the temporary
148683 ** table iParm.
148684 */
148685 #ifndef SQLITE_OMIT_COMPOUND_SELECT
148686 case SRT_Union: {
148687 int r1;
148688 r1 = sqlite3GetTempReg(pParse);
148689 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
148690 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
148691 sqlite3ReleaseTempReg(pParse, r1);
148692 break;
148693 }
148694
148695 /* Construct a record from the query result, but instead of
148696 ** saving that record, use it as a key to delete elements from
148697 ** the temporary table iParm.
148698 */
148699 case SRT_Except: {
148700 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
148701 break;
148702 }
148703 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
148704
148705 /* Store the result as data using a unique key.
148706 */
148707 case SRT_Fifo:
148708 case SRT_DistFifo:
148709 case SRT_Table:
@@ -149986,11 +150106,11 @@
149986 **
149987 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
149988 ** function is responsible for ensuring that this structure is eventually
149989 ** freed.
149990 */
149991 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
149992 ExprList *pOrderBy = p->pOrderBy;
149993 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
149994 sqlite3 *db = pParse->db;
149995 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
149996 if( pRet ){
@@ -150121,21 +150241,41 @@
150121
150122 /* Allocate cursors for Current, Queue, and Distinct. */
150123 regCurrent = ++pParse->nMem;
150124 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
150125 if( pOrderBy ){
150126 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
150127 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
150128 (char*)pKeyInfo, P4_KEYINFO);
150129 destQueue.pOrderBy = pOrderBy;
150130 }else{
150131 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
150132 }
150133 VdbeComment((v, "Queue table"));
150134 if( iDistinct ){
150135 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
150136 p->selFlags |= SF_UsesEphemeral;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150137 }
150138
150139 /* Detach the ORDER BY clause from the compound SELECT */
150140 p->pOrderBy = 0;
150141
@@ -150206,11 +150346,11 @@
150206 return;
150207 }
150208 #endif /* SQLITE_OMIT_CTE */
150209
150210 /* Forward references */
150211 static int multiSelectOrderBy(
150212 Parse *pParse, /* Parsing context */
150213 Select *p, /* The right-most of SELECTs to be coded */
150214 SelectDest *pDest /* What to do with query results */
150215 );
150216
@@ -150355,317 +150495,80 @@
150355 #ifndef SQLITE_OMIT_CTE
150356 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
150357 generateWithRecursiveQuery(pParse, p, &dest);
150358 }else
150359 #endif
150360
150361 /* Compound SELECTs that have an ORDER BY clause are handled separately.
150362 */
150363 if( p->pOrderBy ){
150364 return multiSelectOrderBy(pParse, p, pDest);
 
 
 
 
 
 
 
 
 
 
 
 
 
150365 }else{
 
 
 
 
150366
150367 #ifndef SQLITE_OMIT_EXPLAIN
150368 if( pPrior->pPrior==0 ){
150369 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
150370 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
150371 }
150372 #endif
150373
150374 /* Generate code for the left and right SELECT statements.
150375 */
150376 switch( p->op ){
150377 case TK_ALL: {
150378 int addr = 0;
150379 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
150380 assert( !pPrior->pLimit );
150381 pPrior->iLimit = p->iLimit;
150382 pPrior->iOffset = p->iOffset;
150383 pPrior->pLimit = p->pLimit;
150384 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
150385 rc = sqlite3Select(pParse, pPrior, &dest);
150386 pPrior->pLimit = 0;
150387 if( rc ){
150388 goto multi_select_end;
150389 }
150390 p->pPrior = 0;
150391 p->iLimit = pPrior->iLimit;
150392 p->iOffset = pPrior->iOffset;
150393 if( p->iLimit ){
150394 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
150395 VdbeComment((v, "Jump ahead if LIMIT reached"));
150396 if( p->iOffset ){
150397 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
150398 p->iLimit, p->iOffset+1, p->iOffset);
150399 }
150400 }
150401 ExplainQueryPlan((pParse, 1, "UNION ALL"));
150402 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
150403 rc = sqlite3Select(pParse, p, &dest);
150404 testcase( rc!=SQLITE_OK );
150405 pDelete = p->pPrior;
150406 p->pPrior = pPrior;
150407 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
150408 if( p->pLimit
150409 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
150410 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
150411 ){
150412 p->nSelectRow = sqlite3LogEst((u64)nLimit);
150413 }
150414 if( addr ){
150415 sqlite3VdbeJumpHere(v, addr);
150416 }
150417 break;
150418 }
150419 case TK_EXCEPT:
150420 case TK_UNION: {
150421 int unionTab; /* Cursor number of the temp table holding result */
150422 u8 op = 0; /* One of the SRT_ operations to apply to self */
150423 int priorOp; /* The SRT_ operation to apply to prior selects */
150424 Expr *pLimit; /* Saved values of p->nLimit */
150425 int addr;
150426 int emptyBypass = 0; /* IfEmpty opcode to bypass RHS */
150427 SelectDest uniondest;
150428
150429
150430 testcase( p->op==TK_EXCEPT );
150431 testcase( p->op==TK_UNION );
150432 priorOp = SRT_Union;
150433 if( dest.eDest==priorOp ){
150434 /* We can reuse a temporary table generated by a SELECT to our
150435 ** right.
150436 */
150437 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
150438 unionTab = dest.iSDParm;
150439 }else{
150440 /* We will need to create our own temporary table to hold the
150441 ** intermediate results.
150442 */
150443 unionTab = pParse->nTab++;
150444 assert( p->pOrderBy==0 );
150445 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
150446 assert( p->addrOpenEphm[0] == -1 );
150447 p->addrOpenEphm[0] = addr;
150448 findRightmost(p)->selFlags |= SF_UsesEphemeral;
150449 assert( p->pEList );
150450 }
150451
150452
150453 /* Code the SELECT statements to our left
150454 */
150455 assert( !pPrior->pOrderBy );
150456 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
150457 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
150458 rc = sqlite3Select(pParse, pPrior, &uniondest);
150459 if( rc ){
150460 goto multi_select_end;
150461 }
150462
150463 /* Code the current SELECT statement
150464 */
150465 if( p->op==TK_EXCEPT ){
150466 op = SRT_Except;
150467 emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, unionTab);
150468 VdbeCoverage(v);
150469 }else{
150470 assert( p->op==TK_UNION );
150471 op = SRT_Union;
150472 }
150473 p->pPrior = 0;
150474 pLimit = p->pLimit;
150475 p->pLimit = 0;
150476 uniondest.eDest = op;
150477 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
150478 sqlite3SelectOpName(p->op)));
150479 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
150480 rc = sqlite3Select(pParse, p, &uniondest);
150481 testcase( rc!=SQLITE_OK );
150482 assert( p->pOrderBy==0 );
150483 pDelete = p->pPrior;
150484 p->pPrior = pPrior;
150485 p->pOrderBy = 0;
150486 if( p->op==TK_UNION ){
150487 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
150488 }
150489 if( emptyBypass ) sqlite3VdbeJumpHere(v, emptyBypass);
150490 sqlite3ExprDelete(db, p->pLimit);
150491 p->pLimit = pLimit;
150492 p->iLimit = 0;
150493 p->iOffset = 0;
150494
150495 /* Convert the data in the temporary table into whatever form
150496 ** it is that we currently need.
150497 */
150498 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
150499 assert( p->pEList || db->mallocFailed );
150500 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
150501 int iCont, iBreak, iStart;
150502 iBreak = sqlite3VdbeMakeLabel(pParse);
150503 iCont = sqlite3VdbeMakeLabel(pParse);
150504 computeLimitRegisters(pParse, p, iBreak);
150505 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
150506 iStart = sqlite3VdbeCurrentAddr(v);
150507 selectInnerLoop(pParse, p, unionTab,
150508 0, 0, &dest, iCont, iBreak);
150509 sqlite3VdbeResolveLabel(v, iCont);
150510 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
150511 sqlite3VdbeResolveLabel(v, iBreak);
150512 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
150513 }
150514 break;
150515 }
150516 default: assert( p->op==TK_INTERSECT ); {
150517 int tab1, tab2;
150518 int iCont, iBreak, iStart;
150519 Expr *pLimit;
150520 int addr, iLimit, iOffset;
150521 SelectDest intersectdest;
150522 int r1;
150523 int emptyBypass;
150524
150525 /* INTERSECT is different from the others since it requires
150526 ** two temporary tables. Hence it has its own case. Begin
150527 ** by allocating the tables we will need.
150528 */
150529 tab1 = pParse->nTab++;
150530 tab2 = pParse->nTab++;
150531 assert( p->pOrderBy==0 );
150532
150533 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
150534 assert( p->addrOpenEphm[0] == -1 );
150535 p->addrOpenEphm[0] = addr;
150536 findRightmost(p)->selFlags |= SF_UsesEphemeral;
150537 assert( p->pEList );
150538
150539 /* Code the SELECTs to our left into temporary table "tab1".
150540 */
150541 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
150542 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
150543 rc = sqlite3Select(pParse, pPrior, &intersectdest);
150544 if( rc ){
150545 goto multi_select_end;
150546 }
150547
150548 /* Initialize LIMIT counters before checking to see if the LHS
150549 ** is empty, in case the jump is taken */
150550 iBreak = sqlite3VdbeMakeLabel(pParse);
150551 computeLimitRegisters(pParse, p, iBreak);
150552 emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, tab1); VdbeCoverage(v);
150553
150554 /* Code the current SELECT into temporary table "tab2"
150555 */
150556 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
150557 assert( p->addrOpenEphm[1] == -1 );
150558 p->addrOpenEphm[1] = addr;
150559
150560 /* Disable prior SELECTs and the LIMIT counters during the computation
150561 ** of the RHS select */
150562 pLimit = p->pLimit;
150563 iLimit = p->iLimit;
150564 iOffset = p->iOffset;
150565 p->pPrior = 0;
150566 p->pLimit = 0;
150567 p->iLimit = 0;
150568 p->iOffset = 0;
150569
150570 intersectdest.iSDParm = tab2;
150571 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
150572 sqlite3SelectOpName(p->op)));
150573 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
150574 rc = sqlite3Select(pParse, p, &intersectdest);
150575 testcase( rc!=SQLITE_OK );
150576 pDelete = p->pPrior;
150577 p->pPrior = pPrior;
150578 if( p->nSelectRow>pPrior->nSelectRow ){
150579 p->nSelectRow = pPrior->nSelectRow;
150580 }
150581 sqlite3ExprDelete(db, p->pLimit);
150582
150583 /* Reinstate the LIMIT counters prior to running the final intersect */
150584 p->pLimit = pLimit;
150585 p->iLimit = iLimit;
150586 p->iOffset = iOffset;
150587
150588 /* Generate code to take the intersection of the two temporary
150589 ** tables.
150590 */
150591 if( rc ) break;
150592 assert( p->pEList );
150593 sqlite3VdbeAddOp1(v, OP_Rewind, tab1);
150594 r1 = sqlite3GetTempReg(pParse);
150595 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
150596 iCont = sqlite3VdbeMakeLabel(pParse);
150597 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
150598 VdbeCoverage(v);
150599 sqlite3ReleaseTempReg(pParse, r1);
150600 selectInnerLoop(pParse, p, tab1,
150601 0, 0, &dest, iCont, iBreak);
150602 sqlite3VdbeResolveLabel(v, iCont);
150603 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
150604 sqlite3VdbeResolveLabel(v, iBreak);
150605 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
150606 sqlite3VdbeJumpHere(v, emptyBypass);
150607 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
150608 break;
150609 }
150610 }
150611
150612 #ifndef SQLITE_OMIT_EXPLAIN
150613 if( p->pNext==0 ){
150614 ExplainQueryPlanPop(pParse);
150615 }
150616 #endif
150617 }
150618 if( pParse->nErr ) goto multi_select_end;
150619
150620 /* Compute collating sequences used by
150621 ** temporary tables needed to implement the compound select.
150622 ** Attach the KeyInfo structure to all temporary tables.
150623 **
150624 ** This section is run by the right-most SELECT statement only.
150625 ** SELECT statements to the left always skip this part. The right-most
150626 ** SELECT might also skip this part if it has no ORDER BY clause and
150627 ** no temp tables are required.
150628 */
150629 if( p->selFlags & SF_UsesEphemeral ){
150630 int i; /* Loop counter */
150631 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
150632 Select *pLoop; /* For looping through SELECT statements */
150633 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
150634 int nCol; /* Number of columns in result set */
150635
150636 assert( p->pNext==0 );
150637 assert( p->pEList!=0 );
150638 nCol = p->pEList->nExpr;
150639 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
150640 if( !pKeyInfo ){
150641 rc = SQLITE_NOMEM_BKPT;
150642 goto multi_select_end;
150643 }
150644 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
150645 *apColl = multiSelectCollSeq(pParse, p, i);
150646 if( 0==*apColl ){
150647 *apColl = db->pDfltColl;
150648 }
150649 }
150650
150651 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
150652 for(i=0; i<2; i++){
150653 int addr = pLoop->addrOpenEphm[i];
150654 if( addr<0 ){
150655 /* If [0] is unused then [1] is also unused. So we can
150656 ** always safely abort as soon as the first unused slot is found */
150657 assert( pLoop->addrOpenEphm[1]<0 );
150658 break;
150659 }
150660 sqlite3VdbeChangeP2(v, addr, nCol);
150661 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
150662 P4_KEYINFO);
150663 pLoop->addrOpenEphm[i] = -1;
150664 }
150665 }
150666 sqlite3KeyInfoUnref(pKeyInfo);
150667 }
150668
150669 multi_select_end:
150670 pDest->iSdst = dest.iSdst;
150671 pDest->nSdst = dest.nSdst;
@@ -150693,12 +150596,12 @@
150693
150694 /*
150695 ** Code an output subroutine for a coroutine implementation of a
150696 ** SELECT statement.
150697 **
150698 ** The data to be output is contained in pIn->iSdst. There are
150699 ** pIn->nSdst columns to be output. pDest is where the output should
150700 ** be sent.
150701 **
150702 ** regReturn is the number of the register holding the subroutine
150703 ** return address.
150704 **
@@ -150723,10 +150626,12 @@
150723 ){
150724 Vdbe *v = pParse->pVdbe;
150725 int iContinue;
150726 int addr;
150727
 
 
150728 addr = sqlite3VdbeCurrentAddr(v);
150729 iContinue = sqlite3VdbeMakeLabel(pParse);
150730
150731 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
150732 */
@@ -150744,26 +150649,63 @@
150744
150745 /* Suppress the first OFFSET entries if there is an OFFSET clause
150746 */
150747 codeOffset(v, p->iOffset, iContinue);
150748
150749 assert( pDest->eDest!=SRT_Exists );
150750 assert( pDest->eDest!=SRT_Table );
150751 switch( pDest->eDest ){
150752 /* Store the result as data using a unique key.
150753 */
 
 
 
150754 case SRT_EphemTab: {
150755 int r1 = sqlite3GetTempReg(pParse);
150756 int r2 = sqlite3GetTempReg(pParse);
 
 
 
 
 
150757 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
150758 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
150759 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150760 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
150761 sqlite3ReleaseTempReg(pParse, r2);
150762 sqlite3ReleaseTempReg(pParse, r1);
150763 break;
150764 }
 
 
 
 
 
 
 
 
150765
150766 #ifndef SQLITE_OMIT_SUBQUERY
150767 /* If we are creating a set for an "expr IN (SELECT ...)".
150768 */
150769 case SRT_Set: {
@@ -150806,14 +150748,74 @@
150806 }
150807 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
150808 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
150809 break;
150810 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150811
150812 /* If none of the above, then the result destination must be
150813 ** SRT_Output. This routine is never called with any other
150814 ** destination other than the ones handled above or SRT_Output.
150815 **
150816 ** For SRT_Output, results are stored in a sequence of registers.
150817 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
150818 ** return the next row of result.
150819 */
@@ -150837,12 +150839,13 @@
150837
150838 return addr;
150839 }
150840
150841 /*
150842 ** Alternative compound select code generator for cases when there
150843 ** is an ORDER BY clause.
 
150844 **
150845 ** We assume a query of the following form:
150846 **
150847 ** <selectA> <operator> <selectB> ORDER BY <orderbylist>
150848 **
@@ -150855,11 +150858,11 @@
150855 ** outA: Move the output of the selectA coroutine into the output
150856 ** of the compound query.
150857 **
150858 ** outB: Move the output of the selectB coroutine into the output
150859 ** of the compound query. (Only generated for UNION and
150860 ** UNION ALL. EXCEPT and INSERTSECT never output a row that
150861 ** appears only in B.)
150862 **
150863 ** AltB: Called when there is data from both coroutines and A<B.
150864 **
150865 ** AeqB: Called when there is data from both coroutines and A==B.
@@ -150919,14 +150922,14 @@
150919 ** End: ...
150920 **
150921 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
150922 ** actually called using Gosub and they do not Return. EofA and EofB loop
150923 ** until all data is exhausted then jump to the "end" label. AltB, AeqB,
150924 ** and AgtB jump to either L2 or to one of EofA or EofB.
150925 */
150926 #ifndef SQLITE_OMIT_COMPOUND_SELECT
150927 static int multiSelectOrderBy(
150928 Parse *pParse, /* Parsing context */
150929 Select *p, /* The right-most of SELECTs to be coded */
150930 SelectDest *pDest /* What to do with query results */
150931 ){
150932 int i, j; /* Loop counters */
@@ -151019,11 +151022,11 @@
151019 assert( pItem!=0 );
151020 assert( pItem->u.x.iOrderByCol>0 );
151021 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
151022 aPermute[i] = pItem->u.x.iOrderByCol - 1;
151023 }
151024 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
151025 }else{
151026 pKeyMerge = 0;
151027 }
151028
151029 /* Allocate a range of temporary registers and the KeyInfo needed
@@ -152131,11 +152134,11 @@
152131 pItem->fg.jointype |= (jointype & JT_LTORJ);
152132 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
152133 }
152134 pSubitem->fg.jointype |= jointype;
152135
152136 /* Now begin substituting subquery result set expressions for
152137 ** references to the iParent in the outer query.
152138 **
152139 ** Example:
152140 **
152141 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
@@ -152143,21 +152146,21 @@
152143 ** \_____________________ outer query ______________________________/
152144 **
152145 ** We look at every expression in the outer query and every place we see
152146 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
152147 */
152148 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
152149 /* At this point, any non-zero iOrderByCol values indicate that the
152150 ** ORDER BY column expression is identical to the iOrderByCol'th
152151 ** expression returned by SELECT statement pSub. Since these values
152152 ** do not necessarily correspond to columns in SELECT statement pParent,
152153 ** zero them before transferring the ORDER BY clause.
152154 **
152155 ** Not doing this may cause an error if a subsequent call to this
152156 ** function attempts to flatten a compound sub-query into pParent
152157 ** (the only way this can happen is if the compound sub-query is
152158 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
152159 ExprList *pOrderBy = pSub->pOrderBy;
152160 for(i=0; i<pOrderBy->nExpr; i++){
152161 pOrderBy->a[i].u.x.iOrderByCol = 0;
152162 }
152163 assert( pParent->pOrderBy==0 );
@@ -153005,18 +153008,18 @@
153005 ** These are rewritten as a subquery:
153006 **
153007 ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
153008 ** ORDER BY ... COLLATE ...
153009 **
153010 ** This transformation is necessary because the multiSelectOrderBy() routine
153011 ** above that generates the code for a compound SELECT with an ORDER BY clause
153012 ** uses a merge algorithm that requires the same collating sequence on the
153013 ** result columns as on the ORDER BY clause. See ticket
153014 ** http://sqlite.org/src/info/6709574d2a
153015 **
153016 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
153017 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
153018 ** there are COLLATE terms in the ORDER BY.
153019 */
153020 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
153021 int i;
153022 Select *pNew;
@@ -154862,11 +154865,10 @@
154862 pWhere->op = TK_INTEGER;
154863 pWhere->u.iValue = 1;
154864 ExprSetProperty(pWhere, EP_IntValue);
154865 assert( p->pWhere!=0 );
154866 pSub->pSrc->a[0].fg.fromExists = 1;
154867 pSub->pSrc->a[0].fg.jointype |= JT_CROSS;
154868 p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc);
154869 if( pSubWhere ){
154870 p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSubWhere);
154871 pSub->pWhere = 0;
154872 }
@@ -155111,12 +155113,11 @@
155111 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
155112 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
155113 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
155114 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
155115 if( IgnorableDistinct(pDest) ){
155116 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
155117 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
155118 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
155119 /* All of these destinations are also able to ignore the ORDER BY clause */
155120 if( p->pOrderBy ){
155121 #if TREETRACE_ENABLED
155122 TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
@@ -155128,11 +155129,10 @@
155128 p->pOrderBy);
155129 testcase( pParse->earlyCleanup );
155130 p->pOrderBy = 0;
155131 }
155132 p->selFlags &= ~(u32)SF_Distinct;
155133 p->selFlags |= SF_NoopOrderBy;
155134 }
155135 sqlite3SelectPrep(pParse, p, 0);
155136 if( pParse->nErr ){
155137 goto select_end;
155138 }
@@ -165904,20 +165904,26 @@
165904 u16 eOp = pOne->eOperator | pTwo->eOperator;
165905 sqlite3 *db; /* Database connection (for malloc) */
165906 Expr *pNew; /* New virtual expression */
165907 int op; /* Operator for the combined expression */
165908 int idxNew; /* Index in pWC of the next virtual term */
 
165909
165910 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
165911 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
165912 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
165913 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
165914 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
165915 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
165916 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
165917 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
165918 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
 
 
 
 
 
165919 /* If we reach this point, it means the two subterms can be combined */
165920 if( (eOp & (eOp-1))!=0 ){
165921 if( eOp & (WO_LT|WO_LE) ){
165922 eOp = WO_LE;
165923 }else{
@@ -165924,11 +165930,11 @@
165924 assert( eOp & (WO_GT|WO_GE) );
165925 eOp = WO_GE;
165926 }
165927 }
165928 db = pWC->pWInfo->pParse->db;
165929 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
165930 if( pNew==0 ) return;
165931 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
165932 pNew->op = op;
165933 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
165934 exprAnalyze(pSrc, pWC, idxNew);
@@ -170766,10 +170772,11 @@
170766
170767 nOutUnadjusted = pNew->nOut;
170768 pNew->rRun += nInMul + nIn;
170769 pNew->nOut += nInMul + nIn;
170770 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
 
170771 rc = whereLoopInsert(pBuilder, pNew);
170772
170773 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
170774 pNew->nOut = saved_nOut;
170775 }else{
@@ -171362,10 +171369,12 @@
171362 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
171363 whereLoopOutputAdjust(pWC, pNew, rSize);
171364 if( pSrc->fg.isSubquery ){
171365 if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE;
171366 pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy;
 
 
171367 }
171368 rc = whereLoopInsert(pBuilder, pNew);
171369 pNew->nOut = rSize;
171370 if( rc ) break;
171371 }else{
@@ -171464,10 +171473,11 @@
171464 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
171465 ** because the cursor used to access the index might not be
171466 ** positioned to the correct row during the right-join no-match
171467 ** loop. */
171468 }else{
 
171469 rc = whereLoopInsert(pBuilder, pNew);
171470 }
171471 pNew->nOut = rSize;
171472 if( rc ) break;
171473 }
@@ -172161,10 +172171,21 @@
172161 ** is itself on the left side of a RIGHT JOIN.
172162 */
172163 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
172164 mPrereq |= mPrior;
172165 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
 
 
 
 
 
 
 
 
 
 
 
172166 }else if( !hasRightJoin ){
172167 mPrereq = 0;
172168 }
172169 #ifndef SQLITE_OMIT_VIRTUALTABLE
172170 if( IsVirtual(pItem->pSTab) ){
@@ -174713,26 +174734,31 @@
174713 VdbeCoverageIf(v, op==OP_SeekGT);
174714 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
174715 }
174716 #endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
174717 }
174718 if( pTabList->a[pLevel->iFrom].fg.fromExists && i==pWInfo->nLevel-1 ){
174719 /* If the EXISTS-to-JOIN optimization was applied, then the EXISTS
174720 ** loop(s) will be the inner-most loops of the join. There might be
174721 ** multiple EXISTS loops, but they will all be nested, and the join
174722 ** order will not have been changed by the query planner. If the
174723 ** inner-most EXISTS loop sees a single successful row, it should
174724 ** break out of *all* EXISTS loops. But only the inner-most of the
174725 ** nested EXISTS loops should do this breakout. */
 
174726 int nOuter = 0; /* Nr of outer EXISTS that this one is nested within */
174727 while( nOuter<i ){
174728 if( !pTabList->a[pLevel[-nOuter-1].iFrom].fg.fromExists ) break;
174729 nOuter++;
174730 }
174731 testcase( nOuter>0 );
174732 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel[-nOuter].addrBrk);
174733 VdbeComment((v, "EXISTS break"));
 
 
 
 
174734 }
174735 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
174736 if( pLevel->op!=OP_Noop ){
174737 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
174738 sqlite3VdbeChangeP5(v, pLevel->p5);
@@ -189501,10 +189527,16 @@
189501 /*
189502 ** Find existing client data.
189503 */
189504 SQLITE_API void *sqlite3_get_clientdata(sqlite3 *db, const char *zName){
189505 DbClientData *p;
 
 
 
 
 
 
189506 sqlite3_mutex_enter(db->mutex);
189507 for(p=db->pDbData; p; p=p->pNext){
189508 if( strcmp(p->zName, zName)==0 ){
189509 void *pResult = p->pData;
189510 sqlite3_mutex_leave(db->mutex);
@@ -211254,11 +211286,14 @@
211254 */
211255 #define JSON_JSON 0x01 /* Result is always JSON */
211256 #define JSON_SQL 0x02 /* Result is always SQL */
211257 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
211258 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
211259 #define JSON_BLOB 0x08 /* Use the BLOB output format */
 
 
 
211260
211261
211262 /* A parsed JSON value. Lifecycle:
211263 **
211264 ** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
@@ -211300,10 +211335,11 @@
211300 /* Allowed values for JsonParse.eEdit */
211301 #define JEDIT_DEL 1 /* Delete if exists */
211302 #define JEDIT_REPL 2 /* Overwrite if exists */
211303 #define JEDIT_INS 3 /* Insert if not exists */
211304 #define JEDIT_SET 4 /* Insert or overwrite */
 
211305
211306 /*
211307 ** Maximum nesting depth of JSON for this implementation.
211308 **
211309 ** This limit is needed to avoid a stack overflow in the recursive
@@ -213796,11 +213832,12 @@
213796 /*
213797 ** Error returns from jsonLookupStep()
213798 */
213799 #define JSON_LOOKUP_ERROR 0xffffffff
213800 #define JSON_LOOKUP_NOTFOUND 0xfffffffe
213801 #define JSON_LOOKUP_PATHERROR 0xfffffffd
 
213802 #define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR)
213803
213804 /* Forward declaration */
213805 static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
213806
@@ -213825,11 +213862,11 @@
213825 ** using the substructure.
213826 */
213827 static u32 jsonCreateEditSubstructure(
213828 JsonParse *pParse, /* The original JSONB that is being edited */
213829 JsonParse *pIns, /* Populate this with the blob data to insert */
213830 const char *zTail /* Tail of the path that determins substructure */
213831 ){
213832 static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
213833 int rc;
213834 memset(pIns, 0, sizeof(*pIns));
213835 pIns->db = pParse->db;
@@ -213860,13 +213897,13 @@
213860 ** label, before returning.
213861 **
213862 ** Return one of the JSON_LOOKUP error codes if problems are seen.
213863 **
213864 ** This routine will also modify the blob. If pParse->eEdit is one of
213865 ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
213866 ** made to the selected value. If an edit is performed, then the return
213867 ** value does not necessarily point to the select element. If an edit
213868 ** is performed, the return value is only useful for detecting error
213869 ** conditions.
213870 */
213871 static u32 jsonLookupStep(
213872 JsonParse *pParse, /* The JSON to search */
@@ -213888,10 +213925,17 @@
213888 iRoot = iLabel;
213889 }
213890 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
213891 }else if( pParse->eEdit==JEDIT_INS ){
213892 /* Already exists, so json_insert() is a no-op */
 
 
 
 
 
 
 
213893 }else{
213894 /* json_set() or json_replace() */
213895 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
213896 }
213897 }
@@ -213959,10 +214003,14 @@
213959 u32 nIns; /* Total bytes to insert (label+value) */
213960 JsonParse v; /* BLOB encoding of the value to be inserted */
213961 JsonParse ix; /* Header of the label to be inserted */
213962 testcase( pParse->eEdit==JEDIT_INS );
213963 testcase( pParse->eEdit==JEDIT_SET );
 
 
 
 
213964 memset(&ix, 0, sizeof(ix));
213965 ix.db = pParse->db;
213966 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0);
213967 pParse->oom |= ix.oom;
213968 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
@@ -214034,10 +214082,11 @@
214034 if( j>iEnd ) return JSON_LOOKUP_ERROR;
214035 if( k>0 ) return JSON_LOOKUP_NOTFOUND;
214036 if( pParse->eEdit>=JEDIT_INS ){
214037 JsonParse v;
214038 testcase( pParse->eEdit==JEDIT_INS );
 
214039 testcase( pParse->eEdit==JEDIT_SET );
214040 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
214041 if( !JSON_LOOKUP_ISERROR(rc)
214042 && jsonBlobMakeEditable(pParse, v.nBlob)
214043 ){
@@ -214358,13 +214407,19 @@
214358 ** If ctx is not NULL then push the error message into ctx and return NULL.
214359 ** If ctx is NULL, then return the text of the error message.
214360 */
214361 static char *jsonBadPathError(
214362 sqlite3_context *ctx, /* The function call containing the error */
214363 const char *zPath /* The path with the problem */
 
214364 ){
214365 char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
 
 
 
 
 
214366 if( ctx==0 ) return zMsg;
214367 if( zMsg ){
214368 sqlite3_result_error(ctx, zMsg, -1);
214369 sqlite3_free(zMsg);
214370 }else{
@@ -214377,17 +214432,17 @@
214377 ** arguments come in pairs where each pair contains a JSON path and
214378 ** content to insert or set at that patch. Do the updates
214379 ** and return the result.
214380 **
214381 ** The specific operation is determined by eEdit, which can be one
214382 ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
214383 */
214384 static void jsonInsertIntoBlob(
214385 sqlite3_context *ctx,
214386 int argc,
214387 sqlite3_value **argv,
214388 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
214389 ){
214390 int i;
214391 u32 rc = 0;
214392 const char *zPath = 0;
214393 int flgs;
@@ -214435,11 +214490,11 @@
214435 jsonInsertIntoBlob_patherror:
214436 jsonParseFree(p);
214437 if( rc==JSON_LOOKUP_ERROR ){
214438 sqlite3_result_error(ctx, "malformed JSON", -1);
214439 }else{
214440 jsonBadPathError(ctx, zPath);
214441 }
214442 return;
214443 }
214444
214445 /*
@@ -214877,11 +214932,11 @@
214877 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
214878 if( JSON_LOOKUP_ISERROR(i) ){
214879 if( i==JSON_LOOKUP_NOTFOUND ){
214880 /* no-op */
214881 }else if( i==JSON_LOOKUP_PATHERROR ){
214882 jsonBadPathError(ctx, zPath);
214883 }else{
214884 sqlite3_result_error(ctx, "malformed JSON", -1);
214885 }
214886 eErr = 1;
214887 i = 0;
@@ -214982,11 +215037,11 @@
214982 }
214983 jsonStringTerminate(&jx);
214984 j = jsonLookupStep(p, 0, jx.zBuf, 0);
214985 jsonStringReset(&jx);
214986 }else{
214987 jsonBadPathError(ctx, zPath);
214988 goto json_extract_error;
214989 }
214990 if( j<p->nBlob ){
214991 if( argc==2 ){
214992 if( flags & JSON_JSON ){
@@ -215017,11 +215072,11 @@
215017 }
215018 }else if( j==JSON_LOOKUP_ERROR ){
215019 sqlite3_result_error(ctx, "malformed JSON", -1);
215020 goto json_extract_error;
215021 }else{
215022 jsonBadPathError(ctx, zPath);
215023 goto json_extract_error;
215024 }
215025 }
215026 if( argc>2 ){
215027 jsonAppendChar(&jx, ']');
@@ -215346,11 +215401,11 @@
215346 rc = jsonLookupStep(p, 0, zPath+1, 0);
215347 if( JSON_LOOKUP_ISERROR(rc) ){
215348 if( rc==JSON_LOOKUP_NOTFOUND ){
215349 continue; /* No-op */
215350 }else if( rc==JSON_LOOKUP_PATHERROR ){
215351 jsonBadPathError(ctx, zPath);
215352 }else{
215353 sqlite3_result_error(ctx, "malformed JSON", -1);
215354 }
215355 goto json_remove_done;
215356 }
@@ -215358,11 +215413,11 @@
215358 jsonReturnParse(ctx, p);
215359 jsonParseFree(p);
215360 return;
215361
215362 json_remove_patherror:
215363 jsonBadPathError(ctx, zPath);
215364
215365 json_remove_done:
215366 jsonParseFree(p);
215367 return;
215368 }
@@ -215402,20 +215457,22 @@
215402 static void jsonSetFunc(
215403 sqlite3_context *ctx,
215404 int argc,
215405 sqlite3_value **argv
215406 ){
215407
215408 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215409 int bIsSet = (flags&JSON_ISSET)!=0;
 
 
215410
215411 if( argc<1 ) return;
 
215412 if( (argc&1)==0 ) {
215413 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
215414 return;
215415 }
215416 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS);
215417 }
215418
215419 /*
215420 ** json_type(JSON)
215421 ** json_type(JSON, PATH)
@@ -215436,19 +215493,19 @@
215436 if( p==0 ) return;
215437 if( argc==2 ){
215438 zPath = (const char*)sqlite3_value_text(argv[1]);
215439 if( zPath==0 ) goto json_type_done;
215440 if( zPath[0]!='$' ){
215441 jsonBadPathError(ctx, zPath);
215442 goto json_type_done;
215443 }
215444 i = jsonLookupStep(p, 0, zPath+1, 0);
215445 if( JSON_LOOKUP_ISERROR(i) ){
215446 if( i==JSON_LOOKUP_NOTFOUND ){
215447 /* no-op */
215448 }else if( i==JSON_LOOKUP_PATHERROR ){
215449 jsonBadPathError(ctx, zPath);
215450 }else{
215451 sqlite3_result_error(ctx, "malformed JSON", -1);
215452 }
215453 goto json_type_done;
215454 }
@@ -215700,16 +215757,15 @@
215700 jsonAppendSqlValue(pStr, argv[0]);
215701 }
215702 }
215703 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
215704 JsonString *pStr;
 
215705 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
215706 if( pStr ){
215707 int flags;
215708 pStr->pCtx = ctx;
215709 jsonAppendChar(pStr, ']');
215710 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215711 if( pStr->eErr ){
215712 jsonReturnString(pStr, 0, 0);
215713 return;
215714 }else if( flags & JSON_BLOB ){
215715 jsonReturnStringAsBlob(pStr);
@@ -215726,10 +215782,13 @@
215726 pStr->bStatic = 1;
215727 }else{
215728 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
215729 jsonStringTrimOneChar(pStr);
215730 }
 
 
 
215731 }else{
215732 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
215733 }
215734 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
215735 }
@@ -215822,16 +215881,15 @@
215822 }
215823 }
215824 }
215825 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
215826 JsonString *pStr;
 
215827 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
215828 if( pStr ){
215829 int flags;
215830 jsonAppendChar(pStr, '}');
215831 pStr->pCtx = ctx;
215832 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215833 if( pStr->eErr ){
215834 jsonReturnString(pStr, 0, 0);
215835 return;
215836 }else if( flags & JSON_BLOB ){
215837 jsonReturnStringAsBlob(pStr);
@@ -215848,10 +215906,13 @@
215848 pStr->bStatic = 1;
215849 }else{
215850 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
215851 jsonStringTrimOneChar(pStr);
215852 }
 
 
 
215853 }else{
215854 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
215855 }
215856 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
215857 }
@@ -216348,11 +216409,11 @@
216348 if( idxNum==3 ){
216349 zRoot = (const char*)sqlite3_value_text(argv[1]);
216350 if( zRoot==0 ) return SQLITE_OK;
216351 if( zRoot[0]!='$' ){
216352 sqlite3_free(cur->pVtab->zErrMsg);
216353 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
216354 jsonEachCursorReset(p);
216355 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
216356 }
216357 p->nRoot = sqlite3Strlen30(zRoot);
216358 if( zRoot[1]==0 ){
@@ -216366,11 +216427,11 @@
216366 p->eType = 0;
216367 p->iEnd = 0;
216368 return SQLITE_OK;
216369 }
216370 sqlite3_free(cur->pVtab->zErrMsg);
216371 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
216372 jsonEachCursorReset(p);
216373 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
216374 }
216375 if( p->sParse.iLabel ){
216376 p->i = p->sParse.iLabel;
@@ -216456,10 +216517,12 @@
216456 /* | | | | | | */
216457 JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc),
216458 JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc),
216459 JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc),
216460 JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc),
 
 
216461 JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc),
216462 JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc),
216463 JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc),
216464 JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc),
216465 JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc),
@@ -232389,31 +232452,31 @@
232389 for(i=0; i<pTab->nCol; i++){
232390 int eType = *a;
232391 int isPK = pTab->abPK[i];
232392 if( bPkOnly && isPK==0 ) continue;
232393
232394 /* It is not possible for eType to be SQLITE_NULL here. The session
232395 ** module does not record changes for rows with NULL values stored in
232396 ** primary key columns. */
232397 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
232398 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
232399 || eType==SQLITE_NULL || eType==0
232400 );
232401 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
232402
232403 if( isPK ){
232404 a++;
232405 h = sessionHashAppendType(h, eType);
232406 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
232407 h = sessionHashAppendI64(h, sessionGetI64(a));
232408 a += 8;
232409 }else{
232410 int n;
232411 a += sessionVarintGet(a, &n);
232412 h = sessionHashAppendBlob(h, n, a);
232413 a += n;
232414 }
 
 
 
 
232415 }else{
232416 a += sessionSerialLen(a);
232417 }
232418 }
232419 return (h % nBucket);
@@ -234818,14 +234881,17 @@
234818 *pnChangeset = 0;
234819 *ppChangeset = 0;
234820 }
234821
234822 if( pSession->rc ) return pSession->rc;
234823 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
234824 if( rc!=SQLITE_OK ) return rc;
234825
234826 sqlite3_mutex_enter(sqlite3_db_mutex(db));
 
 
 
 
 
234827
234828 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
234829 if( pTab->nEntry ){
234830 const char *zName = pTab->zName;
234831 int i; /* Used to iterate through hash buckets */
@@ -235377,12 +235443,19 @@
235377
235378 while( rc==SQLITE_OK ){
235379 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
235380 nRead++;
235381 }
 
 
 
 
235382 if( (pIn->iNext + nRead)<pIn->nData ) break;
235383 rc = sessionInputBuffer(pIn, nRead + 100);
 
 
 
235384 }
235385 *pnByte = nRead+1;
235386 return rc;
235387 }
235388
@@ -253281,11 +253354,11 @@
253281 ){
253282 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
253283 int iSegid = pSeg->pSeg->iSegid;
253284 u8 *aPg = pSeg->pLeaf->p;
253285 int nPg = pSeg->pLeaf->nn;
253286 int iPgIdx = pSeg->pLeaf->szLeaf;
253287
253288 u64 iDelta = 0;
253289 int iNextOff = 0;
253290 int iOff = 0;
253291 int nIdx = 0;
@@ -253360,11 +253433,11 @@
253360 iStart = iSOP + (nPos/2);
253361 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
253362 iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
253363 }
253364 assert_nc( iSOP==pSeg->iLeafOffset );
253365 iNextOff = pSeg->iLeafOffset + pSeg->nPos;
253366 }
253367 }
253368
253369 iOff = iStart;
253370
@@ -253440,35 +253513,35 @@
253440 if( iNextOff!=iPgIdx ){
253441 /* This is the only position-list associated with the term, and there
253442 ** is another term following it on this page. So the subsequent term
253443 ** needs to be moved to replace the term associated with the entry
253444 ** being removed. */
253445 int nPrefix = 0;
253446 int nSuffix = 0;
253447 int nPrefix2 = 0;
253448 int nSuffix2 = 0;
253449
253450 iDelKeyOff = iNextOff;
253451 iNextOff += fts5GetVarint32(&aPg[iNextOff], nPrefix2);
253452 iNextOff += fts5GetVarint32(&aPg[iNextOff], nSuffix2);
253453
253454 if( iKey!=1 ){
253455 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nPrefix);
253456 }
253457 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nSuffix);
253458
253459 nPrefix = MIN(nPrefix, nPrefix2);
253460 nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
253461
253462 if( (iKeyOff+nSuffix)>iPgIdx || (iNextOff+nSuffix2)>iPgIdx ){
253463 FTS5_CORRUPT_IDX(p);
253464 }else{
253465 if( iKey!=1 ){
253466 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix);
253467 }
253468 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix);
253469 if( nPrefix2>pSeg->term.n ){
253470 FTS5_CORRUPT_IDX(p);
253471 }else if( nPrefix2>nPrefix ){
253472 memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix);
253473 iOff += (nPrefix2-nPrefix);
253474 }
@@ -253495,11 +253568,11 @@
253495 Fts5Data *pTerm = fts5DataRead(p, iId);
253496 if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
253497 u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
253498 int nTermIdx = pTerm->nn - pTerm->szLeaf;
253499 int iTermIdx = 0;
253500 int iTermOff = 0;
253501
253502 while( 1 ){
253503 u32 iVal = 0;
253504 int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal);
253505 iTermOff += iVal;
@@ -253506,16 +253579,19 @@
253506 if( (iTermIdx+nByte)>=nTermIdx ) break;
253507 iTermIdx += nByte;
253508 }
253509 nTermIdx = iTermIdx;
253510
253511 memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
253512 fts5PutU16(&pTerm->p[2], iTermOff);
253513
253514 fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
253515 if( nTermIdx==0 ){
253516 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
 
 
 
253517 }
253518 }
253519 fts5DataRelease(pTerm);
253520 }
253521 }
@@ -253534,11 +253610,13 @@
253534 int nShift = iNextOff - iOff; /* Distance to move them */
253535
253536 int iPrevKeyOut = 0;
253537 int iKeyIn = 0;
253538
253539 memmove(&aPg[iOff], &aPg[iNextOff], nMove);
 
 
253540 iPgIdx -= nShift;
253541 nPg = iPgIdx;
253542 fts5PutU16(&aPg[2], iPgIdx);
253543
253544 for(iIdx=0; iIdx<nIdx; /* no-op */){
@@ -261180,11 +261258,11 @@
261180 int nArg, /* Number of args */
261181 sqlite3_value **apUnused /* Function arguments */
261182 ){
261183 assert( nArg==0 );
261184 UNUSED_PARAM2(nArg, apUnused);
261185 sqlite3_result_text(pCtx, "fts5: 2026-01-09 00:41:35 9adab8b2bef4130abd358d53384cb5f4dd691b808336bb7102793b0165b1c516", -1, SQLITE_TRANSIENT);
261186 }
261187
261188 /*
261189 ** Implementation of fts5_locale(LOCALE, TEXT) function.
261190 **
261191
--- 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 ** 4733d351ec2376291f093ba8d2ba71d82c6f with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 #define SQLITE_VERSION "3.52.0"
471 #define SQLITE_VERSION_NUMBER 3052000
472 #define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
473 #define SQLITE_SCM_BRANCH "trunk"
474 #define SQLITE_SCM_TAGS ""
475 #define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
476
477 /*
478 ** CAPI3REF: Run-Time Library Version Numbers
479 ** KEYWORDS: sqlite3_version sqlite3_sourceid
480 **
@@ -5194,12 +5194,12 @@
5194 ** it should be a pointer to well-formed UTF8 text.
5195 ** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
5196 ** it should be a pointer to well-formed UTF16 text.
5197 ** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
5198 ** it should be a pointer to a well-formed unicode string that is
5199 ** either UTF8 if the sixth parameter is SQLITE_UTF8 or SQLITE_UTF8_ZT,
5200 ** or UTF16 otherwise.
5201 **
5202 ** [[byte-order determination rules]] ^The byte-order of
5203 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5204 ** found in the first character, which is removed, or in the absence of a BOM
5205 ** the byte order is the native byte order of the host
@@ -5241,14 +5241,19 @@
5241 ** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
5242 ** object is to be copied prior to the return from sqlite3_bind_*(). ^The
5243 ** object and pointer to it must remain valid until then. ^SQLite will then
5244 ** manage the lifetime of its private copy.
5245 **
5246 ** ^The sixth argument (the E argument)
5247 ** to sqlite3_bind_text64(S,K,Z,N,D,E) must be one of
5248 ** [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
5249 ** or [SQLITE_UTF16LE] to specify the encoding of the text in the
5250 ** third parameter, Z. The special value [SQLITE_UTF8_ZT] means that the
5251 ** string argument is both UTF-8 encoded and is zero-terminated. In other
5252 ** words, SQLITE_UTF8_ZT means that the Z array is allocated to hold at
5253 ** least N+1 bytes and that the Z&#91;N&#93; byte is zero. If
5254 ** the E argument to sqlite3_bind_text64(S,K,Z,N,D,E) is not one of the
5255 ** allowed values shown above, or if the text encoding is different
5256 ** from the encoding specified by the sixth parameter, then the behavior
5257 ** is undefined.
5258 **
5259 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -6111,17 +6116,63 @@
6116 /*
6117 ** CAPI3REF: Text Encodings
6118 **
6119 ** These constants define integer codes that represent the various
6120 ** text encodings supported by SQLite.
6121 **
6122 ** <dl>
6123 ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
6124 **
6125 ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
6126 ** with each code point being expressed "little endian" - the least significant
6127 ** byte first. This is the usual encoding, for example on Windows.</dd>
6128 **
6129 ** [[SQLITE_UTF16BE]] <dt>SQLITE_UTF16BE</dt><dd>Text is encoding as UTF-16
6130 ** with each code point being expressed "big endian" - the most significant
6131 ** byte first. This encoding is less common, but is still sometimes seen,
6132 ** specially on older systems.
6133 **
6134 ** [[SQLITE_UTF16]] <dt>SQLITE_UTF16</dt><dd>Text is encoding as UTF-16
6135 ** with each code point being expressed either little endian or as big
6136 ** endian, according to the native endianness of the host computer.
6137 **
6138 ** [[SQLITE_ANY]] <dt>SQLITE_ANY</dt><dd>This encoding value may only be used
6139 ** to declare the preferred text for [application-defined SQL functions]
6140 ** created using [sqlite3_create_function()] and similar. If the preferred
6141 ** encoding (the 4th parameter to sqlite3_create_function() - the eTextRep
6142 ** parameter) is SQLITE_ANY, that indicates that the function does not have
6143 ** a preference regarding the text encoding of its parameters and can take
6144 ** any text encoding that the SQLite core find convenient to supply. This
6145 ** option is deprecated. Please do not use it in new applications.
6146 **
6147 ** [[SQLITE_UTF16_ALIGNED]] <dt>SQLITE_UTF16_ALIGNED</dt><dd>This encoding
6148 ** value may be used as the 3rd parameter (the eTextRep parameter) to
6149 ** [sqlite3_create_collation()] and similar. This encoding value means
6150 ** that the application-defined collating sequence created expects its
6151 ** input strings to be in UTF16 in native byte order, and that the start
6152 ** of the strings must be aligned to a 2-byte boundary.
6153 **
6154 ** [[SQLITE_UTF8_ZT]] <dt>SQLITE_UTF8_ZT</dt><dd>This option can only be
6155 ** used to specify the text encoding to strings input to [sqlite3_result_text64()]
6156 ** and [sqlite3_bind_text64()]. It means that the input string (call it "z")
6157 ** is UTF-8 encoded and that it is zero-terminated. If the length parameter
6158 ** (call it "n") is non-negative, this encoding option means that the caller
6159 ** guarantees that z array contains at least n+1 bytes and that the z&#91;n&#93;
6160 ** byte has a value of zero.
6161 ** This option gives the same output as SQLITE_UTF8, but can be more efficient
6162 ** by avoiding the need to make a copy of the input string, in some cases.
6163 ** However, if z is allocated to hold fewer than n+1 bytes or if the
6164 ** z&#91;n&#93; byte is not zero, undefined behavior may result.
6165 ** </dl>
6166 */
6167 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
6168 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
6169 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
6170 #define SQLITE_UTF16 4 /* Use native byte order */
6171 #define SQLITE_ANY 5 /* Deprecated */
6172 #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
6173 #define SQLITE_UTF8_ZT 16 /* Zero-terminated UTF8 */
6174
6175 /*
6176 ** CAPI3REF: Function Flags
6177 **
6178 ** These constants may be ORed together with the
@@ -6738,14 +6789,18 @@
6789 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
6790 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6791 ** set the return value of the application-defined function to be
6792 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6793 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6794 ** ^The sqlite3_result_text64(C,Z,N,D,E) interface sets the return value of an
6795 ** application-defined function to be a text string in an encoding
6796 ** specified the E parameter, which must be one
6797 ** of [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
6798 ** or [SQLITE_UTF16LE]. ^The special value [SQLITE_UTF8_ZT] means that
6799 ** the result text is both UTF-8 and zero-terminated. In other words,
6800 ** SQLITE_UTF8_ZT means that the Z array holds at least N+1 byes and that
6801 ** the Z&#91;N&#93; is zero.
6802 ** ^SQLite takes the text result from the application from
6803 ** the 2nd parameter of the sqlite3_result_text* interfaces.
6804 ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6805 ** other than sqlite3_result_text64() is negative, then SQLite computes
6806 ** the string length itself by searching the 2nd parameter for the first
@@ -6828,11 +6883,11 @@
6883 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6884 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6885 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6886 SQLITE_API void sqlite3_result_null(sqlite3_context*);
6887 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6888 SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char *z, sqlite3_uint64 n,
6889 void(*)(void*), unsigned char encoding);
6890 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6891 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6892 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6893 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
@@ -14373,10 +14428,31 @@
14428 #ifndef SQLITE_MAX_LENGTH
14429 # define SQLITE_MAX_LENGTH 1000000000
14430 #endif
14431 #define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */
14432
14433 /*
14434 ** Maximum size of any single memory allocation.
14435 **
14436 ** This is not a limit on the total amount of memory used. This is
14437 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
14438 **
14439 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
14440 ** This provides a 256-byte safety margin for defense against 32-bit
14441 ** signed integer overflow bugs when computing memory allocation sizes.
14442 ** Paranoid applications might want to reduce the maximum allocation size
14443 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
14444 ** or even smaller would be reasonable upper bounds on the size of a memory
14445 ** allocations for most applications.
14446 */
14447 #ifndef SQLITE_MAX_ALLOCATION_SIZE
14448 # define SQLITE_MAX_ALLOCATION_SIZE 2147483391
14449 #endif
14450 #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
14451 # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
14452 #endif
14453
14454 /*
14455 ** This is the maximum number of
14456 **
14457 ** * Columns in a table
14458 ** * Columns in an index
@@ -20223,31 +20299,17 @@
20299 };
20300
20301 /*
20302 ** An instance of the following structure contains all information
20303 ** needed to generate code for a single SELECT statement.
 
 
 
 
 
 
 
 
 
 
 
 
 
20304 */
20305 struct Select {
20306 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
20307 LogEst nSelectRow; /* Estimated number of result rows */
20308 u32 selFlags; /* Various SF_* values */
20309 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
20310 u32 selId; /* Unique identifier number for this SELECT */
 
20311 ExprList *pEList; /* The fields of the result */
20312 SrcList *pSrc; /* The FROM clause */
20313 Expr *pWhere; /* The WHERE clause */
20314 ExprList *pGroupBy; /* The GROUP BY clause */
20315 Expr *pHaving; /* The HAVING clause */
@@ -20275,28 +20337,28 @@
20337 #define SF_Distinct 0x0000001 /* Output should be DISTINCT */
20338 #define SF_All 0x0000002 /* Includes the ALL keyword */
20339 #define SF_Resolved 0x0000004 /* Identifiers have been resolved */
20340 #define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
20341 #define SF_HasAgg 0x0000010 /* Contains aggregate functions */
20342 /* 0x0000020 // available for reuse */
20343 #define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
20344 #define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
20345 #define SF_Compound 0x0000100 /* Part of a compound query */
20346 #define SF_Values 0x0000200 /* Synthesized from VALUES clause */
20347 #define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
20348 #define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
20349 #define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
20350 #define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
20351 #define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
20352 /* 0x0008000 // available for reuse */
20353 #define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
20354 #define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
20355 #define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
20356 #define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
20357 #define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
20358 #define SF_View 0x0200000 /* SELECT statement is a view */
20359 /* 0x0400000 // available for reuse */
20360 #define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
20361 #define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
20362 #define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
20363 #define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
20364 #define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
@@ -20312,15 +20374,10 @@
20374 /*
20375 ** The results of a SELECT can be distributed in several ways, as defined
20376 ** by one of the following macros. The "SRT" prefix means "SELECT Result
20377 ** Type".
20378 **
 
 
 
 
 
20379 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
20380 ** set is not empty.
20381 **
20382 ** SRT_Discard Throw the results away. This is used by SELECT
20383 ** statements within triggers whose only purpose is
@@ -20380,34 +20437,32 @@
20437 ** column returned by the SELECT is used as the integer
20438 ** key. If (pDest->iSDParm>0), then the table is an index
20439 ** table. (pDest->iSDParm) is the number of key columns in
20440 ** each index record in this case.
20441 */
20442 #define SRT_Exists 1 /* Store 1 if the result is not empty */
20443 #define SRT_Discard 2 /* Do not save the results anywhere */
20444 #define SRT_DistFifo 3 /* Like SRT_Fifo, but unique results only */
20445 #define SRT_DistQueue 4 /* Like SRT_Queue, but unique results only */
 
 
20446
20447 /* The DISTINCT clause is ignored for all of the above. Not that
20448 ** IgnorableDistinct() implies IgnorableOrderby() */
20449 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
20450
20451 #define SRT_Queue 5 /* Store result in an queue */
20452 #define SRT_Fifo 6 /* Store result as data with an automatic rowid */
20453
20454 /* The ORDER BY clause is ignored for all of the above */
20455 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
20456
20457 #define SRT_Output 7 /* Output each row of result */
20458 #define SRT_Mem 8 /* Store result in a memory cell */
20459 #define SRT_Set 9 /* Store results as keys in an index */
20460 #define SRT_EphemTab 10 /* Create transient tab and store like SRT_Table */
20461 #define SRT_Coroutine 11 /* Generate a single row of result */
20462 #define SRT_Table 12 /* Store result as data with an automatic rowid */
20463 #define SRT_Upfrom 13 /* Store result as data with rowid */
20464
20465 /*
20466 ** An instance of this object describes where to put of the results of
20467 ** a SELECT statement.
20468 */
@@ -24510,10 +24565,11 @@
24565 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
24566 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
24567 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
24568 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
24569 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
24570 SQLITE_PRIVATE int sqlite3VdbeMemSetText(Mem*, const char*, i64, void(*)(void*));
24571 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
24572 #ifdef SQLITE_OMIT_FLOATING_POINT
24573 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
24574 #else
24575 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
@@ -31356,31 +31412,10 @@
31412 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
31413 }
31414 *pp = p;
31415 }
31416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31417 /*
31418 ** Allocate memory. This routine is like sqlite3_malloc() except that it
31419 ** assumes the memory subsystem has already been initialized.
31420 */
31421 SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
@@ -31600,12 +31635,11 @@
31635 }
31636 if( nBytes==0 ){
31637 sqlite3_free(pOld); /* IMP: R-26507-47431 */
31638 return 0;
31639 }
31640 if( nBytes>SQLITE_MAX_ALLOCATION_SIZE ){
 
31641 return 0;
31642 }
31643 nOld = sqlite3MallocSize(pOld);
31644 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
31645 ** argument to xRealloc is always a value returned by a prior call to
@@ -77429,11 +77463,11 @@
77463 }
77464 assert( cursorHoldsMutex(pCur) );
77465
77466 getCellInfo(pCur);
77467 aPayload = pCur->info.pPayload;
77468 assert( (u64)offset+(u64)amt <= (u64)pCur->info.nPayload );
77469
77470 assert( aPayload > pPage->aData );
77471 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
77472 /* Trying to read or write past the end of the data is an error. The
77473 ** conditional above is really:
@@ -77986,11 +78020,11 @@
78020 SQLITE_PRIVATE int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes){
78021 int rc;
78022
78023 assert( cursorOwnsBtShared(pCur) );
78024 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
78025 if( NEVER(pCur->eState==CURSOR_VALID) ){
78026 *pRes = 0;
78027 return SQLITE_OK;
78028 }
78029 rc = moveToRoot(pCur);
78030 if( rc==SQLITE_EMPTY ){
@@ -85879,10 +85913,88 @@
85913 #endif
85914
85915
85916 return SQLITE_OK;
85917 }
85918
85919 /* Like sqlite3VdbeMemSetStr() except:
85920 **
85921 ** enc is always SQLITE_UTF8
85922 ** pMem->db is always non-NULL
85923 */
85924 SQLITE_PRIVATE int sqlite3VdbeMemSetText(
85925 Mem *pMem, /* Memory cell to set to string value */
85926 const char *z, /* String pointer */
85927 i64 n, /* Bytes in string, or negative */
85928 void (*xDel)(void*) /* Destructor function */
85929 ){
85930 i64 nByte = n; /* New value for pMem->n */
85931 u16 flags;
85932
85933 assert( pMem!=0 );
85934 assert( pMem->db!=0 );
85935 assert( sqlite3_mutex_held(pMem->db->mutex) );
85936 assert( !sqlite3VdbeMemIsRowSet(pMem) );
85937
85938 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
85939 if( !z ){
85940 sqlite3VdbeMemSetNull(pMem);
85941 return SQLITE_OK;
85942 }
85943
85944 if( nByte<0 ){
85945 nByte = strlen(z);
85946 flags = MEM_Str|MEM_Term;
85947 }else{
85948 flags = MEM_Str;
85949 }
85950 if( nByte>(i64)pMem->db->aLimit[SQLITE_LIMIT_LENGTH] ){
85951 if( xDel && xDel!=SQLITE_TRANSIENT ){
85952 if( xDel==SQLITE_DYNAMIC ){
85953 sqlite3DbFree(pMem->db, (void*)z);
85954 }else{
85955 xDel((void*)z);
85956 }
85957 }
85958 sqlite3VdbeMemSetNull(pMem);
85959 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
85960 }
85961
85962 /* The following block sets the new values of Mem.z and Mem.xDel. It
85963 ** also sets a flag in local variable "flags" to indicate the memory
85964 ** management (one of MEM_Dyn or MEM_Static).
85965 */
85966 if( xDel==SQLITE_TRANSIENT ){
85967 i64 nAlloc = nByte + 1;
85968 testcase( nAlloc==31 );
85969 testcase( nAlloc==32 );
85970 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
85971 return SQLITE_NOMEM_BKPT;
85972 }
85973 assert( pMem->z!=0 );
85974 memcpy(pMem->z, z, nByte);
85975 pMem->z[nByte] = 0;
85976 }else{
85977 sqlite3VdbeMemRelease(pMem);
85978 pMem->z = (char *)z;
85979 if( xDel==SQLITE_DYNAMIC ){
85980 pMem->zMalloc = pMem->z;
85981 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
85982 pMem->xDel = 0;
85983 }else if( xDel==SQLITE_STATIC ){
85984 pMem->xDel = xDel;
85985 flags |= MEM_Static;
85986 }else{
85987 pMem->xDel = xDel;
85988 flags |= MEM_Dyn;
85989 }
85990 }
85991 pMem->flags = flags;
85992 pMem->n = (int)(nByte & 0x7fffffff);
85993 pMem->enc = SQLITE_UTF8;
85994 return SQLITE_OK;
85995 }
85996
85997 /*
85998 ** Move data out of a btree key or data field and into a Mem structure.
85999 ** The data is payload from the entry that pCur is currently pointing
86000 ** to. offset and amt determine what portion of the data or key to retrieve.
@@ -85903,11 +86015,16 @@
86015 u32 amt, /* Number of bytes to return. */
86016 Mem *pMem /* OUT: Return data in this Mem structure. */
86017 ){
86018 int rc;
86019 pMem->flags = MEM_Null;
86020 testcase( amt==SQLITE_MAX_ALLOCATION_SIZE-1 );
86021 testcase( amt==SQLITE_MAX_ALLOCATION_SIZE );
86022 if( amt>=SQLITE_MAX_ALLOCATION_SIZE ){
86023 return SQLITE_NOMEM_BKPT;
86024 }
86025 if( (u64)amt + (u64)offset > (u64)sqlite3BtreeMaxRecordSize(pCur) ){
86026 return SQLITE_CORRUPT_BKPT;
86027 }
86028 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
86029 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
86030 if( rc==SQLITE_OK ){
@@ -89587,11 +89704,11 @@
89704 assert( !zName || xDel!=SQLITE_DYNAMIC );
89705 return SQLITE_NOMEM_BKPT;
89706 }
89707 assert( p->aColName!=0 );
89708 pColName = &(p->aColName[idx+var*p->nResAlloc]);
89709 rc = sqlite3VdbeMemSetText(pColName, zName, -1, xDel);
89710 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
89711 return rc;
89712 }
89713
89714 /*
@@ -92665,11 +92782,27 @@
92782 int n, /* Bytes in string, or negative */
92783 u8 enc, /* Encoding of z. 0 for BLOBs */
92784 void (*xDel)(void*) /* Destructor function */
92785 ){
92786 Mem *pOut = pCtx->pOut;
92787 int rc;
92788 if( enc==SQLITE_UTF8 ){
92789 rc = sqlite3VdbeMemSetText(pOut, z, n, xDel);
92790 }else if( enc==SQLITE_UTF8_ZT ){
92791 /* It is usually considered improper to assert() on an input. However,
92792 ** the following assert() is checking for inputs that are documented
92793 ** to result in undefined behavior. */
92794 assert( z==0
92795 || n<0
92796 || n>pOut->db->aLimit[SQLITE_LIMIT_LENGTH]
92797 || z[n]==0
92798 );
92799 rc = sqlite3VdbeMemSetText(pOut, z, n, xDel);
92800 pOut->flags |= MEM_Term;
92801 }else{
92802 rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
92803 }
92804 if( rc ){
92805 if( rc==SQLITE_TOOBIG ){
92806 sqlite3_result_error_toobig(pCtx);
92807 }else{
92808 /* The only errors possible from sqlite3VdbeMemSetStr are
@@ -92858,11 +92991,11 @@
92991 return;
92992 }
92993 #endif
92994 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92995 assert( xDel!=SQLITE_DYNAMIC );
92996 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF8_ZT ){
92997 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
92998 n &= ~(u64)1;
92999 }
93000 if( n>0x7fffffff ){
93001 (void)invokeValueDestructor(z, xDel, pCtx);
@@ -93318,11 +93451,11 @@
93451 if( rc==SQLITE_OK ){
93452 u32 sz; /* Size of current row in bytes */
93453 Mem sMem; /* Raw content of current row */
93454 memset(&sMem, 0, sizeof(sMem));
93455 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
93456 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,sz,&sMem);
93457 if( rc==SQLITE_OK ){
93458 u8 *zBuf = (u8*)sMem.z;
93459 u32 iSerial;
93460 sqlite3_value *pOut = pRhs->pOut;
93461 int iOff = 1 + getVarint32(&zBuf[1], iSerial);
@@ -93967,17 +94100,29 @@
94100 rc = vdbeUnbind(p, (u32)(i-1));
94101 if( rc==SQLITE_OK ){
94102 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
94103 if( zData!=0 ){
94104 pVar = &p->aVar[i-1];
94105 if( encoding==SQLITE_UTF8 ){
94106 rc = sqlite3VdbeMemSetText(pVar, zData, nData, xDel);
94107 }else if( encoding==SQLITE_UTF8_ZT ){
94108 /* It is usually consider improper to assert() on an input.
94109 ** However, the following assert() is checking for inputs
94110 ** that are documented to result in undefined behavior. */
94111 assert( zData==0
94112 || nData<0
94113 || nData>pVar->db->aLimit[SQLITE_LIMIT_LENGTH]
94114 || ((u8*)zData)[nData]==0
94115 );
94116 rc = sqlite3VdbeMemSetText(pVar, zData, nData, xDel);
94117 pVar->flags |= MEM_Term;
94118 }else{
94119 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
94120 if( encoding==0 ) pVar->enc = ENC(p->db);
94121 }
94122 if( rc==SQLITE_OK && encoding!=0 ){
94123 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
94124 }
94125 if( rc ){
94126 sqlite3Error(p->db, rc);
94127 rc = sqlite3ApiExit(p->db, rc);
94128 }
@@ -94085,11 +94230,11 @@
94230 sqlite3_uint64 nData,
94231 void (*xDel)(void*),
94232 unsigned char enc
94233 ){
94234 assert( xDel!=SQLITE_DYNAMIC );
94235 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF8_ZT ){
94236 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
94237 nData &= ~(u64)1;
94238 }
94239 return bindText(pStmt, i, zData, nData, xDel, enc);
94240 }
@@ -101783,24 +101928,19 @@
101928 rc = sqlite3VdbeSorterWrite(pC, pIn2);
101929 if( rc) goto abort_due_to_error;
101930 break;
101931 }
101932
101933 /* Opcode: IdxDelete P1 P2 P3 * *
101934 ** Synopsis: key=r[P2@P3]
101935 **
101936 ** The content of P3 registers starting at register P2 form
101937 ** an unpacked index key. This opcode removes that entry from the
101938 ** index opened by cursor P1.
101939 **
101940 ** Raise an SQLITE_CORRUPT_INDEX error if no matching index entry is found
101941 ** and not in writable_schema mode.
 
 
 
 
 
101942 */
101943 case OP_IdxDelete: {
101944 VdbeCursor *pC;
101945 BtCursor *pCrsr;
101946 int res;
@@ -101822,11 +101962,11 @@
101962 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
101963 if( rc ) goto abort_due_to_error;
101964 if( res==0 ){
101965 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
101966 if( rc ) goto abort_due_to_error;
101967 }else if( !sqlite3WritableSchema(db) ){
101968 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
101969 goto abort_due_to_error;
101970 }
101971 assert( pC->deferredMoveto==0 );
101972 pC->cacheStatus = CACHE_STALE;
@@ -113272,13 +113412,11 @@
113412 pNew->pNext = pNext;
113413 pNew->pPrior = 0;
113414 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
113415 pNew->iLimit = 0;
113416 pNew->iOffset = 0;
113417 pNew->selFlags = p->selFlags;
 
 
113418 pNew->nSelectRow = p->nSelectRow;
113419 pNew->pWith = sqlite3WithDup(db, p->pWith);
113420 #ifndef SQLITE_OMIT_WINDOWFUNC
113421 pNew->pWin = 0;
113422 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
@@ -115496,12 +115634,13 @@
115634 if( destIfFalse==destIfNull ){
115635 /* Combine Step 3 and Step 5 into a single opcode */
115636 if( ExprHasProperty(pExpr, EP_Subrtn) ){
115637 const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
115638 assert( pOp->opcode==OP_Once || pParse->nErr );
115639 if( pOp->p3>0 ){ /* tag-202407032019 */
115640 assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter)
115641 || pParse->nErr );
115642 sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse,
115643 rLhs, nVector); VdbeCoverage(v);
115644 }
115645 }
115646 sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
@@ -132240,11 +132379,10 @@
132379 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
132380 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
132381 &iPartIdxLabel, pPrior, r1);
132382 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
132383 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
 
132384 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
132385 pPrior = pIdx;
132386 }
132387 }
132388
@@ -133587,11 +133725,11 @@
133725 }else{
133726 goto unistr_error;
133727 }
133728 }
133729 zOut[j] = 0;
133730 sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8_ZT);
133731 return;
133732
133733 unistr_error:
133734 sqlite3_free(zOut);
133735 sqlite3_result_error(context, "invalid Unicode escape", -1);
@@ -133680,11 +133818,11 @@
133818 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
133819 *zOut++ = 0x80 + (u8)(c & 0x3F);
133820 } \
133821 }
133822 *zOut = 0;
133823 sqlite3_result_text64(context, (char*)z, zOut-z,sqlite3_free,SQLITE_UTF8_ZT);
133824 }
133825
133826 /*
133827 ** The hex() function. Interpret the argument as a blob. Return
133828 ** a hexadecimal rendering as text.
@@ -133709,11 +133847,11 @@
133847 *(z++) = hexdigits[(c>>4)&0xf];
133848 *(z++) = hexdigits[c&0xf];
133849 }
133850 *z = 0;
133851 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
133852 sqlite3_free, SQLITE_UTF8_ZT);
133853 }
133854 }
133855
133856 /*
133857 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
@@ -134047,11 +134185,11 @@
134185 }
134186 }
134187 }
134188 z[j] = 0;
134189 assert( j<=n );
134190 sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8_ZT);
134191 }
134192
134193 /*
134194 ** The CONCAT(...) function. Generate a string result that is the
134195 ** concatentation of all non-null arguments.
@@ -141235,10 +141373,11 @@
141373 int (*set_errmsg)(sqlite3*,int,const char*);
141374 int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
141375 /* Version 3.52.0 and later */
141376 void (*str_truncate)(sqlite3_str*,int);
141377 void (*str_free)(sqlite3_str*);
141378 int (*carray_bind)(sqlite3_stmt*,int,void*,int,int,void(*)(void*));
141379 };
141380
141381 /*
141382 ** This is the function signature used for all extension entry points. It
141383 ** is also defined in the file "loadext.c".
@@ -141576,10 +141715,11 @@
141715 #define sqlite3_set_errmsg sqlite3_api->set_errmsg
141716 #define sqlite3_db_status64 sqlite3_api->db_status64
141717 /* Version 3.52.0 and later */
141718 #define sqlite3_str_truncate sqlite3_api->str_truncate
141719 #define sqlite3_str_free sqlite3_api->str_free
141720 #define sqlite3_carray_bind sqlite3_api->carray_bind
141721 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
141722
141723 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
141724 /* This case when the file really is being compiled as a loadable
141725 ** extension */
@@ -142105,11 +142245,16 @@
142245 /* Version 3.51.0 and later */
142246 sqlite3_set_errmsg,
142247 sqlite3_db_status64,
142248 /* Version 3.52.0 and later */
142249 sqlite3_str_truncate,
142250 sqlite3_str_free,
142251 #ifdef SQLITE_ENABLE_CARRAY
142252 sqlite3_carray_bind
142253 #else
142254 0
142255 #endif
142256 };
142257
142258 /* True if x is the directory separator character
142259 */
142260 #if SQLITE_OS_WIN
@@ -147528,12 +147673,10 @@
147673 pNew->op = TK_SELECT;
147674 pNew->selFlags = selFlags;
147675 pNew->iLimit = 0;
147676 pNew->iOffset = 0;
147677 pNew->selId = ++pParse->nSelect;
 
 
147678 pNew->nSelectRow = 0;
147679 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1);
147680 pNew->pSrc = pSrc;
147681 pNew->pWhere = pWhere;
147682 pNew->pGroupBy = pGroupBy;
@@ -148677,33 +148820,10 @@
148820 codeOffset(v, p->iOffset, iContinue);
148821 }
148822 }
148823
148824 switch( eDest ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148825 /* Store the result as data using a unique key.
148826 */
148827 case SRT_Fifo:
148828 case SRT_DistFifo:
148829 case SRT_Table:
@@ -149986,11 +150106,11 @@
150106 **
150107 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
150108 ** function is responsible for ensuring that this structure is eventually
150109 ** freed.
150110 */
150111 static KeyInfo *multiSelectByMergeKeyInfo(Parse *pParse, Select *p, int nExtra){
150112 ExprList *pOrderBy = p->pOrderBy;
150113 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
150114 sqlite3 *db = pParse->db;
150115 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
150116 if( pRet ){
@@ -150121,21 +150241,41 @@
150241
150242 /* Allocate cursors for Current, Queue, and Distinct. */
150243 regCurrent = ++pParse->nMem;
150244 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
150245 if( pOrderBy ){
150246 KeyInfo *pKeyInfo = multiSelectByMergeKeyInfo(pParse, p, 1);
150247 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
150248 (char*)pKeyInfo, P4_KEYINFO);
150249 destQueue.pOrderBy = pOrderBy;
150250 }else{
150251 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
150252 }
150253 VdbeComment((v, "Queue table"));
150254 if( iDistinct ){
150255 /* Generate an ephemeral table used to enforce distinctness on the
150256 ** output of the recursive part of the CTE.
150257 */
150258 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
150259 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
150260
150261 assert( p->pNext==0 );
150262 assert( p->pEList!=0 );
150263 nCol = p->pEList->nExpr;
150264 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nCol, 1);
150265 if( pKeyInfo ){
150266 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
150267 *apColl = multiSelectCollSeq(pParse, p, i);
150268 if( 0==*apColl ){
150269 *apColl = pParse->db->pDfltColl;
150270 }
150271 }
150272 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iDistinct, nCol, 0,
150273 (void*)pKeyInfo, P4_KEYINFO);
150274 }else{
150275 assert( pParse->nErr>0 );
150276 }
150277 }
150278
150279 /* Detach the ORDER BY clause from the compound SELECT */
150280 p->pOrderBy = 0;
150281
@@ -150206,11 +150346,11 @@
150346 return;
150347 }
150348 #endif /* SQLITE_OMIT_CTE */
150349
150350 /* Forward references */
150351 static int multiSelectByMerge(
150352 Parse *pParse, /* Parsing context */
150353 Select *p, /* The right-most of SELECTs to be coded */
150354 SelectDest *pDest /* What to do with query results */
150355 );
150356
@@ -150355,317 +150495,80 @@
150495 #ifndef SQLITE_OMIT_CTE
150496 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
150497 generateWithRecursiveQuery(pParse, p, &dest);
150498 }else
150499 #endif
 
 
 
150500 if( p->pOrderBy ){
150501 /* If the compound has an ORDER BY clause, then always use the merge
150502 ** algorithm. */
150503 return multiSelectByMerge(pParse, p, pDest);
150504 }else if( p->op!=TK_ALL ){
150505 /* If the compound is EXCEPT, INTERSECT, or UNION (anything other than
150506 ** UNION ALL) then also always use the merge algorithm. However, the
150507 ** multiSelectByMerge() routine requires that the compound have an
150508 ** ORDER BY clause, and it doesn't right now. So invent one first. */
150509 Expr *pOne = sqlite3ExprInt32(db, 1);
150510 p->pOrderBy = sqlite3ExprListAppend(pParse, 0, pOne);
150511 if( pParse->nErr ) goto multi_select_end;
150512 assert( p->pOrderBy!=0 );
150513 p->pOrderBy->a[0].u.x.iOrderByCol = 1;
150514 return multiSelectByMerge(pParse, p, pDest);
150515 }else{
150516 /* For a UNION ALL compound without ORDER BY, simply run the left
150517 ** query, then run the right query */
150518 int addr = 0;
150519 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
150520
150521 #ifndef SQLITE_OMIT_EXPLAIN
150522 if( pPrior->pPrior==0 ){
150523 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
150524 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
150525 }
150526 #endif
150527 assert( !pPrior->pLimit );
150528 pPrior->iLimit = p->iLimit;
150529 pPrior->iOffset = p->iOffset;
150530 pPrior->pLimit = sqlite3ExprDup(db, p->pLimit, 0);
150531 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
150532 rc = sqlite3Select(pParse, pPrior, &dest);
150533 sqlite3ExprDelete(db, pPrior->pLimit);
150534 pPrior->pLimit = 0;
150535 if( rc ){
150536 goto multi_select_end;
150537 }
150538 p->pPrior = 0;
150539 p->iLimit = pPrior->iLimit;
150540 p->iOffset = pPrior->iOffset;
150541 if( p->iLimit ){
150542 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
150543 VdbeComment((v, "Jump ahead if LIMIT reached"));
150544 if( p->iOffset ){
150545 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
150546 p->iLimit, p->iOffset+1, p->iOffset);
150547 }
150548 }
150549 ExplainQueryPlan((pParse, 1, "UNION ALL"));
150550 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
150551 rc = sqlite3Select(pParse, p, &dest);
150552 testcase( rc!=SQLITE_OK );
150553 pDelete = p->pPrior;
150554 p->pPrior = pPrior;
150555 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
150556 if( p->pLimit
150557 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
150558 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
150559 ){
150560 p->nSelectRow = sqlite3LogEst((u64)nLimit);
150561 }
150562 if( addr ){
150563 sqlite3VdbeJumpHere(v, addr);
150564 }
150565 #ifndef SQLITE_OMIT_EXPLAIN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150566 if( p->pNext==0 ){
150567 ExplainQueryPlanPop(pParse);
150568 }
150569 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150570 }
150571
150572 multi_select_end:
150573 pDest->iSdst = dest.iSdst;
150574 pDest->nSdst = dest.nSdst;
@@ -150693,12 +150596,12 @@
150596
150597 /*
150598 ** Code an output subroutine for a coroutine implementation of a
150599 ** SELECT statement.
150600 **
150601 ** The data to be output is contained in an array of pIn->nSdst registers
150602 ** starting at register pIn->iSdst. pDest is where the output should
150603 ** be sent.
150604 **
150605 ** regReturn is the number of the register holding the subroutine
150606 ** return address.
150607 **
@@ -150723,10 +150626,12 @@
150626 ){
150627 Vdbe *v = pParse->pVdbe;
150628 int iContinue;
150629 int addr;
150630
150631 assert( pIn->eDest==SRT_Coroutine );
150632
150633 addr = sqlite3VdbeCurrentAddr(v);
150634 iContinue = sqlite3VdbeMakeLabel(pParse);
150635
150636 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
150637 */
@@ -150744,26 +150649,63 @@
150649
150650 /* Suppress the first OFFSET entries if there is an OFFSET clause
150651 */
150652 codeOffset(v, p->iOffset, iContinue);
150653
 
 
150654 switch( pDest->eDest ){
150655 /* Store the result as data using a unique key.
150656 */
150657 case SRT_Fifo:
150658 case SRT_DistFifo:
150659 case SRT_Table:
150660 case SRT_EphemTab: {
150661 int r1 = sqlite3GetTempReg(pParse);
150662 int r2 = sqlite3GetTempReg(pParse);
150663 int iParm = pDest->iSDParm;
150664 testcase( pDest->eDest==SRT_Table );
150665 testcase( pDest->eDest==SRT_EphemTab );
150666 testcase( pDest->eDest==SRT_Fifo );
150667 testcase( pDest->eDest==SRT_DistFifo );
150668 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
150669 #if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG)
150670 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
150671 ** that this is an "UPDATE ... FROM" on a virtual table or view. In this
150672 ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC.
150673 ** This does not affect operation in any way - it just allows MakeRecord
150674 ** to process OPFLAG_NOCHANGE values without an assert() failing. */
150675 if( pDest->eDest==SRT_Table && pDest->iSDParm2 ){
150676 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
150677 }
150678 #endif
150679 #ifndef SQLITE_OMIT_CTE
150680 if( pDest->eDest==SRT_DistFifo ){
150681 /* If the destination is DistFifo, then cursor (iParm+1) is open
150682 ** on an ephemeral index that is used to enforce uniqueness on the
150683 ** total result. At this point, we are processing the setup portion
150684 ** of the recursive CTE using the merge algorithm, so the results are
150685 ** guaranteed to be unique anyhow. But we still need to populate the
150686 ** (iParm+1) cursor for use by the subsequent recursive phase.
150687 */
150688 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,
150689 pIn->iSdst, pIn->nSdst);
150690 }
150691 #endif
150692 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
150693 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
150694 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
150695 sqlite3ReleaseTempReg(pParse, r2);
150696 sqlite3ReleaseTempReg(pParse, r1);
150697 break;
150698 }
150699
150700 /* If any row exist in the result set, record that fact and abort.
150701 */
150702 case SRT_Exists: {
150703 sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
150704 /* The LIMIT clause will terminate the loop for us */
150705 break;
150706 }
150707
150708 #ifndef SQLITE_OMIT_SUBQUERY
150709 /* If we are creating a set for an "expr IN (SELECT ...)".
150710 */
150711 case SRT_Set: {
@@ -150806,14 +150748,74 @@
150748 }
150749 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
150750 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
150751 break;
150752 }
150753
150754 #ifndef SQLITE_OMIT_CTE
150755 /* Write the results into a priority queue that is order according to
150756 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
150757 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
150758 ** pSO->nExpr columns, then make sure all keys are unique by adding a
150759 ** final OP_Sequence column. The last column is the record as a blob.
150760 */
150761 case SRT_DistQueue:
150762 case SRT_Queue: {
150763 int nKey;
150764 int r1, r2, r3, ii;
150765 ExprList *pSO;
150766 int iParm = pDest->iSDParm;
150767 pSO = pDest->pOrderBy;
150768 assert( pSO );
150769 nKey = pSO->nExpr;
150770 r1 = sqlite3GetTempReg(pParse);
150771 r2 = sqlite3GetTempRange(pParse, nKey+2);
150772 r3 = r2+nKey+1;
150773
150774 #if 0 /* <-- Why the next block of code is commented out: (tag-20260125-a)
150775 **
150776 ** If the destination is DistQueue, then cursor (iParm+1) is open
150777 ** on a second ephemeral index that holds all values previously
150778 ** added to the queue. This code only runs during the setup phase
150779 ** using the merge algorithm, and so the values here are already
150780 ** guaranteed to be unique.
150781 */
150782 if( pDest->eDest==SRT_DistQueue ){
150783 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
150784 pIn->iSdst, pIn->nSdst);
150785 VdbeCoverage(v);
150786 }
150787 #endif
150788 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r3);
150789 if( pDest->eDest==SRT_DistQueue ){
150790 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
150791 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
150792 }
150793 for(ii=0; ii<nKey; ii++){
150794 sqlite3VdbeAddOp2(v, OP_SCopy,
150795 pIn->iSdst + pSO->a[ii].u.x.iOrderByCol - 1,
150796 r2+ii);
150797 }
150798 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
150799 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
150800 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
150801 #if 0 /* tag-20260125-a */
150802 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
150803 #endif
150804 sqlite3ReleaseTempReg(pParse, r1);
150805 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
150806 break;
150807 }
150808 #endif /* SQLITE_OMIT_CTE */
150809
150810 /* Ignore the output */
150811 case SRT_Discard: {
150812 break;
150813 }
150814
150815 /* If none of the above, then the result destination must be
150816 ** SRT_Output.
 
150817 **
150818 ** For SRT_Output, results are stored in a sequence of registers.
150819 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
150820 ** return the next row of result.
150821 */
@@ -150837,12 +150839,13 @@
150839
150840 return addr;
150841 }
150842
150843 /*
150844 ** Generate code for a compound SELECT statement using a merge
150845 ** algorithm. The compound must have an ORDER BY clause for this
150846 ** to work.
150847 **
150848 ** We assume a query of the following form:
150849 **
150850 ** <selectA> <operator> <selectB> ORDER BY <orderbylist>
150851 **
@@ -150855,11 +150858,11 @@
150858 ** outA: Move the output of the selectA coroutine into the output
150859 ** of the compound query.
150860 **
150861 ** outB: Move the output of the selectB coroutine into the output
150862 ** of the compound query. (Only generated for UNION and
150863 ** UNION ALL. EXCEPT and INTERSECT never output a row that
150864 ** appears only in B.)
150865 **
150866 ** AltB: Called when there is data from both coroutines and A<B.
150867 **
150868 ** AeqB: Called when there is data from both coroutines and A==B.
@@ -150919,14 +150922,14 @@
150922 ** End: ...
150923 **
150924 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
150925 ** actually called using Gosub and they do not Return. EofA and EofB loop
150926 ** until all data is exhausted then jump to the "end" label. AltB, AeqB,
150927 ** and AgtB jump to either Cmpr or to one of EofA or EofB.
150928 */
150929 #ifndef SQLITE_OMIT_COMPOUND_SELECT
150930 static int multiSelectByMerge(
150931 Parse *pParse, /* Parsing context */
150932 Select *p, /* The right-most of SELECTs to be coded */
150933 SelectDest *pDest /* What to do with query results */
150934 ){
150935 int i, j; /* Loop counters */
@@ -151019,11 +151022,11 @@
151022 assert( pItem!=0 );
151023 assert( pItem->u.x.iOrderByCol>0 );
151024 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
151025 aPermute[i] = pItem->u.x.iOrderByCol - 1;
151026 }
151027 pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1);
151028 }else{
151029 pKeyMerge = 0;
151030 }
151031
151032 /* Allocate a range of temporary registers and the KeyInfo needed
@@ -152131,11 +152134,11 @@
152134 pItem->fg.jointype |= (jointype & JT_LTORJ);
152135 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
152136 }
152137 pSubitem->fg.jointype |= jointype;
152138
152139 /* Begin substituting subquery result set expressions for
152140 ** references to the iParent in the outer query.
152141 **
152142 ** Example:
152143 **
152144 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
@@ -152143,21 +152146,21 @@
152146 ** \_____________________ outer query ______________________________/
152147 **
152148 ** We look at every expression in the outer query and every place we see
152149 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
152150 */
152151 if( pSub->pOrderBy ){
152152 /* At this point, any non-zero iOrderByCol values indicate that the
152153 ** ORDER BY column expression is identical to the iOrderByCol'th
152154 ** expression returned by SELECT statement pSub. Since these values
152155 ** do not necessarily correspond to columns in SELECT statement pParent,
152156 ** zero them before transferring the ORDER BY clause.
152157 **
152158 ** Not doing this may cause an error if a subsequent call to this
152159 ** function attempts to flatten a compound sub-query into pParent.
152160 ** See ticket [d11a6e908f].
152161 */
152162 ExprList *pOrderBy = pSub->pOrderBy;
152163 for(i=0; i<pOrderBy->nExpr; i++){
152164 pOrderBy->a[i].u.x.iOrderByCol = 0;
152165 }
152166 assert( pParent->pOrderBy==0 );
@@ -153005,18 +153008,18 @@
153008 ** These are rewritten as a subquery:
153009 **
153010 ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
153011 ** ORDER BY ... COLLATE ...
153012 **
153013 ** This transformation is necessary because the multiSelectByMerge() routine
153014 ** above that generates the code for a compound SELECT with an ORDER BY clause
153015 ** uses a merge algorithm that requires the same collating sequence on the
153016 ** result columns as on the ORDER BY clause. See ticket
153017 ** http://sqlite.org/src/info/6709574d2a
153018 **
153019 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
153020 ** The UNION ALL operator works fine with multiSelectByMerge() even when
153021 ** there are COLLATE terms in the ORDER BY.
153022 */
153023 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
153024 int i;
153025 Select *pNew;
@@ -154862,11 +154865,10 @@
154865 pWhere->op = TK_INTEGER;
154866 pWhere->u.iValue = 1;
154867 ExprSetProperty(pWhere, EP_IntValue);
154868 assert( p->pWhere!=0 );
154869 pSub->pSrc->a[0].fg.fromExists = 1;
 
154870 p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc);
154871 if( pSubWhere ){
154872 p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSubWhere);
154873 pSub->pWhere = 0;
154874 }
@@ -155111,12 +155113,11 @@
155113 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
155114 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
155115 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
155116 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
155117 if( IgnorableDistinct(pDest) ){
155118 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Discard ||
 
155119 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
155120 /* All of these destinations are also able to ignore the ORDER BY clause */
155121 if( p->pOrderBy ){
155122 #if TREETRACE_ENABLED
155123 TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
@@ -155128,11 +155129,10 @@
155129 p->pOrderBy);
155130 testcase( pParse->earlyCleanup );
155131 p->pOrderBy = 0;
155132 }
155133 p->selFlags &= ~(u32)SF_Distinct;
 
155134 }
155135 sqlite3SelectPrep(pParse, p, 0);
155136 if( pParse->nErr ){
155137 goto select_end;
155138 }
@@ -165904,20 +165904,26 @@
165904 u16 eOp = pOne->eOperator | pTwo->eOperator;
165905 sqlite3 *db; /* Database connection (for malloc) */
165906 Expr *pNew; /* New virtual expression */
165907 int op; /* Operator for the combined expression */
165908 int idxNew; /* Index in pWC of the next virtual term */
165909 Expr *pA, *pB; /* Expressions associated with pOne and pTwo */
165910
165911 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
165912 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
165913 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
165914 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
165915 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
165916 pA = pOne->pExpr;
165917 pB = pTwo->pExpr;
165918 assert( pA->pLeft!=0 && pA->pRight!=0 );
165919 assert( pB->pLeft!=0 && pB->pRight!=0 );
165920 if( sqlite3ExprCompare(0,pA->pLeft, pB->pLeft, -1) ) return;
165921 if( sqlite3ExprCompare(0,pA->pRight, pB->pRight,-1) ) return;
165922 if( ExprHasProperty(pA,EP_Commuted)!=ExprHasProperty(pB,EP_Commuted) ){
165923 return;
165924 }
165925 /* If we reach this point, it means the two subterms can be combined */
165926 if( (eOp & (eOp-1))!=0 ){
165927 if( eOp & (WO_LT|WO_LE) ){
165928 eOp = WO_LE;
165929 }else{
@@ -165924,11 +165930,11 @@
165930 assert( eOp & (WO_GT|WO_GE) );
165931 eOp = WO_GE;
165932 }
165933 }
165934 db = pWC->pWInfo->pParse->db;
165935 pNew = sqlite3ExprDup(db, pA, 0);
165936 if( pNew==0 ) return;
165937 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
165938 pNew->op = op;
165939 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
165940 exprAnalyze(pSrc, pWC, idxNew);
@@ -170766,10 +170772,11 @@
170772
170773 nOutUnadjusted = pNew->nOut;
170774 pNew->rRun += nInMul + nIn;
170775 pNew->nOut += nInMul + nIn;
170776 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
170777 if( pSrc->fg.fromExists ) pNew->nOut = 0;
170778 rc = whereLoopInsert(pBuilder, pNew);
170779
170780 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
170781 pNew->nOut = saved_nOut;
170782 }else{
@@ -171362,10 +171369,12 @@
171369 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
171370 whereLoopOutputAdjust(pWC, pNew, rSize);
171371 if( pSrc->fg.isSubquery ){
171372 if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE;
171373 pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy;
171374 }else if( pSrc->fg.fromExists ){
171375 pNew->nOut = 0;
171376 }
171377 rc = whereLoopInsert(pBuilder, pNew);
171378 pNew->nOut = rSize;
171379 if( rc ) break;
171380 }else{
@@ -171464,10 +171473,11 @@
171473 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
171474 ** because the cursor used to access the index might not be
171475 ** positioned to the correct row during the right-join no-match
171476 ** loop. */
171477 }else{
171478 if( pSrc->fg.fromExists ) pNew->nOut = 0;
171479 rc = whereLoopInsert(pBuilder, pNew);
171480 }
171481 pNew->nOut = rSize;
171482 if( rc ) break;
171483 }
@@ -172161,10 +172171,21 @@
172171 ** is itself on the left side of a RIGHT JOIN.
172172 */
172173 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
172174 mPrereq |= mPrior;
172175 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
172176 }else if( pItem->fg.fromExists ){
172177 /* joins that result from the EXISTS-to-JOIN optimization should not
172178 ** be moved to the left of any of their dependencies */
172179 WhereClause *pWC = &pWInfo->sWC;
172180 WhereTerm *pTerm;
172181 int i;
172182 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
172183 if( (pNew->maskSelf & pTerm->prereqAll)!=0 ){
172184 mPrereq |= (pTerm->prereqAll & (pNew->maskSelf-1));
172185 }
172186 }
172187 }else if( !hasRightJoin ){
172188 mPrereq = 0;
172189 }
172190 #ifndef SQLITE_OMIT_VIRTUALTABLE
172191 if( IsVirtual(pItem->pSTab) ){
@@ -174713,26 +174734,31 @@
174734 VdbeCoverageIf(v, op==OP_SeekGT);
174735 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
174736 }
174737 #endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
174738 }
174739 if( pTabList->a[pLevel->iFrom].fg.fromExists
174740 && (i==pWInfo->nLevel-1
174741 || pTabList->a[pWInfo->a[i+1].iFrom].fg.fromExists==0)
174742 ){
174743 /* This is an EXISTS-to-JOIN optimization which is either the
174744 ** inner-most loop, or the inner-most of a group of nested
174745 ** EXISTS-to-JOIN optimization loops. If this loop sees a successful
174746 ** row, it should break out of itself as well as other EXISTS-to-JOIN
174747 ** loops in which is is directly nested. */
174748 int nOuter = 0; /* Nr of outer EXISTS that this one is nested within */
174749 while( nOuter<i ){
174750 if( !pTabList->a[pLevel[-nOuter-1].iFrom].fg.fromExists ) break;
174751 nOuter++;
174752 }
174753 testcase( nOuter>0 );
174754 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel[-nOuter].addrBrk);
174755 if( nOuter ){
174756 VdbeComment((v, "EXISTS break %d..%d", i-nOuter, i));
174757 }else{
174758 VdbeComment((v, "EXISTS break %d", i));
174759 }
174760 }
174761 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
174762 if( pLevel->op!=OP_Noop ){
174763 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
174764 sqlite3VdbeChangeP5(v, pLevel->p5);
@@ -189501,10 +189527,16 @@
189527 /*
189528 ** Find existing client data.
189529 */
189530 SQLITE_API void *sqlite3_get_clientdata(sqlite3 *db, const char *zName){
189531 DbClientData *p;
189532 #ifdef SQLITE_ENABLE_API_ARMOR
189533 if( !zName || !sqlite3SafetyCheckOk(db) ){
189534 (void)SQLITE_MISUSE_BKPT;
189535 return 0;
189536 }
189537 #endif
189538 sqlite3_mutex_enter(db->mutex);
189539 for(p=db->pDbData; p; p=p->pNext){
189540 if( strcmp(p->zName, zName)==0 ){
189541 void *pResult = p->pData;
189542 sqlite3_mutex_leave(db->mutex);
@@ -211254,11 +211286,14 @@
211286 */
211287 #define JSON_JSON 0x01 /* Result is always JSON */
211288 #define JSON_SQL 0x02 /* Result is always SQL */
211289 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
211290 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
211291 #define JSON_AINS 0x08 /* json_array_insert(), not json_insert() */
211292 #define JSON_BLOB 0x10 /* Use the BLOB output format */
211293
211294 #define JSON_INSERT_TYPE(X) (((X)&0xC)>>2)
211295
211296
211297 /* A parsed JSON value. Lifecycle:
211298 **
211299 ** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
@@ -211300,10 +211335,11 @@
211335 /* Allowed values for JsonParse.eEdit */
211336 #define JEDIT_DEL 1 /* Delete if exists */
211337 #define JEDIT_REPL 2 /* Overwrite if exists */
211338 #define JEDIT_INS 3 /* Insert if not exists */
211339 #define JEDIT_SET 4 /* Insert or overwrite */
211340 #define JEDIT_AINS 5 /* array_insert() */
211341
211342 /*
211343 ** Maximum nesting depth of JSON for this implementation.
211344 **
211345 ** This limit is needed to avoid a stack overflow in the recursive
@@ -213796,11 +213832,12 @@
213832 /*
213833 ** Error returns from jsonLookupStep()
213834 */
213835 #define JSON_LOOKUP_ERROR 0xffffffff
213836 #define JSON_LOOKUP_NOTFOUND 0xfffffffe
213837 #define JSON_LOOKUP_NOTARRAY 0xfffffffd
213838 #define JSON_LOOKUP_PATHERROR 0xfffffffc
213839 #define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR)
213840
213841 /* Forward declaration */
213842 static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
213843
@@ -213825,11 +213862,11 @@
213862 ** using the substructure.
213863 */
213864 static u32 jsonCreateEditSubstructure(
213865 JsonParse *pParse, /* The original JSONB that is being edited */
213866 JsonParse *pIns, /* Populate this with the blob data to insert */
213867 const char *zTail /* Tail of the path that determines substructure */
213868 ){
213869 static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
213870 int rc;
213871 memset(pIns, 0, sizeof(*pIns));
213872 pIns->db = pParse->db;
@@ -213860,13 +213897,13 @@
213897 ** label, before returning.
213898 **
213899 ** Return one of the JSON_LOOKUP error codes if problems are seen.
213900 **
213901 ** This routine will also modify the blob. If pParse->eEdit is one of
213902 ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, JEDIT_SET, or JEDIT_AINS, then changes
213903 ** might be made to the selected value. If an edit is performed, then the
213904 ** return value does not necessarily point to the select element. If an edit
213905 ** is performed, the return value is only useful for detecting error
213906 ** conditions.
213907 */
213908 static u32 jsonLookupStep(
213909 JsonParse *pParse, /* The JSON to search */
@@ -213888,10 +213925,17 @@
213925 iRoot = iLabel;
213926 }
213927 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
213928 }else if( pParse->eEdit==JEDIT_INS ){
213929 /* Already exists, so json_insert() is a no-op */
213930 }else if( pParse->eEdit==JEDIT_AINS ){
213931 /* json_array_insert() */
213932 if( zPath[-1]!=']' ){
213933 return JSON_LOOKUP_NOTARRAY;
213934 }else{
213935 jsonBlobEdit(pParse, iRoot, 0, pParse->aIns, pParse->nIns);
213936 }
213937 }else{
213938 /* json_set() or json_replace() */
213939 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
213940 }
213941 }
@@ -213959,10 +214003,14 @@
214003 u32 nIns; /* Total bytes to insert (label+value) */
214004 JsonParse v; /* BLOB encoding of the value to be inserted */
214005 JsonParse ix; /* Header of the label to be inserted */
214006 testcase( pParse->eEdit==JEDIT_INS );
214007 testcase( pParse->eEdit==JEDIT_SET );
214008 testcase( pParse->eEdit==JEDIT_AINS );
214009 if( pParse->eEdit==JEDIT_AINS && sqlite3_strglob("*]",&zPath[i])!=0 ){
214010 return JSON_LOOKUP_NOTARRAY;
214011 }
214012 memset(&ix, 0, sizeof(ix));
214013 ix.db = pParse->db;
214014 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0);
214015 pParse->oom |= ix.oom;
214016 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
@@ -214034,10 +214082,11 @@
214082 if( j>iEnd ) return JSON_LOOKUP_ERROR;
214083 if( k>0 ) return JSON_LOOKUP_NOTFOUND;
214084 if( pParse->eEdit>=JEDIT_INS ){
214085 JsonParse v;
214086 testcase( pParse->eEdit==JEDIT_INS );
214087 testcase( pParse->eEdit==JEDIT_AINS );
214088 testcase( pParse->eEdit==JEDIT_SET );
214089 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
214090 if( !JSON_LOOKUP_ISERROR(rc)
214091 && jsonBlobMakeEditable(pParse, v.nBlob)
214092 ){
@@ -214358,13 +214407,19 @@
214407 ** If ctx is not NULL then push the error message into ctx and return NULL.
214408 ** If ctx is NULL, then return the text of the error message.
214409 */
214410 static char *jsonBadPathError(
214411 sqlite3_context *ctx, /* The function call containing the error */
214412 const char *zPath, /* The path with the problem */
214413 int rc /* Maybe JSON_LOOKUP_NOTARRAY */
214414 ){
214415 char *zMsg;
214416 if( rc==(int)JSON_LOOKUP_NOTARRAY ){
214417 zMsg = sqlite3_mprintf("not an array element: %Q", zPath);
214418 }else{
214419 zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
214420 }
214421 if( ctx==0 ) return zMsg;
214422 if( zMsg ){
214423 sqlite3_result_error(ctx, zMsg, -1);
214424 sqlite3_free(zMsg);
214425 }else{
@@ -214377,17 +214432,17 @@
214432 ** arguments come in pairs where each pair contains a JSON path and
214433 ** content to insert or set at that patch. Do the updates
214434 ** and return the result.
214435 **
214436 ** The specific operation is determined by eEdit, which can be one
214437 ** of JEDIT_INS, JEDIT_REPL, JEDIT_SET, or JEDIT_AINS.
214438 */
214439 static void jsonInsertIntoBlob(
214440 sqlite3_context *ctx,
214441 int argc,
214442 sqlite3_value **argv,
214443 int eEdit /* JEDIT_INS, JEDIT_REPL, JEDIT_SET, JEDIT_AINS */
214444 ){
214445 int i;
214446 u32 rc = 0;
214447 const char *zPath = 0;
214448 int flgs;
@@ -214435,11 +214490,11 @@
214490 jsonInsertIntoBlob_patherror:
214491 jsonParseFree(p);
214492 if( rc==JSON_LOOKUP_ERROR ){
214493 sqlite3_result_error(ctx, "malformed JSON", -1);
214494 }else{
214495 jsonBadPathError(ctx, zPath, rc);
214496 }
214497 return;
214498 }
214499
214500 /*
@@ -214877,11 +214932,11 @@
214932 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
214933 if( JSON_LOOKUP_ISERROR(i) ){
214934 if( i==JSON_LOOKUP_NOTFOUND ){
214935 /* no-op */
214936 }else if( i==JSON_LOOKUP_PATHERROR ){
214937 jsonBadPathError(ctx, zPath, 0);
214938 }else{
214939 sqlite3_result_error(ctx, "malformed JSON", -1);
214940 }
214941 eErr = 1;
214942 i = 0;
@@ -214982,11 +215037,11 @@
215037 }
215038 jsonStringTerminate(&jx);
215039 j = jsonLookupStep(p, 0, jx.zBuf, 0);
215040 jsonStringReset(&jx);
215041 }else{
215042 jsonBadPathError(ctx, zPath, 0);
215043 goto json_extract_error;
215044 }
215045 if( j<p->nBlob ){
215046 if( argc==2 ){
215047 if( flags & JSON_JSON ){
@@ -215017,11 +215072,11 @@
215072 }
215073 }else if( j==JSON_LOOKUP_ERROR ){
215074 sqlite3_result_error(ctx, "malformed JSON", -1);
215075 goto json_extract_error;
215076 }else{
215077 jsonBadPathError(ctx, zPath, 0);
215078 goto json_extract_error;
215079 }
215080 }
215081 if( argc>2 ){
215082 jsonAppendChar(&jx, ']');
@@ -215346,11 +215401,11 @@
215401 rc = jsonLookupStep(p, 0, zPath+1, 0);
215402 if( JSON_LOOKUP_ISERROR(rc) ){
215403 if( rc==JSON_LOOKUP_NOTFOUND ){
215404 continue; /* No-op */
215405 }else if( rc==JSON_LOOKUP_PATHERROR ){
215406 jsonBadPathError(ctx, zPath, rc);
215407 }else{
215408 sqlite3_result_error(ctx, "malformed JSON", -1);
215409 }
215410 goto json_remove_done;
215411 }
@@ -215358,11 +215413,11 @@
215413 jsonReturnParse(ctx, p);
215414 jsonParseFree(p);
215415 return;
215416
215417 json_remove_patherror:
215418 jsonBadPathError(ctx, zPath, 0);
215419
215420 json_remove_done:
215421 jsonParseFree(p);
215422 return;
215423 }
@@ -215402,20 +215457,22 @@
215457 static void jsonSetFunc(
215458 sqlite3_context *ctx,
215459 int argc,
215460 sqlite3_value **argv
215461 ){
 
215462 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215463 int eInsType = JSON_INSERT_TYPE(flags);
215464 static const char *azInsType[] = { "insert", "set", "array_insert" };
215465 static const u8 aEditType[] = { JEDIT_INS, JEDIT_SET, JEDIT_AINS };
215466
215467 if( argc<1 ) return;
215468 assert( eInsType>=0 && eInsType<=2 );
215469 if( (argc&1)==0 ) {
215470 jsonWrongNumArgs(ctx, azInsType[eInsType]);
215471 return;
215472 }
215473 jsonInsertIntoBlob(ctx, argc, argv, aEditType[eInsType]);
215474 }
215475
215476 /*
215477 ** json_type(JSON)
215478 ** json_type(JSON, PATH)
@@ -215436,19 +215493,19 @@
215493 if( p==0 ) return;
215494 if( argc==2 ){
215495 zPath = (const char*)sqlite3_value_text(argv[1]);
215496 if( zPath==0 ) goto json_type_done;
215497 if( zPath[0]!='$' ){
215498 jsonBadPathError(ctx, zPath, 0);
215499 goto json_type_done;
215500 }
215501 i = jsonLookupStep(p, 0, zPath+1, 0);
215502 if( JSON_LOOKUP_ISERROR(i) ){
215503 if( i==JSON_LOOKUP_NOTFOUND ){
215504 /* no-op */
215505 }else if( i==JSON_LOOKUP_PATHERROR ){
215506 jsonBadPathError(ctx, zPath, 0);
215507 }else{
215508 sqlite3_result_error(ctx, "malformed JSON", -1);
215509 }
215510 goto json_type_done;
215511 }
@@ -215700,16 +215757,15 @@
215757 jsonAppendSqlValue(pStr, argv[0]);
215758 }
215759 }
215760 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
215761 JsonString *pStr;
215762 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215763 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
215764 if( pStr ){
 
215765 pStr->pCtx = ctx;
215766 jsonAppendChar(pStr, ']');
 
215767 if( pStr->eErr ){
215768 jsonReturnString(pStr, 0, 0);
215769 return;
215770 }else if( flags & JSON_BLOB ){
215771 jsonReturnStringAsBlob(pStr);
@@ -215726,10 +215782,13 @@
215782 pStr->bStatic = 1;
215783 }else{
215784 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
215785 jsonStringTrimOneChar(pStr);
215786 }
215787 }else if( flags & JSON_BLOB ){
215788 static const u8 emptyArray = 0x0b;
215789 sqlite3_result_blob(ctx, &emptyArray, 1, SQLITE_STATIC);
215790 }else{
215791 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
215792 }
215793 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
215794 }
@@ -215822,16 +215881,15 @@
215881 }
215882 }
215883 }
215884 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
215885 JsonString *pStr;
215886 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
215887 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
215888 if( pStr ){
 
215889 jsonAppendChar(pStr, '}');
215890 pStr->pCtx = ctx;
 
215891 if( pStr->eErr ){
215892 jsonReturnString(pStr, 0, 0);
215893 return;
215894 }else if( flags & JSON_BLOB ){
215895 jsonReturnStringAsBlob(pStr);
@@ -215848,10 +215906,13 @@
215906 pStr->bStatic = 1;
215907 }else{
215908 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
215909 jsonStringTrimOneChar(pStr);
215910 }
215911 }else if( flags & JSON_BLOB ){
215912 static const unsigned char emptyObject = 0x0c;
215913 sqlite3_result_blob(ctx, &emptyObject, 1, SQLITE_STATIC);
215914 }else{
215915 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
215916 }
215917 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
215918 }
@@ -216348,11 +216409,11 @@
216409 if( idxNum==3 ){
216410 zRoot = (const char*)sqlite3_value_text(argv[1]);
216411 if( zRoot==0 ) return SQLITE_OK;
216412 if( zRoot[0]!='$' ){
216413 sqlite3_free(cur->pVtab->zErrMsg);
216414 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot, 0);
216415 jsonEachCursorReset(p);
216416 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
216417 }
216418 p->nRoot = sqlite3Strlen30(zRoot);
216419 if( zRoot[1]==0 ){
@@ -216366,11 +216427,11 @@
216427 p->eType = 0;
216428 p->iEnd = 0;
216429 return SQLITE_OK;
216430 }
216431 sqlite3_free(cur->pVtab->zErrMsg);
216432 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot, 0);
216433 jsonEachCursorReset(p);
216434 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
216435 }
216436 if( p->sParse.iLabel ){
216437 p->i = p->sParse.iLabel;
@@ -216456,10 +216517,12 @@
216517 /* | | | | | | */
216518 JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc),
216519 JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc),
216520 JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc),
216521 JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc),
216522 JFUNCTION(json_array_insert, -1,1,1, 1,0,JSON_AINS, jsonSetFunc),
216523 JFUNCTION(jsonb_array_insert,-1,1,0, 1,1,JSON_AINS, jsonSetFunc),
216524 JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc),
216525 JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc),
216526 JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc),
216527 JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc),
216528 JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc),
@@ -232389,31 +232452,31 @@
232452 for(i=0; i<pTab->nCol; i++){
232453 int eType = *a;
232454 int isPK = pTab->abPK[i];
232455 if( bPkOnly && isPK==0 ) continue;
232456
 
 
 
232457 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
232458 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
232459 || eType==SQLITE_NULL || eType==0
232460 );
 
232461
232462 if( isPK ){
232463 a++;
232464 h = sessionHashAppendType(h, eType);
232465 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
232466 h = sessionHashAppendI64(h, sessionGetI64(a));
232467 a += 8;
232468 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
232469 int n;
232470 a += sessionVarintGet(a, &n);
232471 h = sessionHashAppendBlob(h, n, a);
232472 a += n;
232473 }
232474 /* It should not be possible for eType to be SQLITE_NULL or 0x00 here,
232475 ** as the session module does not record changes for rows with NULL
232476 ** values stored in primary key columns. But a corrupt changesets
232477 ** may contain such a value. */
232478 }else{
232479 a += sessionSerialLen(a);
232480 }
232481 }
232482 return (h % nBucket);
@@ -234818,14 +234881,17 @@
234881 *pnChangeset = 0;
234882 *ppChangeset = 0;
234883 }
234884
234885 if( pSession->rc ) return pSession->rc;
 
 
234886
234887 sqlite3_mutex_enter(sqlite3_db_mutex(db));
234888 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
234889 if( rc!=SQLITE_OK ){
234890 sqlite3_mutex_leave(sqlite3_db_mutex(db));
234891 return rc;
234892 }
234893
234894 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
234895 if( pTab->nEntry ){
234896 const char *zName = pTab->zName;
234897 int i; /* Used to iterate through hash buckets */
@@ -235377,12 +235443,19 @@
235443
235444 while( rc==SQLITE_OK ){
235445 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
235446 nRead++;
235447 }
235448
235449 /* Break out of the loop if if the nul-terminator byte has been found.
235450 ** Otherwise, read some more input data and keep seeking. If there is
235451 ** no more input data, consider the changeset corrupt. */
235452 if( (pIn->iNext + nRead)<pIn->nData ) break;
235453 rc = sessionInputBuffer(pIn, nRead + 100);
235454 if( rc==SQLITE_OK && (pIn->iNext + nRead)>=pIn->nData ){
235455 rc = SQLITE_CORRUPT_BKPT;
235456 }
235457 }
235458 *pnByte = nRead+1;
235459 return rc;
235460 }
235461
@@ -253281,11 +253354,11 @@
253354 ){
253355 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
253356 int iSegid = pSeg->pSeg->iSegid;
253357 u8 *aPg = pSeg->pLeaf->p;
253358 int nPg = pSeg->pLeaf->nn;
253359 int iPgIdx = pSeg->pLeaf->szLeaf; /* Offset of page footer */
253360
253361 u64 iDelta = 0;
253362 int iNextOff = 0;
253363 int iOff = 0;
253364 int nIdx = 0;
@@ -253360,11 +253433,11 @@
253433 iStart = iSOP + (nPos/2);
253434 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
253435 iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
253436 }
253437 assert_nc( iSOP==pSeg->iLeafOffset );
253438 iNextOff = iSOP + pSeg->nPos;
253439 }
253440 }
253441
253442 iOff = iStart;
253443
@@ -253440,35 +253513,35 @@
253513 if( iNextOff!=iPgIdx ){
253514 /* This is the only position-list associated with the term, and there
253515 ** is another term following it on this page. So the subsequent term
253516 ** needs to be moved to replace the term associated with the entry
253517 ** being removed. */
253518 u64 nPrefix = 0;
253519 u64 nSuffix = 0;
253520 u64 nPrefix2 = 0;
253521 u64 nSuffix2 = 0;
253522
253523 iDelKeyOff = iNextOff;
253524 iNextOff += fts5GetVarint(&aPg[iNextOff], &nPrefix2);
253525 iNextOff += fts5GetVarint(&aPg[iNextOff], &nSuffix2);
253526
253527 if( iKey!=1 ){
253528 iKeyOff += fts5GetVarint(&aPg[iKeyOff], &nPrefix);
253529 }
253530 iKeyOff += fts5GetVarint(&aPg[iKeyOff], &nSuffix);
253531
253532 nPrefix = MIN(nPrefix, nPrefix2);
253533 nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
253534
253535 if( (iKeyOff+nSuffix)>(u64)iPgIdx || (iNextOff+nSuffix2)>(u64)iPgIdx ){
253536 FTS5_CORRUPT_IDX(p);
253537 }else{
253538 if( iKey!=1 ){
253539 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix);
253540 }
253541 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix);
253542 if( nPrefix2>(u64)pSeg->term.n ){
253543 FTS5_CORRUPT_IDX(p);
253544 }else if( nPrefix2>nPrefix ){
253545 memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix);
253546 iOff += (nPrefix2-nPrefix);
253547 }
@@ -253495,11 +253568,11 @@
253568 Fts5Data *pTerm = fts5DataRead(p, iId);
253569 if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
253570 u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
253571 int nTermIdx = pTerm->nn - pTerm->szLeaf;
253572 int iTermIdx = 0;
253573 i64 iTermOff = 0;
253574
253575 while( 1 ){
253576 u32 iVal = 0;
253577 int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal);
253578 iTermOff += iVal;
@@ -253506,16 +253579,19 @@
253579 if( (iTermIdx+nByte)>=nTermIdx ) break;
253580 iTermIdx += nByte;
253581 }
253582 nTermIdx = iTermIdx;
253583
253584 if( iTermOff>pTerm->szLeaf ){
253585 FTS5_CORRUPT_IDX(p);
253586 }else{
253587 memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
253588 fts5PutU16(&pTerm->p[2], iTermOff);
253589 fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
253590 if( nTermIdx==0 ){
253591 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
253592 }
253593 }
253594 }
253595 fts5DataRelease(pTerm);
253596 }
253597 }
@@ -253534,11 +253610,13 @@
253610 int nShift = iNextOff - iOff; /* Distance to move them */
253611
253612 int iPrevKeyOut = 0;
253613 int iKeyIn = 0;
253614
253615 if( nMove>0 ){
253616 memmove(&aPg[iOff], &aPg[iNextOff], nMove);
253617 }
253618 iPgIdx -= nShift;
253619 nPg = iPgIdx;
253620 fts5PutU16(&aPg[2], iPgIdx);
253621
253622 for(iIdx=0; iIdx<nIdx; /* no-op */){
@@ -261180,11 +261258,11 @@
261258 int nArg, /* Number of args */
261259 sqlite3_value **apUnused /* Function arguments */
261260 ){
261261 assert( nArg==0 );
261262 UNUSED_PARAM2(nArg, apUnused);
261263 sqlite3_result_text(pCtx, "fts5: 2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71", -1, SQLITE_TRANSIENT);
261264 }
261265
261266 /*
261267 ** Implementation of fts5_locale(LOCALE, TEXT) function.
261268 **
261269
+67 -12
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.52.0"
150150
#define SQLITE_VERSION_NUMBER 3052000
151
-#define SQLITE_SOURCE_ID "2026-01-09 00:41:35 9adab8b2bef4130abd358d53384cb5f4dd691b808336bb7102793b0165b1c516"
151
+#define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
152152
#define SQLITE_SCM_BRANCH "trunk"
153153
#define SQLITE_SCM_TAGS ""
154
-#define SQLITE_SCM_DATETIME "2026-01-09T00:41:35.433Z"
154
+#define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
155155
156156
/*
157157
** CAPI3REF: Run-Time Library Version Numbers
158158
** KEYWORDS: sqlite3_version sqlite3_sourceid
159159
**
@@ -4873,12 +4873,12 @@
48734873
** it should be a pointer to well-formed UTF8 text.
48744874
** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
48754875
** it should be a pointer to well-formed UTF16 text.
48764876
** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
48774877
** it should be a pointer to a well-formed unicode string that is
4878
-** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4879
-** otherwise.
4878
+** either UTF8 if the sixth parameter is SQLITE_UTF8 or SQLITE_UTF8_ZT,
4879
+** or UTF16 otherwise.
48804880
**
48814881
** [[byte-order determination rules]] ^The byte-order of
48824882
** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
48834883
** found in the first character, which is removed, or in the absence of a BOM
48844884
** the byte order is the native byte order of the host
@@ -4920,14 +4920,19 @@
49204920
** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
49214921
** object is to be copied prior to the return from sqlite3_bind_*(). ^The
49224922
** object and pointer to it must remain valid until then. ^SQLite will then
49234923
** manage the lifetime of its private copy.
49244924
**
4925
-** ^The sixth argument to sqlite3_bind_text64() must be one of
4926
-** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4927
-** to specify the encoding of the text in the third parameter. If
4928
-** the sixth argument to sqlite3_bind_text64() is not one of the
4925
+** ^The sixth argument (the E argument)
4926
+** to sqlite3_bind_text64(S,K,Z,N,D,E) must be one of
4927
+** [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
4928
+** or [SQLITE_UTF16LE] to specify the encoding of the text in the
4929
+** third parameter, Z. The special value [SQLITE_UTF8_ZT] means that the
4930
+** string argument is both UTF-8 encoded and is zero-terminated. In other
4931
+** words, SQLITE_UTF8_ZT means that the Z array is allocated to hold at
4932
+** least N+1 bytes and that the Z&#91;N&#93; byte is zero. If
4933
+** the E argument to sqlite3_bind_text64(S,K,Z,N,D,E) is not one of the
49294934
** allowed values shown above, or if the text encoding is different
49304935
** from the encoding specified by the sixth parameter, then the behavior
49314936
** is undefined.
49324937
**
49334938
** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -5790,17 +5795,63 @@
57905795
/*
57915796
** CAPI3REF: Text Encodings
57925797
**
57935798
** These constants define integer codes that represent the various
57945799
** text encodings supported by SQLite.
5800
+**
5801
+** <dl>
5802
+** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
5803
+**
5804
+** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
5805
+** with each code point being expressed "little endian" - the least significant
5806
+** byte first. This is the usual encoding, for example on Windows.</dd>
5807
+**
5808
+** [[SQLITE_UTF16BE]] <dt>SQLITE_UTF16BE</dt><dd>Text is encoding as UTF-16
5809
+** with each code point being expressed "big endian" - the most significant
5810
+** byte first. This encoding is less common, but is still sometimes seen,
5811
+** specially on older systems.
5812
+**
5813
+** [[SQLITE_UTF16]] <dt>SQLITE_UTF16</dt><dd>Text is encoding as UTF-16
5814
+** with each code point being expressed either little endian or as big
5815
+** endian, according to the native endianness of the host computer.
5816
+**
5817
+** [[SQLITE_ANY]] <dt>SQLITE_ANY</dt><dd>This encoding value may only be used
5818
+** to declare the preferred text for [application-defined SQL functions]
5819
+** created using [sqlite3_create_function()] and similar. If the preferred
5820
+** encoding (the 4th parameter to sqlite3_create_function() - the eTextRep
5821
+** parameter) is SQLITE_ANY, that indicates that the function does not have
5822
+** a preference regarding the text encoding of its parameters and can take
5823
+** any text encoding that the SQLite core find convenient to supply. This
5824
+** option is deprecated. Please do not use it in new applications.
5825
+**
5826
+** [[SQLITE_UTF16_ALIGNED]] <dt>SQLITE_UTF16_ALIGNED</dt><dd>This encoding
5827
+** value may be used as the 3rd parameter (the eTextRep parameter) to
5828
+** [sqlite3_create_collation()] and similar. This encoding value means
5829
+** that the application-defined collating sequence created expects its
5830
+** input strings to be in UTF16 in native byte order, and that the start
5831
+** of the strings must be aligned to a 2-byte boundary.
5832
+**
5833
+** [[SQLITE_UTF8_ZT]] <dt>SQLITE_UTF8_ZT</dt><dd>This option can only be
5834
+** used to specify the text encoding to strings input to [sqlite3_result_text64()]
5835
+** and [sqlite3_bind_text64()]. It means that the input string (call it "z")
5836
+** is UTF-8 encoded and that it is zero-terminated. If the length parameter
5837
+** (call it "n") is non-negative, this encoding option means that the caller
5838
+** guarantees that z array contains at least n+1 bytes and that the z&#91;n&#93;
5839
+** byte has a value of zero.
5840
+** This option gives the same output as SQLITE_UTF8, but can be more efficient
5841
+** by avoiding the need to make a copy of the input string, in some cases.
5842
+** However, if z is allocated to hold fewer than n+1 bytes or if the
5843
+** z&#91;n&#93; byte is not zero, undefined behavior may result.
5844
+** </dl>
57955845
*/
57965846
#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
57975847
#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
57985848
#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
57995849
#define SQLITE_UTF16 4 /* Use native byte order */
58005850
#define SQLITE_ANY 5 /* Deprecated */
58015851
#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
5852
+#define SQLITE_UTF8_ZT 16 /* Zero-terminated UTF8 */
58025853
58035854
/*
58045855
** CAPI3REF: Function Flags
58055856
**
58065857
** These constants may be ORed together with the
@@ -6417,14 +6468,18 @@
64176468
** ^The sqlite3_result_text(), sqlite3_result_text16(),
64186469
** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
64196470
** set the return value of the application-defined function to be
64206471
** a text string which is represented as UTF-8, UTF-16 native byte order,
64216472
** UTF-16 little endian, or UTF-16 big endian, respectively.
6422
-** ^The sqlite3_result_text64() interface sets the return value of an
6473
+** ^The sqlite3_result_text64(C,Z,N,D,E) interface sets the return value of an
64236474
** application-defined function to be a text string in an encoding
6424
-** specified by the fifth (and last) parameter, which must be one
6425
-** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6475
+** specified the E parameter, which must be one
6476
+** of [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
6477
+** or [SQLITE_UTF16LE]. ^The special value [SQLITE_UTF8_ZT] means that
6478
+** the result text is both UTF-8 and zero-terminated. In other words,
6479
+** SQLITE_UTF8_ZT means that the Z array holds at least N+1 byes and that
6480
+** the Z&#91;N&#93; is zero.
64266481
** ^SQLite takes the text result from the application from
64276482
** the 2nd parameter of the sqlite3_result_text* interfaces.
64286483
** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
64296484
** other than sqlite3_result_text64() is negative, then SQLite computes
64306485
** the string length itself by searching the 2nd parameter for the first
@@ -6507,11 +6562,11 @@
65076562
SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
65086563
SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
65096564
SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
65106565
SQLITE_API void sqlite3_result_null(sqlite3_context*);
65116566
SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6512
-SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6567
+SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char *z, sqlite3_uint64 n,
65136568
void(*)(void*), unsigned char encoding);
65146569
SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
65156570
SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
65166571
SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
65176572
SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
65186573
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.52.0"
150 #define SQLITE_VERSION_NUMBER 3052000
151 #define SQLITE_SOURCE_ID "2026-01-09 00:41:35 9adab8b2bef4130abd358d53384cb5f4dd691b808336bb7102793b0165b1c516"
152 #define SQLITE_SCM_BRANCH "trunk"
153 #define SQLITE_SCM_TAGS ""
154 #define SQLITE_SCM_DATETIME "2026-01-09T00:41:35.433Z"
155
156 /*
157 ** CAPI3REF: Run-Time Library Version Numbers
158 ** KEYWORDS: sqlite3_version sqlite3_sourceid
159 **
@@ -4873,12 +4873,12 @@
4873 ** it should be a pointer to well-formed UTF8 text.
4874 ** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4875 ** it should be a pointer to well-formed UTF16 text.
4876 ** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4877 ** it should be a pointer to a well-formed unicode string that is
4878 ** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4879 ** otherwise.
4880 **
4881 ** [[byte-order determination rules]] ^The byte-order of
4882 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4883 ** found in the first character, which is removed, or in the absence of a BOM
4884 ** the byte order is the native byte order of the host
@@ -4920,14 +4920,19 @@
4920 ** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
4921 ** object is to be copied prior to the return from sqlite3_bind_*(). ^The
4922 ** object and pointer to it must remain valid until then. ^SQLite will then
4923 ** manage the lifetime of its private copy.
4924 **
4925 ** ^The sixth argument to sqlite3_bind_text64() must be one of
4926 ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4927 ** to specify the encoding of the text in the third parameter. If
4928 ** the sixth argument to sqlite3_bind_text64() is not one of the
 
 
 
 
 
4929 ** allowed values shown above, or if the text encoding is different
4930 ** from the encoding specified by the sixth parameter, then the behavior
4931 ** is undefined.
4932 **
4933 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -5790,17 +5795,63 @@
5790 /*
5791 ** CAPI3REF: Text Encodings
5792 **
5793 ** These constants define integer codes that represent the various
5794 ** text encodings supported by SQLite.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5795 */
5796 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5797 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5798 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5799 #define SQLITE_UTF16 4 /* Use native byte order */
5800 #define SQLITE_ANY 5 /* Deprecated */
5801 #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
 
5802
5803 /*
5804 ** CAPI3REF: Function Flags
5805 **
5806 ** These constants may be ORed together with the
@@ -6417,14 +6468,18 @@
6417 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
6418 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6419 ** set the return value of the application-defined function to be
6420 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6421 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6422 ** ^The sqlite3_result_text64() interface sets the return value of an
6423 ** application-defined function to be a text string in an encoding
6424 ** specified by the fifth (and last) parameter, which must be one
6425 ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
 
 
 
 
6426 ** ^SQLite takes the text result from the application from
6427 ** the 2nd parameter of the sqlite3_result_text* interfaces.
6428 ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6429 ** other than sqlite3_result_text64() is negative, then SQLite computes
6430 ** the string length itself by searching the 2nd parameter for the first
@@ -6507,11 +6562,11 @@
6507 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6508 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6509 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6510 SQLITE_API void sqlite3_result_null(sqlite3_context*);
6511 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6512 SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6513 void(*)(void*), unsigned char encoding);
6514 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6515 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6516 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6517 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6518
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.52.0"
150 #define SQLITE_VERSION_NUMBER 3052000
151 #define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
152 #define SQLITE_SCM_BRANCH "trunk"
153 #define SQLITE_SCM_TAGS ""
154 #define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
155
156 /*
157 ** CAPI3REF: Run-Time Library Version Numbers
158 ** KEYWORDS: sqlite3_version sqlite3_sourceid
159 **
@@ -4873,12 +4873,12 @@
4873 ** it should be a pointer to well-formed UTF8 text.
4874 ** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4875 ** it should be a pointer to well-formed UTF16 text.
4876 ** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4877 ** it should be a pointer to a well-formed unicode string that is
4878 ** either UTF8 if the sixth parameter is SQLITE_UTF8 or SQLITE_UTF8_ZT,
4879 ** or UTF16 otherwise.
4880 **
4881 ** [[byte-order determination rules]] ^The byte-order of
4882 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4883 ** found in the first character, which is removed, or in the absence of a BOM
4884 ** the byte order is the native byte order of the host
@@ -4920,14 +4920,19 @@
4920 ** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
4921 ** object is to be copied prior to the return from sqlite3_bind_*(). ^The
4922 ** object and pointer to it must remain valid until then. ^SQLite will then
4923 ** manage the lifetime of its private copy.
4924 **
4925 ** ^The sixth argument (the E argument)
4926 ** to sqlite3_bind_text64(S,K,Z,N,D,E) must be one of
4927 ** [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
4928 ** or [SQLITE_UTF16LE] to specify the encoding of the text in the
4929 ** third parameter, Z. The special value [SQLITE_UTF8_ZT] means that the
4930 ** string argument is both UTF-8 encoded and is zero-terminated. In other
4931 ** words, SQLITE_UTF8_ZT means that the Z array is allocated to hold at
4932 ** least N+1 bytes and that the Z&#91;N&#93; byte is zero. If
4933 ** the E argument to sqlite3_bind_text64(S,K,Z,N,D,E) is not one of the
4934 ** allowed values shown above, or if the text encoding is different
4935 ** from the encoding specified by the sixth parameter, then the behavior
4936 ** is undefined.
4937 **
4938 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
@@ -5790,17 +5795,63 @@
5795 /*
5796 ** CAPI3REF: Text Encodings
5797 **
5798 ** These constants define integer codes that represent the various
5799 ** text encodings supported by SQLite.
5800 **
5801 ** <dl>
5802 ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
5803 **
5804 ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
5805 ** with each code point being expressed "little endian" - the least significant
5806 ** byte first. This is the usual encoding, for example on Windows.</dd>
5807 **
5808 ** [[SQLITE_UTF16BE]] <dt>SQLITE_UTF16BE</dt><dd>Text is encoding as UTF-16
5809 ** with each code point being expressed "big endian" - the most significant
5810 ** byte first. This encoding is less common, but is still sometimes seen,
5811 ** specially on older systems.
5812 **
5813 ** [[SQLITE_UTF16]] <dt>SQLITE_UTF16</dt><dd>Text is encoding as UTF-16
5814 ** with each code point being expressed either little endian or as big
5815 ** endian, according to the native endianness of the host computer.
5816 **
5817 ** [[SQLITE_ANY]] <dt>SQLITE_ANY</dt><dd>This encoding value may only be used
5818 ** to declare the preferred text for [application-defined SQL functions]
5819 ** created using [sqlite3_create_function()] and similar. If the preferred
5820 ** encoding (the 4th parameter to sqlite3_create_function() - the eTextRep
5821 ** parameter) is SQLITE_ANY, that indicates that the function does not have
5822 ** a preference regarding the text encoding of its parameters and can take
5823 ** any text encoding that the SQLite core find convenient to supply. This
5824 ** option is deprecated. Please do not use it in new applications.
5825 **
5826 ** [[SQLITE_UTF16_ALIGNED]] <dt>SQLITE_UTF16_ALIGNED</dt><dd>This encoding
5827 ** value may be used as the 3rd parameter (the eTextRep parameter) to
5828 ** [sqlite3_create_collation()] and similar. This encoding value means
5829 ** that the application-defined collating sequence created expects its
5830 ** input strings to be in UTF16 in native byte order, and that the start
5831 ** of the strings must be aligned to a 2-byte boundary.
5832 **
5833 ** [[SQLITE_UTF8_ZT]] <dt>SQLITE_UTF8_ZT</dt><dd>This option can only be
5834 ** used to specify the text encoding to strings input to [sqlite3_result_text64()]
5835 ** and [sqlite3_bind_text64()]. It means that the input string (call it "z")
5836 ** is UTF-8 encoded and that it is zero-terminated. If the length parameter
5837 ** (call it "n") is non-negative, this encoding option means that the caller
5838 ** guarantees that z array contains at least n+1 bytes and that the z&#91;n&#93;
5839 ** byte has a value of zero.
5840 ** This option gives the same output as SQLITE_UTF8, but can be more efficient
5841 ** by avoiding the need to make a copy of the input string, in some cases.
5842 ** However, if z is allocated to hold fewer than n+1 bytes or if the
5843 ** z&#91;n&#93; byte is not zero, undefined behavior may result.
5844 ** </dl>
5845 */
5846 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5847 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5848 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5849 #define SQLITE_UTF16 4 /* Use native byte order */
5850 #define SQLITE_ANY 5 /* Deprecated */
5851 #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
5852 #define SQLITE_UTF8_ZT 16 /* Zero-terminated UTF8 */
5853
5854 /*
5855 ** CAPI3REF: Function Flags
5856 **
5857 ** These constants may be ORed together with the
@@ -6417,14 +6468,18 @@
6468 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
6469 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6470 ** set the return value of the application-defined function to be
6471 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6472 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6473 ** ^The sqlite3_result_text64(C,Z,N,D,E) interface sets the return value of an
6474 ** application-defined function to be a text string in an encoding
6475 ** specified the E parameter, which must be one
6476 ** of [SQLITE_UTF8], [SQLITE_UTF8_ZT], [SQLITE_UTF16], [SQLITE_UTF16BE],
6477 ** or [SQLITE_UTF16LE]. ^The special value [SQLITE_UTF8_ZT] means that
6478 ** the result text is both UTF-8 and zero-terminated. In other words,
6479 ** SQLITE_UTF8_ZT means that the Z array holds at least N+1 byes and that
6480 ** the Z&#91;N&#93; is zero.
6481 ** ^SQLite takes the text result from the application from
6482 ** the 2nd parameter of the sqlite3_result_text* interfaces.
6483 ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6484 ** other than sqlite3_result_text64() is negative, then SQLite computes
6485 ** the string length itself by searching the 2nd parameter for the first
@@ -6507,11 +6562,11 @@
6562 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6563 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6564 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6565 SQLITE_API void sqlite3_result_null(sqlite3_context*);
6566 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6567 SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char *z, sqlite3_uint64 n,
6568 void(*)(void*), unsigned char encoding);
6569 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6570 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6571 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6572 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6573

Keyboard Shortcuts

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