Fossil SCM

Import SQLite 3.46.0-beta-1 for testing.

drh 2024-05-08 11:59 trunk
Commit c09fea32997d778f654d103c7cb3d34584b11e8c87b7755c8902abdb2286ce25
3 files changed +154 -59 +488 -170 +25 -1
+154 -59
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -6002,17 +6002,17 @@
60026002
**
60036003
** The query plan selected by seriesBestIndex is passed in the idxNum
60046004
** parameter. (idxStr is not used in this implementation.) idxNum
60056005
** is a bitmask showing which constraints are available:
60066006
**
6007
-** 1: start=VALUE
6008
-** 2: stop=VALUE
6009
-** 4: step=VALUE
6010
-**
6011
-** Also, if bit 8 is set, that means that the series should be output
6012
-** in descending order rather than in ascending order. If bit 16 is
6013
-** set, then output must appear in ascending order.
6007
+** 0x01: start=VALUE
6008
+** 0x02: stop=VALUE
6009
+** 0x04: step=VALUE
6010
+** 0x08: descending order
6011
+** 0x10: ascending order
6012
+** 0x20: LIMIT VALUE
6013
+** 0x40: OFFSET VALUE
60146014
**
60156015
** This routine should initialize the cursor and position it so that it
60166016
** is pointing at the first row, or pointing off the end of the table
60176017
** (so that seriesEof() will return true) if the table is empty.
60186018
*/
@@ -6022,29 +6022,47 @@
60226022
int argc, sqlite3_value **argv
60236023
){
60246024
series_cursor *pCur = (series_cursor *)pVtabCursor;
60256025
int i = 0;
60266026
(void)idxStrUnused;
6027
- if( idxNum & 1 ){
6027
+ if( idxNum & 0x01 ){
60286028
pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
60296029
}else{
60306030
pCur->ss.iBase = 0;
60316031
}
6032
- if( idxNum & 2 ){
6032
+ if( idxNum & 0x02 ){
60336033
pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
60346034
}else{
60356035
pCur->ss.iTerm = 0xffffffff;
60366036
}
6037
- if( idxNum & 4 ){
6037
+ if( idxNum & 0x04 ){
60386038
pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
60396039
if( pCur->ss.iStep==0 ){
60406040
pCur->ss.iStep = 1;
60416041
}else if( pCur->ss.iStep<0 ){
6042
- if( (idxNum & 16)==0 ) idxNum |= 8;
6042
+ if( (idxNum & 0x10)==0 ) idxNum |= 0x08;
60436043
}
60446044
}else{
60456045
pCur->ss.iStep = 1;
6046
+ }
6047
+ if( idxNum & 0x20 ){
6048
+ sqlite3_int64 iLimit = sqlite3_value_int64(argv[i++]);
6049
+ sqlite3_int64 iTerm;
6050
+ if( idxNum & 0x40 ){
6051
+ sqlite3_int64 iOffset = sqlite3_value_int64(argv[i++]);
6052
+ if( iOffset>0 ){
6053
+ pCur->ss.iBase += pCur->ss.iStep*iOffset;
6054
+ }
6055
+ }
6056
+ if( iLimit>=0 ){
6057
+ iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep;
6058
+ if( pCur->ss.iStep<0 ){
6059
+ if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6060
+ }else{
6061
+ if( iTerm<pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6062
+ }
6063
+ }
60466064
}
60476065
for(i=0; i<argc; i++){
60486066
if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
60496067
/* If any of the constraints have a NULL value, then return no rows.
60506068
** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
@@ -6052,11 +6070,11 @@
60526070
pCur->ss.iTerm = 0;
60536071
pCur->ss.iStep = 1;
60546072
break;
60556073
}
60566074
}
6057
- if( idxNum & 8 ){
6075
+ if( idxNum & 0x08 ){
60586076
pCur->ss.isReversing = pCur->ss.iStep > 0;
60596077
}else{
60606078
pCur->ss.isReversing = pCur->ss.iStep < 0;
60616079
}
60626080
setupSequence( &pCur->ss );
@@ -6072,54 +6090,85 @@
60726090
** In this implementation idxNum is used to represent the
60736091
** query plan. idxStr is unused.
60746092
**
60756093
** The query plan is represented by bits in idxNum:
60766094
**
6077
-** (1) start = $value -- constraint exists
6078
-** (2) stop = $value -- constraint exists
6079
-** (4) step = $value -- constraint exists
6080
-** (8) output in descending order
6095
+** 0x01 start = $value -- constraint exists
6096
+** 0x02 stop = $value -- constraint exists
6097
+** 0x04 step = $value -- constraint exists
6098
+** 0x08 output is in descending order
6099
+** 0x10 output is in ascending order
6100
+** 0x20 LIMIT $value -- constraint exists
6101
+** 0x40 OFFSET $value -- constraint exists
60816102
*/
60826103
static int seriesBestIndex(
60836104
sqlite3_vtab *pVTab,
60846105
sqlite3_index_info *pIdxInfo
60856106
){
60866107
int i, j; /* Loop over constraints */
60876108
int idxNum = 0; /* The query plan bitmask */
6109
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
60886110
int bStartSeen = 0; /* EQ constraint seen on the START column */
6111
+#endif
60896112
int unusableMask = 0; /* Mask of unusable constraints */
60906113
int nArg = 0; /* Number of arguments that seriesFilter() expects */
6091
- int aIdx[3]; /* Constraints on start, stop, and step */
6114
+ int aIdx[5]; /* Constraints on start, stop, step, LIMIT, OFFSET */
60926115
const struct sqlite3_index_constraint *pConstraint;
60936116
60946117
/* This implementation assumes that the start, stop, and step columns
60956118
** are the last three columns in the virtual table. */
60966119
assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
60976120
assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
60986121
6099
- aIdx[0] = aIdx[1] = aIdx[2] = -1;
6122
+ aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = -1;
61006123
pConstraint = pIdxInfo->aConstraint;
61016124
for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
61026125
int iCol; /* 0 for start, 1 for stop, 2 for step */
61036126
int iMask; /* bitmask for those column */
6127
+ int op = pConstraint->op;
6128
+ if( op>=SQLITE_INDEX_CONSTRAINT_LIMIT
6129
+ && op<=SQLITE_INDEX_CONSTRAINT_OFFSET
6130
+ ){
6131
+ if( pConstraint->usable==0 ){
6132
+ /* do nothing */
6133
+ }else if( op==SQLITE_INDEX_CONSTRAINT_LIMIT ){
6134
+ aIdx[3] = i;
6135
+ idxNum |= 0x20;
6136
+ }else{
6137
+ assert( op==SQLITE_INDEX_CONSTRAINT_OFFSET );
6138
+ aIdx[4] = i;
6139
+ idxNum |= 0x40;
6140
+ }
6141
+ continue;
6142
+ }
61046143
if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
61056144
iCol = pConstraint->iColumn - SERIES_COLUMN_START;
61066145
assert( iCol>=0 && iCol<=2 );
61076146
iMask = 1 << iCol;
6108
- if( iCol==0 ) bStartSeen = 1;
6147
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
6148
+ if( iCol==0 && op==SQLITE_INDEX_CONSTRAINT_EQ ){
6149
+ bStartSeen = 1;
6150
+ }
6151
+#endif
61096152
if( pConstraint->usable==0 ){
61106153
unusableMask |= iMask;
61116154
continue;
6112
- }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6155
+ }else if( op==SQLITE_INDEX_CONSTRAINT_EQ ){
61136156
idxNum |= iMask;
61146157
aIdx[iCol] = i;
61156158
}
61166159
}
6117
- for(i=0; i<3; i++){
6160
+ if( aIdx[3]==0 ){
6161
+ /* Ignore OFFSET if LIMIT is omitted */
6162
+ idxNum &= ~0x60;
6163
+ aIdx[4] = 0;
6164
+ }
6165
+ for(i=0; i<5; i++){
61186166
if( (j = aIdx[i])>=0 ){
61196167
pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
6120
- pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
6168
+ pIdxInfo->aConstraintUsage[j].omit =
6169
+ !SQLITE_SERIES_CONSTRAINT_VERIFY || i>=3;
61216170
}
61226171
}
61236172
/* The current generate_column() implementation requires at least one
61246173
** argument (the START value). Legacy versions assumed START=0 if the
61256174
** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
@@ -6136,23 +6185,26 @@
61366185
/* The start, stop, and step columns are inputs. Therefore if there
61376186
** are unusable constraints on any of start, stop, or step then
61386187
** this plan is unusable */
61396188
return SQLITE_CONSTRAINT;
61406189
}
6141
- if( (idxNum & 3)==3 ){
6190
+ if( (idxNum & 0x03)==0x03 ){
61426191
/* Both start= and stop= boundaries are available. This is the
61436192
** the preferred case */
61446193
pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
61456194
pIdxInfo->estimatedRows = 1000;
61466195
if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
61476196
if( pIdxInfo->aOrderBy[0].desc ){
6148
- idxNum |= 8;
6197
+ idxNum |= 0x08;
61496198
}else{
6150
- idxNum |= 16;
6199
+ idxNum |= 0x10;
61516200
}
61526201
pIdxInfo->orderByConsumed = 1;
61536202
}
6203
+ }else if( (idxNum & 0x21)==0x21 ){
6204
+ /* We have start= and LIMIT */
6205
+ pIdxInfo->estimatedRows = 2500;
61546206
}else{
61556207
/* If either boundary is missing, we have to generate a huge span
61566208
** of numbers. Make this case very expensive so that the query
61576209
** planner will work hard to avoid it. */
61586210
pIdxInfo->estimatedRows = 2147483647;
@@ -7475,11 +7527,13 @@
74757527
){
74767528
if( zFile==0 ) return 1;
74777529
#if !defined(_WIN32) && !defined(WIN32)
74787530
if( S_ISLNK(mode) ){
74797531
const char *zTo = (const char*)sqlite3_value_text(pData);
7480
- if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
7532
+ if( zTo==0 ) return 1;
7533
+ unlink(zFile);
7534
+ if( symlink(zTo, zFile)<0 ) return 1;
74817535
}else
74827536
#endif
74837537
{
74847538
if( S_ISDIR(mode) ){
74857539
if( mkdir(zFile, mode) ){
@@ -7561,17 +7615,23 @@
75617615
times[1].tv_sec = mtime;
75627616
if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
75637617
return 1;
75647618
}
75657619
#else
7566
- /* Legacy unix */
7567
- struct timeval times[2];
7568
- times[0].tv_usec = times[1].tv_usec = 0;
7569
- times[0].tv_sec = time(0);
7570
- times[1].tv_sec = mtime;
7571
- if( utimes(zFile, times) ){
7572
- return 1;
7620
+ /* Legacy unix.
7621
+ **
7622
+ ** Do not use utimes() on a symbolic link - it sees through the link and
7623
+ ** modifies the timestamps on the target. Or fails if the target does
7624
+ ** not exist. */
7625
+ if( 0==S_ISLNK(mode) ){
7626
+ struct timeval times[2];
7627
+ times[0].tv_usec = times[1].tv_usec = 0;
7628
+ times[0].tv_sec = time(0);
7629
+ times[1].tv_sec = mtime;
7630
+ if( utimes(zFile, times) ){
7631
+ return 1;
7632
+ }
75737633
}
75747634
#endif
75757635
}
75767636
75777637
return 0;
@@ -11639,26 +11699,27 @@
1163911699
sqlite3_context *context,
1164011700
int argc,
1164111701
sqlite3_value **argv
1164211702
){
1164311703
uLong nData;
11644
- uLongf sz;
11704
+ sqlite3_int64 sz;
1164511705
1164611706
assert( argc==2 );
1164711707
sz = sqlite3_value_int(argv[1]);
1164811708
1164911709
if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
1165011710
sqlite3_result_value(context, argv[0]);
1165111711
}else{
11712
+ uLongf szf = sz;
1165211713
const Bytef *pData= sqlite3_value_blob(argv[0]);
1165311714
Bytef *pOut = sqlite3_malloc(sz);
1165411715
if( pOut==0 ){
1165511716
sqlite3_result_error_nomem(context);
11656
- }else if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
11717
+ }else if( Z_OK!=uncompress(pOut, &szf, pData, nData) ){
1165711718
sqlite3_result_error(context, "error in uncompress()", -1);
1165811719
}else{
11659
- sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
11720
+ sqlite3_result_blob(context, pOut, szf, SQLITE_TRANSIENT);
1166011721
}
1166111722
sqlite3_free(pOut);
1166211723
}
1166311724
}
1166411725
@@ -15476,10 +15537,19 @@
1547615537
1547715538
#define DBDATA_PADDING_BYTES 100
1547815539
1547915540
typedef struct DbdataTable DbdataTable;
1548015541
typedef struct DbdataCursor DbdataCursor;
15542
+typedef struct DbdataBuffer DbdataBuffer;
15543
+
15544
+/*
15545
+** Buffer type.
15546
+*/
15547
+struct DbdataBuffer {
15548
+ u8 *aBuf;
15549
+ sqlite3_int64 nBuf;
15550
+};
1548115551
1548215552
/* Cursor object */
1548315553
struct DbdataCursor {
1548415554
sqlite3_vtab_cursor base; /* Base class. Must be first */
1548515555
sqlite3_stmt *pStmt; /* For fetching database pages */
@@ -15492,11 +15562,11 @@
1549215562
int bOnePage; /* True to stop after one page */
1549315563
int szDb;
1549415564
sqlite3_int64 iRowid;
1549515565
1549615566
/* Only for the sqlite_dbdata table */
15497
- u8 *pRec; /* Buffer containing current record */
15567
+ DbdataBuffer rec;
1549815568
sqlite3_int64 nRec; /* Size of pRec[] in bytes */
1549915569
sqlite3_int64 nHdr; /* Size of header in bytes */
1550015570
int iField; /* Current field number */
1550115571
u8 *pHdrPtr;
1550215572
u8 *pPtr;
@@ -15536,10 +15606,35 @@
1553615606
"CREATE TABLE x(" \
1553715607
" pgno INTEGER," \
1553815608
" child INTEGER," \
1553915609
" schema TEXT HIDDEN" \
1554015610
")"
15611
+
15612
+/*
15613
+** Ensure the buffer passed as the first argument is at least nMin bytes
15614
+** in size. If an error occurs while attempting to resize the buffer,
15615
+** SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
15616
+*/
15617
+static int dbdataBufferSize(DbdataBuffer *pBuf, sqlite3_int64 nMin){
15618
+ if( nMin>pBuf->nBuf ){
15619
+ sqlite3_int64 nNew = nMin+16384;
15620
+ u8 *aNew = (u8*)sqlite3_realloc64(pBuf->aBuf, nNew);
15621
+
15622
+ if( aNew==0 ) return SQLITE_NOMEM;
15623
+ pBuf->aBuf = aNew;
15624
+ pBuf->nBuf = nNew;
15625
+ }
15626
+ return SQLITE_OK;
15627
+}
15628
+
15629
+/*
15630
+** Release the allocation managed by buffer pBuf.
15631
+*/
15632
+static void dbdataBufferFree(DbdataBuffer *pBuf){
15633
+ sqlite3_free(pBuf->aBuf);
15634
+ memset(pBuf, 0, sizeof(*pBuf));
15635
+}
1554115636
1554215637
/*
1554315638
** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
1554415639
** table.
1554515640
*/
@@ -15677,12 +15772,11 @@
1567715772
pCsr->iPgno = 1;
1567815773
pCsr->iCell = 0;
1567915774
pCsr->iField = 0;
1568015775
pCsr->bOnePage = 0;
1568115776
sqlite3_free(pCsr->aPage);
15682
- sqlite3_free(pCsr->pRec);
15683
- pCsr->pRec = 0;
15777
+ dbdataBufferFree(&pCsr->rec);
1568415778
pCsr->aPage = 0;
1568515779
}
1568615780
1568715781
/*
1568815782
** Close an sqlite_dbdata or sqlite_dbptr cursor.
@@ -15939,11 +16033,11 @@
1593916033
}else{
1594016034
return SQLITE_OK;
1594116035
}
1594216036
}else{
1594316037
/* If there is no record loaded, load it now. */
15944
- if( pCsr->pRec==0 ){
16038
+ if( pCsr->nRec==0 ){
1594516039
int bHasRowid = 0;
1594616040
int nPointer = 0;
1594716041
sqlite3_int64 nPayload = 0;
1594816042
sqlite3_int64 nHdr = 0;
1594916043
int iHdr;
@@ -15983,10 +16077,11 @@
1598316077
if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){
1598416078
bNextPage = 1;
1598516079
}else{
1598616080
iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
1598716081
if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
16082
+ if( nPayload==0 ) nPayload = 1;
1598816083
}
1598916084
1599016085
/* If this is a leaf intkey cell, load the rowid */
1599116086
if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
1599216087
iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
@@ -16017,17 +16112,16 @@
1601716112
}else{
1601816113
1601916114
/* Allocate space for payload. And a bit more to catch small buffer
1602016115
** overruns caused by attempting to read a varint or similar from
1602116116
** near the end of a corrupt record. */
16022
- pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
16023
- if( pCsr->pRec==0 ) return SQLITE_NOMEM;
16024
- memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
16025
- pCsr->nRec = nPayload;
16117
+ rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES);
16118
+ if( rc!=SQLITE_OK ) return rc;
16119
+ assert( nPayload!=0 );
1602616120
1602716121
/* Load the nLocal bytes of payload */
16028
- memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
16122
+ memcpy(pCsr->rec.aBuf, &pCsr->aPage[iOff], nLocal);
1602916123
iOff += nLocal;
1603016124
1603116125
/* Load content from overflow pages */
1603216126
if( nPayload>nLocal ){
1603316127
sqlite3_int64 nRem = nPayload - nLocal;
@@ -16041,63 +16135,64 @@
1604116135
if( rc!=SQLITE_OK ) return rc;
1604216136
if( aOvfl==0 ) break;
1604316137
1604416138
nCopy = U-4;
1604516139
if( nCopy>nRem ) nCopy = nRem;
16046
- memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
16140
+ memcpy(&pCsr->rec.aBuf[nPayload-nRem], &aOvfl[4], nCopy);
1604716141
nRem -= nCopy;
1604816142
1604916143
pgnoOvfl = get_uint32(aOvfl);
1605016144
sqlite3_free(aOvfl);
1605116145
}
16146
+ nPayload -= nRem;
1605216147
}
16148
+ memset(&pCsr->rec.aBuf[nPayload], 0, DBDATA_PADDING_BYTES);
16149
+ pCsr->nRec = nPayload;
1605316150
16054
- iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
16151
+ iHdr = dbdataGetVarintU32(pCsr->rec.aBuf, &nHdr);
1605516152
if( nHdr>nPayload ) nHdr = 0;
1605616153
pCsr->nHdr = nHdr;
16057
- pCsr->pHdrPtr = &pCsr->pRec[iHdr];
16058
- pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
16154
+ pCsr->pHdrPtr = &pCsr->rec.aBuf[iHdr];
16155
+ pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nHdr];
1605916156
pCsr->iField = (bHasRowid ? -1 : 0);
1606016157
}
1606116158
}
1606216159
}else{
1606316160
pCsr->iField++;
1606416161
if( pCsr->iField>0 ){
1606516162
sqlite3_int64 iType;
16066
- if( pCsr->pHdrPtr>=&pCsr->pRec[pCsr->nRec]
16163
+ if( pCsr->pHdrPtr>=&pCsr->rec.aBuf[pCsr->nRec]
1606716164
|| pCsr->iField>=DBDATA_MX_FIELD
1606816165
){
1606916166
bNextPage = 1;
1607016167
}else{
1607116168
int szField = 0;
1607216169
pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
1607316170
szField = dbdataValueBytes(iType);
16074
- if( (pCsr->nRec - (pCsr->pPtr - pCsr->pRec))<szField ){
16075
- pCsr->pPtr = &pCsr->pRec[pCsr->nRec];
16171
+ if( (pCsr->nRec - (pCsr->pPtr - pCsr->rec.aBuf))<szField ){
16172
+ pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nRec];
1607616173
}else{
1607716174
pCsr->pPtr += szField;
1607816175
}
1607916176
}
1608016177
}
1608116178
}
1608216179
1608316180
if( bNextPage ){
1608416181
sqlite3_free(pCsr->aPage);
16085
- sqlite3_free(pCsr->pRec);
1608616182
pCsr->aPage = 0;
16087
- pCsr->pRec = 0;
16183
+ pCsr->nRec = 0;
1608816184
if( pCsr->bOnePage ) return SQLITE_OK;
1608916185
pCsr->iPgno++;
1609016186
}else{
16091
- if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
16187
+ if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->rec.aBuf[pCsr->nHdr] ){
1609216188
return SQLITE_OK;
1609316189
}
1609416190
1609516191
/* Advance to the next cell. The next iteration of the loop will load
1609616192
** the record and so on. */
16097
- sqlite3_free(pCsr->pRec);
16098
- pCsr->pRec = 0;
16193
+ pCsr->nRec = 0;
1609916194
pCsr->iCell++;
1610016195
}
1610116196
}
1610216197
}
1610316198
@@ -16283,16 +16378,16 @@
1628316378
sqlite3_result_int(ctx, pCsr->iField);
1628416379
break;
1628516380
case DBDATA_COLUMN_VALUE: {
1628616381
if( pCsr->iField<0 ){
1628716382
sqlite3_result_int64(ctx, pCsr->iIntkey);
16288
- }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){
16383
+ }else if( &pCsr->rec.aBuf[pCsr->nRec] >= pCsr->pPtr ){
1628916384
sqlite3_int64 iType;
1629016385
dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
1629116386
dbdataValue(
1629216387
ctx, pCsr->enc, iType, pCsr->pPtr,
16293
- &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
16388
+ &pCsr->rec.aBuf[pCsr->nRec] - pCsr->pPtr
1629416389
);
1629516390
}
1629616391
break;
1629716392
}
1629816393
}
1629916394
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -6002,17 +6002,17 @@
6002 **
6003 ** The query plan selected by seriesBestIndex is passed in the idxNum
6004 ** parameter. (idxStr is not used in this implementation.) idxNum
6005 ** is a bitmask showing which constraints are available:
6006 **
6007 ** 1: start=VALUE
6008 ** 2: stop=VALUE
6009 ** 4: step=VALUE
6010 **
6011 ** Also, if bit 8 is set, that means that the series should be output
6012 ** in descending order rather than in ascending order. If bit 16 is
6013 ** set, then output must appear in ascending order.
6014 **
6015 ** This routine should initialize the cursor and position it so that it
6016 ** is pointing at the first row, or pointing off the end of the table
6017 ** (so that seriesEof() will return true) if the table is empty.
6018 */
@@ -6022,29 +6022,47 @@
6022 int argc, sqlite3_value **argv
6023 ){
6024 series_cursor *pCur = (series_cursor *)pVtabCursor;
6025 int i = 0;
6026 (void)idxStrUnused;
6027 if( idxNum & 1 ){
6028 pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
6029 }else{
6030 pCur->ss.iBase = 0;
6031 }
6032 if( idxNum & 2 ){
6033 pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
6034 }else{
6035 pCur->ss.iTerm = 0xffffffff;
6036 }
6037 if( idxNum & 4 ){
6038 pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
6039 if( pCur->ss.iStep==0 ){
6040 pCur->ss.iStep = 1;
6041 }else if( pCur->ss.iStep<0 ){
6042 if( (idxNum & 16)==0 ) idxNum |= 8;
6043 }
6044 }else{
6045 pCur->ss.iStep = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6046 }
6047 for(i=0; i<argc; i++){
6048 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6049 /* If any of the constraints have a NULL value, then return no rows.
6050 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
@@ -6052,11 +6070,11 @@
6052 pCur->ss.iTerm = 0;
6053 pCur->ss.iStep = 1;
6054 break;
6055 }
6056 }
6057 if( idxNum & 8 ){
6058 pCur->ss.isReversing = pCur->ss.iStep > 0;
6059 }else{
6060 pCur->ss.isReversing = pCur->ss.iStep < 0;
6061 }
6062 setupSequence( &pCur->ss );
@@ -6072,54 +6090,85 @@
6072 ** In this implementation idxNum is used to represent the
6073 ** query plan. idxStr is unused.
6074 **
6075 ** The query plan is represented by bits in idxNum:
6076 **
6077 ** (1) start = $value -- constraint exists
6078 ** (2) stop = $value -- constraint exists
6079 ** (4) step = $value -- constraint exists
6080 ** (8) output in descending order
 
 
 
6081 */
6082 static int seriesBestIndex(
6083 sqlite3_vtab *pVTab,
6084 sqlite3_index_info *pIdxInfo
6085 ){
6086 int i, j; /* Loop over constraints */
6087 int idxNum = 0; /* The query plan bitmask */
 
6088 int bStartSeen = 0; /* EQ constraint seen on the START column */
 
6089 int unusableMask = 0; /* Mask of unusable constraints */
6090 int nArg = 0; /* Number of arguments that seriesFilter() expects */
6091 int aIdx[3]; /* Constraints on start, stop, and step */
6092 const struct sqlite3_index_constraint *pConstraint;
6093
6094 /* This implementation assumes that the start, stop, and step columns
6095 ** are the last three columns in the virtual table. */
6096 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
6097 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
6098
6099 aIdx[0] = aIdx[1] = aIdx[2] = -1;
6100 pConstraint = pIdxInfo->aConstraint;
6101 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
6102 int iCol; /* 0 for start, 1 for stop, 2 for step */
6103 int iMask; /* bitmask for those column */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6104 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
6105 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
6106 assert( iCol>=0 && iCol<=2 );
6107 iMask = 1 << iCol;
6108 if( iCol==0 ) bStartSeen = 1;
 
 
 
 
6109 if( pConstraint->usable==0 ){
6110 unusableMask |= iMask;
6111 continue;
6112 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6113 idxNum |= iMask;
6114 aIdx[iCol] = i;
6115 }
6116 }
6117 for(i=0; i<3; i++){
 
 
 
 
 
6118 if( (j = aIdx[i])>=0 ){
6119 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
6120 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
 
6121 }
6122 }
6123 /* The current generate_column() implementation requires at least one
6124 ** argument (the START value). Legacy versions assumed START=0 if the
6125 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
@@ -6136,23 +6185,26 @@
6136 /* The start, stop, and step columns are inputs. Therefore if there
6137 ** are unusable constraints on any of start, stop, or step then
6138 ** this plan is unusable */
6139 return SQLITE_CONSTRAINT;
6140 }
6141 if( (idxNum & 3)==3 ){
6142 /* Both start= and stop= boundaries are available. This is the
6143 ** the preferred case */
6144 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
6145 pIdxInfo->estimatedRows = 1000;
6146 if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
6147 if( pIdxInfo->aOrderBy[0].desc ){
6148 idxNum |= 8;
6149 }else{
6150 idxNum |= 16;
6151 }
6152 pIdxInfo->orderByConsumed = 1;
6153 }
 
 
 
6154 }else{
6155 /* If either boundary is missing, we have to generate a huge span
6156 ** of numbers. Make this case very expensive so that the query
6157 ** planner will work hard to avoid it. */
6158 pIdxInfo->estimatedRows = 2147483647;
@@ -7475,11 +7527,13 @@
7475 ){
7476 if( zFile==0 ) return 1;
7477 #if !defined(_WIN32) && !defined(WIN32)
7478 if( S_ISLNK(mode) ){
7479 const char *zTo = (const char*)sqlite3_value_text(pData);
7480 if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
 
 
7481 }else
7482 #endif
7483 {
7484 if( S_ISDIR(mode) ){
7485 if( mkdir(zFile, mode) ){
@@ -7561,17 +7615,23 @@
7561 times[1].tv_sec = mtime;
7562 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
7563 return 1;
7564 }
7565 #else
7566 /* Legacy unix */
7567 struct timeval times[2];
7568 times[0].tv_usec = times[1].tv_usec = 0;
7569 times[0].tv_sec = time(0);
7570 times[1].tv_sec = mtime;
7571 if( utimes(zFile, times) ){
7572 return 1;
 
 
 
 
 
 
7573 }
7574 #endif
7575 }
7576
7577 return 0;
@@ -11639,26 +11699,27 @@
11639 sqlite3_context *context,
11640 int argc,
11641 sqlite3_value **argv
11642 ){
11643 uLong nData;
11644 uLongf sz;
11645
11646 assert( argc==2 );
11647 sz = sqlite3_value_int(argv[1]);
11648
11649 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
11650 sqlite3_result_value(context, argv[0]);
11651 }else{
 
11652 const Bytef *pData= sqlite3_value_blob(argv[0]);
11653 Bytef *pOut = sqlite3_malloc(sz);
11654 if( pOut==0 ){
11655 sqlite3_result_error_nomem(context);
11656 }else if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
11657 sqlite3_result_error(context, "error in uncompress()", -1);
11658 }else{
11659 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
11660 }
11661 sqlite3_free(pOut);
11662 }
11663 }
11664
@@ -15476,10 +15537,19 @@
15476
15477 #define DBDATA_PADDING_BYTES 100
15478
15479 typedef struct DbdataTable DbdataTable;
15480 typedef struct DbdataCursor DbdataCursor;
 
 
 
 
 
 
 
 
 
15481
15482 /* Cursor object */
15483 struct DbdataCursor {
15484 sqlite3_vtab_cursor base; /* Base class. Must be first */
15485 sqlite3_stmt *pStmt; /* For fetching database pages */
@@ -15492,11 +15562,11 @@
15492 int bOnePage; /* True to stop after one page */
15493 int szDb;
15494 sqlite3_int64 iRowid;
15495
15496 /* Only for the sqlite_dbdata table */
15497 u8 *pRec; /* Buffer containing current record */
15498 sqlite3_int64 nRec; /* Size of pRec[] in bytes */
15499 sqlite3_int64 nHdr; /* Size of header in bytes */
15500 int iField; /* Current field number */
15501 u8 *pHdrPtr;
15502 u8 *pPtr;
@@ -15536,10 +15606,35 @@
15536 "CREATE TABLE x(" \
15537 " pgno INTEGER," \
15538 " child INTEGER," \
15539 " schema TEXT HIDDEN" \
15540 ")"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15541
15542 /*
15543 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
15544 ** table.
15545 */
@@ -15677,12 +15772,11 @@
15677 pCsr->iPgno = 1;
15678 pCsr->iCell = 0;
15679 pCsr->iField = 0;
15680 pCsr->bOnePage = 0;
15681 sqlite3_free(pCsr->aPage);
15682 sqlite3_free(pCsr->pRec);
15683 pCsr->pRec = 0;
15684 pCsr->aPage = 0;
15685 }
15686
15687 /*
15688 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
@@ -15939,11 +16033,11 @@
15939 }else{
15940 return SQLITE_OK;
15941 }
15942 }else{
15943 /* If there is no record loaded, load it now. */
15944 if( pCsr->pRec==0 ){
15945 int bHasRowid = 0;
15946 int nPointer = 0;
15947 sqlite3_int64 nPayload = 0;
15948 sqlite3_int64 nHdr = 0;
15949 int iHdr;
@@ -15983,10 +16077,11 @@
15983 if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){
15984 bNextPage = 1;
15985 }else{
15986 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
15987 if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
 
15988 }
15989
15990 /* If this is a leaf intkey cell, load the rowid */
15991 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
15992 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
@@ -16017,17 +16112,16 @@
16017 }else{
16018
16019 /* Allocate space for payload. And a bit more to catch small buffer
16020 ** overruns caused by attempting to read a varint or similar from
16021 ** near the end of a corrupt record. */
16022 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
16023 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
16024 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
16025 pCsr->nRec = nPayload;
16026
16027 /* Load the nLocal bytes of payload */
16028 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
16029 iOff += nLocal;
16030
16031 /* Load content from overflow pages */
16032 if( nPayload>nLocal ){
16033 sqlite3_int64 nRem = nPayload - nLocal;
@@ -16041,63 +16135,64 @@
16041 if( rc!=SQLITE_OK ) return rc;
16042 if( aOvfl==0 ) break;
16043
16044 nCopy = U-4;
16045 if( nCopy>nRem ) nCopy = nRem;
16046 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
16047 nRem -= nCopy;
16048
16049 pgnoOvfl = get_uint32(aOvfl);
16050 sqlite3_free(aOvfl);
16051 }
 
16052 }
 
 
16053
16054 iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
16055 if( nHdr>nPayload ) nHdr = 0;
16056 pCsr->nHdr = nHdr;
16057 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
16058 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
16059 pCsr->iField = (bHasRowid ? -1 : 0);
16060 }
16061 }
16062 }else{
16063 pCsr->iField++;
16064 if( pCsr->iField>0 ){
16065 sqlite3_int64 iType;
16066 if( pCsr->pHdrPtr>=&pCsr->pRec[pCsr->nRec]
16067 || pCsr->iField>=DBDATA_MX_FIELD
16068 ){
16069 bNextPage = 1;
16070 }else{
16071 int szField = 0;
16072 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
16073 szField = dbdataValueBytes(iType);
16074 if( (pCsr->nRec - (pCsr->pPtr - pCsr->pRec))<szField ){
16075 pCsr->pPtr = &pCsr->pRec[pCsr->nRec];
16076 }else{
16077 pCsr->pPtr += szField;
16078 }
16079 }
16080 }
16081 }
16082
16083 if( bNextPage ){
16084 sqlite3_free(pCsr->aPage);
16085 sqlite3_free(pCsr->pRec);
16086 pCsr->aPage = 0;
16087 pCsr->pRec = 0;
16088 if( pCsr->bOnePage ) return SQLITE_OK;
16089 pCsr->iPgno++;
16090 }else{
16091 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
16092 return SQLITE_OK;
16093 }
16094
16095 /* Advance to the next cell. The next iteration of the loop will load
16096 ** the record and so on. */
16097 sqlite3_free(pCsr->pRec);
16098 pCsr->pRec = 0;
16099 pCsr->iCell++;
16100 }
16101 }
16102 }
16103
@@ -16283,16 +16378,16 @@
16283 sqlite3_result_int(ctx, pCsr->iField);
16284 break;
16285 case DBDATA_COLUMN_VALUE: {
16286 if( pCsr->iField<0 ){
16287 sqlite3_result_int64(ctx, pCsr->iIntkey);
16288 }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){
16289 sqlite3_int64 iType;
16290 dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
16291 dbdataValue(
16292 ctx, pCsr->enc, iType, pCsr->pPtr,
16293 &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
16294 );
16295 }
16296 break;
16297 }
16298 }
16299
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -6002,17 +6002,17 @@
6002 **
6003 ** The query plan selected by seriesBestIndex is passed in the idxNum
6004 ** parameter. (idxStr is not used in this implementation.) idxNum
6005 ** is a bitmask showing which constraints are available:
6006 **
6007 ** 0x01: start=VALUE
6008 ** 0x02: stop=VALUE
6009 ** 0x04: step=VALUE
6010 ** 0x08: descending order
6011 ** 0x10: ascending order
6012 ** 0x20: LIMIT VALUE
6013 ** 0x40: OFFSET VALUE
6014 **
6015 ** This routine should initialize the cursor and position it so that it
6016 ** is pointing at the first row, or pointing off the end of the table
6017 ** (so that seriesEof() will return true) if the table is empty.
6018 */
@@ -6022,29 +6022,47 @@
6022 int argc, sqlite3_value **argv
6023 ){
6024 series_cursor *pCur = (series_cursor *)pVtabCursor;
6025 int i = 0;
6026 (void)idxStrUnused;
6027 if( idxNum & 0x01 ){
6028 pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
6029 }else{
6030 pCur->ss.iBase = 0;
6031 }
6032 if( idxNum & 0x02 ){
6033 pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
6034 }else{
6035 pCur->ss.iTerm = 0xffffffff;
6036 }
6037 if( idxNum & 0x04 ){
6038 pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
6039 if( pCur->ss.iStep==0 ){
6040 pCur->ss.iStep = 1;
6041 }else if( pCur->ss.iStep<0 ){
6042 if( (idxNum & 0x10)==0 ) idxNum |= 0x08;
6043 }
6044 }else{
6045 pCur->ss.iStep = 1;
6046 }
6047 if( idxNum & 0x20 ){
6048 sqlite3_int64 iLimit = sqlite3_value_int64(argv[i++]);
6049 sqlite3_int64 iTerm;
6050 if( idxNum & 0x40 ){
6051 sqlite3_int64 iOffset = sqlite3_value_int64(argv[i++]);
6052 if( iOffset>0 ){
6053 pCur->ss.iBase += pCur->ss.iStep*iOffset;
6054 }
6055 }
6056 if( iLimit>=0 ){
6057 iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep;
6058 if( pCur->ss.iStep<0 ){
6059 if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6060 }else{
6061 if( iTerm<pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6062 }
6063 }
6064 }
6065 for(i=0; i<argc; i++){
6066 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6067 /* If any of the constraints have a NULL value, then return no rows.
6068 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
@@ -6052,11 +6070,11 @@
6070 pCur->ss.iTerm = 0;
6071 pCur->ss.iStep = 1;
6072 break;
6073 }
6074 }
6075 if( idxNum & 0x08 ){
6076 pCur->ss.isReversing = pCur->ss.iStep > 0;
6077 }else{
6078 pCur->ss.isReversing = pCur->ss.iStep < 0;
6079 }
6080 setupSequence( &pCur->ss );
@@ -6072,54 +6090,85 @@
6090 ** In this implementation idxNum is used to represent the
6091 ** query plan. idxStr is unused.
6092 **
6093 ** The query plan is represented by bits in idxNum:
6094 **
6095 ** 0x01 start = $value -- constraint exists
6096 ** 0x02 stop = $value -- constraint exists
6097 ** 0x04 step = $value -- constraint exists
6098 ** 0x08 output is in descending order
6099 ** 0x10 output is in ascending order
6100 ** 0x20 LIMIT $value -- constraint exists
6101 ** 0x40 OFFSET $value -- constraint exists
6102 */
6103 static int seriesBestIndex(
6104 sqlite3_vtab *pVTab,
6105 sqlite3_index_info *pIdxInfo
6106 ){
6107 int i, j; /* Loop over constraints */
6108 int idxNum = 0; /* The query plan bitmask */
6109 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6110 int bStartSeen = 0; /* EQ constraint seen on the START column */
6111 #endif
6112 int unusableMask = 0; /* Mask of unusable constraints */
6113 int nArg = 0; /* Number of arguments that seriesFilter() expects */
6114 int aIdx[5]; /* Constraints on start, stop, step, LIMIT, OFFSET */
6115 const struct sqlite3_index_constraint *pConstraint;
6116
6117 /* This implementation assumes that the start, stop, and step columns
6118 ** are the last three columns in the virtual table. */
6119 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
6120 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
6121
6122 aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = -1;
6123 pConstraint = pIdxInfo->aConstraint;
6124 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
6125 int iCol; /* 0 for start, 1 for stop, 2 for step */
6126 int iMask; /* bitmask for those column */
6127 int op = pConstraint->op;
6128 if( op>=SQLITE_INDEX_CONSTRAINT_LIMIT
6129 && op<=SQLITE_INDEX_CONSTRAINT_OFFSET
6130 ){
6131 if( pConstraint->usable==0 ){
6132 /* do nothing */
6133 }else if( op==SQLITE_INDEX_CONSTRAINT_LIMIT ){
6134 aIdx[3] = i;
6135 idxNum |= 0x20;
6136 }else{
6137 assert( op==SQLITE_INDEX_CONSTRAINT_OFFSET );
6138 aIdx[4] = i;
6139 idxNum |= 0x40;
6140 }
6141 continue;
6142 }
6143 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
6144 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
6145 assert( iCol>=0 && iCol<=2 );
6146 iMask = 1 << iCol;
6147 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6148 if( iCol==0 && op==SQLITE_INDEX_CONSTRAINT_EQ ){
6149 bStartSeen = 1;
6150 }
6151 #endif
6152 if( pConstraint->usable==0 ){
6153 unusableMask |= iMask;
6154 continue;
6155 }else if( op==SQLITE_INDEX_CONSTRAINT_EQ ){
6156 idxNum |= iMask;
6157 aIdx[iCol] = i;
6158 }
6159 }
6160 if( aIdx[3]==0 ){
6161 /* Ignore OFFSET if LIMIT is omitted */
6162 idxNum &= ~0x60;
6163 aIdx[4] = 0;
6164 }
6165 for(i=0; i<5; i++){
6166 if( (j = aIdx[i])>=0 ){
6167 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
6168 pIdxInfo->aConstraintUsage[j].omit =
6169 !SQLITE_SERIES_CONSTRAINT_VERIFY || i>=3;
6170 }
6171 }
6172 /* The current generate_column() implementation requires at least one
6173 ** argument (the START value). Legacy versions assumed START=0 if the
6174 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
@@ -6136,23 +6185,26 @@
6185 /* The start, stop, and step columns are inputs. Therefore if there
6186 ** are unusable constraints on any of start, stop, or step then
6187 ** this plan is unusable */
6188 return SQLITE_CONSTRAINT;
6189 }
6190 if( (idxNum & 0x03)==0x03 ){
6191 /* Both start= and stop= boundaries are available. This is the
6192 ** the preferred case */
6193 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
6194 pIdxInfo->estimatedRows = 1000;
6195 if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
6196 if( pIdxInfo->aOrderBy[0].desc ){
6197 idxNum |= 0x08;
6198 }else{
6199 idxNum |= 0x10;
6200 }
6201 pIdxInfo->orderByConsumed = 1;
6202 }
6203 }else if( (idxNum & 0x21)==0x21 ){
6204 /* We have start= and LIMIT */
6205 pIdxInfo->estimatedRows = 2500;
6206 }else{
6207 /* If either boundary is missing, we have to generate a huge span
6208 ** of numbers. Make this case very expensive so that the query
6209 ** planner will work hard to avoid it. */
6210 pIdxInfo->estimatedRows = 2147483647;
@@ -7475,11 +7527,13 @@
7527 ){
7528 if( zFile==0 ) return 1;
7529 #if !defined(_WIN32) && !defined(WIN32)
7530 if( S_ISLNK(mode) ){
7531 const char *zTo = (const char*)sqlite3_value_text(pData);
7532 if( zTo==0 ) return 1;
7533 unlink(zFile);
7534 if( symlink(zTo, zFile)<0 ) return 1;
7535 }else
7536 #endif
7537 {
7538 if( S_ISDIR(mode) ){
7539 if( mkdir(zFile, mode) ){
@@ -7561,17 +7615,23 @@
7615 times[1].tv_sec = mtime;
7616 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
7617 return 1;
7618 }
7619 #else
7620 /* Legacy unix.
7621 **
7622 ** Do not use utimes() on a symbolic link - it sees through the link and
7623 ** modifies the timestamps on the target. Or fails if the target does
7624 ** not exist. */
7625 if( 0==S_ISLNK(mode) ){
7626 struct timeval times[2];
7627 times[0].tv_usec = times[1].tv_usec = 0;
7628 times[0].tv_sec = time(0);
7629 times[1].tv_sec = mtime;
7630 if( utimes(zFile, times) ){
7631 return 1;
7632 }
7633 }
7634 #endif
7635 }
7636
7637 return 0;
@@ -11639,26 +11699,27 @@
11699 sqlite3_context *context,
11700 int argc,
11701 sqlite3_value **argv
11702 ){
11703 uLong nData;
11704 sqlite3_int64 sz;
11705
11706 assert( argc==2 );
11707 sz = sqlite3_value_int(argv[1]);
11708
11709 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
11710 sqlite3_result_value(context, argv[0]);
11711 }else{
11712 uLongf szf = sz;
11713 const Bytef *pData= sqlite3_value_blob(argv[0]);
11714 Bytef *pOut = sqlite3_malloc(sz);
11715 if( pOut==0 ){
11716 sqlite3_result_error_nomem(context);
11717 }else if( Z_OK!=uncompress(pOut, &szf, pData, nData) ){
11718 sqlite3_result_error(context, "error in uncompress()", -1);
11719 }else{
11720 sqlite3_result_blob(context, pOut, szf, SQLITE_TRANSIENT);
11721 }
11722 sqlite3_free(pOut);
11723 }
11724 }
11725
@@ -15476,10 +15537,19 @@
15537
15538 #define DBDATA_PADDING_BYTES 100
15539
15540 typedef struct DbdataTable DbdataTable;
15541 typedef struct DbdataCursor DbdataCursor;
15542 typedef struct DbdataBuffer DbdataBuffer;
15543
15544 /*
15545 ** Buffer type.
15546 */
15547 struct DbdataBuffer {
15548 u8 *aBuf;
15549 sqlite3_int64 nBuf;
15550 };
15551
15552 /* Cursor object */
15553 struct DbdataCursor {
15554 sqlite3_vtab_cursor base; /* Base class. Must be first */
15555 sqlite3_stmt *pStmt; /* For fetching database pages */
@@ -15492,11 +15562,11 @@
15562 int bOnePage; /* True to stop after one page */
15563 int szDb;
15564 sqlite3_int64 iRowid;
15565
15566 /* Only for the sqlite_dbdata table */
15567 DbdataBuffer rec;
15568 sqlite3_int64 nRec; /* Size of pRec[] in bytes */
15569 sqlite3_int64 nHdr; /* Size of header in bytes */
15570 int iField; /* Current field number */
15571 u8 *pHdrPtr;
15572 u8 *pPtr;
@@ -15536,10 +15606,35 @@
15606 "CREATE TABLE x(" \
15607 " pgno INTEGER," \
15608 " child INTEGER," \
15609 " schema TEXT HIDDEN" \
15610 ")"
15611
15612 /*
15613 ** Ensure the buffer passed as the first argument is at least nMin bytes
15614 ** in size. If an error occurs while attempting to resize the buffer,
15615 ** SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
15616 */
15617 static int dbdataBufferSize(DbdataBuffer *pBuf, sqlite3_int64 nMin){
15618 if( nMin>pBuf->nBuf ){
15619 sqlite3_int64 nNew = nMin+16384;
15620 u8 *aNew = (u8*)sqlite3_realloc64(pBuf->aBuf, nNew);
15621
15622 if( aNew==0 ) return SQLITE_NOMEM;
15623 pBuf->aBuf = aNew;
15624 pBuf->nBuf = nNew;
15625 }
15626 return SQLITE_OK;
15627 }
15628
15629 /*
15630 ** Release the allocation managed by buffer pBuf.
15631 */
15632 static void dbdataBufferFree(DbdataBuffer *pBuf){
15633 sqlite3_free(pBuf->aBuf);
15634 memset(pBuf, 0, sizeof(*pBuf));
15635 }
15636
15637 /*
15638 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
15639 ** table.
15640 */
@@ -15677,12 +15772,11 @@
15772 pCsr->iPgno = 1;
15773 pCsr->iCell = 0;
15774 pCsr->iField = 0;
15775 pCsr->bOnePage = 0;
15776 sqlite3_free(pCsr->aPage);
15777 dbdataBufferFree(&pCsr->rec);
 
15778 pCsr->aPage = 0;
15779 }
15780
15781 /*
15782 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
@@ -15939,11 +16033,11 @@
16033 }else{
16034 return SQLITE_OK;
16035 }
16036 }else{
16037 /* If there is no record loaded, load it now. */
16038 if( pCsr->nRec==0 ){
16039 int bHasRowid = 0;
16040 int nPointer = 0;
16041 sqlite3_int64 nPayload = 0;
16042 sqlite3_int64 nHdr = 0;
16043 int iHdr;
@@ -15983,10 +16077,11 @@
16077 if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){
16078 bNextPage = 1;
16079 }else{
16080 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
16081 if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
16082 if( nPayload==0 ) nPayload = 1;
16083 }
16084
16085 /* If this is a leaf intkey cell, load the rowid */
16086 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
16087 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
@@ -16017,17 +16112,16 @@
16112 }else{
16113
16114 /* Allocate space for payload. And a bit more to catch small buffer
16115 ** overruns caused by attempting to read a varint or similar from
16116 ** near the end of a corrupt record. */
16117 rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES);
16118 if( rc!=SQLITE_OK ) return rc;
16119 assert( nPayload!=0 );
 
16120
16121 /* Load the nLocal bytes of payload */
16122 memcpy(pCsr->rec.aBuf, &pCsr->aPage[iOff], nLocal);
16123 iOff += nLocal;
16124
16125 /* Load content from overflow pages */
16126 if( nPayload>nLocal ){
16127 sqlite3_int64 nRem = nPayload - nLocal;
@@ -16041,63 +16135,64 @@
16135 if( rc!=SQLITE_OK ) return rc;
16136 if( aOvfl==0 ) break;
16137
16138 nCopy = U-4;
16139 if( nCopy>nRem ) nCopy = nRem;
16140 memcpy(&pCsr->rec.aBuf[nPayload-nRem], &aOvfl[4], nCopy);
16141 nRem -= nCopy;
16142
16143 pgnoOvfl = get_uint32(aOvfl);
16144 sqlite3_free(aOvfl);
16145 }
16146 nPayload -= nRem;
16147 }
16148 memset(&pCsr->rec.aBuf[nPayload], 0, DBDATA_PADDING_BYTES);
16149 pCsr->nRec = nPayload;
16150
16151 iHdr = dbdataGetVarintU32(pCsr->rec.aBuf, &nHdr);
16152 if( nHdr>nPayload ) nHdr = 0;
16153 pCsr->nHdr = nHdr;
16154 pCsr->pHdrPtr = &pCsr->rec.aBuf[iHdr];
16155 pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nHdr];
16156 pCsr->iField = (bHasRowid ? -1 : 0);
16157 }
16158 }
16159 }else{
16160 pCsr->iField++;
16161 if( pCsr->iField>0 ){
16162 sqlite3_int64 iType;
16163 if( pCsr->pHdrPtr>=&pCsr->rec.aBuf[pCsr->nRec]
16164 || pCsr->iField>=DBDATA_MX_FIELD
16165 ){
16166 bNextPage = 1;
16167 }else{
16168 int szField = 0;
16169 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
16170 szField = dbdataValueBytes(iType);
16171 if( (pCsr->nRec - (pCsr->pPtr - pCsr->rec.aBuf))<szField ){
16172 pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nRec];
16173 }else{
16174 pCsr->pPtr += szField;
16175 }
16176 }
16177 }
16178 }
16179
16180 if( bNextPage ){
16181 sqlite3_free(pCsr->aPage);
 
16182 pCsr->aPage = 0;
16183 pCsr->nRec = 0;
16184 if( pCsr->bOnePage ) return SQLITE_OK;
16185 pCsr->iPgno++;
16186 }else{
16187 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->rec.aBuf[pCsr->nHdr] ){
16188 return SQLITE_OK;
16189 }
16190
16191 /* Advance to the next cell. The next iteration of the loop will load
16192 ** the record and so on. */
16193 pCsr->nRec = 0;
 
16194 pCsr->iCell++;
16195 }
16196 }
16197 }
16198
@@ -16283,16 +16378,16 @@
16378 sqlite3_result_int(ctx, pCsr->iField);
16379 break;
16380 case DBDATA_COLUMN_VALUE: {
16381 if( pCsr->iField<0 ){
16382 sqlite3_result_int64(ctx, pCsr->iIntkey);
16383 }else if( &pCsr->rec.aBuf[pCsr->nRec] >= pCsr->pPtr ){
16384 sqlite3_int64 iType;
16385 dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
16386 dbdataValue(
16387 ctx, pCsr->enc, iType, pCsr->pPtr,
16388 &pCsr->rec.aBuf[pCsr->nRec] - pCsr->pPtr
16389 );
16390 }
16391 break;
16392 }
16393 }
16394
+488 -170
--- 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
-** 8c0f69e0e4ae0a446838cc193bfd4395fd25.
21
+** 42d67c6fed3a5f21d7b71515aca471ba61d3.
2222
*/
2323
#define SQLITE_CORE 1
2424
#define SQLITE_AMALGAMATION 1
2525
#ifndef SQLITE_PRIVATE
2626
# define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459459
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460460
** [sqlite_version()] and [sqlite_source_id()].
461461
*/
462462
#define SQLITE_VERSION "3.46.0"
463463
#define SQLITE_VERSION_NUMBER 3046000
464
-#define SQLITE_SOURCE_ID "2024-04-18 16:11:01 8c0f69e0e4ae0a446838cc193bfd4395fd251f3c7659b35ac388e5a0a7650a66"
464
+#define SQLITE_SOURCE_ID "2024-05-08 11:51:56 42d67c6fed3a5f21d7b71515aca471ba61d387e620022735a2e7929fa3a237cf"
465465
466466
/*
467467
** CAPI3REF: Run-Time Library Version Numbers
468468
** KEYWORDS: sqlite3_version sqlite3_sourceid
469469
**
@@ -12314,10 +12314,34 @@
1231412314
**
1231512315
** In all cases, if an error occurs the state of the final contents of the
1231612316
** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
1231712317
*/
1231812318
SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12319
+
12320
+/*
12321
+** CAPI3REF: Add A Single Change To A Changegroup
12322
+** METHOD: sqlite3_changegroup
12323
+**
12324
+** This function adds the single change currently indicated by the iterator
12325
+** passed as the second argument to the changegroup object. The rules for
12326
+** adding the change are just as described for [sqlite3changegroup_add()].
12327
+**
12328
+** If the change is successfully added to the changegroup, SQLITE_OK is
12329
+** returned. Otherwise, an SQLite error code is returned.
12330
+**
12331
+** The iterator must point to a valid entry when this function is called.
12332
+** If it does not, SQLITE_ERROR is returned and no change is added to the
12333
+** changegroup. Additionally, the iterator must not have been opened with
12334
+** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12335
+** returned.
12336
+*/
12337
+SQLITE_API int sqlite3changegroup_add_change(
12338
+ sqlite3_changegroup*,
12339
+ sqlite3_changeset_iter*
12340
+);
12341
+
12342
+
1231912343
1232012344
/*
1232112345
** CAPI3REF: Obtain A Composite Changeset From A Changegroup
1232212346
** METHOD: sqlite3_changegroup
1232312347
**
@@ -14612,12 +14636,12 @@
1461214636
#define TK_AGG_FUNCTION 168
1461314637
#define TK_AGG_COLUMN 169
1461414638
#define TK_TRUEFALSE 170
1461514639
#define TK_ISNOT 171
1461614640
#define TK_FUNCTION 172
14617
-#define TK_UMINUS 173
14618
-#define TK_UPLUS 174
14641
+#define TK_UPLUS 173
14642
+#define TK_UMINUS 174
1461914643
#define TK_TRUTH 175
1462014644
#define TK_REGISTER 176
1462114645
#define TK_VECTOR 177
1462214646
#define TK_SELECT_COLUMN 178
1462314647
#define TK_IF_NULL_ROW 179
@@ -31945,11 +31969,11 @@
3194531969
sqlite3_str_appendall(pAccum, pItem->zName);
3194631970
}else if( pItem->zAlias ){
3194731971
sqlite3_str_appendall(pAccum, pItem->zAlias);
3194831972
}else{
3194931973
Select *pSel = pItem->pSelect;
31950
- assert( pSel!=0 );
31974
+ assert( pSel!=0 ); /* Because of tag-20240424-1 */
3195131975
if( pSel->selFlags & SF_NestedFrom ){
3195231976
sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
3195331977
}else if( pSel->selFlags & SF_MultiValue ){
3195431978
assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy );
3195531979
sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
@@ -32769,16 +32793,18 @@
3276932793
if( pItem->fg.isUsing ) n++;
3277032794
if( pItem->fg.isUsing ){
3277132795
sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
3277232796
}
3277332797
if( pItem->pSelect ){
32798
+ sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc);
3277432799
if( pItem->pTab ){
3277532800
Table *pTab = pItem->pTab;
3277632801
sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
3277732802
}
3277832803
assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
3277932804
sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
32805
+ sqlite3TreeViewPop(&pView);
3278032806
}
3278132807
if( pItem->fg.isTabFunc ){
3278232808
sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
3278332809
}
3278432810
sqlite3TreeViewPop(&pView);
@@ -32878,11 +32904,11 @@
3287832904
}
3287932905
if( p->pLimit ){
3288032906
sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
3288132907
sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
3288232908
if( p->pLimit->pRight ){
32883
- sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
32909
+ sqlite3TreeViewItem(pView, "OFFSET", 0);
3288432910
sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
3288532911
sqlite3TreeViewPop(&pView);
3288632912
}
3288732913
sqlite3TreeViewPop(&pView);
3288832914
}
@@ -84677,14 +84703,14 @@
8467784703
int nRec, /* Size of buffer pRec in bytes */
8467884704
int iCol, /* Column to extract */
8467984705
sqlite3_value **ppVal /* OUT: Extracted value */
8468084706
){
8468184707
u32 t = 0; /* a column type code */
84682
- int nHdr; /* Size of the header in the record */
84683
- int iHdr; /* Next unread header byte */
84684
- int iField; /* Next unread data byte */
84685
- int szField = 0; /* Size of the current data field */
84708
+ u32 nHdr; /* Size of the header in the record */
84709
+ u32 iHdr; /* Next unread header byte */
84710
+ i64 iField; /* Next unread data byte */
84711
+ u32 szField = 0; /* Size of the current data field */
8468684712
int i; /* Column index */
8468784713
u8 *a = (u8*)pRec; /* Typecast byte array */
8468884714
Mem *pMem = *ppVal; /* Write result into this Mem object */
8468984715
8469084716
assert( iCol>0 );
@@ -97641,11 +97667,12 @@
9764197667
** row output from the sorter so that the row can be decomposed into
9764297668
** individual columns using the OP_Column opcode. The OP_Column opcode
9764397669
** is the only cursor opcode that works with a pseudo-table.
9764497670
**
9764597671
** P3 is the number of fields in the records that will be stored by
97646
-** the pseudo-table.
97672
+** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
97673
+** will return NULL for every column.
9764797674
*/
9764897675
case OP_OpenPseudo: {
9764997676
VdbeCursor *pCx;
9765097677
9765197678
assert( pOp->p1>=0 );
@@ -105799,14 +105826,14 @@
105799105826
break;
105800105827
}
105801105828
105802105829
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
105803105830
case 9: /* nexec */
105804
- sqlite3_result_int(ctx, pOp->nExec);
105831
+ sqlite3_result_int64(ctx, pOp->nExec);
105805105832
break;
105806105833
case 10: /* ncycle */
105807
- sqlite3_result_int(ctx, pOp->nCycle);
105834
+ sqlite3_result_int64(ctx, pOp->nCycle);
105808105835
break;
105809105836
#else
105810105837
case 9: /* nexec */
105811105838
case 10: /* ncycle */
105812105839
sqlite3_result_int(ctx, 0);
@@ -107195,11 +107222,12 @@
107195107222
int op = pParse->eTriggerOp;
107196107223
assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
107197107224
if( pParse->bReturning ){
107198107225
if( (pNC->ncFlags & NC_UBaseReg)!=0
107199107226
&& ALWAYS(zTab==0
107200
- || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
107227
+ || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0
107228
+ || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))
107201107229
){
107202107230
pExpr->iTable = op!=TK_DELETE;
107203107231
pTab = pParse->pTriggerTab;
107204107232
}
107205107233
}else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){
@@ -108556,10 +108584,11 @@
108556108584
/* Recursively resolve names in all subqueries in the FROM clause
108557108585
*/
108558108586
if( pOuterNC ) pOuterNC->nNestedSelect++;
108559108587
for(i=0; i<p->pSrc->nSrc; i++){
108560108588
SrcItem *pItem = &p->pSrc->a[i];
108589
+ assert( pItem->zName!=0 || pItem->pSelect!=0 );/* Test of tag-20240424-1*/
108561108590
if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108562108591
int nRef = pOuterNC ? pOuterNC->nRef : 0;
108563108592
const char *zSavedContext = pParse->zAuthContext;
108564108593
108565108594
if( pItem->zName ) pParse->zAuthContext = pItem->zName;
@@ -110315,10 +110344,11 @@
110315110344
** Recursively delete an expression tree.
110316110345
*/
110317110346
static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110318110347
assert( p!=0 );
110319110348
assert( db!=0 );
110349
+exprDeleteRestart:
110320110350
assert( !ExprUseUValue(p) || p->u.iValue>=0 );
110321110351
assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
110322110352
assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
110323110353
assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
110324110354
#ifdef SQLITE_DEBUG
@@ -110330,11 +110360,10 @@
110330110360
}
110331110361
#endif
110332110362
if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
110333110363
/* The Expr.x union is never used at the same time as Expr.pRight */
110334110364
assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
110335
- if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
110336110365
if( p->pRight ){
110337110366
assert( !ExprHasProperty(p, EP_WinFunc) );
110338110367
sqlite3ExprDeleteNN(db, p->pRight);
110339110368
}else if( ExprUseXSelect(p) ){
110340110369
assert( !ExprHasProperty(p, EP_WinFunc) );
@@ -110344,10 +110373,23 @@
110344110373
#ifndef SQLITE_OMIT_WINDOWFUNC
110345110374
if( ExprHasProperty(p, EP_WinFunc) ){
110346110375
sqlite3WindowDelete(db, p->y.pWin);
110347110376
}
110348110377
#endif
110378
+ }
110379
+ if( p->pLeft && p->op!=TK_SELECT_COLUMN ){
110380
+ Expr *pLeft = p->pLeft;
110381
+ if( !ExprHasProperty(p, EP_Static)
110382
+ && !ExprHasProperty(pLeft, EP_Static)
110383
+ ){
110384
+ /* Avoid unnecessary recursion on unary operators */
110385
+ sqlite3DbNNFreeNN(db, p);
110386
+ p = pLeft;
110387
+ goto exprDeleteRestart;
110388
+ }else{
110389
+ sqlite3ExprDeleteNN(db, pLeft);
110390
+ }
110349110391
}
110350110392
}
110351110393
if( !ExprHasProperty(p, EP_Static) ){
110352110394
sqlite3DbNNFreeNN(db, p);
110353110395
}
@@ -111340,11 +111382,11 @@
111340111382
|| ExprHasProperty(pExpr, EP_WinFunc)
111341111383
){
111342111384
pWalker->eCode = 0;
111343111385
return WRC_Abort;
111344111386
}
111345
- return WRC_Continue;
111387
+ return WRC_Prune;
111346111388
}
111347111389
111348111390
111349111391
/*
111350111392
** These routines are Walker callbacks used to check expressions to
@@ -124219,13 +124261,10 @@
124219124261
if( p->tabFlags & TF_HasGenerated ){
124220124262
sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
124221124263
sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
124222124264
db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124223124265
}
124224
- sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
124225
- sqlite3MPrintf(db, "PRAGMA \"%w\".integrity_check(%Q)",
124226
- db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124227124266
}
124228124267
124229124268
/* Add the table to the in-memory representation of the database.
124230124269
*/
124231124270
if( db->init.busy ){
@@ -140351,11 +140390,11 @@
140351140390
pParse->nMem = 6;
140352140391
140353140392
/* Set the maximum error count */
140354140393
mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140355140394
if( zRight ){
140356
- if( sqlite3GetInt32(zRight, &mxErr) ){
140395
+ if( sqlite3GetInt32(pValue->z, &mxErr) ){
140357140396
if( mxErr<=0 ){
140358140397
mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140359140398
}
140360140399
}else{
140361140400
pObjTab = sqlite3LocateTable(pParse, 0, zRight,
@@ -141559,10 +141598,11 @@
141559141598
/* Clear all content from pragma virtual table cursor. */
141560141599
static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
141561141600
int i;
141562141601
sqlite3_finalize(pCsr->pPragma);
141563141602
pCsr->pPragma = 0;
141603
+ pCsr->iRowid = 0;
141564141604
for(i=0; i<ArraySize(pCsr->azArg); i++){
141565141605
sqlite3_free(pCsr->azArg[i]);
141566141606
pCsr->azArg[i] = 0;
141567141607
}
141568141608
}
@@ -145144,21 +145184,26 @@
145144145184
memset(&sNC, 0, sizeof(sNC));
145145145185
sNC.pSrcList = pSelect->pSrc;
145146145186
for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
145147145187
const char *zType;
145148145188
i64 n;
145189
+ int m = 0;
145190
+ Select *pS2 = pSelect;
145149145191
pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
145150145192
p = a[i].pExpr;
145151145193
/* pCol->szEst = ... // Column size est for SELECT tables never used */
145152145194
pCol->affinity = sqlite3ExprAffinity(p);
145195
+ while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){
145196
+ m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145197
+ pS2 = pS2->pNext;
145198
+ pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
145199
+ }
145153145200
if( pCol->affinity<=SQLITE_AFF_NONE ){
145154145201
pCol->affinity = aff;
145155145202
}
145156
- if( pCol->affinity>=SQLITE_AFF_TEXT && pSelect->pNext ){
145157
- int m = 0;
145158
- Select *pS2;
145159
- for(m=0, pS2=pSelect->pNext; pS2; pS2=pS2->pNext){
145203
+ if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){
145204
+ for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
145160145205
m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145161145206
}
145162145207
if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
145163145208
pCol->affinity = SQLITE_AFF_BLOB;
145164145209
}else
@@ -152576,10 +152621,76 @@
152576152621
}
152577152622
}
152578152623
}
152579152624
return pNew;
152580152625
}
152626
+
152627
+/* If the Expr node is a subquery or an EXISTS operator or an IN operator that
152628
+** uses a subquery, and if the subquery is SF_Correlated, then mark the
152629
+** expression as EP_VarSelect.
152630
+*/
152631
+static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){
152632
+ UNUSED_PARAMETER(NotUsed);
152633
+ if( ExprUseXSelect(pExpr)
152634
+ && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0
152635
+ ){
152636
+ testcase( ExprHasProperty(pExpr, EP_VarSelect) );
152637
+ ExprSetProperty(pExpr, EP_VarSelect);
152638
+ }
152639
+ return WRC_Continue;
152640
+}
152641
+
152642
+
152643
+/*
152644
+** If the SELECT references the table pWalker->u.pTab, then do two things:
152645
+**
152646
+** (1) Mark the SELECT as as SF_Correlated.
152647
+** (2) Set pWalker->eCode to non-zero so that the caller will know
152648
+** that (1) has happened.
152649
+*/
152650
+static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){
152651
+ int i;
152652
+ SrcList *pSrc;
152653
+ assert( pSelect!=0 );
152654
+ pSrc = pSelect->pSrc;
152655
+ assert( pSrc!=0 );
152656
+ for(i=0; i<pSrc->nSrc; i++){
152657
+ if( pSrc->a[i].pTab==pWalker->u.pTab ){
152658
+ testcase( pSelect->selFlags & SF_Correlated );
152659
+ pSelect->selFlags |= SF_Correlated;
152660
+ pWalker->eCode = 1;
152661
+ break;
152662
+ }
152663
+ }
152664
+ return WRC_Continue;
152665
+}
152666
+
152667
+/*
152668
+** Scan the expression list that is the argument to RETURNING looking
152669
+** for subqueries that depend on the table which is being modified in the
152670
+** statement that is hosting the RETURNING clause (pTab). Mark all such
152671
+** subqueries as SF_Correlated. If the subqueries are part of an
152672
+** expression, mark the expression as EP_VarSelect.
152673
+**
152674
+** https://sqlite.org/forum/forumpost/2c83569ce8945d39
152675
+*/
152676
+static void sqlite3ProcessReturningSubqueries(
152677
+ ExprList *pEList,
152678
+ Table *pTab
152679
+){
152680
+ Walker w;
152681
+ memset(&w, 0, sizeof(w));
152682
+ w.xExprCallback = sqlite3ExprWalkNoop;
152683
+ w.xSelectCallback = sqlite3ReturningSubqueryCorrelated;
152684
+ w.u.pTab = pTab;
152685
+ sqlite3WalkExprList(&w, pEList);
152686
+ if( w.eCode ){
152687
+ w.xExprCallback = sqlite3ReturningSubqueryVarSelect;
152688
+ w.xSelectCallback = sqlite3SelectWalkNoop;
152689
+ sqlite3WalkExprList(&w, pEList);
152690
+ }
152691
+}
152581152692
152582152693
/*
152583152694
** Generate code for the RETURNING trigger. Unlike other triggers
152584152695
** that invoke a subprogram in the bytecode, the code for RETURNING
152585152696
** is generated in-line.
@@ -152613,10 +152724,11 @@
152613152724
memset(&sFrom, 0, sizeof(sFrom));
152614152725
sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
152615152726
sSelect.pSrc = &sFrom;
152616152727
sFrom.nSrc = 1;
152617152728
sFrom.a[0].pTab = pTab;
152729
+ sFrom.a[0].zName = pTab->zName; /* tag-20240424-1 */
152618152730
sFrom.a[0].iCursor = -1;
152619152731
sqlite3SelectPrep(pParse, &sSelect, 0);
152620152732
if( pParse->nErr==0 ){
152621152733
assert( db->mallocFailed==0 );
152622152734
sqlite3GenerateColumnNames(pParse, &sSelect);
@@ -152639,10 +152751,11 @@
152639152751
&& ALWAYS(!db->mallocFailed)
152640152752
){
152641152753
int i;
152642152754
int nCol = pNew->nExpr;
152643152755
int reg = pParse->nMem+1;
152756
+ sqlite3ProcessReturningSubqueries(pNew, pTab);
152644152757
pParse->nMem += nCol+2;
152645152758
pReturning->iRetReg = reg;
152646152759
for(i=0; i<nCol; i++){
152647152760
Expr *pCol = pNew->a[i].pExpr;
152648152761
assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
@@ -158590,10 +158703,31 @@
158590158703
}
158591158704
pLevel->regFilter = 0;
158592158705
pLevel->addrBrk = 0;
158593158706
}
158594158707
}
158708
+
158709
+/*
158710
+** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...)
158711
+** operator. Return true if level pLoop is guaranteed to visit only one
158712
+** row for each key generated for the index.
158713
+*/
158714
+static int whereLoopIsOneRow(WhereLoop *pLoop){
158715
+ if( pLoop->u.btree.pIndex->onError
158716
+ && pLoop->nSkip==0
158717
+ && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
158718
+ ){
158719
+ int ii;
158720
+ for(ii=0; ii<pLoop->u.btree.nEq; ii++){
158721
+ if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){
158722
+ return 0;
158723
+ }
158724
+ }
158725
+ return 1;
158726
+ }
158727
+ return 0;
158728
+}
158595158729
158596158730
/*
158597158731
** Generate code for the start of the iLevel-th loop in the WHERE clause
158598158732
** implementation described by pWInfo.
158599158733
*/
@@ -159338,11 +159472,13 @@
159338159472
** a LEFT JOIN: */
159339159473
assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
159340159474
}
159341159475
159342159476
/* Record the instruction used to terminate the loop. */
159343
- if( pLoop->wsFlags & WHERE_ONEROW ){
159477
+ if( (pLoop->wsFlags & WHERE_ONEROW)
159478
+ || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
159479
+ ){
159344159480
pLevel->op = OP_Noop;
159345159481
}else if( bRev ){
159346159482
pLevel->op = OP_Prev;
159347159483
}else{
159348159484
pLevel->op = OP_Next;
@@ -159993,16 +160129,15 @@
159993160129
if( pRight->fg.viaCoroutine ){
159994160130
sqlite3VdbeAddOp3(
159995160131
v, OP_Null, 0, pRight->regResult,
159996160132
pRight->regResult + pRight->pSelect->pEList->nExpr-1
159997160133
);
159998
- }else{
159999
- sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
160000
- iIdxCur = pWInfo->a[k].iIdxCur;
160001
- if( iIdxCur ){
160002
- sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
160003
- }
160134
+ }
160135
+ sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
160136
+ iIdxCur = pWInfo->a[k].iIdxCur;
160137
+ if( iIdxCur ){
160138
+ sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
160004160139
}
160005160140
}
160006160141
if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
160007160142
mAll |= pLoop->maskSelf;
160008160143
for(k=0; k<pWC->nTerm; k++){
@@ -161700,10 +161835,11 @@
161700161835
** will only be added if each of the child terms passes the
161701161836
** (leftCursor==iCsr) test below. */
161702161837
continue;
161703161838
}
161704161839
if( pWC->a[ii].leftCursor!=iCsr ) return;
161840
+ if( pWC->a[ii].prereqRight!=0 ) return;
161705161841
}
161706161842
161707161843
/* Check condition (5). Return early if it is not met. */
161708161844
if( pOrderBy ){
161709161845
for(ii=0; ii<pOrderBy->nExpr; ii++){
@@ -161714,16 +161850,18 @@
161714161850
}
161715161851
}
161716161852
161717161853
/* All conditions are met. Add the terms to the where-clause object. */
161718161854
assert( p->pLimit->op==TK_LIMIT );
161719
- whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
161720
- iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
161721
- if( p->iOffset>0 ){
161855
+ if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){
161722161856
whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
161723161857
iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
161724161858
}
161859
+ if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){
161860
+ whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
161861
+ iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
161862
+ }
161725161863
}
161726161864
}
161727161865
161728161866
/*
161729161867
** Initialize a preallocated WhereClause structure.
@@ -162236,10 +162374,46 @@
162236162374
if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
162237162375
return p;
162238162376
}
162239162377
return 0;
162240162378
}
162379
+
162380
+/*
162381
+** Term pTerm is guaranteed to be a WO_IN term. It may be a component term
162382
+** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)".
162383
+** This function checks to see if the term is compatible with an index
162384
+** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so,
162385
+** it returns a pointer to the name of the collation sequence (e.g. "BINARY"
162386
+** or "NOCASE") used by the comparison in pTerm. If it is not compatible
162387
+** with affinity idxaff, NULL is returned.
162388
+*/
162389
+static SQLITE_NOINLINE const char *indexInAffinityOk(
162390
+ Parse *pParse,
162391
+ WhereTerm *pTerm,
162392
+ u8 idxaff
162393
+){
162394
+ Expr *pX = pTerm->pExpr;
162395
+ Expr inexpr;
162396
+
162397
+ assert( pTerm->eOperator & WO_IN );
162398
+
162399
+ if( sqlite3ExprIsVector(pX->pLeft) ){
162400
+ int iField = pTerm->u.x.iField - 1;
162401
+ inexpr.flags = 0;
162402
+ inexpr.op = TK_EQ;
162403
+ inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
162404
+ assert( ExprUseXSelect(pX) );
162405
+ inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
162406
+ pX = &inexpr;
162407
+ }
162408
+
162409
+ if( sqlite3IndexAffinityOk(pX, idxaff) ){
162410
+ CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX);
162411
+ return pRet ? pRet->zName : sqlite3StrBINARY;
162412
+ }
162413
+ return 0;
162414
+}
162241162415
162242162416
/*
162243162417
** Advance to the next WhereTerm that matches according to the criteria
162244162418
** established when the pScan object was initialized by whereScanInit().
162245162419
** Return NULL if there are no more matching WhereTerms.
@@ -162287,20 +162461,28 @@
162287162461
}
162288162462
}
162289162463
if( (pTerm->eOperator & pScan->opMask)!=0 ){
162290162464
/* Verify the affinity and collating sequence match */
162291162465
if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
162292
- CollSeq *pColl;
162466
+ const char *zCollName;
162293162467
Parse *pParse = pWC->pWInfo->pParse;
162294162468
pX = pTerm->pExpr;
162295
- if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
162296
- continue;
162469
+
162470
+ if( (pTerm->eOperator & WO_IN) ){
162471
+ zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
162472
+ if( !zCollName ) continue;
162473
+ }else{
162474
+ CollSeq *pColl;
162475
+ if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
162476
+ continue;
162477
+ }
162478
+ assert(pX->pLeft);
162479
+ pColl = sqlite3ExprCompareCollSeq(pParse, pX);
162480
+ zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
162297162481
}
162298
- assert(pX->pLeft);
162299
- pColl = sqlite3ExprCompareCollSeq(pParse, pX);
162300
- if( pColl==0 ) pColl = pParse->db->pDfltColl;
162301
- if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
162482
+
162483
+ if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
162302162484
continue;
162303162485
}
162304162486
}
162305162487
if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
162306162488
&& (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
@@ -165947,10 +166129,25 @@
165947166129
static int isLimitTerm(WhereTerm *pTerm){
165948166130
assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
165949166131
return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
165950166132
&& pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
165951166133
}
166134
+
166135
+/*
166136
+** Return true if the first nCons constraints in the pUsage array are
166137
+** marked as in-use (have argvIndex>0). False otherwise.
166138
+*/
166139
+static int allConstraintsUsed(
166140
+ struct sqlite3_index_constraint_usage *aUsage,
166141
+ int nCons
166142
+){
166143
+ int ii;
166144
+ for(ii=0; ii<nCons; ii++){
166145
+ if( aUsage[ii].argvIndex<=0 ) return 0;
166146
+ }
166147
+ return 1;
166148
+}
165952166149
165953166150
/*
165954166151
** Argument pIdxInfo is already populated with all constraints that may
165955166152
** be used by the virtual table identified by pBuilder->pNew->iTab. This
165956166153
** function marks a subset of those constraints usable, invokes the
@@ -166088,17 +166285,24 @@
166088166285
pIdxInfo->orderByConsumed = 0;
166089166286
pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
166090166287
*pbIn = 1; assert( (mExclude & WO_IN)==0 );
166091166288
}
166092166289
166290
+ /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
166291
+ ** terms. And if there are any, they should follow all other terms. */
166093166292
assert( pbRetryLimit || !isLimitTerm(pTerm) );
166094
- if( isLimitTerm(pTerm) && *pbIn ){
166293
+ assert( !isLimitTerm(pTerm) || i>=nConstraint-2 );
166294
+ assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) );
166295
+
166296
+ if( isLimitTerm(pTerm) && (*pbIn || !allConstraintsUsed(pUsage, i)) ){
166095166297
/* If there is an IN(...) term handled as an == (separate call to
166096166298
** xFilter for each value on the RHS of the IN) and a LIMIT or
166097
- ** OFFSET term handled as well, the plan is unusable. Set output
166098
- ** variable *pbRetryLimit to true to tell the caller to retry with
166099
- ** LIMIT and OFFSET disabled. */
166299
+ ** OFFSET term handled as well, the plan is unusable. Similarly,
166300
+ ** if there is a LIMIT/OFFSET and there are other unused terms,
166301
+ ** the plan cannot be used. In these cases set variable *pbRetryLimit
166302
+ ** to true to tell the caller to retry with LIMIT and OFFSET
166303
+ ** disabled. */
166100166304
if( pIdxInfo->needToFreeIdxStr ){
166101166305
sqlite3_free(pIdxInfo->idxStr);
166102166306
pIdxInfo->idxStr = 0;
166103166307
pIdxInfo->needToFreeIdxStr = 0;
166104166308
}
@@ -167857,10 +168061,62 @@
167857168061
}
167858168062
}
167859168063
nSearch += pLoop->nOut;
167860168064
}
167861168065
}
168066
+
168067
+/*
168068
+** Expression Node callback for sqlite3ExprCanReturnSubtype().
168069
+**
168070
+** Only a function call is able to return a subtype. So if the node
168071
+** is not a function call, return WRC_Prune immediately.
168072
+**
168073
+** A function call is able to return a subtype if it has the
168074
+** SQLITE_RESULT_SUBTYPE property.
168075
+**
168076
+** Assume that every function is able to pass-through a subtype from
168077
+** one of its argument (using sqlite3_result_value()). Most functions
168078
+** are not this way, but we don't have a mechanism to distinguish those
168079
+** that are from those that are not, so assume they all work this way.
168080
+** That means that if one of its arguments is another function and that
168081
+** other function is able to return a subtype, then this function is
168082
+** able to return a subtype.
168083
+*/
168084
+static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){
168085
+ int n;
168086
+ FuncDef *pDef;
168087
+ sqlite3 *db;
168088
+ if( pExpr->op!=TK_FUNCTION ){
168089
+ return WRC_Prune;
168090
+ }
168091
+ assert( ExprUseXList(pExpr) );
168092
+ db = pWalker->pParse->db;
168093
+ n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
168094
+ pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
168095
+ if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
168096
+ pWalker->eCode = 1;
168097
+ return WRC_Prune;
168098
+ }
168099
+ return WRC_Continue;
168100
+}
168101
+
168102
+/*
168103
+** Return TRUE if expression pExpr is able to return a subtype.
168104
+**
168105
+** A TRUE return does not guarantee that a subtype will be returned.
168106
+** It only indicates that a subtype return is possible. False positives
168107
+** are acceptable as they only disable an optimization. False negatives,
168108
+** on the other hand, can lead to incorrect answers.
168109
+*/
168110
+static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){
168111
+ Walker w;
168112
+ memset(&w, 0, sizeof(w));
168113
+ w.pParse = pParse;
168114
+ w.xExprCallback = exprNodeCanReturnSubtype;
168115
+ sqlite3WalkExpr(&w, pExpr);
168116
+ return w.eCode;
168117
+}
167862168118
167863168119
/*
167864168120
** The index pIdx is used by a query and contains one or more expressions.
167865168121
** In other words pIdx is an index on an expression. iIdxCur is the cursor
167866168122
** number for the index and iDataCur is the cursor number for the corresponding
@@ -167891,23 +168147,15 @@
167891168147
pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
167892168148
}else{
167893168149
continue;
167894168150
}
167895168151
if( sqlite3ExprIsConstant(0,pExpr) ) continue;
167896
- if( pExpr->op==TK_FUNCTION ){
168152
+ if( pExpr->op==TK_FUNCTION && sqlite3ExprCanReturnSubtype(pParse,pExpr) ){
167897168153
/* Functions that might set a subtype should not be replaced by the
167898168154
** value taken from an expression index since the index omits the
167899168155
** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
167900
- int n;
167901
- FuncDef *pDef;
167902
- sqlite3 *db = pParse->db;
167903
- assert( ExprUseXList(pExpr) );
167904
- n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
167905
- pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
167906
- if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
167907
- continue;
167908
- }
168156
+ continue;
167909168157
}
167910168158
p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
167911168159
if( p==0 ) break;
167912168160
p->pIENext = pParse->pIdxEpr;
167913168161
#ifdef WHERETRACE_ENABLED
@@ -168883,13 +169131,12 @@
168883169131
int m, n;
168884169132
n = pSrc->regResult;
168885169133
assert( pSrc->pTab!=0 );
168886169134
m = pSrc->pTab->nCol;
168887169135
sqlite3VdbeAddOp3(v, OP_Null, 0, n, n+m-1);
168888
- }else{
168889
- sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
168890169136
}
169137
+ sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
168891169138
}
168892169139
if( (ws & WHERE_INDEXED)
168893169140
|| ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
168894169141
){
168895169142
if( ws & WHERE_MULTI_OR ){
@@ -172588,12 +172835,12 @@
172588172835
#define TK_AGG_FUNCTION 168
172589172836
#define TK_AGG_COLUMN 169
172590172837
#define TK_TRUEFALSE 170
172591172838
#define TK_ISNOT 171
172592172839
#define TK_FUNCTION 172
172593
-#define TK_UMINUS 173
172594
-#define TK_UPLUS 174
172840
+#define TK_UPLUS 173
172841
+#define TK_UMINUS 174
172595172842
#define TK_TRUTH 175
172596172843
#define TK_REGISTER 176
172597172844
#define TK_VECTOR 177
172598172845
#define TK_SELECT_COLUMN 178
172599172846
#define TK_IF_NULL_ROW 179
@@ -173620,12 +173867,12 @@
173620173867
0, /* AGG_FUNCTION => nothing */
173621173868
0, /* AGG_COLUMN => nothing */
173622173869
0, /* TRUEFALSE => nothing */
173623173870
0, /* ISNOT => nothing */
173624173871
0, /* FUNCTION => nothing */
173625
- 0, /* UMINUS => nothing */
173626173872
0, /* UPLUS => nothing */
173873
+ 0, /* UMINUS => nothing */
173627173874
0, /* TRUTH => nothing */
173628173875
0, /* REGISTER => nothing */
173629173876
0, /* VECTOR => nothing */
173630173877
0, /* SELECT_COLUMN => nothing */
173631173878
0, /* IF_NULL_ROW => nothing */
@@ -173889,12 +174136,12 @@
173889174136
/* 168 */ "AGG_FUNCTION",
173890174137
/* 169 */ "AGG_COLUMN",
173891174138
/* 170 */ "TRUEFALSE",
173892174139
/* 171 */ "ISNOT",
173893174140
/* 172 */ "FUNCTION",
173894
- /* 173 */ "UMINUS",
173895
- /* 174 */ "UPLUS",
174141
+ /* 173 */ "UPLUS",
174142
+ /* 174 */ "UMINUS",
173896174143
/* 175 */ "TRUTH",
173897174144
/* 176 */ "REGISTER",
173898174145
/* 177 */ "VECTOR",
173899174146
/* 178 */ "SELECT_COLUMN",
173900174147
/* 179 */ "IF_NULL_ROW",
@@ -176751,12 +176998,21 @@
176751176998
case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
176752176999
{yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy454, 0);/*A-overwrites-B*/}
176753177000
break;
176754177001
case 216: /* expr ::= PLUS|MINUS expr */
176755177002
{
176756
- yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy454, 0);
176757
- /*A-overwrites-B*/
177003
+ Expr *p = yymsp[0].minor.yy454;
177004
+ u8 op = yymsp[-1].major + (TK_UPLUS-TK_PLUS);
177005
+ assert( TK_UPLUS>TK_PLUS );
177006
+ assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) );
177007
+ if( p && p->op==TK_UPLUS ){
177008
+ p->op = op;
177009
+ yymsp[-1].minor.yy454 = p;
177010
+ }else{
177011
+ yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, op, p, 0);
177012
+ /*A-overwrites-B*/
177013
+ }
176758177014
}
176759177015
break;
176760177016
case 217: /* expr ::= expr PTR expr */
176761177017
{
176762177018
ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy454);
@@ -228296,18 +228552,18 @@
228296228552
** sufficient either for the 'T' or 'P' byte and the varint that follows
228297228553
** it, or for the two single byte values otherwise. */
228298228554
p->rc = sessionInputBuffer(&p->in, 2);
228299228555
if( p->rc!=SQLITE_OK ) return p->rc;
228300228556
228557
+ sessionDiscardData(&p->in);
228558
+ p->in.iCurrent = p->in.iNext;
228559
+
228301228560
/* If the iterator is already at the end of the changeset, return DONE. */
228302228561
if( p->in.iNext>=p->in.nData ){
228303228562
return SQLITE_DONE;
228304228563
}
228305228564
228306
- sessionDiscardData(&p->in);
228307
- p->in.iCurrent = p->in.iNext;
228308
-
228309228565
op = p->in.aData[p->in.iNext++];
228310228566
while( op=='T' || op=='P' ){
228311228567
if( pbNew ) *pbNew = 1;
228312228568
p->bPatchset = (op=='P');
228313228569
if( sessionChangesetReadTblhdr(p) ) return p->rc;
@@ -230038,10 +230294,11 @@
230038230294
*/
230039230295
struct sqlite3_changegroup {
230040230296
int rc; /* Error code */
230041230297
int bPatch; /* True to accumulate patchsets */
230042230298
SessionTable *pList; /* List of tables in current patch */
230299
+ SessionBuffer rec;
230043230300
230044230301
sqlite3 *db; /* Configured by changegroup_schema() */
230045230302
char *zDb; /* Configured by changegroup_schema() */
230046230303
};
230047230304
@@ -230336,112 +230593,132 @@
230336230593
230337230594
return rc;
230338230595
}
230339230596
230340230597
/*
230341
-** Add all changes in the changeset traversed by the iterator passed as
230342
-** the first argument to the changegroup hash tables.
230598
+** Locate or create a SessionTable object that may be used to add the
230599
+** change currently pointed to by iterator pIter to changegroup pGrp.
230600
+** If successful, set output variable (*ppTab) to point to the table
230601
+** object and return SQLITE_OK. Otherwise, if some error occurs, return
230602
+** an SQLite error code and leave (*ppTab) set to NULL.
230343230603
*/
230344
-static int sessionChangesetToHash(
230345
- sqlite3_changeset_iter *pIter, /* Iterator to read from */
230346
- sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
230347
- int bRebase /* True if hash table is for rebasing */
230604
+static int sessionChangesetFindTable(
230605
+ sqlite3_changegroup *pGrp,
230606
+ const char *zTab,
230607
+ sqlite3_changeset_iter *pIter,
230608
+ SessionTable **ppTab
230348230609
){
230349
- u8 *aRec;
230350
- int nRec;
230351230610
int rc = SQLITE_OK;
230611
+ SessionTable *pTab = 0;
230612
+ int nTab = (int)strlen(zTab);
230613
+ u8 *abPK = 0;
230614
+ int nCol = 0;
230615
+
230616
+ *ppTab = 0;
230617
+ sqlite3changeset_pk(pIter, &abPK, &nCol);
230618
+
230619
+ /* Search the list for an existing table */
230620
+ for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
230621
+ if( 0==sqlite3_strnicmp(pTab->zName, zTab, nTab+1) ) break;
230622
+ }
230623
+
230624
+ /* If one was not found above, create a new table now */
230625
+ if( !pTab ){
230626
+ SessionTable **ppNew;
230627
+
230628
+ pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nTab+1);
230629
+ if( !pTab ){
230630
+ return SQLITE_NOMEM;
230631
+ }
230632
+ memset(pTab, 0, sizeof(SessionTable));
230633
+ pTab->nCol = nCol;
230634
+ pTab->abPK = (u8*)&pTab[1];
230635
+ memcpy(pTab->abPK, abPK, nCol);
230636
+ pTab->zName = (char*)&pTab->abPK[nCol];
230637
+ memcpy(pTab->zName, zTab, nTab+1);
230638
+
230639
+ if( pGrp->db ){
230640
+ pTab->nCol = 0;
230641
+ rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
230642
+ if( rc ){
230643
+ assert( pTab->azCol==0 );
230644
+ sqlite3_free(pTab);
230645
+ return rc;
230646
+ }
230647
+ }
230648
+
230649
+ /* The new object must be linked on to the end of the list, not
230650
+ ** simply added to the start of it. This is to ensure that the
230651
+ ** tables within the output of sqlite3changegroup_output() are in
230652
+ ** the right order. */
230653
+ for(ppNew=&pGrp->pList; *ppNew; ppNew=&(*ppNew)->pNext);
230654
+ *ppNew = pTab;
230655
+ }
230656
+
230657
+ /* Check that the table is compatible. */
230658
+ if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
230659
+ rc = SQLITE_SCHEMA;
230660
+ }
230661
+
230662
+ *ppTab = pTab;
230663
+ return rc;
230664
+}
230665
+
230666
+/*
230667
+** Add the change currently indicated by iterator pIter to the hash table
230668
+** belonging to changegroup pGrp.
230669
+*/
230670
+static int sessionOneChangeToHash(
230671
+ sqlite3_changegroup *pGrp,
230672
+ sqlite3_changeset_iter *pIter,
230673
+ int bRebase
230674
+){
230675
+ int rc = SQLITE_OK;
230676
+ int nCol = 0;
230677
+ int op = 0;
230678
+ int iHash = 0;
230679
+ int bIndirect = 0;
230680
+ SessionChange *pChange = 0;
230681
+ SessionChange *pExist = 0;
230682
+ SessionChange **pp = 0;
230352230683
SessionTable *pTab = 0;
230353
- SessionBuffer rec = {0, 0, 0};
230354
-
230355
- while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
230356
- const char *zNew;
230357
- int nCol;
230358
- int op;
230359
- int iHash;
230360
- int bIndirect;
230361
- SessionChange *pChange;
230362
- SessionChange *pExist = 0;
230363
- SessionChange **pp;
230364
-
230365
- /* Ensure that only changesets, or only patchsets, but not a mixture
230366
- ** of both, are being combined. It is an error to try to combine a
230367
- ** changeset and a patchset. */
230368
- if( pGrp->pList==0 ){
230369
- pGrp->bPatch = pIter->bPatchset;
230370
- }else if( pIter->bPatchset!=pGrp->bPatch ){
230371
- rc = SQLITE_ERROR;
230372
- break;
230373
- }
230374
-
230375
- sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
230376
- if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
230377
- /* Search the list for a matching table */
230378
- int nNew = (int)strlen(zNew);
230379
- u8 *abPK;
230380
-
230381
- sqlite3changeset_pk(pIter, &abPK, 0);
230382
- for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
230383
- if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
230384
- }
230385
- if( !pTab ){
230386
- SessionTable **ppTab;
230387
-
230388
- pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
230389
- if( !pTab ){
230390
- rc = SQLITE_NOMEM;
230391
- break;
230392
- }
230393
- memset(pTab, 0, sizeof(SessionTable));
230394
- pTab->nCol = nCol;
230395
- pTab->abPK = (u8*)&pTab[1];
230396
- memcpy(pTab->abPK, abPK, nCol);
230397
- pTab->zName = (char*)&pTab->abPK[nCol];
230398
- memcpy(pTab->zName, zNew, nNew+1);
230399
-
230400
- if( pGrp->db ){
230401
- pTab->nCol = 0;
230402
- rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
230403
- if( rc ){
230404
- assert( pTab->azCol==0 );
230405
- sqlite3_free(pTab);
230406
- break;
230407
- }
230408
- }
230409
-
230410
- /* The new object must be linked on to the end of the list, not
230411
- ** simply added to the start of it. This is to ensure that the
230412
- ** tables within the output of sqlite3changegroup_output() are in
230413
- ** the right order. */
230414
- for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
230415
- *ppTab = pTab;
230416
- }
230417
-
230418
- if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
230419
- rc = SQLITE_SCHEMA;
230420
- break;
230421
- }
230422
- }
230423
-
230424
- if( nCol<pTab->nCol ){
230425
- assert( pGrp->db );
230426
- rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, &rec);
230427
- if( rc ) break;
230428
- aRec = rec.aBuf;
230429
- nRec = rec.nBuf;
230430
- }
230431
-
230432
- if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
230433
- rc = SQLITE_NOMEM;
230434
- break;
230435
- }
230684
+ u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
230685
+ int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
230686
+
230687
+ /* Ensure that only changesets, or only patchsets, but not a mixture
230688
+ ** of both, are being combined. It is an error to try to combine a
230689
+ ** changeset and a patchset. */
230690
+ if( pGrp->pList==0 ){
230691
+ pGrp->bPatch = pIter->bPatchset;
230692
+ }else if( pIter->bPatchset!=pGrp->bPatch ){
230693
+ rc = SQLITE_ERROR;
230694
+ }
230695
+
230696
+ if( rc==SQLITE_OK ){
230697
+ const char *zTab = 0;
230698
+ sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect);
230699
+ rc = sessionChangesetFindTable(pGrp, zTab, pIter, &pTab);
230700
+ }
230701
+
230702
+ if( rc==SQLITE_OK && nCol<pTab->nCol ){
230703
+ SessionBuffer *pBuf = &pGrp->rec;
230704
+ rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, pBuf);
230705
+ aRec = pBuf->aBuf;
230706
+ nRec = pBuf->nBuf;
230707
+ assert( pGrp->db );
230708
+ }
230709
+
230710
+ if( rc==SQLITE_OK && sessionGrowHash(0, pIter->bPatchset, pTab) ){
230711
+ rc = SQLITE_NOMEM;
230712
+ }
230713
+
230714
+ if( rc==SQLITE_OK ){
230715
+ /* Search for existing entry. If found, remove it from the hash table.
230716
+ ** Code below may link it back in. */
230436230717
iHash = sessionChangeHash(
230437230718
pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
230438230719
);
230439
-
230440
- /* Search for existing entry. If found, remove it from the hash table.
230441
- ** Code below may link it back in.
230442
- */
230443230720
for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
230444230721
int bPkOnly1 = 0;
230445230722
int bPkOnly2 = 0;
230446230723
if( pIter->bPatchset ){
230447230724
bPkOnly1 = (*pp)->op==SQLITE_DELETE;
@@ -230452,23 +230729,45 @@
230452230729
*pp = (*pp)->pNext;
230453230730
pTab->nEntry--;
230454230731
break;
230455230732
}
230456230733
}
230734
+ }
230457230735
230736
+ if( rc==SQLITE_OK ){
230458230737
rc = sessionChangeMerge(pTab, bRebase,
230459230738
pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
230460230739
);
230461
- if( rc ) break;
230462
- if( pChange ){
230463
- pChange->pNext = pTab->apChange[iHash];
230464
- pTab->apChange[iHash] = pChange;
230465
- pTab->nEntry++;
230466
- }
230740
+ }
230741
+ if( rc==SQLITE_OK && pChange ){
230742
+ pChange->pNext = pTab->apChange[iHash];
230743
+ pTab->apChange[iHash] = pChange;
230744
+ pTab->nEntry++;
230467230745
}
230468230746
230469
- sqlite3_free(rec.aBuf);
230747
+ if( rc==SQLITE_OK ) rc = pIter->rc;
230748
+ return rc;
230749
+}
230750
+
230751
+/*
230752
+** Add all changes in the changeset traversed by the iterator passed as
230753
+** the first argument to the changegroup hash tables.
230754
+*/
230755
+static int sessionChangesetToHash(
230756
+ sqlite3_changeset_iter *pIter, /* Iterator to read from */
230757
+ sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
230758
+ int bRebase /* True if hash table is for rebasing */
230759
+){
230760
+ u8 *aRec;
230761
+ int nRec;
230762
+ int rc = SQLITE_OK;
230763
+
230764
+ while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
230765
+ rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
230766
+ if( rc!=SQLITE_OK ) break;
230767
+ }
230768
+
230470230769
if( rc==SQLITE_OK ) rc = pIter->rc;
230471230770
return rc;
230472230771
}
230473230772
230474230773
/*
@@ -230591,10 +230890,27 @@
230591230890
rc = sessionChangesetToHash(pIter, pGrp, 0);
230592230891
}
230593230892
sqlite3changeset_finalize(pIter);
230594230893
return rc;
230595230894
}
230895
+
230896
+/*
230897
+** Add a single change to a changeset-group.
230898
+*/
230899
+SQLITE_API int sqlite3changegroup_add_change(
230900
+ sqlite3_changegroup *pGrp,
230901
+ sqlite3_changeset_iter *pIter
230902
+){
230903
+ if( pIter->in.iCurrent==pIter->in.iNext
230904
+ || pIter->rc!=SQLITE_OK
230905
+ || pIter->bInvert
230906
+ ){
230907
+ /* Iterator does not point to any valid entry or is an INVERT iterator. */
230908
+ return SQLITE_ERROR;
230909
+ }
230910
+ return sessionOneChangeToHash(pGrp, pIter, 0);
230911
+}
230596230912
230597230913
/*
230598230914
** Obtain a buffer containing a changeset representing the concatenation
230599230915
** of all changesets added to the group so far.
230600230916
*/
@@ -230641,10 +230957,11 @@
230641230957
*/
230642230958
SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
230643230959
if( pGrp ){
230644230960
sqlite3_free(pGrp->zDb);
230645230961
sessionDeleteTable(0, pGrp->pList);
230962
+ sqlite3_free(pGrp->rec.aBuf);
230646230963
sqlite3_free(pGrp);
230647230964
}
230648230965
}
230649230966
230650230967
/*
@@ -231042,10 +231359,11 @@
231042231359
** Destroy a rebaser object
231043231360
*/
231044231361
SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
231045231362
if( p ){
231046231363
sessionDeleteTable(0, p->grp.pList);
231364
+ sqlite3_free(p->grp.rec.aBuf);
231047231365
sqlite3_free(p);
231048231366
}
231049231367
}
231050231368
231051231369
/*
@@ -252193,11 +252511,11 @@
252193252511
int nArg, /* Number of args */
252194252512
sqlite3_value **apUnused /* Function arguments */
252195252513
){
252196252514
assert( nArg==0 );
252197252515
UNUSED_PARAM2(nArg, apUnused);
252198
- sqlite3_result_text(pCtx, "fts5: 2024-04-18 16:11:01 8c0f69e0e4ae0a446838cc193bfd4395fd251f3c7659b35ac388e5a0a7650a66", -1, SQLITE_TRANSIENT);
252516
+ sqlite3_result_text(pCtx, "fts5: 2024-05-08 11:51:56 42d67c6fed3a5f21d7b71515aca471ba61d387e620022735a2e7929fa3a237cf", -1, SQLITE_TRANSIENT);
252199252517
}
252200252518
252201252519
/*
252202252520
** Return true if zName is the extension on one of the shadow tables used
252203252521
** by this module.
252204252522
--- 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 ** 8c0f69e0e4ae0a446838cc193bfd4395fd25.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.46.0"
463 #define SQLITE_VERSION_NUMBER 3046000
464 #define SQLITE_SOURCE_ID "2024-04-18 16:11:01 8c0f69e0e4ae0a446838cc193bfd4395fd251f3c7659b35ac388e5a0a7650a66"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -12314,10 +12314,34 @@
12314 **
12315 ** In all cases, if an error occurs the state of the final contents of the
12316 ** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12317 */
12318 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12319
12320 /*
12321 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12322 ** METHOD: sqlite3_changegroup
12323 **
@@ -14612,12 +14636,12 @@
14612 #define TK_AGG_FUNCTION 168
14613 #define TK_AGG_COLUMN 169
14614 #define TK_TRUEFALSE 170
14615 #define TK_ISNOT 171
14616 #define TK_FUNCTION 172
14617 #define TK_UMINUS 173
14618 #define TK_UPLUS 174
14619 #define TK_TRUTH 175
14620 #define TK_REGISTER 176
14621 #define TK_VECTOR 177
14622 #define TK_SELECT_COLUMN 178
14623 #define TK_IF_NULL_ROW 179
@@ -31945,11 +31969,11 @@
31945 sqlite3_str_appendall(pAccum, pItem->zName);
31946 }else if( pItem->zAlias ){
31947 sqlite3_str_appendall(pAccum, pItem->zAlias);
31948 }else{
31949 Select *pSel = pItem->pSelect;
31950 assert( pSel!=0 );
31951 if( pSel->selFlags & SF_NestedFrom ){
31952 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
31953 }else if( pSel->selFlags & SF_MultiValue ){
31954 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy );
31955 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
@@ -32769,16 +32793,18 @@
32769 if( pItem->fg.isUsing ) n++;
32770 if( pItem->fg.isUsing ){
32771 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
32772 }
32773 if( pItem->pSelect ){
 
32774 if( pItem->pTab ){
32775 Table *pTab = pItem->pTab;
32776 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
32777 }
32778 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
32779 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
 
32780 }
32781 if( pItem->fg.isTabFunc ){
32782 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
32783 }
32784 sqlite3TreeViewPop(&pView);
@@ -32878,11 +32904,11 @@
32878 }
32879 if( p->pLimit ){
32880 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
32881 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
32882 if( p->pLimit->pRight ){
32883 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
32884 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
32885 sqlite3TreeViewPop(&pView);
32886 }
32887 sqlite3TreeViewPop(&pView);
32888 }
@@ -84677,14 +84703,14 @@
84677 int nRec, /* Size of buffer pRec in bytes */
84678 int iCol, /* Column to extract */
84679 sqlite3_value **ppVal /* OUT: Extracted value */
84680 ){
84681 u32 t = 0; /* a column type code */
84682 int nHdr; /* Size of the header in the record */
84683 int iHdr; /* Next unread header byte */
84684 int iField; /* Next unread data byte */
84685 int szField = 0; /* Size of the current data field */
84686 int i; /* Column index */
84687 u8 *a = (u8*)pRec; /* Typecast byte array */
84688 Mem *pMem = *ppVal; /* Write result into this Mem object */
84689
84690 assert( iCol>0 );
@@ -97641,11 +97667,12 @@
97641 ** row output from the sorter so that the row can be decomposed into
97642 ** individual columns using the OP_Column opcode. The OP_Column opcode
97643 ** is the only cursor opcode that works with a pseudo-table.
97644 **
97645 ** P3 is the number of fields in the records that will be stored by
97646 ** the pseudo-table.
 
97647 */
97648 case OP_OpenPseudo: {
97649 VdbeCursor *pCx;
97650
97651 assert( pOp->p1>=0 );
@@ -105799,14 +105826,14 @@
105799 break;
105800 }
105801
105802 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
105803 case 9: /* nexec */
105804 sqlite3_result_int(ctx, pOp->nExec);
105805 break;
105806 case 10: /* ncycle */
105807 sqlite3_result_int(ctx, pOp->nCycle);
105808 break;
105809 #else
105810 case 9: /* nexec */
105811 case 10: /* ncycle */
105812 sqlite3_result_int(ctx, 0);
@@ -107195,11 +107222,12 @@
107195 int op = pParse->eTriggerOp;
107196 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
107197 if( pParse->bReturning ){
107198 if( (pNC->ncFlags & NC_UBaseReg)!=0
107199 && ALWAYS(zTab==0
107200 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
 
107201 ){
107202 pExpr->iTable = op!=TK_DELETE;
107203 pTab = pParse->pTriggerTab;
107204 }
107205 }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){
@@ -108556,10 +108584,11 @@
108556 /* Recursively resolve names in all subqueries in the FROM clause
108557 */
108558 if( pOuterNC ) pOuterNC->nNestedSelect++;
108559 for(i=0; i<p->pSrc->nSrc; i++){
108560 SrcItem *pItem = &p->pSrc->a[i];
 
108561 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108562 int nRef = pOuterNC ? pOuterNC->nRef : 0;
108563 const char *zSavedContext = pParse->zAuthContext;
108564
108565 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
@@ -110315,10 +110344,11 @@
110315 ** Recursively delete an expression tree.
110316 */
110317 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110318 assert( p!=0 );
110319 assert( db!=0 );
 
110320 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
110321 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
110322 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
110323 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
110324 #ifdef SQLITE_DEBUG
@@ -110330,11 +110360,10 @@
110330 }
110331 #endif
110332 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
110333 /* The Expr.x union is never used at the same time as Expr.pRight */
110334 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
110335 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
110336 if( p->pRight ){
110337 assert( !ExprHasProperty(p, EP_WinFunc) );
110338 sqlite3ExprDeleteNN(db, p->pRight);
110339 }else if( ExprUseXSelect(p) ){
110340 assert( !ExprHasProperty(p, EP_WinFunc) );
@@ -110344,10 +110373,23 @@
110344 #ifndef SQLITE_OMIT_WINDOWFUNC
110345 if( ExprHasProperty(p, EP_WinFunc) ){
110346 sqlite3WindowDelete(db, p->y.pWin);
110347 }
110348 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
110349 }
110350 }
110351 if( !ExprHasProperty(p, EP_Static) ){
110352 sqlite3DbNNFreeNN(db, p);
110353 }
@@ -111340,11 +111382,11 @@
111340 || ExprHasProperty(pExpr, EP_WinFunc)
111341 ){
111342 pWalker->eCode = 0;
111343 return WRC_Abort;
111344 }
111345 return WRC_Continue;
111346 }
111347
111348
111349 /*
111350 ** These routines are Walker callbacks used to check expressions to
@@ -124219,13 +124261,10 @@
124219 if( p->tabFlags & TF_HasGenerated ){
124220 sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
124221 sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
124222 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124223 }
124224 sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
124225 sqlite3MPrintf(db, "PRAGMA \"%w\".integrity_check(%Q)",
124226 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124227 }
124228
124229 /* Add the table to the in-memory representation of the database.
124230 */
124231 if( db->init.busy ){
@@ -140351,11 +140390,11 @@
140351 pParse->nMem = 6;
140352
140353 /* Set the maximum error count */
140354 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140355 if( zRight ){
140356 if( sqlite3GetInt32(zRight, &mxErr) ){
140357 if( mxErr<=0 ){
140358 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140359 }
140360 }else{
140361 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
@@ -141559,10 +141598,11 @@
141559 /* Clear all content from pragma virtual table cursor. */
141560 static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
141561 int i;
141562 sqlite3_finalize(pCsr->pPragma);
141563 pCsr->pPragma = 0;
 
141564 for(i=0; i<ArraySize(pCsr->azArg); i++){
141565 sqlite3_free(pCsr->azArg[i]);
141566 pCsr->azArg[i] = 0;
141567 }
141568 }
@@ -145144,21 +145184,26 @@
145144 memset(&sNC, 0, sizeof(sNC));
145145 sNC.pSrcList = pSelect->pSrc;
145146 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
145147 const char *zType;
145148 i64 n;
 
 
145149 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
145150 p = a[i].pExpr;
145151 /* pCol->szEst = ... // Column size est for SELECT tables never used */
145152 pCol->affinity = sqlite3ExprAffinity(p);
 
 
 
 
 
145153 if( pCol->affinity<=SQLITE_AFF_NONE ){
145154 pCol->affinity = aff;
145155 }
145156 if( pCol->affinity>=SQLITE_AFF_TEXT && pSelect->pNext ){
145157 int m = 0;
145158 Select *pS2;
145159 for(m=0, pS2=pSelect->pNext; pS2; pS2=pS2->pNext){
145160 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145161 }
145162 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
145163 pCol->affinity = SQLITE_AFF_BLOB;
145164 }else
@@ -152576,10 +152621,76 @@
152576 }
152577 }
152578 }
152579 return pNew;
152580 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152581
152582 /*
152583 ** Generate code for the RETURNING trigger. Unlike other triggers
152584 ** that invoke a subprogram in the bytecode, the code for RETURNING
152585 ** is generated in-line.
@@ -152613,10 +152724,11 @@
152613 memset(&sFrom, 0, sizeof(sFrom));
152614 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
152615 sSelect.pSrc = &sFrom;
152616 sFrom.nSrc = 1;
152617 sFrom.a[0].pTab = pTab;
 
152618 sFrom.a[0].iCursor = -1;
152619 sqlite3SelectPrep(pParse, &sSelect, 0);
152620 if( pParse->nErr==0 ){
152621 assert( db->mallocFailed==0 );
152622 sqlite3GenerateColumnNames(pParse, &sSelect);
@@ -152639,10 +152751,11 @@
152639 && ALWAYS(!db->mallocFailed)
152640 ){
152641 int i;
152642 int nCol = pNew->nExpr;
152643 int reg = pParse->nMem+1;
 
152644 pParse->nMem += nCol+2;
152645 pReturning->iRetReg = reg;
152646 for(i=0; i<nCol; i++){
152647 Expr *pCol = pNew->a[i].pExpr;
152648 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
@@ -158590,10 +158703,31 @@
158590 }
158591 pLevel->regFilter = 0;
158592 pLevel->addrBrk = 0;
158593 }
158594 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158595
158596 /*
158597 ** Generate code for the start of the iLevel-th loop in the WHERE clause
158598 ** implementation described by pWInfo.
158599 */
@@ -159338,11 +159472,13 @@
159338 ** a LEFT JOIN: */
159339 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
159340 }
159341
159342 /* Record the instruction used to terminate the loop. */
159343 if( pLoop->wsFlags & WHERE_ONEROW ){
 
 
159344 pLevel->op = OP_Noop;
159345 }else if( bRev ){
159346 pLevel->op = OP_Prev;
159347 }else{
159348 pLevel->op = OP_Next;
@@ -159993,16 +160129,15 @@
159993 if( pRight->fg.viaCoroutine ){
159994 sqlite3VdbeAddOp3(
159995 v, OP_Null, 0, pRight->regResult,
159996 pRight->regResult + pRight->pSelect->pEList->nExpr-1
159997 );
159998 }else{
159999 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
160000 iIdxCur = pWInfo->a[k].iIdxCur;
160001 if( iIdxCur ){
160002 sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
160003 }
160004 }
160005 }
160006 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
160007 mAll |= pLoop->maskSelf;
160008 for(k=0; k<pWC->nTerm; k++){
@@ -161700,10 +161835,11 @@
161700 ** will only be added if each of the child terms passes the
161701 ** (leftCursor==iCsr) test below. */
161702 continue;
161703 }
161704 if( pWC->a[ii].leftCursor!=iCsr ) return;
 
161705 }
161706
161707 /* Check condition (5). Return early if it is not met. */
161708 if( pOrderBy ){
161709 for(ii=0; ii<pOrderBy->nExpr; ii++){
@@ -161714,16 +161850,18 @@
161714 }
161715 }
161716
161717 /* All conditions are met. Add the terms to the where-clause object. */
161718 assert( p->pLimit->op==TK_LIMIT );
161719 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
161720 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
161721 if( p->iOffset>0 ){
161722 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
161723 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
161724 }
 
 
 
 
161725 }
161726 }
161727
161728 /*
161729 ** Initialize a preallocated WhereClause structure.
@@ -162236,10 +162374,46 @@
162236 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
162237 return p;
162238 }
162239 return 0;
162240 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162241
162242 /*
162243 ** Advance to the next WhereTerm that matches according to the criteria
162244 ** established when the pScan object was initialized by whereScanInit().
162245 ** Return NULL if there are no more matching WhereTerms.
@@ -162287,20 +162461,28 @@
162287 }
162288 }
162289 if( (pTerm->eOperator & pScan->opMask)!=0 ){
162290 /* Verify the affinity and collating sequence match */
162291 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
162292 CollSeq *pColl;
162293 Parse *pParse = pWC->pWInfo->pParse;
162294 pX = pTerm->pExpr;
162295 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
162296 continue;
 
 
 
 
 
 
 
 
 
 
162297 }
162298 assert(pX->pLeft);
162299 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
162300 if( pColl==0 ) pColl = pParse->db->pDfltColl;
162301 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
162302 continue;
162303 }
162304 }
162305 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
162306 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
@@ -165947,10 +166129,25 @@
165947 static int isLimitTerm(WhereTerm *pTerm){
165948 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
165949 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
165950 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
165951 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165952
165953 /*
165954 ** Argument pIdxInfo is already populated with all constraints that may
165955 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
165956 ** function marks a subset of those constraints usable, invokes the
@@ -166088,17 +166285,24 @@
166088 pIdxInfo->orderByConsumed = 0;
166089 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
166090 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
166091 }
166092
 
 
166093 assert( pbRetryLimit || !isLimitTerm(pTerm) );
166094 if( isLimitTerm(pTerm) && *pbIn ){
 
 
 
166095 /* If there is an IN(...) term handled as an == (separate call to
166096 ** xFilter for each value on the RHS of the IN) and a LIMIT or
166097 ** OFFSET term handled as well, the plan is unusable. Set output
166098 ** variable *pbRetryLimit to true to tell the caller to retry with
166099 ** LIMIT and OFFSET disabled. */
 
 
166100 if( pIdxInfo->needToFreeIdxStr ){
166101 sqlite3_free(pIdxInfo->idxStr);
166102 pIdxInfo->idxStr = 0;
166103 pIdxInfo->needToFreeIdxStr = 0;
166104 }
@@ -167857,10 +168061,62 @@
167857 }
167858 }
167859 nSearch += pLoop->nOut;
167860 }
167861 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167862
167863 /*
167864 ** The index pIdx is used by a query and contains one or more expressions.
167865 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
167866 ** number for the index and iDataCur is the cursor number for the corresponding
@@ -167891,23 +168147,15 @@
167891 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
167892 }else{
167893 continue;
167894 }
167895 if( sqlite3ExprIsConstant(0,pExpr) ) continue;
167896 if( pExpr->op==TK_FUNCTION ){
167897 /* Functions that might set a subtype should not be replaced by the
167898 ** value taken from an expression index since the index omits the
167899 ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
167900 int n;
167901 FuncDef *pDef;
167902 sqlite3 *db = pParse->db;
167903 assert( ExprUseXList(pExpr) );
167904 n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
167905 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
167906 if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
167907 continue;
167908 }
167909 }
167910 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
167911 if( p==0 ) break;
167912 p->pIENext = pParse->pIdxEpr;
167913 #ifdef WHERETRACE_ENABLED
@@ -168883,13 +169131,12 @@
168883 int m, n;
168884 n = pSrc->regResult;
168885 assert( pSrc->pTab!=0 );
168886 m = pSrc->pTab->nCol;
168887 sqlite3VdbeAddOp3(v, OP_Null, 0, n, n+m-1);
168888 }else{
168889 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
168890 }
 
168891 }
168892 if( (ws & WHERE_INDEXED)
168893 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
168894 ){
168895 if( ws & WHERE_MULTI_OR ){
@@ -172588,12 +172835,12 @@
172588 #define TK_AGG_FUNCTION 168
172589 #define TK_AGG_COLUMN 169
172590 #define TK_TRUEFALSE 170
172591 #define TK_ISNOT 171
172592 #define TK_FUNCTION 172
172593 #define TK_UMINUS 173
172594 #define TK_UPLUS 174
172595 #define TK_TRUTH 175
172596 #define TK_REGISTER 176
172597 #define TK_VECTOR 177
172598 #define TK_SELECT_COLUMN 178
172599 #define TK_IF_NULL_ROW 179
@@ -173620,12 +173867,12 @@
173620 0, /* AGG_FUNCTION => nothing */
173621 0, /* AGG_COLUMN => nothing */
173622 0, /* TRUEFALSE => nothing */
173623 0, /* ISNOT => nothing */
173624 0, /* FUNCTION => nothing */
173625 0, /* UMINUS => nothing */
173626 0, /* UPLUS => nothing */
 
173627 0, /* TRUTH => nothing */
173628 0, /* REGISTER => nothing */
173629 0, /* VECTOR => nothing */
173630 0, /* SELECT_COLUMN => nothing */
173631 0, /* IF_NULL_ROW => nothing */
@@ -173889,12 +174136,12 @@
173889 /* 168 */ "AGG_FUNCTION",
173890 /* 169 */ "AGG_COLUMN",
173891 /* 170 */ "TRUEFALSE",
173892 /* 171 */ "ISNOT",
173893 /* 172 */ "FUNCTION",
173894 /* 173 */ "UMINUS",
173895 /* 174 */ "UPLUS",
173896 /* 175 */ "TRUTH",
173897 /* 176 */ "REGISTER",
173898 /* 177 */ "VECTOR",
173899 /* 178 */ "SELECT_COLUMN",
173900 /* 179 */ "IF_NULL_ROW",
@@ -176751,12 +176998,21 @@
176751 case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
176752 {yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy454, 0);/*A-overwrites-B*/}
176753 break;
176754 case 216: /* expr ::= PLUS|MINUS expr */
176755 {
176756 yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy454, 0);
176757 /*A-overwrites-B*/
 
 
 
 
 
 
 
 
 
176758 }
176759 break;
176760 case 217: /* expr ::= expr PTR expr */
176761 {
176762 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy454);
@@ -228296,18 +228552,18 @@
228296 ** sufficient either for the 'T' or 'P' byte and the varint that follows
228297 ** it, or for the two single byte values otherwise. */
228298 p->rc = sessionInputBuffer(&p->in, 2);
228299 if( p->rc!=SQLITE_OK ) return p->rc;
228300
 
 
 
228301 /* If the iterator is already at the end of the changeset, return DONE. */
228302 if( p->in.iNext>=p->in.nData ){
228303 return SQLITE_DONE;
228304 }
228305
228306 sessionDiscardData(&p->in);
228307 p->in.iCurrent = p->in.iNext;
228308
228309 op = p->in.aData[p->in.iNext++];
228310 while( op=='T' || op=='P' ){
228311 if( pbNew ) *pbNew = 1;
228312 p->bPatchset = (op=='P');
228313 if( sessionChangesetReadTblhdr(p) ) return p->rc;
@@ -230038,10 +230294,11 @@
230038 */
230039 struct sqlite3_changegroup {
230040 int rc; /* Error code */
230041 int bPatch; /* True to accumulate patchsets */
230042 SessionTable *pList; /* List of tables in current patch */
 
230043
230044 sqlite3 *db; /* Configured by changegroup_schema() */
230045 char *zDb; /* Configured by changegroup_schema() */
230046 };
230047
@@ -230336,112 +230593,132 @@
230336
230337 return rc;
230338 }
230339
230340 /*
230341 ** Add all changes in the changeset traversed by the iterator passed as
230342 ** the first argument to the changegroup hash tables.
 
 
 
230343 */
230344 static int sessionChangesetToHash(
230345 sqlite3_changeset_iter *pIter, /* Iterator to read from */
230346 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
230347 int bRebase /* True if hash table is for rebasing */
 
230348 ){
230349 u8 *aRec;
230350 int nRec;
230351 int rc = SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230352 SessionTable *pTab = 0;
230353 SessionBuffer rec = {0, 0, 0};
230354
230355 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
230356 const char *zNew;
230357 int nCol;
230358 int op;
230359 int iHash;
230360 int bIndirect;
230361 SessionChange *pChange;
230362 SessionChange *pExist = 0;
230363 SessionChange **pp;
230364
230365 /* Ensure that only changesets, or only patchsets, but not a mixture
230366 ** of both, are being combined. It is an error to try to combine a
230367 ** changeset and a patchset. */
230368 if( pGrp->pList==0 ){
230369 pGrp->bPatch = pIter->bPatchset;
230370 }else if( pIter->bPatchset!=pGrp->bPatch ){
230371 rc = SQLITE_ERROR;
230372 break;
230373 }
230374
230375 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
230376 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
230377 /* Search the list for a matching table */
230378 int nNew = (int)strlen(zNew);
230379 u8 *abPK;
230380
230381 sqlite3changeset_pk(pIter, &abPK, 0);
230382 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
230383 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
230384 }
230385 if( !pTab ){
230386 SessionTable **ppTab;
230387
230388 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
230389 if( !pTab ){
230390 rc = SQLITE_NOMEM;
230391 break;
230392 }
230393 memset(pTab, 0, sizeof(SessionTable));
230394 pTab->nCol = nCol;
230395 pTab->abPK = (u8*)&pTab[1];
230396 memcpy(pTab->abPK, abPK, nCol);
230397 pTab->zName = (char*)&pTab->abPK[nCol];
230398 memcpy(pTab->zName, zNew, nNew+1);
230399
230400 if( pGrp->db ){
230401 pTab->nCol = 0;
230402 rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
230403 if( rc ){
230404 assert( pTab->azCol==0 );
230405 sqlite3_free(pTab);
230406 break;
230407 }
230408 }
230409
230410 /* The new object must be linked on to the end of the list, not
230411 ** simply added to the start of it. This is to ensure that the
230412 ** tables within the output of sqlite3changegroup_output() are in
230413 ** the right order. */
230414 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
230415 *ppTab = pTab;
230416 }
230417
230418 if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
230419 rc = SQLITE_SCHEMA;
230420 break;
230421 }
230422 }
230423
230424 if( nCol<pTab->nCol ){
230425 assert( pGrp->db );
230426 rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, &rec);
230427 if( rc ) break;
230428 aRec = rec.aBuf;
230429 nRec = rec.nBuf;
230430 }
230431
230432 if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
230433 rc = SQLITE_NOMEM;
230434 break;
230435 }
230436 iHash = sessionChangeHash(
230437 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
230438 );
230439
230440 /* Search for existing entry. If found, remove it from the hash table.
230441 ** Code below may link it back in.
230442 */
230443 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
230444 int bPkOnly1 = 0;
230445 int bPkOnly2 = 0;
230446 if( pIter->bPatchset ){
230447 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
@@ -230452,23 +230729,45 @@
230452 *pp = (*pp)->pNext;
230453 pTab->nEntry--;
230454 break;
230455 }
230456 }
 
230457
 
230458 rc = sessionChangeMerge(pTab, bRebase,
230459 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
230460 );
230461 if( rc ) break;
230462 if( pChange ){
230463 pChange->pNext = pTab->apChange[iHash];
230464 pTab->apChange[iHash] = pChange;
230465 pTab->nEntry++;
230466 }
230467 }
230468
230469 sqlite3_free(rec.aBuf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230470 if( rc==SQLITE_OK ) rc = pIter->rc;
230471 return rc;
230472 }
230473
230474 /*
@@ -230591,10 +230890,27 @@
230591 rc = sessionChangesetToHash(pIter, pGrp, 0);
230592 }
230593 sqlite3changeset_finalize(pIter);
230594 return rc;
230595 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230596
230597 /*
230598 ** Obtain a buffer containing a changeset representing the concatenation
230599 ** of all changesets added to the group so far.
230600 */
@@ -230641,10 +230957,11 @@
230641 */
230642 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
230643 if( pGrp ){
230644 sqlite3_free(pGrp->zDb);
230645 sessionDeleteTable(0, pGrp->pList);
 
230646 sqlite3_free(pGrp);
230647 }
230648 }
230649
230650 /*
@@ -231042,10 +231359,11 @@
231042 ** Destroy a rebaser object
231043 */
231044 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
231045 if( p ){
231046 sessionDeleteTable(0, p->grp.pList);
 
231047 sqlite3_free(p);
231048 }
231049 }
231050
231051 /*
@@ -252193,11 +252511,11 @@
252193 int nArg, /* Number of args */
252194 sqlite3_value **apUnused /* Function arguments */
252195 ){
252196 assert( nArg==0 );
252197 UNUSED_PARAM2(nArg, apUnused);
252198 sqlite3_result_text(pCtx, "fts5: 2024-04-18 16:11:01 8c0f69e0e4ae0a446838cc193bfd4395fd251f3c7659b35ac388e5a0a7650a66", -1, SQLITE_TRANSIENT);
252199 }
252200
252201 /*
252202 ** Return true if zName is the extension on one of the shadow tables used
252203 ** by this module.
252204
--- 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 ** 42d67c6fed3a5f21d7b71515aca471ba61d3.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.46.0"
463 #define SQLITE_VERSION_NUMBER 3046000
464 #define SQLITE_SOURCE_ID "2024-05-08 11:51:56 42d67c6fed3a5f21d7b71515aca471ba61d387e620022735a2e7929fa3a237cf"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -12314,10 +12314,34 @@
12314 **
12315 ** In all cases, if an error occurs the state of the final contents of the
12316 ** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12317 */
12318 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12319
12320 /*
12321 ** CAPI3REF: Add A Single Change To A Changegroup
12322 ** METHOD: sqlite3_changegroup
12323 **
12324 ** This function adds the single change currently indicated by the iterator
12325 ** passed as the second argument to the changegroup object. The rules for
12326 ** adding the change are just as described for [sqlite3changegroup_add()].
12327 **
12328 ** If the change is successfully added to the changegroup, SQLITE_OK is
12329 ** returned. Otherwise, an SQLite error code is returned.
12330 **
12331 ** The iterator must point to a valid entry when this function is called.
12332 ** If it does not, SQLITE_ERROR is returned and no change is added to the
12333 ** changegroup. Additionally, the iterator must not have been opened with
12334 ** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12335 ** returned.
12336 */
12337 SQLITE_API int sqlite3changegroup_add_change(
12338 sqlite3_changegroup*,
12339 sqlite3_changeset_iter*
12340 );
12341
12342
12343
12344 /*
12345 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12346 ** METHOD: sqlite3_changegroup
12347 **
@@ -14612,12 +14636,12 @@
14636 #define TK_AGG_FUNCTION 168
14637 #define TK_AGG_COLUMN 169
14638 #define TK_TRUEFALSE 170
14639 #define TK_ISNOT 171
14640 #define TK_FUNCTION 172
14641 #define TK_UPLUS 173
14642 #define TK_UMINUS 174
14643 #define TK_TRUTH 175
14644 #define TK_REGISTER 176
14645 #define TK_VECTOR 177
14646 #define TK_SELECT_COLUMN 178
14647 #define TK_IF_NULL_ROW 179
@@ -31945,11 +31969,11 @@
31969 sqlite3_str_appendall(pAccum, pItem->zName);
31970 }else if( pItem->zAlias ){
31971 sqlite3_str_appendall(pAccum, pItem->zAlias);
31972 }else{
31973 Select *pSel = pItem->pSelect;
31974 assert( pSel!=0 ); /* Because of tag-20240424-1 */
31975 if( pSel->selFlags & SF_NestedFrom ){
31976 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
31977 }else if( pSel->selFlags & SF_MultiValue ){
31978 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy );
31979 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
@@ -32769,16 +32793,18 @@
32793 if( pItem->fg.isUsing ) n++;
32794 if( pItem->fg.isUsing ){
32795 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
32796 }
32797 if( pItem->pSelect ){
32798 sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc);
32799 if( pItem->pTab ){
32800 Table *pTab = pItem->pTab;
32801 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
32802 }
32803 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
32804 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
32805 sqlite3TreeViewPop(&pView);
32806 }
32807 if( pItem->fg.isTabFunc ){
32808 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
32809 }
32810 sqlite3TreeViewPop(&pView);
@@ -32878,11 +32904,11 @@
32904 }
32905 if( p->pLimit ){
32906 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
32907 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
32908 if( p->pLimit->pRight ){
32909 sqlite3TreeViewItem(pView, "OFFSET", 0);
32910 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
32911 sqlite3TreeViewPop(&pView);
32912 }
32913 sqlite3TreeViewPop(&pView);
32914 }
@@ -84677,14 +84703,14 @@
84703 int nRec, /* Size of buffer pRec in bytes */
84704 int iCol, /* Column to extract */
84705 sqlite3_value **ppVal /* OUT: Extracted value */
84706 ){
84707 u32 t = 0; /* a column type code */
84708 u32 nHdr; /* Size of the header in the record */
84709 u32 iHdr; /* Next unread header byte */
84710 i64 iField; /* Next unread data byte */
84711 u32 szField = 0; /* Size of the current data field */
84712 int i; /* Column index */
84713 u8 *a = (u8*)pRec; /* Typecast byte array */
84714 Mem *pMem = *ppVal; /* Write result into this Mem object */
84715
84716 assert( iCol>0 );
@@ -97641,11 +97667,12 @@
97667 ** row output from the sorter so that the row can be decomposed into
97668 ** individual columns using the OP_Column opcode. The OP_Column opcode
97669 ** is the only cursor opcode that works with a pseudo-table.
97670 **
97671 ** P3 is the number of fields in the records that will be stored by
97672 ** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
97673 ** will return NULL for every column.
97674 */
97675 case OP_OpenPseudo: {
97676 VdbeCursor *pCx;
97677
97678 assert( pOp->p1>=0 );
@@ -105799,14 +105826,14 @@
105826 break;
105827 }
105828
105829 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
105830 case 9: /* nexec */
105831 sqlite3_result_int64(ctx, pOp->nExec);
105832 break;
105833 case 10: /* ncycle */
105834 sqlite3_result_int64(ctx, pOp->nCycle);
105835 break;
105836 #else
105837 case 9: /* nexec */
105838 case 10: /* ncycle */
105839 sqlite3_result_int(ctx, 0);
@@ -107195,11 +107222,12 @@
107222 int op = pParse->eTriggerOp;
107223 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
107224 if( pParse->bReturning ){
107225 if( (pNC->ncFlags & NC_UBaseReg)!=0
107226 && ALWAYS(zTab==0
107227 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0
107228 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))
107229 ){
107230 pExpr->iTable = op!=TK_DELETE;
107231 pTab = pParse->pTriggerTab;
107232 }
107233 }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){
@@ -108556,10 +108584,11 @@
108584 /* Recursively resolve names in all subqueries in the FROM clause
108585 */
108586 if( pOuterNC ) pOuterNC->nNestedSelect++;
108587 for(i=0; i<p->pSrc->nSrc; i++){
108588 SrcItem *pItem = &p->pSrc->a[i];
108589 assert( pItem->zName!=0 || pItem->pSelect!=0 );/* Test of tag-20240424-1*/
108590 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108591 int nRef = pOuterNC ? pOuterNC->nRef : 0;
108592 const char *zSavedContext = pParse->zAuthContext;
108593
108594 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
@@ -110315,10 +110344,11 @@
110344 ** Recursively delete an expression tree.
110345 */
110346 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110347 assert( p!=0 );
110348 assert( db!=0 );
110349 exprDeleteRestart:
110350 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
110351 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
110352 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
110353 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
110354 #ifdef SQLITE_DEBUG
@@ -110330,11 +110360,10 @@
110360 }
110361 #endif
110362 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
110363 /* The Expr.x union is never used at the same time as Expr.pRight */
110364 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
 
110365 if( p->pRight ){
110366 assert( !ExprHasProperty(p, EP_WinFunc) );
110367 sqlite3ExprDeleteNN(db, p->pRight);
110368 }else if( ExprUseXSelect(p) ){
110369 assert( !ExprHasProperty(p, EP_WinFunc) );
@@ -110344,10 +110373,23 @@
110373 #ifndef SQLITE_OMIT_WINDOWFUNC
110374 if( ExprHasProperty(p, EP_WinFunc) ){
110375 sqlite3WindowDelete(db, p->y.pWin);
110376 }
110377 #endif
110378 }
110379 if( p->pLeft && p->op!=TK_SELECT_COLUMN ){
110380 Expr *pLeft = p->pLeft;
110381 if( !ExprHasProperty(p, EP_Static)
110382 && !ExprHasProperty(pLeft, EP_Static)
110383 ){
110384 /* Avoid unnecessary recursion on unary operators */
110385 sqlite3DbNNFreeNN(db, p);
110386 p = pLeft;
110387 goto exprDeleteRestart;
110388 }else{
110389 sqlite3ExprDeleteNN(db, pLeft);
110390 }
110391 }
110392 }
110393 if( !ExprHasProperty(p, EP_Static) ){
110394 sqlite3DbNNFreeNN(db, p);
110395 }
@@ -111340,11 +111382,11 @@
111382 || ExprHasProperty(pExpr, EP_WinFunc)
111383 ){
111384 pWalker->eCode = 0;
111385 return WRC_Abort;
111386 }
111387 return WRC_Prune;
111388 }
111389
111390
111391 /*
111392 ** These routines are Walker callbacks used to check expressions to
@@ -124219,13 +124261,10 @@
124261 if( p->tabFlags & TF_HasGenerated ){
124262 sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
124263 sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
124264 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124265 }
 
 
 
124266 }
124267
124268 /* Add the table to the in-memory representation of the database.
124269 */
124270 if( db->init.busy ){
@@ -140351,11 +140390,11 @@
140390 pParse->nMem = 6;
140391
140392 /* Set the maximum error count */
140393 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140394 if( zRight ){
140395 if( sqlite3GetInt32(pValue->z, &mxErr) ){
140396 if( mxErr<=0 ){
140397 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
140398 }
140399 }else{
140400 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
@@ -141559,10 +141598,11 @@
141598 /* Clear all content from pragma virtual table cursor. */
141599 static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
141600 int i;
141601 sqlite3_finalize(pCsr->pPragma);
141602 pCsr->pPragma = 0;
141603 pCsr->iRowid = 0;
141604 for(i=0; i<ArraySize(pCsr->azArg); i++){
141605 sqlite3_free(pCsr->azArg[i]);
141606 pCsr->azArg[i] = 0;
141607 }
141608 }
@@ -145144,21 +145184,26 @@
145184 memset(&sNC, 0, sizeof(sNC));
145185 sNC.pSrcList = pSelect->pSrc;
145186 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
145187 const char *zType;
145188 i64 n;
145189 int m = 0;
145190 Select *pS2 = pSelect;
145191 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
145192 p = a[i].pExpr;
145193 /* pCol->szEst = ... // Column size est for SELECT tables never used */
145194 pCol->affinity = sqlite3ExprAffinity(p);
145195 while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){
145196 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145197 pS2 = pS2->pNext;
145198 pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
145199 }
145200 if( pCol->affinity<=SQLITE_AFF_NONE ){
145201 pCol->affinity = aff;
145202 }
145203 if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){
145204 for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
 
 
145205 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145206 }
145207 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
145208 pCol->affinity = SQLITE_AFF_BLOB;
145209 }else
@@ -152576,10 +152621,76 @@
152621 }
152622 }
152623 }
152624 return pNew;
152625 }
152626
152627 /* If the Expr node is a subquery or an EXISTS operator or an IN operator that
152628 ** uses a subquery, and if the subquery is SF_Correlated, then mark the
152629 ** expression as EP_VarSelect.
152630 */
152631 static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){
152632 UNUSED_PARAMETER(NotUsed);
152633 if( ExprUseXSelect(pExpr)
152634 && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0
152635 ){
152636 testcase( ExprHasProperty(pExpr, EP_VarSelect) );
152637 ExprSetProperty(pExpr, EP_VarSelect);
152638 }
152639 return WRC_Continue;
152640 }
152641
152642
152643 /*
152644 ** If the SELECT references the table pWalker->u.pTab, then do two things:
152645 **
152646 ** (1) Mark the SELECT as as SF_Correlated.
152647 ** (2) Set pWalker->eCode to non-zero so that the caller will know
152648 ** that (1) has happened.
152649 */
152650 static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){
152651 int i;
152652 SrcList *pSrc;
152653 assert( pSelect!=0 );
152654 pSrc = pSelect->pSrc;
152655 assert( pSrc!=0 );
152656 for(i=0; i<pSrc->nSrc; i++){
152657 if( pSrc->a[i].pTab==pWalker->u.pTab ){
152658 testcase( pSelect->selFlags & SF_Correlated );
152659 pSelect->selFlags |= SF_Correlated;
152660 pWalker->eCode = 1;
152661 break;
152662 }
152663 }
152664 return WRC_Continue;
152665 }
152666
152667 /*
152668 ** Scan the expression list that is the argument to RETURNING looking
152669 ** for subqueries that depend on the table which is being modified in the
152670 ** statement that is hosting the RETURNING clause (pTab). Mark all such
152671 ** subqueries as SF_Correlated. If the subqueries are part of an
152672 ** expression, mark the expression as EP_VarSelect.
152673 **
152674 ** https://sqlite.org/forum/forumpost/2c83569ce8945d39
152675 */
152676 static void sqlite3ProcessReturningSubqueries(
152677 ExprList *pEList,
152678 Table *pTab
152679 ){
152680 Walker w;
152681 memset(&w, 0, sizeof(w));
152682 w.xExprCallback = sqlite3ExprWalkNoop;
152683 w.xSelectCallback = sqlite3ReturningSubqueryCorrelated;
152684 w.u.pTab = pTab;
152685 sqlite3WalkExprList(&w, pEList);
152686 if( w.eCode ){
152687 w.xExprCallback = sqlite3ReturningSubqueryVarSelect;
152688 w.xSelectCallback = sqlite3SelectWalkNoop;
152689 sqlite3WalkExprList(&w, pEList);
152690 }
152691 }
152692
152693 /*
152694 ** Generate code for the RETURNING trigger. Unlike other triggers
152695 ** that invoke a subprogram in the bytecode, the code for RETURNING
152696 ** is generated in-line.
@@ -152613,10 +152724,11 @@
152724 memset(&sFrom, 0, sizeof(sFrom));
152725 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
152726 sSelect.pSrc = &sFrom;
152727 sFrom.nSrc = 1;
152728 sFrom.a[0].pTab = pTab;
152729 sFrom.a[0].zName = pTab->zName; /* tag-20240424-1 */
152730 sFrom.a[0].iCursor = -1;
152731 sqlite3SelectPrep(pParse, &sSelect, 0);
152732 if( pParse->nErr==0 ){
152733 assert( db->mallocFailed==0 );
152734 sqlite3GenerateColumnNames(pParse, &sSelect);
@@ -152639,10 +152751,11 @@
152751 && ALWAYS(!db->mallocFailed)
152752 ){
152753 int i;
152754 int nCol = pNew->nExpr;
152755 int reg = pParse->nMem+1;
152756 sqlite3ProcessReturningSubqueries(pNew, pTab);
152757 pParse->nMem += nCol+2;
152758 pReturning->iRetReg = reg;
152759 for(i=0; i<nCol; i++){
152760 Expr *pCol = pNew->a[i].pExpr;
152761 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
@@ -158590,10 +158703,31 @@
158703 }
158704 pLevel->regFilter = 0;
158705 pLevel->addrBrk = 0;
158706 }
158707 }
158708
158709 /*
158710 ** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...)
158711 ** operator. Return true if level pLoop is guaranteed to visit only one
158712 ** row for each key generated for the index.
158713 */
158714 static int whereLoopIsOneRow(WhereLoop *pLoop){
158715 if( pLoop->u.btree.pIndex->onError
158716 && pLoop->nSkip==0
158717 && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
158718 ){
158719 int ii;
158720 for(ii=0; ii<pLoop->u.btree.nEq; ii++){
158721 if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){
158722 return 0;
158723 }
158724 }
158725 return 1;
158726 }
158727 return 0;
158728 }
158729
158730 /*
158731 ** Generate code for the start of the iLevel-th loop in the WHERE clause
158732 ** implementation described by pWInfo.
158733 */
@@ -159338,11 +159472,13 @@
159472 ** a LEFT JOIN: */
159473 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
159474 }
159475
159476 /* Record the instruction used to terminate the loop. */
159477 if( (pLoop->wsFlags & WHERE_ONEROW)
159478 || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
159479 ){
159480 pLevel->op = OP_Noop;
159481 }else if( bRev ){
159482 pLevel->op = OP_Prev;
159483 }else{
159484 pLevel->op = OP_Next;
@@ -159993,16 +160129,15 @@
160129 if( pRight->fg.viaCoroutine ){
160130 sqlite3VdbeAddOp3(
160131 v, OP_Null, 0, pRight->regResult,
160132 pRight->regResult + pRight->pSelect->pEList->nExpr-1
160133 );
160134 }
160135 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
160136 iIdxCur = pWInfo->a[k].iIdxCur;
160137 if( iIdxCur ){
160138 sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
 
160139 }
160140 }
160141 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
160142 mAll |= pLoop->maskSelf;
160143 for(k=0; k<pWC->nTerm; k++){
@@ -161700,10 +161835,11 @@
161835 ** will only be added if each of the child terms passes the
161836 ** (leftCursor==iCsr) test below. */
161837 continue;
161838 }
161839 if( pWC->a[ii].leftCursor!=iCsr ) return;
161840 if( pWC->a[ii].prereqRight!=0 ) return;
161841 }
161842
161843 /* Check condition (5). Return early if it is not met. */
161844 if( pOrderBy ){
161845 for(ii=0; ii<pOrderBy->nExpr; ii++){
@@ -161714,16 +161850,18 @@
161850 }
161851 }
161852
161853 /* All conditions are met. Add the terms to the where-clause object. */
161854 assert( p->pLimit->op==TK_LIMIT );
161855 if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){
 
 
161856 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
161857 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
161858 }
161859 if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){
161860 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
161861 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
161862 }
161863 }
161864 }
161865
161866 /*
161867 ** Initialize a preallocated WhereClause structure.
@@ -162236,10 +162374,46 @@
162374 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
162375 return p;
162376 }
162377 return 0;
162378 }
162379
162380 /*
162381 ** Term pTerm is guaranteed to be a WO_IN term. It may be a component term
162382 ** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)".
162383 ** This function checks to see if the term is compatible with an index
162384 ** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so,
162385 ** it returns a pointer to the name of the collation sequence (e.g. "BINARY"
162386 ** or "NOCASE") used by the comparison in pTerm. If it is not compatible
162387 ** with affinity idxaff, NULL is returned.
162388 */
162389 static SQLITE_NOINLINE const char *indexInAffinityOk(
162390 Parse *pParse,
162391 WhereTerm *pTerm,
162392 u8 idxaff
162393 ){
162394 Expr *pX = pTerm->pExpr;
162395 Expr inexpr;
162396
162397 assert( pTerm->eOperator & WO_IN );
162398
162399 if( sqlite3ExprIsVector(pX->pLeft) ){
162400 int iField = pTerm->u.x.iField - 1;
162401 inexpr.flags = 0;
162402 inexpr.op = TK_EQ;
162403 inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
162404 assert( ExprUseXSelect(pX) );
162405 inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
162406 pX = &inexpr;
162407 }
162408
162409 if( sqlite3IndexAffinityOk(pX, idxaff) ){
162410 CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX);
162411 return pRet ? pRet->zName : sqlite3StrBINARY;
162412 }
162413 return 0;
162414 }
162415
162416 /*
162417 ** Advance to the next WhereTerm that matches according to the criteria
162418 ** established when the pScan object was initialized by whereScanInit().
162419 ** Return NULL if there are no more matching WhereTerms.
@@ -162287,20 +162461,28 @@
162461 }
162462 }
162463 if( (pTerm->eOperator & pScan->opMask)!=0 ){
162464 /* Verify the affinity and collating sequence match */
162465 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
162466 const char *zCollName;
162467 Parse *pParse = pWC->pWInfo->pParse;
162468 pX = pTerm->pExpr;
162469
162470 if( (pTerm->eOperator & WO_IN) ){
162471 zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
162472 if( !zCollName ) continue;
162473 }else{
162474 CollSeq *pColl;
162475 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
162476 continue;
162477 }
162478 assert(pX->pLeft);
162479 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
162480 zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
162481 }
162482
162483 if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
 
 
162484 continue;
162485 }
162486 }
162487 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
162488 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
@@ -165947,10 +166129,25 @@
166129 static int isLimitTerm(WhereTerm *pTerm){
166130 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
166131 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
166132 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
166133 }
166134
166135 /*
166136 ** Return true if the first nCons constraints in the pUsage array are
166137 ** marked as in-use (have argvIndex>0). False otherwise.
166138 */
166139 static int allConstraintsUsed(
166140 struct sqlite3_index_constraint_usage *aUsage,
166141 int nCons
166142 ){
166143 int ii;
166144 for(ii=0; ii<nCons; ii++){
166145 if( aUsage[ii].argvIndex<=0 ) return 0;
166146 }
166147 return 1;
166148 }
166149
166150 /*
166151 ** Argument pIdxInfo is already populated with all constraints that may
166152 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
166153 ** function marks a subset of those constraints usable, invokes the
@@ -166088,17 +166285,24 @@
166285 pIdxInfo->orderByConsumed = 0;
166286 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
166287 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
166288 }
166289
166290 /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
166291 ** terms. And if there are any, they should follow all other terms. */
166292 assert( pbRetryLimit || !isLimitTerm(pTerm) );
166293 assert( !isLimitTerm(pTerm) || i>=nConstraint-2 );
166294 assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) );
166295
166296 if( isLimitTerm(pTerm) && (*pbIn || !allConstraintsUsed(pUsage, i)) ){
166297 /* If there is an IN(...) term handled as an == (separate call to
166298 ** xFilter for each value on the RHS of the IN) and a LIMIT or
166299 ** OFFSET term handled as well, the plan is unusable. Similarly,
166300 ** if there is a LIMIT/OFFSET and there are other unused terms,
166301 ** the plan cannot be used. In these cases set variable *pbRetryLimit
166302 ** to true to tell the caller to retry with LIMIT and OFFSET
166303 ** disabled. */
166304 if( pIdxInfo->needToFreeIdxStr ){
166305 sqlite3_free(pIdxInfo->idxStr);
166306 pIdxInfo->idxStr = 0;
166307 pIdxInfo->needToFreeIdxStr = 0;
166308 }
@@ -167857,10 +168061,62 @@
168061 }
168062 }
168063 nSearch += pLoop->nOut;
168064 }
168065 }
168066
168067 /*
168068 ** Expression Node callback for sqlite3ExprCanReturnSubtype().
168069 **
168070 ** Only a function call is able to return a subtype. So if the node
168071 ** is not a function call, return WRC_Prune immediately.
168072 **
168073 ** A function call is able to return a subtype if it has the
168074 ** SQLITE_RESULT_SUBTYPE property.
168075 **
168076 ** Assume that every function is able to pass-through a subtype from
168077 ** one of its argument (using sqlite3_result_value()). Most functions
168078 ** are not this way, but we don't have a mechanism to distinguish those
168079 ** that are from those that are not, so assume they all work this way.
168080 ** That means that if one of its arguments is another function and that
168081 ** other function is able to return a subtype, then this function is
168082 ** able to return a subtype.
168083 */
168084 static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){
168085 int n;
168086 FuncDef *pDef;
168087 sqlite3 *db;
168088 if( pExpr->op!=TK_FUNCTION ){
168089 return WRC_Prune;
168090 }
168091 assert( ExprUseXList(pExpr) );
168092 db = pWalker->pParse->db;
168093 n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
168094 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
168095 if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
168096 pWalker->eCode = 1;
168097 return WRC_Prune;
168098 }
168099 return WRC_Continue;
168100 }
168101
168102 /*
168103 ** Return TRUE if expression pExpr is able to return a subtype.
168104 **
168105 ** A TRUE return does not guarantee that a subtype will be returned.
168106 ** It only indicates that a subtype return is possible. False positives
168107 ** are acceptable as they only disable an optimization. False negatives,
168108 ** on the other hand, can lead to incorrect answers.
168109 */
168110 static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){
168111 Walker w;
168112 memset(&w, 0, sizeof(w));
168113 w.pParse = pParse;
168114 w.xExprCallback = exprNodeCanReturnSubtype;
168115 sqlite3WalkExpr(&w, pExpr);
168116 return w.eCode;
168117 }
168118
168119 /*
168120 ** The index pIdx is used by a query and contains one or more expressions.
168121 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
168122 ** number for the index and iDataCur is the cursor number for the corresponding
@@ -167891,23 +168147,15 @@
168147 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
168148 }else{
168149 continue;
168150 }
168151 if( sqlite3ExprIsConstant(0,pExpr) ) continue;
168152 if( pExpr->op==TK_FUNCTION && sqlite3ExprCanReturnSubtype(pParse,pExpr) ){
168153 /* Functions that might set a subtype should not be replaced by the
168154 ** value taken from an expression index since the index omits the
168155 ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
168156 continue;
 
 
 
 
 
 
 
 
168157 }
168158 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
168159 if( p==0 ) break;
168160 p->pIENext = pParse->pIdxEpr;
168161 #ifdef WHERETRACE_ENABLED
@@ -168883,13 +169131,12 @@
169131 int m, n;
169132 n = pSrc->regResult;
169133 assert( pSrc->pTab!=0 );
169134 m = pSrc->pTab->nCol;
169135 sqlite3VdbeAddOp3(v, OP_Null, 0, n, n+m-1);
 
 
169136 }
169137 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
169138 }
169139 if( (ws & WHERE_INDEXED)
169140 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
169141 ){
169142 if( ws & WHERE_MULTI_OR ){
@@ -172588,12 +172835,12 @@
172835 #define TK_AGG_FUNCTION 168
172836 #define TK_AGG_COLUMN 169
172837 #define TK_TRUEFALSE 170
172838 #define TK_ISNOT 171
172839 #define TK_FUNCTION 172
172840 #define TK_UPLUS 173
172841 #define TK_UMINUS 174
172842 #define TK_TRUTH 175
172843 #define TK_REGISTER 176
172844 #define TK_VECTOR 177
172845 #define TK_SELECT_COLUMN 178
172846 #define TK_IF_NULL_ROW 179
@@ -173620,12 +173867,12 @@
173867 0, /* AGG_FUNCTION => nothing */
173868 0, /* AGG_COLUMN => nothing */
173869 0, /* TRUEFALSE => nothing */
173870 0, /* ISNOT => nothing */
173871 0, /* FUNCTION => nothing */
 
173872 0, /* UPLUS => nothing */
173873 0, /* UMINUS => nothing */
173874 0, /* TRUTH => nothing */
173875 0, /* REGISTER => nothing */
173876 0, /* VECTOR => nothing */
173877 0, /* SELECT_COLUMN => nothing */
173878 0, /* IF_NULL_ROW => nothing */
@@ -173889,12 +174136,12 @@
174136 /* 168 */ "AGG_FUNCTION",
174137 /* 169 */ "AGG_COLUMN",
174138 /* 170 */ "TRUEFALSE",
174139 /* 171 */ "ISNOT",
174140 /* 172 */ "FUNCTION",
174141 /* 173 */ "UPLUS",
174142 /* 174 */ "UMINUS",
174143 /* 175 */ "TRUTH",
174144 /* 176 */ "REGISTER",
174145 /* 177 */ "VECTOR",
174146 /* 178 */ "SELECT_COLUMN",
174147 /* 179 */ "IF_NULL_ROW",
@@ -176751,12 +176998,21 @@
176998 case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
176999 {yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy454, 0);/*A-overwrites-B*/}
177000 break;
177001 case 216: /* expr ::= PLUS|MINUS expr */
177002 {
177003 Expr *p = yymsp[0].minor.yy454;
177004 u8 op = yymsp[-1].major + (TK_UPLUS-TK_PLUS);
177005 assert( TK_UPLUS>TK_PLUS );
177006 assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) );
177007 if( p && p->op==TK_UPLUS ){
177008 p->op = op;
177009 yymsp[-1].minor.yy454 = p;
177010 }else{
177011 yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, op, p, 0);
177012 /*A-overwrites-B*/
177013 }
177014 }
177015 break;
177016 case 217: /* expr ::= expr PTR expr */
177017 {
177018 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy454);
@@ -228296,18 +228552,18 @@
228552 ** sufficient either for the 'T' or 'P' byte and the varint that follows
228553 ** it, or for the two single byte values otherwise. */
228554 p->rc = sessionInputBuffer(&p->in, 2);
228555 if( p->rc!=SQLITE_OK ) return p->rc;
228556
228557 sessionDiscardData(&p->in);
228558 p->in.iCurrent = p->in.iNext;
228559
228560 /* If the iterator is already at the end of the changeset, return DONE. */
228561 if( p->in.iNext>=p->in.nData ){
228562 return SQLITE_DONE;
228563 }
228564
 
 
 
228565 op = p->in.aData[p->in.iNext++];
228566 while( op=='T' || op=='P' ){
228567 if( pbNew ) *pbNew = 1;
228568 p->bPatchset = (op=='P');
228569 if( sessionChangesetReadTblhdr(p) ) return p->rc;
@@ -230038,10 +230294,11 @@
230294 */
230295 struct sqlite3_changegroup {
230296 int rc; /* Error code */
230297 int bPatch; /* True to accumulate patchsets */
230298 SessionTable *pList; /* List of tables in current patch */
230299 SessionBuffer rec;
230300
230301 sqlite3 *db; /* Configured by changegroup_schema() */
230302 char *zDb; /* Configured by changegroup_schema() */
230303 };
230304
@@ -230336,112 +230593,132 @@
230593
230594 return rc;
230595 }
230596
230597 /*
230598 ** Locate or create a SessionTable object that may be used to add the
230599 ** change currently pointed to by iterator pIter to changegroup pGrp.
230600 ** If successful, set output variable (*ppTab) to point to the table
230601 ** object and return SQLITE_OK. Otherwise, if some error occurs, return
230602 ** an SQLite error code and leave (*ppTab) set to NULL.
230603 */
230604 static int sessionChangesetFindTable(
230605 sqlite3_changegroup *pGrp,
230606 const char *zTab,
230607 sqlite3_changeset_iter *pIter,
230608 SessionTable **ppTab
230609 ){
 
 
230610 int rc = SQLITE_OK;
230611 SessionTable *pTab = 0;
230612 int nTab = (int)strlen(zTab);
230613 u8 *abPK = 0;
230614 int nCol = 0;
230615
230616 *ppTab = 0;
230617 sqlite3changeset_pk(pIter, &abPK, &nCol);
230618
230619 /* Search the list for an existing table */
230620 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
230621 if( 0==sqlite3_strnicmp(pTab->zName, zTab, nTab+1) ) break;
230622 }
230623
230624 /* If one was not found above, create a new table now */
230625 if( !pTab ){
230626 SessionTable **ppNew;
230627
230628 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nTab+1);
230629 if( !pTab ){
230630 return SQLITE_NOMEM;
230631 }
230632 memset(pTab, 0, sizeof(SessionTable));
230633 pTab->nCol = nCol;
230634 pTab->abPK = (u8*)&pTab[1];
230635 memcpy(pTab->abPK, abPK, nCol);
230636 pTab->zName = (char*)&pTab->abPK[nCol];
230637 memcpy(pTab->zName, zTab, nTab+1);
230638
230639 if( pGrp->db ){
230640 pTab->nCol = 0;
230641 rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
230642 if( rc ){
230643 assert( pTab->azCol==0 );
230644 sqlite3_free(pTab);
230645 return rc;
230646 }
230647 }
230648
230649 /* The new object must be linked on to the end of the list, not
230650 ** simply added to the start of it. This is to ensure that the
230651 ** tables within the output of sqlite3changegroup_output() are in
230652 ** the right order. */
230653 for(ppNew=&pGrp->pList; *ppNew; ppNew=&(*ppNew)->pNext);
230654 *ppNew = pTab;
230655 }
230656
230657 /* Check that the table is compatible. */
230658 if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
230659 rc = SQLITE_SCHEMA;
230660 }
230661
230662 *ppTab = pTab;
230663 return rc;
230664 }
230665
230666 /*
230667 ** Add the change currently indicated by iterator pIter to the hash table
230668 ** belonging to changegroup pGrp.
230669 */
230670 static int sessionOneChangeToHash(
230671 sqlite3_changegroup *pGrp,
230672 sqlite3_changeset_iter *pIter,
230673 int bRebase
230674 ){
230675 int rc = SQLITE_OK;
230676 int nCol = 0;
230677 int op = 0;
230678 int iHash = 0;
230679 int bIndirect = 0;
230680 SessionChange *pChange = 0;
230681 SessionChange *pExist = 0;
230682 SessionChange **pp = 0;
230683 SessionTable *pTab = 0;
230684 u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
230685 int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
230686
230687 /* Ensure that only changesets, or only patchsets, but not a mixture
230688 ** of both, are being combined. It is an error to try to combine a
230689 ** changeset and a patchset. */
230690 if( pGrp->pList==0 ){
230691 pGrp->bPatch = pIter->bPatchset;
230692 }else if( pIter->bPatchset!=pGrp->bPatch ){
230693 rc = SQLITE_ERROR;
230694 }
230695
230696 if( rc==SQLITE_OK ){
230697 const char *zTab = 0;
230698 sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect);
230699 rc = sessionChangesetFindTable(pGrp, zTab, pIter, &pTab);
230700 }
230701
230702 if( rc==SQLITE_OK && nCol<pTab->nCol ){
230703 SessionBuffer *pBuf = &pGrp->rec;
230704 rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, pBuf);
230705 aRec = pBuf->aBuf;
230706 nRec = pBuf->nBuf;
230707 assert( pGrp->db );
230708 }
230709
230710 if( rc==SQLITE_OK && sessionGrowHash(0, pIter->bPatchset, pTab) ){
230711 rc = SQLITE_NOMEM;
230712 }
230713
230714 if( rc==SQLITE_OK ){
230715 /* Search for existing entry. If found, remove it from the hash table.
230716 ** Code below may link it back in. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230717 iHash = sessionChangeHash(
230718 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
230719 );
 
 
 
 
230720 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
230721 int bPkOnly1 = 0;
230722 int bPkOnly2 = 0;
230723 if( pIter->bPatchset ){
230724 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
@@ -230452,23 +230729,45 @@
230729 *pp = (*pp)->pNext;
230730 pTab->nEntry--;
230731 break;
230732 }
230733 }
230734 }
230735
230736 if( rc==SQLITE_OK ){
230737 rc = sessionChangeMerge(pTab, bRebase,
230738 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
230739 );
230740 }
230741 if( rc==SQLITE_OK && pChange ){
230742 pChange->pNext = pTab->apChange[iHash];
230743 pTab->apChange[iHash] = pChange;
230744 pTab->nEntry++;
 
230745 }
230746
230747 if( rc==SQLITE_OK ) rc = pIter->rc;
230748 return rc;
230749 }
230750
230751 /*
230752 ** Add all changes in the changeset traversed by the iterator passed as
230753 ** the first argument to the changegroup hash tables.
230754 */
230755 static int sessionChangesetToHash(
230756 sqlite3_changeset_iter *pIter, /* Iterator to read from */
230757 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
230758 int bRebase /* True if hash table is for rebasing */
230759 ){
230760 u8 *aRec;
230761 int nRec;
230762 int rc = SQLITE_OK;
230763
230764 while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
230765 rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
230766 if( rc!=SQLITE_OK ) break;
230767 }
230768
230769 if( rc==SQLITE_OK ) rc = pIter->rc;
230770 return rc;
230771 }
230772
230773 /*
@@ -230591,10 +230890,27 @@
230890 rc = sessionChangesetToHash(pIter, pGrp, 0);
230891 }
230892 sqlite3changeset_finalize(pIter);
230893 return rc;
230894 }
230895
230896 /*
230897 ** Add a single change to a changeset-group.
230898 */
230899 SQLITE_API int sqlite3changegroup_add_change(
230900 sqlite3_changegroup *pGrp,
230901 sqlite3_changeset_iter *pIter
230902 ){
230903 if( pIter->in.iCurrent==pIter->in.iNext
230904 || pIter->rc!=SQLITE_OK
230905 || pIter->bInvert
230906 ){
230907 /* Iterator does not point to any valid entry or is an INVERT iterator. */
230908 return SQLITE_ERROR;
230909 }
230910 return sessionOneChangeToHash(pGrp, pIter, 0);
230911 }
230912
230913 /*
230914 ** Obtain a buffer containing a changeset representing the concatenation
230915 ** of all changesets added to the group so far.
230916 */
@@ -230641,10 +230957,11 @@
230957 */
230958 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
230959 if( pGrp ){
230960 sqlite3_free(pGrp->zDb);
230961 sessionDeleteTable(0, pGrp->pList);
230962 sqlite3_free(pGrp->rec.aBuf);
230963 sqlite3_free(pGrp);
230964 }
230965 }
230966
230967 /*
@@ -231042,10 +231359,11 @@
231359 ** Destroy a rebaser object
231360 */
231361 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
231362 if( p ){
231363 sessionDeleteTable(0, p->grp.pList);
231364 sqlite3_free(p->grp.rec.aBuf);
231365 sqlite3_free(p);
231366 }
231367 }
231368
231369 /*
@@ -252193,11 +252511,11 @@
252511 int nArg, /* Number of args */
252512 sqlite3_value **apUnused /* Function arguments */
252513 ){
252514 assert( nArg==0 );
252515 UNUSED_PARAM2(nArg, apUnused);
252516 sqlite3_result_text(pCtx, "fts5: 2024-05-08 11:51:56 42d67c6fed3a5f21d7b71515aca471ba61d387e620022735a2e7929fa3a237cf", -1, SQLITE_TRANSIENT);
252517 }
252518
252519 /*
252520 ** Return true if zName is the extension on one of the shadow tables used
252521 ** by this module.
252522
+25 -1
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.46.0"
150150
#define SQLITE_VERSION_NUMBER 3046000
151
-#define SQLITE_SOURCE_ID "2024-04-18 16:11:01 8c0f69e0e4ae0a446838cc193bfd4395fd251f3c7659b35ac388e5a0a7650a66"
151
+#define SQLITE_SOURCE_ID "2024-05-08 11:51:56 42d67c6fed3a5f21d7b71515aca471ba61d387e620022735a2e7929fa3a237cf"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -12001,10 +12001,34 @@
1200112001
**
1200212002
** In all cases, if an error occurs the state of the final contents of the
1200312003
** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
1200412004
*/
1200512005
SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12006
+
12007
+/*
12008
+** CAPI3REF: Add A Single Change To A Changegroup
12009
+** METHOD: sqlite3_changegroup
12010
+**
12011
+** This function adds the single change currently indicated by the iterator
12012
+** passed as the second argument to the changegroup object. The rules for
12013
+** adding the change are just as described for [sqlite3changegroup_add()].
12014
+**
12015
+** If the change is successfully added to the changegroup, SQLITE_OK is
12016
+** returned. Otherwise, an SQLite error code is returned.
12017
+**
12018
+** The iterator must point to a valid entry when this function is called.
12019
+** If it does not, SQLITE_ERROR is returned and no change is added to the
12020
+** changegroup. Additionally, the iterator must not have been opened with
12021
+** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12022
+** returned.
12023
+*/
12024
+SQLITE_API int sqlite3changegroup_add_change(
12025
+ sqlite3_changegroup*,
12026
+ sqlite3_changeset_iter*
12027
+);
12028
+
12029
+
1200612030
1200712031
/*
1200812032
** CAPI3REF: Obtain A Composite Changeset From A Changegroup
1200912033
** METHOD: sqlite3_changegroup
1201012034
**
1201112035
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.46.0"
150 #define SQLITE_VERSION_NUMBER 3046000
151 #define SQLITE_SOURCE_ID "2024-04-18 16:11:01 8c0f69e0e4ae0a446838cc193bfd4395fd251f3c7659b35ac388e5a0a7650a66"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -12001,10 +12001,34 @@
12001 **
12002 ** In all cases, if an error occurs the state of the final contents of the
12003 ** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12004 */
12005 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12006
12007 /*
12008 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12009 ** METHOD: sqlite3_changegroup
12010 **
12011
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.46.0"
150 #define SQLITE_VERSION_NUMBER 3046000
151 #define SQLITE_SOURCE_ID "2024-05-08 11:51:56 42d67c6fed3a5f21d7b71515aca471ba61d387e620022735a2e7929fa3a237cf"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -12001,10 +12001,34 @@
12001 **
12002 ** In all cases, if an error occurs the state of the final contents of the
12003 ** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12004 */
12005 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12006
12007 /*
12008 ** CAPI3REF: Add A Single Change To A Changegroup
12009 ** METHOD: sqlite3_changegroup
12010 **
12011 ** This function adds the single change currently indicated by the iterator
12012 ** passed as the second argument to the changegroup object. The rules for
12013 ** adding the change are just as described for [sqlite3changegroup_add()].
12014 **
12015 ** If the change is successfully added to the changegroup, SQLITE_OK is
12016 ** returned. Otherwise, an SQLite error code is returned.
12017 **
12018 ** The iterator must point to a valid entry when this function is called.
12019 ** If it does not, SQLITE_ERROR is returned and no change is added to the
12020 ** changegroup. Additionally, the iterator must not have been opened with
12021 ** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12022 ** returned.
12023 */
12024 SQLITE_API int sqlite3changegroup_add_change(
12025 sqlite3_changegroup*,
12026 sqlite3_changeset_iter*
12027 );
12028
12029
12030
12031 /*
12032 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12033 ** METHOD: sqlite3_changegroup
12034 **
12035

Keyboard Shortcuts

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