Fossil SCM

Update the built-in SQLite to the latest 3.35.0 alpha version.

drh 2020-12-30 20:52 trunk
Commit 00e031bb578e5cb111a380431039555da665633308b72b64f3defaa2bdbc1432
3 files changed +8 -6 +230 -97 +10 -1
+8 -6
--- src/shell.c
+++ src/shell.c
@@ -17903,20 +17903,21 @@
1790317903
static const struct {
1790417904
const char *zCtrlName; /* Name of a test-control option */
1790517905
int ctrlCode; /* Integer code for that option */
1790617906
const char *zUsage; /* Usage notes */
1790717907
} aCtrl[] = {
17908
- { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
1790917908
{ "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
17910
- /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
17911
- { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
17912
- { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
17913
- /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
17914
- { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
17909
+ { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
1791517910
{ "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
1791617911
{ "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
17912
+ { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
17913
+ /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
17914
+ { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
1791717915
{ "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
17916
+ { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
17917
+ { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
17918
+ /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
1791817919
};
1791917920
int filectrl = -1;
1792017921
int iCtrl = -1;
1792117922
sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
1792217923
int isOk = 0; /* 0: usage 1: %lld 2: no-result */
@@ -17999,10 +18000,11 @@
1799918000
sqlite3_file_control(p->db, zSchema, filectrl, &x);
1800018001
iRes = x;
1800118002
isOk = 1;
1800218003
break;
1800318004
}
18005
+ case SQLITE_FCNTL_DATA_VERSION:
1800418006
case SQLITE_FCNTL_HAS_MOVED: {
1800518007
int x;
1800618008
if( nArg!=2 ) break;
1800718009
sqlite3_file_control(p->db, zSchema, filectrl, &x);
1800818010
iRes = x;
1800918011
--- src/shell.c
+++ src/shell.c
@@ -17903,20 +17903,21 @@
17903 static const struct {
17904 const char *zCtrlName; /* Name of a test-control option */
17905 int ctrlCode; /* Integer code for that option */
17906 const char *zUsage; /* Usage notes */
17907 } aCtrl[] = {
17908 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
17909 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
17910 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
17911 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
17912 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
17913 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
17914 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
17915 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
17916 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
 
 
 
17917 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
 
 
 
17918 };
17919 int filectrl = -1;
17920 int iCtrl = -1;
17921 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
17922 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
@@ -17999,10 +18000,11 @@
17999 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18000 iRes = x;
18001 isOk = 1;
18002 break;
18003 }
 
18004 case SQLITE_FCNTL_HAS_MOVED: {
18005 int x;
18006 if( nArg!=2 ) break;
18007 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18008 iRes = x;
18009
--- src/shell.c
+++ src/shell.c
@@ -17903,20 +17903,21 @@
17903 static const struct {
17904 const char *zCtrlName; /* Name of a test-control option */
17905 int ctrlCode; /* Integer code for that option */
17906 const char *zUsage; /* Usage notes */
17907 } aCtrl[] = {
 
17908 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
17909 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
 
 
 
 
17910 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
17911 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
17912 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
17913 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
17914 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
17915 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
17916 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
17917 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
17918 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
17919 };
17920 int filectrl = -1;
17921 int iCtrl = -1;
17922 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
17923 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
@@ -17999,10 +18000,11 @@
18000 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18001 iRes = x;
18002 isOk = 1;
18003 break;
18004 }
18005 case SQLITE_FCNTL_DATA_VERSION:
18006 case SQLITE_FCNTL_HAS_MOVED: {
18007 int x;
18008 if( nArg!=2 ) break;
18009 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18010 iRes = x;
18011
+230 -97
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -283,11 +283,11 @@
283283
#endif
284284
#ifdef SQLITE_ENABLE_LOCKING_STYLE
285285
"ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
286286
#endif
287287
#if SQLITE_ENABLE_MATH_FUNCTIONS
288
- "ENABLE_MATH_FUNCTIONS"
288
+ "ENABLE_MATH_FUNCTIONS",
289289
#endif
290290
#if SQLITE_ENABLE_MEMORY_MANAGEMENT
291291
"ENABLE_MEMORY_MANAGEMENT",
292292
#endif
293293
#if SQLITE_ENABLE_MEMSYS3
@@ -1186,11 +1186,11 @@
11861186
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11871187
** [sqlite_version()] and [sqlite_source_id()].
11881188
*/
11891189
#define SQLITE_VERSION "3.35.0"
11901190
#define SQLITE_VERSION_NUMBER 3035000
1191
-#define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660"
1191
+#define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f"
11921192
11931193
/*
11941194
** CAPI3REF: Run-Time Library Version Numbers
11951195
** KEYWORDS: sqlite3_version sqlite3_sourceid
11961196
**
@@ -4560,10 +4560,11 @@
45604560
** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
45614561
** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
45624562
** that uses dot-files in place of posix advisory locking.
45634563
** <tr><td> file:data.db?mode=readonly <td>
45644564
** An error. "readonly" is not a valid option for the "mode" parameter.
4565
+** Use "ro" instead: "file:data.db?mode=ro".
45654566
** </table>
45664567
**
45674568
** ^URI hexadecimal escape sequences (%HH) are supported within the path and
45684569
** query components of a URI. A hexadecimal escape sequence consists of a
45694570
** percent sign - "%" - followed by exactly two hexadecimal digits
@@ -11500,10 +11501,18 @@
1150011501
** guaranteed that a call to sqlite3session_changeset() will return a
1150111502
** changeset containing zero changes.
1150211503
*/
1150311504
SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
1150411505
11506
+/*
11507
+** CAPI3REF: Query for the amount of heap memory used by a session object.
11508
+**
11509
+** This API returns the total amount of heap memory in bytes currently
11510
+** used by the session object passed as the only argument.
11511
+*/
11512
+SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
11513
+
1150511514
/*
1150611515
** CAPI3REF: Create An Iterator To Traverse A Changeset
1150711516
** CONSTRUCTOR: sqlite3_changeset_iter
1150811517
**
1150911518
** Create an iterator used to iterate through the contents of a changeset.
@@ -14626,13 +14635,13 @@
1462614635
#endif
1462714636
1462814637
/*
1462914638
** Macros for "wheretrace"
1463014639
*/
14640
+SQLITE_PRIVATE u32 sqlite3WhereTrace;
1463114641
#if defined(SQLITE_DEBUG) \
1463214642
&& (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
14633
-SQLITE_PRIVATE u32 sqlite3WhereTrace;
1463414643
# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
1463514644
# define WHERETRACE_ENABLED 1
1463614645
#else
1463714646
# define WHERETRACE(K,X)
1463814647
#endif
@@ -18158,11 +18167,11 @@
1815818167
** TK_SELECT: 1st register of result vector */
1815918168
ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
1816018169
** TK_VARIABLE: variable number (always >= 1).
1816118170
** TK_SELECT_COLUMN: column of the result vector */
1816218171
i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18163
- i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
18172
+ int iRightJoinTable; /* If EP_FromJoin, the right table of the join */
1816418173
AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
1816518174
union {
1816618175
Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
1816718176
** for a column of an index on an expression */
1816818177
Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
@@ -19361,11 +19370,10 @@
1936119370
SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
1936219371
SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*, int);
1936319372
SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
1936419373
SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
1936519374
SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
19366
-SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
1936719375
SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
1936819376
SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
1936919377
SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
1937019378
SQLITE_PRIVATE void sqlite3WindowFunctions(void);
1937119379
SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
@@ -20098,10 +20106,11 @@
2009820106
SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
2009920107
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
2010020108
SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
2010120109
SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
2010220110
SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
20111
+SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
2010320112
SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
2010420113
SQLITE_PRIVATE int sqlite3MatchEName(
2010520114
const struct ExprList_item*,
2010620115
const char*,
2010720116
const char*,
@@ -73754,10 +73763,13 @@
7375473763
getCellInfo(pSrc);
7375573764
aOut += putVarint32(aOut, pSrc->info.nPayload);
7375673765
if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
7375773766
nIn = pSrc->info.nLocal;
7375873767
aIn = pSrc->info.pPayload;
73768
+ if( aIn+nIn>pSrc->pPage->aDataEnd ){
73769
+ return SQLITE_CORRUPT_BKPT;
73770
+ }
7375973771
nRem = pSrc->info.nPayload;
7376073772
if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
7376173773
memcpy(aOut, aIn, nIn);
7376273774
pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
7376373775
}else{
@@ -73774,10 +73786,13 @@
7377473786
pPgnoOut = &aOut[nOut];
7377573787
pBt->nPreformatSize += 4;
7377673788
}
7377773789
7377873790
if( nRem>nIn ){
73791
+ if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
73792
+ return SQLITE_CORRUPT_BKPT;
73793
+ }
7377973794
ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
7378073795
}
7378173796
7378273797
do {
7378373798
nRem -= nOut;
@@ -85613,11 +85628,11 @@
8561385628
testcase( zRawSql[0]=='#' );
8561485629
idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
8561585630
assert( idx>0 );
8561685631
}
8561785632
zRawSql += nToken;
85618
- nextIndex = idx + 1;
85633
+ nextIndex = MAX(idx + 1, nextIndex);
8561985634
assert( idx>0 && idx<=p->nVar );
8562085635
pVar = &p->aVar[idx-1];
8562185636
if( pVar->flags & MEM_Null ){
8562285637
sqlite3_str_append(&out, "NULL", 4);
8562385638
}else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
@@ -90934,10 +90949,12 @@
9093490949
case OP_RowCell: {
9093590950
VdbeCursor *pDest; /* Cursor to write to */
9093690951
VdbeCursor *pSrc; /* Cursor to read from */
9093790952
i64 iKey; /* Rowid value to insert with */
9093890953
assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
90954
+ assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
90955
+ assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
9093990956
assert( pOp[1].p5 & OPFLAG_PREFORMAT );
9094090957
pDest = p->apCsr[pOp->p1];
9094190958
pSrc = p->apCsr[pOp->p2];
9094290959
iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
9094390960
rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
@@ -98200,11 +98217,11 @@
9820098217
#if !defined(SQLITE_OMIT_WINDOWFUNC)
9820198218
/*
9820298219
** Walk all expressions linked into the list of Window objects passed
9820398220
** as the second argument.
9820498221
*/
98205
-static int walkWindowList(Walker *pWalker, Window *pList){
98222
+static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
9820698223
Window *pWin;
9820798224
for(pWin=pList; pWin; pWin=pWin->pNextWin){
9820898225
int rc;
9820998226
rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
9821098227
if( rc ) return WRC_Abort;
@@ -98219,10 +98236,11 @@
9821998236
** not matter anyhow. */
9822098237
rc = sqlite3WalkExpr(pWalker, pWin->pStart);
9822198238
if( NEVER(rc) ) return WRC_Abort;
9822298239
rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
9822398240
if( NEVER(rc) ) return WRC_Abort;
98241
+ if( bOneOnly ) break;
9822498242
}
9822598243
return WRC_Continue;
9822698244
}
9822798245
#endif
9822898246
@@ -98266,11 +98284,11 @@
9826698284
if( pExpr->x.pList ){
9826798285
if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
9826898286
}
9826998287
#ifndef SQLITE_OMIT_WINDOWFUNC
9827098288
if( ExprHasProperty(pExpr, EP_WinFunc) ){
98271
- if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
98289
+ if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
9827298290
}
9827398291
#endif
9827498292
}
9827598293
}
9827698294
break;
@@ -98313,11 +98331,11 @@
9831398331
{
9831498332
Parse *pParse = pWalker->pParse;
9831598333
if( pParse && IN_RENAME_OBJECT ){
9831698334
/* The following may return WRC_Abort if there are unresolvable
9831798335
** symbols (e.g. a table that does not exist) in a window definition. */
98318
- int rc = walkWindowList(pWalker, p->pWinDefn);
98336
+ int rc = walkWindowList(pWalker, p->pWinDefn, 0);
9831998337
return rc;
9832098338
}
9832198339
}
9832298340
#endif
9832398341
return WRC_Continue;
@@ -123431,11 +123449,11 @@
123431123449
** to some other UNIQUE constraint is FAIL or IGNORE, then we need
123432123450
** to defer the running of the rowid conflict checking until after
123433123451
** the UNIQUE constraints have run.
123434123452
*/
123435123453
if( onError==OE_Replace /* IPK rule is REPLACE */
123436
- && onError!=overrideError /* Rules for other contraints are different */
123454
+ && onError!=overrideError /* Rules for other constraints are different */
123437123455
&& pTab->pIndex /* There exist other constraints */
123438123456
){
123439123457
ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
123440123458
VdbeComment((v, "defer IPK REPLACE until last"));
123441123459
}
@@ -124484,11 +124502,11 @@
124484124502
if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
124485124503
}
124486124504
if( i==pSrcIdx->nColumn ){
124487124505
idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
124488124506
sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
124489
- sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regData);
124507
+ sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc);
124490124508
}
124491124509
}else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
124492124510
idxInsFlags |= OPFLAG_NCHANGE;
124493124511
}
124494124512
if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
@@ -130742,11 +130760,11 @@
130742130760
pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
130743130761
if( pEq && isOuterJoin ){
130744130762
ExprSetProperty(pEq, EP_FromJoin);
130745130763
assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
130746130764
ExprSetVVAProperty(pEq, EP_NoReduce);
130747
- pEq->iRightJoinTable = (i16)pE2->iTable;
130765
+ pEq->iRightJoinTable = pE2->iTable;
130748130766
}
130749130767
*ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
130750130768
}
130751130769
130752130770
/*
@@ -130778,11 +130796,11 @@
130778130796
SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){
130779130797
while( p ){
130780130798
ExprSetProperty(p, EP_FromJoin);
130781130799
assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
130782130800
ExprSetVVAProperty(p, EP_NoReduce);
130783
- p->iRightJoinTable = (i16)iTable;
130801
+ p->iRightJoinTable = iTable;
130784130802
if( p->op==TK_FUNCTION && p->x.pList ){
130785130803
int i;
130786130804
for(i=0; i<p->x.pList->nExpr; i++){
130787130805
sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
130788130806
}
@@ -134044,10 +134062,92 @@
134044134062
pSrcItem->colUsed = 0;
134045134063
sqlite3WalkSelect(&w, pSelect);
134046134064
}
134047134065
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
134048134066
134067
+#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134068
+/*
134069
+** Assign new cursor numbers to each of the items in pSrc. For each
134070
+** new cursor number assigned, set an entry in the aCsrMap[] array
134071
+** to map the old cursor number to the new:
134072
+**
134073
+** aCsrMap[iOld] = iNew;
134074
+**
134075
+** The array is guaranteed by the caller to be large enough for all
134076
+** existing cursor numbers in pSrc.
134077
+**
134078
+** If pSrc contains any sub-selects, call this routine recursively
134079
+** on the FROM clause of each such sub-select, with iExcept set to -1.
134080
+*/
134081
+static void srclistRenumberCursors(
134082
+ Parse *pParse, /* Parse context */
134083
+ int *aCsrMap, /* Array to store cursor mappings in */
134084
+ SrcList *pSrc, /* FROM clause to renumber */
134085
+ int iExcept /* FROM clause item to skip */
134086
+){
134087
+ int i;
134088
+ struct SrcList_item *pItem;
134089
+ for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
134090
+ if( i!=iExcept ){
134091
+ Select *p;
134092
+ pItem->iCursor = aCsrMap[pItem->iCursor] = pParse->nTab++;
134093
+ for(p=pItem->pSelect; p; p=p->pPrior){
134094
+ srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
134095
+ }
134096
+ }
134097
+ }
134098
+}
134099
+
134100
+/*
134101
+** Expression walker callback used by renumberCursors() to update
134102
+** Expr objects to match newly assigned cursor numbers.
134103
+*/
134104
+static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
134105
+ int *aCsrMap = pWalker->u.aiCol;
134106
+ if( pExpr->op==TK_COLUMN && aCsrMap[pExpr->iTable] ){
134107
+ pExpr->iTable = aCsrMap[pExpr->iTable];
134108
+ }
134109
+ if( ExprHasProperty(pExpr, EP_FromJoin) && aCsrMap[pExpr->iRightJoinTable] ){
134110
+ pExpr->iRightJoinTable = aCsrMap[pExpr->iRightJoinTable];
134111
+ }
134112
+ return WRC_Continue;
134113
+}
134114
+
134115
+/*
134116
+** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
134117
+** of the SELECT statement passed as the second argument, and to each
134118
+** cursor in the FROM clause of any FROM clause sub-selects, recursively.
134119
+** Except, do not assign a new cursor number to the iExcept'th element in
134120
+** the FROM clause of (*p). Update all expressions and other references
134121
+** to refer to the new cursor numbers.
134122
+**
134123
+** Argument aCsrMap is an array that may be used for temporary working
134124
+** space. Two guarantees are made by the caller:
134125
+**
134126
+** * the array is larger than the largest cursor number used within the
134127
+** select statement passed as an argument, and
134128
+**
134129
+** * the array entries for all cursor numbers that do *not* appear in
134130
+** FROM clauses of the select statement as described above are
134131
+** initialized to zero.
134132
+*/
134133
+static void renumberCursors(
134134
+ Parse *pParse, /* Parse context */
134135
+ Select *p, /* Select to renumber cursors within */
134136
+ int iExcept, /* FROM clause item to skip */
134137
+ int *aCsrMap /* Working space */
134138
+){
134139
+ Walker w;
134140
+ srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
134141
+ memset(&w, 0, sizeof(w));
134142
+ w.u.aiCol = aCsrMap;
134143
+ w.xExprCallback = renumberCursorsCb;
134144
+ w.xSelectCallback = sqlite3SelectWalkNoop;
134145
+ sqlite3WalkSelect(&w, p);
134146
+}
134147
+#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
134148
+
134049134149
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134050134150
/*
134051134151
** This routine attempts to flatten subqueries as a performance optimization.
134052134152
** This routine returns 1 if it makes changes and 0 if no flattening occurs.
134053134153
**
@@ -134138,13 +134238,13 @@
134138134238
** (17b) no terms within the subquery compound may be aggregate
134139134239
** or DISTINCT, and
134140134240
** (17c) every term within the subquery compound must have a FROM clause
134141134241
** (17d) the outer query may not be
134142134242
** (17d1) aggregate, or
134143
-** (17d2) DISTINCT, or
134144
-** (17d3) a join.
134145
-** (17e) the subquery may not contain window functions
134243
+** (17d2) DISTINCT
134244
+** (17e) the subquery may not contain window functions, and
134245
+** (17f) the subquery must not be the RHS of a LEFT JOIN.
134146134246
**
134147134247
** The parent and sub-query may contain WHERE clauses. Subject to
134148134248
** rules (11), (13) and (14), they may also contain ORDER BY,
134149134249
** LIMIT and OFFSET clauses. The subquery cannot use any compound
134150134250
** operator other than UNION ALL because all the other compound
@@ -134156,12 +134256,12 @@
134156134256
** SELECT statement, but all the code here does is make sure that no
134157134257
** such (illegal) sub-query is flattened. The caller will detect the
134158134258
** syntax error and return a detailed message.
134159134259
**
134160134260
** (18) If the sub-query is a compound select, then all terms of the
134161
-** ORDER BY clause of the parent must be simple references to
134162
-** columns of the sub-query.
134261
+** ORDER BY clause of the parent must be copies of a term returned
134262
+** by the parent query.
134163134263
**
134164134264
** (19) If the subquery uses LIMIT then the outer query may not
134165134265
** have a WHERE clause.
134166134266
**
134167134267
** (20) If the sub-query is a compound select, then it must not use
@@ -134173,13 +134273,12 @@
134173134273
** (21) If the subquery uses LIMIT then the outer query may not be
134174134274
** DISTINCT. (See ticket [752e1646fc]).
134175134275
**
134176134276
** (22) The subquery may not be a recursive CTE.
134177134277
**
134178
-** (**) Subsumed into restriction (17d3). Was: If the outer query is
134179
-** a recursive CTE, then the sub-query may not be a compound query.
134180
-** This restriction is because transforming the
134278
+** (23) If the outer query is a recursive CTE, then the sub-query may not be
134279
+** a compound query. This restriction is because transforming the
134181134280
** parent to a compound query confuses the code that handles
134182134281
** recursive queries in multiSelect().
134183134282
**
134184134283
** (**) We no longer attempt to flatten aggregate subqueries. Was:
134185134284
** The subquery may not be an aggregate that uses the built-in min() or
@@ -134220,10 +134319,11 @@
134220134319
int i; /* Loop counter */
134221134320
Expr *pWhere; /* The WHERE clause */
134222134321
struct SrcList_item *pSubitem; /* The subquery */
134223134322
sqlite3 *db = pParse->db;
134224134323
Walker w; /* Walker to persist agginfo data */
134324
+ int *aCsrMap = 0;
134225134325
134226134326
/* Check to see if flattening is permitted. Return 0 if not.
134227134327
*/
134228134328
assert( p!=0 );
134229134329
assert( p->pPrior==0 );
@@ -134315,17 +134415,18 @@
134315134415
*/
134316134416
if( pSub->pPrior ){
134317134417
if( pSub->pOrderBy ){
134318134418
return 0; /* Restriction (20) */
134319134419
}
134320
- if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
134321
- return 0; /* (17d1), (17d2), or (17d3) */
134420
+ if( isAgg || (p->selFlags & SF_Distinct)!=0 || isLeftJoin>0 ){
134421
+ return 0; /* (17d1), (17d2), or (17f) */
134322134422
}
134323134423
for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
134324134424
testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
134325134425
testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
134326134426
assert( pSub->pSrc!=0 );
134427
+ assert( (pSub->selFlags & SF_Recursive)==0 );
134327134428
assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
134328134429
if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
134329134430
|| (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
134330134431
|| pSub1->pSrc->nSrc<1 /* (17c) */
134331134432
#ifndef SQLITE_OMIT_WINDOWFUNC
@@ -134342,19 +134443,18 @@
134342134443
int ii;
134343134444
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
134344134445
if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
134345134446
}
134346134447
}
134347
- }
134348
-
134349
- /* Ex-restriction (23):
134350
- ** The only way that the recursive part of a CTE can contain a compound
134351
- ** subquery is for the subquery to be one term of a join. But if the
134352
- ** subquery is a join, then the flattening has already been stopped by
134353
- ** restriction (17d3)
134354
- */
134355
- assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
134448
+
134449
+ /* Restriction (23) */
134450
+ if( (p->selFlags & SF_Recursive) ) return 0;
134451
+
134452
+ if( pSrc->nSrc>1 ){
134453
+ aCsrMap = sqlite3DbMallocZero(db, pParse->nTab*sizeof(int));
134454
+ }
134455
+ }
134356134456
134357134457
/***** If we reach this point, flattening is permitted. *****/
134358134458
SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
134359134459
pSub->selId, pSub, iFrom));
134360134460
@@ -134361,10 +134461,21 @@
134361134461
/* Authorize the subquery */
134362134462
pParse->zAuthContext = pSubitem->zName;
134363134463
TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
134364134464
testcase( i==SQLITE_DENY );
134365134465
pParse->zAuthContext = zSavedAuthContext;
134466
+
134467
+ /* Delete the transient structures associated with thesubquery */
134468
+ pSub1 = pSubitem->pSelect;
134469
+ sqlite3DbFree(db, pSubitem->zDatabase);
134470
+ sqlite3DbFree(db, pSubitem->zName);
134471
+ sqlite3DbFree(db, pSubitem->zAlias);
134472
+ pSubitem->zDatabase = 0;
134473
+ pSubitem->zName = 0;
134474
+ pSubitem->zAlias = 0;
134475
+ pSubitem->pSelect = 0;
134476
+ assert( pSubitem->pOn==0 );
134366134477
134367134478
/* If the sub-query is a compound SELECT statement, then (by restrictions
134368134479
** 17 and 18 above) it must be a UNION ALL and the parent query must
134369134480
** be of the form:
134370134481
**
@@ -134400,47 +134511,40 @@
134400134511
for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
134401134512
Select *pNew;
134402134513
ExprList *pOrderBy = p->pOrderBy;
134403134514
Expr *pLimit = p->pLimit;
134404134515
Select *pPrior = p->pPrior;
134516
+ Table *pItemTab = pSubitem->pTab;
134517
+ pSubitem->pTab = 0;
134405134518
p->pOrderBy = 0;
134406
- p->pSrc = 0;
134407134519
p->pPrior = 0;
134408134520
p->pLimit = 0;
134409134521
pNew = sqlite3SelectDup(db, p, 0);
134410134522
p->pLimit = pLimit;
134411134523
p->pOrderBy = pOrderBy;
134412
- p->pSrc = pSrc;
134413134524
p->op = TK_ALL;
134525
+ pSubitem->pTab = pItemTab;
134414134526
if( pNew==0 ){
134415134527
p->pPrior = pPrior;
134416134528
}else{
134529
+ if( aCsrMap && db->mallocFailed==0 ){
134530
+ renumberCursors(pParse, pNew, iFrom, aCsrMap);
134531
+ }
134417134532
pNew->pPrior = pPrior;
134418134533
if( pPrior ) pPrior->pNext = pNew;
134419134534
pNew->pNext = p;
134420134535
p->pPrior = pNew;
134421134536
SELECTTRACE(2,pParse,p,("compound-subquery flattener"
134422134537
" creates %u as peer\n",pNew->selId));
134423134538
}
134424
- if( db->mallocFailed ) return 1;
134425
- }
134426
-
134427
- /* Begin flattening the iFrom-th entry of the FROM clause
134428
- ** in the outer query.
134429
- */
134430
- pSub = pSub1 = pSubitem->pSelect;
134431
-
134432
- /* Delete the transient table structure associated with the
134433
- ** subquery
134434
- */
134435
- sqlite3DbFree(db, pSubitem->zDatabase);
134436
- sqlite3DbFree(db, pSubitem->zName);
134437
- sqlite3DbFree(db, pSubitem->zAlias);
134438
- pSubitem->zDatabase = 0;
134439
- pSubitem->zName = 0;
134440
- pSubitem->zAlias = 0;
134441
- pSubitem->pSelect = 0;
134539
+ assert( pSubitem->pSelect==0 );
134540
+ }
134541
+ sqlite3DbFree(db, aCsrMap);
134542
+ if( db->mallocFailed ){
134543
+ pSubitem->pSelect = pSub1;
134544
+ return 1;
134545
+ }
134442134546
134443134547
/* Defer deleting the Table object associated with the
134444134548
** subquery until code generation is
134445134549
** complete, since there may still exist Expr.pTab entries that
134446134550
** refer to the subquery even after flattening. Ticket #3346.
@@ -134470,26 +134574,21 @@
134470134574
** iParent. The iParent cursor will never be used. Subsequent code
134471134575
** will scan expressions looking for iParent references and replace
134472134576
** those references with expressions that resolve to the subquery FROM
134473134577
** elements we are now copying in.
134474134578
*/
134579
+ pSub = pSub1;
134475134580
for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
134476134581
int nSubSrc;
134477134582
u8 jointype = 0;
134478134583
assert( pSub!=0 );
134479134584
pSubSrc = pSub->pSrc; /* FROM clause of subquery */
134480134585
nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
134481134586
pSrc = pParent->pSrc; /* FROM clause of the outer query */
134482134587
134483
- if( pSrc ){
134484
- assert( pParent==p ); /* First time through the loop */
134485
- jointype = pSubitem->fg.jointype;
134486
- }else{
134487
- assert( pParent!=p ); /* 2nd and subsequent times through the loop */
134488
- pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
134489
- if( pSrc==0 ) break;
134490
- pParent->pSrc = pSrc;
134588
+ if( pParent==p ){
134589
+ jointype = pSubitem->fg.jointype; /* First time through the loop */
134491134590
}
134492134591
134493134592
/* The subquery uses a single slot of the FROM clause of the outer
134494134593
** query. If the subquery has more than one element in its FROM clause,
134495134594
** then expand the outer query to make space for it to hold all elements
@@ -136010,11 +136109,13 @@
136010136109
** within the HAVING expression with a constant "1".
136011136110
*/
136012136111
static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
136013136112
if( pExpr->op!=TK_AND ){
136014136113
Select *pS = pWalker->u.pSelect;
136015
- if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
136114
+ if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
136115
+ && ExprAlwaysFalse(pExpr)==0
136116
+ ){
136016136117
sqlite3 *db = pWalker->pParse->db;
136017136118
Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
136018136119
if( pNew ){
136019136120
Expr *pWhere = pS->pWhere;
136020136121
SWAP(Expr, *pNew, *pExpr);
@@ -137284,19 +137385,17 @@
137284137385
sqlite3ExprListDelete(db, pMinMaxOrderBy);
137285137386
#ifdef SQLITE_DEBUG
137286137387
if( pAggInfo && !db->mallocFailed ){
137287137388
for(i=0; i<pAggInfo->nColumn; i++){
137288137389
Expr *pExpr = pAggInfo->aCol[i].pCExpr;
137289
- assert( pExpr!=0 || db->mallocFailed );
137290
- if( pExpr==0 ) continue;
137390
+ assert( pExpr!=0 );
137291137391
assert( pExpr->pAggInfo==pAggInfo );
137292137392
assert( pExpr->iAgg==i );
137293137393
}
137294137394
for(i=0; i<pAggInfo->nFunc; i++){
137295137395
Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
137296
- assert( pExpr!=0 || db->mallocFailed );
137297
- if( pExpr==0 ) continue;
137396
+ assert( pExpr!=0 );
137298137397
assert( pExpr->pAggInfo==pAggInfo );
137299137398
assert( pExpr->iAgg==i );
137300137399
}
137301137400
}
137302137401
#endif
@@ -149427,11 +149526,11 @@
149427149526
pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
149428149527
);
149429149528
pBtm = pTerm;
149430149529
pTop = 0;
149431149530
if( pTerm->wtFlags & TERM_LIKEOPT ){
149432
- /* Range contraints that come from the LIKE optimization are
149531
+ /* Range constraints that come from the LIKE optimization are
149433149532
** always used in pairs. */
149434149533
pTop = &pTerm[1];
149435149534
assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
149436149535
assert( pTop->wtFlags & TERM_LIKEOPT );
149437149536
assert( pTop->eOperator==WO_LT );
@@ -203997,10 +204096,11 @@
203997204096
int bIndirect; /* True if all changes are indirect */
203998204097
int bAutoAttach; /* True to auto-attach tables */
203999204098
int rc; /* Non-zero if an error has occurred */
204000204099
void *pFilterCtx; /* First argument to pass to xTableFilter */
204001204100
int (*xTableFilter)(void *pCtx, const char *zTab);
204101
+ i64 nMalloc; /* Number of bytes of data allocated */
204002204102
sqlite3_value *pZeroBlob; /* Value containing X'' */
204003204103
sqlite3_session *pNext; /* Next session object on same db. */
204004204104
SessionTable *pTable; /* List of attached tables */
204005204105
SessionHook hook; /* APIs to grab new and old data with */
204006204106
};
@@ -204380,10 +204480,30 @@
204380204480
204381204481
if( pnWrite ) *pnWrite += nByte;
204382204482
return SQLITE_OK;
204383204483
}
204384204484
204485
+/*
204486
+** Allocate and return a pointer to a buffer nByte bytes in size. If
204487
+** pSession is not NULL, increase the sqlite3_session.nMalloc variable
204488
+** by the number of bytes allocated.
204489
+*/
204490
+static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
204491
+ void *pRet = sqlite3_malloc64(nByte);
204492
+ if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
204493
+ return pRet;
204494
+}
204495
+
204496
+/*
204497
+** Free buffer pFree, which must have been allocated by an earlier
204498
+** call to sessionMalloc64(). If pSession is not NULL, decrease the
204499
+** sqlite3_session.nMalloc counter by the number of bytes freed.
204500
+*/
204501
+static void sessionFree(sqlite3_session *pSession, void *pFree){
204502
+ if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
204503
+ sqlite3_free(pFree);
204504
+}
204385204505
204386204506
/*
204387204507
** This macro is used to calculate hash key values for data structures. In
204388204508
** order to use this macro, the entire data structure must be represented
204389204509
** as a series of unsigned integers. In order to calculate a hash-key value
@@ -204847,17 +204967,23 @@
204847204967
** It is possible that a non-fatal OOM error occurs in this function. In
204848204968
** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
204849204969
** Growing the hash table in this case is a performance optimization only,
204850204970
** it is not required for correct operation.
204851204971
*/
204852
-static int sessionGrowHash(int bPatchset, SessionTable *pTab){
204972
+static int sessionGrowHash(
204973
+ sqlite3_session *pSession, /* For memory accounting. May be NULL */
204974
+ int bPatchset,
204975
+ SessionTable *pTab
204976
+){
204853204977
if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
204854204978
int i;
204855204979
SessionChange **apNew;
204856204980
sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
204857204981
204858
- apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew);
204982
+ apNew = (SessionChange**)sessionMalloc64(
204983
+ pSession, sizeof(SessionChange*) * nNew
204984
+ );
204859204985
if( apNew==0 ){
204860204986
if( pTab->nChange==0 ){
204861204987
return SQLITE_ERROR;
204862204988
}
204863204989
return SQLITE_OK;
@@ -204874,11 +205000,11 @@
204874205000
p->pNext = apNew[iHash];
204875205001
apNew[iHash] = p;
204876205002
}
204877205003
}
204878205004
204879
- sqlite3_free(pTab->apChange);
205005
+ sessionFree(pSession, pTab->apChange);
204880205006
pTab->nChange = nNew;
204881205007
pTab->apChange = apNew;
204882205008
}
204883205009
204884205010
return SQLITE_OK;
@@ -204908,10 +205034,11 @@
204908205034
**
204909205035
** All returned buffers are part of the same single allocation, which must
204910205036
** be freed using sqlite3_free() by the caller
204911205037
*/
204912205038
static int sessionTableInfo(
205039
+ sqlite3_session *pSession, /* For memory accounting. May be NULL */
204913205040
sqlite3 *db, /* Database connection */
204914205041
const char *zDb, /* Name of attached database (e.g. "main") */
204915205042
const char *zThis, /* Table name */
204916205043
int *pnCol, /* OUT: number of columns */
204917205044
const char **pzTab, /* OUT: Copy of zThis */
@@ -204962,11 +205089,11 @@
204962205089
}
204963205090
rc = sqlite3_reset(pStmt);
204964205091
204965205092
if( rc==SQLITE_OK ){
204966205093
nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
204967
- pAlloc = sqlite3_malloc64(nByte);
205094
+ pAlloc = sessionMalloc64(pSession, nByte);
204968205095
if( pAlloc==0 ){
204969205096
rc = SQLITE_NOMEM;
204970205097
}
204971205098
}
204972205099
if( rc==SQLITE_OK ){
@@ -205005,11 +205132,11 @@
205005205132
}else{
205006205133
*pazCol = 0;
205007205134
*pabPK = 0;
205008205135
*pnCol = 0;
205009205136
if( pzTab ) *pzTab = 0;
205010
- sqlite3_free(azCol);
205137
+ sessionFree(pSession, azCol);
205011205138
}
205012205139
sqlite3_finalize(pStmt);
205013205140
return rc;
205014205141
}
205015205142
@@ -205027,11 +205154,11 @@
205027205154
*/
205028205155
static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
205029205156
if( pTab->nCol==0 ){
205030205157
u8 *abPK;
205031205158
assert( pTab->azCol==0 || pTab->abPK==0 );
205032
- pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
205159
+ pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
205033205160
pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
205034205161
);
205035205162
if( pSession->rc==SQLITE_OK ){
205036205163
int i;
205037205164
for(i=0; i<pTab->nCol; i++){
@@ -205118,11 +205245,11 @@
205118205245
pSession->rc = SQLITE_SCHEMA;
205119205246
return;
205120205247
}
205121205248
205122205249
/* Grow the hash table if required */
205123
- if( sessionGrowHash(0, pTab) ){
205250
+ if( sessionGrowHash(pSession, 0, pTab) ){
205124205251
pSession->rc = SQLITE_NOMEM;
205125205252
return;
205126205253
}
205127205254
205128205255
if( pTab->bStat1 ){
@@ -205185,11 +205312,11 @@
205185205312
rc = sessionSerializeValue(0, p, &nByte);
205186205313
if( rc!=SQLITE_OK ) goto error_out;
205187205314
}
205188205315
205189205316
/* Allocate the change object */
205190
- pChange = (SessionChange *)sqlite3_malloc64(nByte);
205317
+ pChange = (SessionChange *)sessionMalloc64(pSession, nByte);
205191205318
if( !pChange ){
205192205319
rc = SQLITE_NOMEM;
205193205320
goto error_out;
205194205321
}else{
205195205322
memset(pChange, 0, sizeof(SessionChange));
@@ -205558,11 +205685,11 @@
205558205685
int bHasPk = 0;
205559205686
int bMismatch = 0;
205560205687
int nCol; /* Columns in zFrom.zTbl */
205561205688
u8 *abPK;
205562205689
const char **azCol = 0;
205563
- rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
205690
+ rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
205564205691
if( rc==SQLITE_OK ){
205565205692
if( pTo->nCol!=nCol ){
205566205693
bMismatch = 1;
205567205694
}else{
205568205695
int i;
@@ -205656,11 +205783,11 @@
205656205783
205657205784
/*
205658205785
** Free the list of table objects passed as the first argument. The contents
205659205786
** of the changed-rows hash tables are also deleted.
205660205787
*/
205661
-static void sessionDeleteTable(SessionTable *pList){
205788
+static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
205662205789
SessionTable *pNext;
205663205790
SessionTable *pTab;
205664205791
205665205792
for(pTab=pList; pTab; pTab=pNext){
205666205793
int i;
@@ -205668,16 +205795,16 @@
205668205795
for(i=0; i<pTab->nChange; i++){
205669205796
SessionChange *p;
205670205797
SessionChange *pNextChange;
205671205798
for(p=pTab->apChange[i]; p; p=pNextChange){
205672205799
pNextChange = p->pNext;
205673
- sqlite3_free(p);
205800
+ sessionFree(pSession, p);
205674205801
}
205675205802
}
205676
- sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
205677
- sqlite3_free(pTab->apChange);
205678
- sqlite3_free(pTab);
205803
+ sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
205804
+ sessionFree(pSession, pTab->apChange);
205805
+ sessionFree(pSession, pTab);
205679205806
}
205680205807
}
205681205808
205682205809
/*
205683205810
** Delete a session object previously allocated using sqlite3session_create().
@@ -205701,13 +205828,15 @@
205701205828
sqlite3_mutex_leave(sqlite3_db_mutex(db));
205702205829
sqlite3ValueFree(pSession->pZeroBlob);
205703205830
205704205831
/* Delete all attached table objects. And the contents of their
205705205832
** associated hash-tables. */
205706
- sessionDeleteTable(pSession->pTable);
205833
+ sessionDeleteTable(pSession, pSession->pTable);
205707205834
205708
- /* Free the session object itself. */
205835
+ /* Assert that all allocations have been freed and then free the
205836
+ ** session object itself. */
205837
+ assert( pSession->nMalloc==0 );
205709205838
sqlite3_free(pSession);
205710205839
}
205711205840
205712205841
/*
205713205842
** Set a table filter on a Session Object.
@@ -205750,11 +205879,12 @@
205750205879
if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
205751205880
}
205752205881
205753205882
if( !pTab ){
205754205883
/* Allocate new SessionTable object. */
205755
- pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1);
205884
+ int nByte = sizeof(SessionTable) + nName + 1;
205885
+ pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
205756205886
if( !pTab ){
205757205887
rc = SQLITE_NOMEM;
205758205888
}else{
205759205889
/* Populate the new SessionTable object and link it into the list.
205760205890
** The new object must be linked onto the end of the list, not
@@ -206347,11 +206477,11 @@
206347206477
sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
206348206478
int nRewind = buf.nBuf; /* Initial size of write buffer */
206349206479
int nNoop; /* Size of buffer after writing tbl header */
206350206480
206351206481
/* Check the table schema is still Ok. */
206352
- rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
206482
+ rc = sessionTableInfo(0, db, pSession->zDb, zName, &nCol, 0,&azCol,&abPK);
206353206483
if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
206354206484
rc = SQLITE_SCHEMA;
206355206485
}
206356206486
206357206487
/* Write a table header */
@@ -206521,10 +206651,17 @@
206521206651
}
206522206652
sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
206523206653
206524206654
return (ret==0);
206525206655
}
206656
+
206657
+/*
206658
+** Return the amount of heap memory in use.
206659
+*/
206660
+SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
206661
+ return pSession->nMalloc;
206662
+}
206526206663
206527206664
/*
206528206665
** Do the work for either sqlite3changeset_start() or start_strm().
206529206666
*/
206530206667
static int sessionChangesetStart(
@@ -208333,11 +208470,11 @@
208333208470
}else{
208334208471
int nMinCol = 0;
208335208472
int i;
208336208473
208337208474
sqlite3changeset_pk(pIter, &abPK, 0);
208338
- rc = sessionTableInfo(
208475
+ rc = sessionTableInfo(0,
208339208476
db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
208340208477
);
208341208478
if( rc!=SQLITE_OK ) break;
208342208479
for(i=0; i<sApply.nCol; i++){
208343208480
if( sApply.abPK[i] ) nMinCol = i+1;
@@ -208812,11 +208949,11 @@
208812208949
rc = SQLITE_SCHEMA;
208813208950
break;
208814208951
}
208815208952
}
208816208953
208817
- if( sessionGrowHash(pIter->bPatchset, pTab) ){
208954
+ if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
208818208955
rc = SQLITE_NOMEM;
208819208956
break;
208820208957
}
208821208958
iHash = sessionChangeHash(
208822208959
pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
@@ -208998,11 +209135,11 @@
208998209135
/*
208999209136
** Delete a changegroup object.
209000209137
*/
209001209138
SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
209002209139
if( pGrp ){
209003
- sessionDeleteTable(pGrp->pList);
209140
+ sessionDeleteTable(0, pGrp->pList);
209004209141
sqlite3_free(pGrp);
209005209142
}
209006209143
}
209007209144
209008209145
/*
@@ -209399,11 +209536,11 @@
209399209536
/*
209400209537
** Destroy a rebaser object
209401209538
*/
209402209539
SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
209403209540
if( p ){
209404
- sessionDeleteTable(p->grp.pList);
209541
+ sessionDeleteTable(0, p->grp.pList);
209405209542
sqlite3_free(p);
209406209543
}
209407209544
}
209408209545
209409209546
/*
@@ -220128,18 +220265,14 @@
220128220265
if( pbNewTerm ) *pbNewTerm = 1;
220129220266
}
220130220267
}else{
220131220268
/* The following could be done by calling fts5SegIterLoadNPos(). But
220132220269
** this block is particularly performance critical, so equivalent
220133
- ** code is inlined.
220134
- **
220135
- ** Later: Switched back to fts5SegIterLoadNPos() because it supports
220136
- ** detail=none mode. Not ideal.
220137
- */
220270
+ ** code is inlined. */
220138220271
int nSz;
220139220272
assert( p->rc==SQLITE_OK );
220140
- assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
220273
+ assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn );
220141220274
fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
220142220275
pIter->bDel = (nSz & 0x0001);
220143220276
pIter->nPos = nSz>>1;
220144220277
assert_nc( pIter->nPos>=0 );
220145220278
}
@@ -227551,11 +227684,11 @@
227551227684
int nArg, /* Number of args */
227552227685
sqlite3_value **apUnused /* Function arguments */
227553227686
){
227554227687
assert( nArg==0 );
227555227688
UNUSED_PARAM2(nArg, apUnused);
227556
- sqlite3_result_text(pCtx, "fts5: 2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660", -1, SQLITE_TRANSIENT);
227689
+ sqlite3_result_text(pCtx, "fts5: 2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f", -1, SQLITE_TRANSIENT);
227557227690
}
227558227691
227559227692
/*
227560227693
** Return true if zName is the extension on one of the shadow tables used
227561227694
** by this module.
@@ -232477,12 +232610,12 @@
232477232610
}
232478232611
#endif /* SQLITE_CORE */
232479232612
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
232480232613
232481232614
/************** End of stmt.c ************************************************/
232482
-#if __LINE__!=232482
232615
+#if __LINE__!=232615
232483232616
#undef SQLITE_SOURCE_ID
232484
-#define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66alt2"
232617
+#define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124dalt2"
232485232618
#endif
232486232619
/* Return the source-id for this library */
232487232620
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
232488232621
/************************** End of sqlite3.c ******************************/
232489232622
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -283,11 +283,11 @@
283 #endif
284 #ifdef SQLITE_ENABLE_LOCKING_STYLE
285 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
286 #endif
287 #if SQLITE_ENABLE_MATH_FUNCTIONS
288 "ENABLE_MATH_FUNCTIONS"
289 #endif
290 #if SQLITE_ENABLE_MEMORY_MANAGEMENT
291 "ENABLE_MEMORY_MANAGEMENT",
292 #endif
293 #if SQLITE_ENABLE_MEMSYS3
@@ -1186,11 +1186,11 @@
1186 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1187 ** [sqlite_version()] and [sqlite_source_id()].
1188 */
1189 #define SQLITE_VERSION "3.35.0"
1190 #define SQLITE_VERSION_NUMBER 3035000
1191 #define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660"
1192
1193 /*
1194 ** CAPI3REF: Run-Time Library Version Numbers
1195 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1196 **
@@ -4560,10 +4560,11 @@
4560 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4561 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4562 ** that uses dot-files in place of posix advisory locking.
4563 ** <tr><td> file:data.db?mode=readonly <td>
4564 ** An error. "readonly" is not a valid option for the "mode" parameter.
 
4565 ** </table>
4566 **
4567 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4568 ** query components of a URI. A hexadecimal escape sequence consists of a
4569 ** percent sign - "%" - followed by exactly two hexadecimal digits
@@ -11500,10 +11501,18 @@
11500 ** guaranteed that a call to sqlite3session_changeset() will return a
11501 ** changeset containing zero changes.
11502 */
11503 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11504
 
 
 
 
 
 
 
 
11505 /*
11506 ** CAPI3REF: Create An Iterator To Traverse A Changeset
11507 ** CONSTRUCTOR: sqlite3_changeset_iter
11508 **
11509 ** Create an iterator used to iterate through the contents of a changeset.
@@ -14626,13 +14635,13 @@
14626 #endif
14627
14628 /*
14629 ** Macros for "wheretrace"
14630 */
 
14631 #if defined(SQLITE_DEBUG) \
14632 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
14633 SQLITE_PRIVATE u32 sqlite3WhereTrace;
14634 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14635 # define WHERETRACE_ENABLED 1
14636 #else
14637 # define WHERETRACE(K,X)
14638 #endif
@@ -18158,11 +18167,11 @@
18158 ** TK_SELECT: 1st register of result vector */
18159 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18160 ** TK_VARIABLE: variable number (always >= 1).
18161 ** TK_SELECT_COLUMN: column of the result vector */
18162 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18163 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
18164 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
18165 union {
18166 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
18167 ** for a column of an index on an expression */
18168 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
@@ -19361,11 +19370,10 @@
19361 SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
19362 SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*, int);
19363 SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
19364 SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
19365 SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
19366 SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
19367 SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
19368 SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
19369 SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
19370 SQLITE_PRIVATE void sqlite3WindowFunctions(void);
19371 SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
@@ -20098,10 +20106,11 @@
20098 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
20099 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
20100 SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
20101 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
20102 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 
20103 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
20104 SQLITE_PRIVATE int sqlite3MatchEName(
20105 const struct ExprList_item*,
20106 const char*,
20107 const char*,
@@ -73754,10 +73763,13 @@
73754 getCellInfo(pSrc);
73755 aOut += putVarint32(aOut, pSrc->info.nPayload);
73756 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
73757 nIn = pSrc->info.nLocal;
73758 aIn = pSrc->info.pPayload;
 
 
 
73759 nRem = pSrc->info.nPayload;
73760 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
73761 memcpy(aOut, aIn, nIn);
73762 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
73763 }else{
@@ -73774,10 +73786,13 @@
73774 pPgnoOut = &aOut[nOut];
73775 pBt->nPreformatSize += 4;
73776 }
73777
73778 if( nRem>nIn ){
 
 
 
73779 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
73780 }
73781
73782 do {
73783 nRem -= nOut;
@@ -85613,11 +85628,11 @@
85613 testcase( zRawSql[0]=='#' );
85614 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
85615 assert( idx>0 );
85616 }
85617 zRawSql += nToken;
85618 nextIndex = idx + 1;
85619 assert( idx>0 && idx<=p->nVar );
85620 pVar = &p->aVar[idx-1];
85621 if( pVar->flags & MEM_Null ){
85622 sqlite3_str_append(&out, "NULL", 4);
85623 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
@@ -90934,10 +90949,12 @@
90934 case OP_RowCell: {
90935 VdbeCursor *pDest; /* Cursor to write to */
90936 VdbeCursor *pSrc; /* Cursor to read from */
90937 i64 iKey; /* Rowid value to insert with */
90938 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
 
 
90939 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
90940 pDest = p->apCsr[pOp->p1];
90941 pSrc = p->apCsr[pOp->p2];
90942 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
90943 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
@@ -98200,11 +98217,11 @@
98200 #if !defined(SQLITE_OMIT_WINDOWFUNC)
98201 /*
98202 ** Walk all expressions linked into the list of Window objects passed
98203 ** as the second argument.
98204 */
98205 static int walkWindowList(Walker *pWalker, Window *pList){
98206 Window *pWin;
98207 for(pWin=pList; pWin; pWin=pWin->pNextWin){
98208 int rc;
98209 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
98210 if( rc ) return WRC_Abort;
@@ -98219,10 +98236,11 @@
98219 ** not matter anyhow. */
98220 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
98221 if( NEVER(rc) ) return WRC_Abort;
98222 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
98223 if( NEVER(rc) ) return WRC_Abort;
 
98224 }
98225 return WRC_Continue;
98226 }
98227 #endif
98228
@@ -98266,11 +98284,11 @@
98266 if( pExpr->x.pList ){
98267 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
98268 }
98269 #ifndef SQLITE_OMIT_WINDOWFUNC
98270 if( ExprHasProperty(pExpr, EP_WinFunc) ){
98271 if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
98272 }
98273 #endif
98274 }
98275 }
98276 break;
@@ -98313,11 +98331,11 @@
98313 {
98314 Parse *pParse = pWalker->pParse;
98315 if( pParse && IN_RENAME_OBJECT ){
98316 /* The following may return WRC_Abort if there are unresolvable
98317 ** symbols (e.g. a table that does not exist) in a window definition. */
98318 int rc = walkWindowList(pWalker, p->pWinDefn);
98319 return rc;
98320 }
98321 }
98322 #endif
98323 return WRC_Continue;
@@ -123431,11 +123449,11 @@
123431 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
123432 ** to defer the running of the rowid conflict checking until after
123433 ** the UNIQUE constraints have run.
123434 */
123435 if( onError==OE_Replace /* IPK rule is REPLACE */
123436 && onError!=overrideError /* Rules for other contraints are different */
123437 && pTab->pIndex /* There exist other constraints */
123438 ){
123439 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
123440 VdbeComment((v, "defer IPK REPLACE until last"));
123441 }
@@ -124484,11 +124502,11 @@
124484 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
124485 }
124486 if( i==pSrcIdx->nColumn ){
124487 idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
124488 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
124489 sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regData);
124490 }
124491 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
124492 idxInsFlags |= OPFLAG_NCHANGE;
124493 }
124494 if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
@@ -130742,11 +130760,11 @@
130742 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
130743 if( pEq && isOuterJoin ){
130744 ExprSetProperty(pEq, EP_FromJoin);
130745 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
130746 ExprSetVVAProperty(pEq, EP_NoReduce);
130747 pEq->iRightJoinTable = (i16)pE2->iTable;
130748 }
130749 *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
130750 }
130751
130752 /*
@@ -130778,11 +130796,11 @@
130778 SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){
130779 while( p ){
130780 ExprSetProperty(p, EP_FromJoin);
130781 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
130782 ExprSetVVAProperty(p, EP_NoReduce);
130783 p->iRightJoinTable = (i16)iTable;
130784 if( p->op==TK_FUNCTION && p->x.pList ){
130785 int i;
130786 for(i=0; i<p->x.pList->nExpr; i++){
130787 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
130788 }
@@ -134044,10 +134062,92 @@
134044 pSrcItem->colUsed = 0;
134045 sqlite3WalkSelect(&w, pSelect);
134046 }
134047 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
134048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134049 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134050 /*
134051 ** This routine attempts to flatten subqueries as a performance optimization.
134052 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
134053 **
@@ -134138,13 +134238,13 @@
134138 ** (17b) no terms within the subquery compound may be aggregate
134139 ** or DISTINCT, and
134140 ** (17c) every term within the subquery compound must have a FROM clause
134141 ** (17d) the outer query may not be
134142 ** (17d1) aggregate, or
134143 ** (17d2) DISTINCT, or
134144 ** (17d3) a join.
134145 ** (17e) the subquery may not contain window functions
134146 **
134147 ** The parent and sub-query may contain WHERE clauses. Subject to
134148 ** rules (11), (13) and (14), they may also contain ORDER BY,
134149 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
134150 ** operator other than UNION ALL because all the other compound
@@ -134156,12 +134256,12 @@
134156 ** SELECT statement, but all the code here does is make sure that no
134157 ** such (illegal) sub-query is flattened. The caller will detect the
134158 ** syntax error and return a detailed message.
134159 **
134160 ** (18) If the sub-query is a compound select, then all terms of the
134161 ** ORDER BY clause of the parent must be simple references to
134162 ** columns of the sub-query.
134163 **
134164 ** (19) If the subquery uses LIMIT then the outer query may not
134165 ** have a WHERE clause.
134166 **
134167 ** (20) If the sub-query is a compound select, then it must not use
@@ -134173,13 +134273,12 @@
134173 ** (21) If the subquery uses LIMIT then the outer query may not be
134174 ** DISTINCT. (See ticket [752e1646fc]).
134175 **
134176 ** (22) The subquery may not be a recursive CTE.
134177 **
134178 ** (**) Subsumed into restriction (17d3). Was: If the outer query is
134179 ** a recursive CTE, then the sub-query may not be a compound query.
134180 ** This restriction is because transforming the
134181 ** parent to a compound query confuses the code that handles
134182 ** recursive queries in multiSelect().
134183 **
134184 ** (**) We no longer attempt to flatten aggregate subqueries. Was:
134185 ** The subquery may not be an aggregate that uses the built-in min() or
@@ -134220,10 +134319,11 @@
134220 int i; /* Loop counter */
134221 Expr *pWhere; /* The WHERE clause */
134222 struct SrcList_item *pSubitem; /* The subquery */
134223 sqlite3 *db = pParse->db;
134224 Walker w; /* Walker to persist agginfo data */
 
134225
134226 /* Check to see if flattening is permitted. Return 0 if not.
134227 */
134228 assert( p!=0 );
134229 assert( p->pPrior==0 );
@@ -134315,17 +134415,18 @@
134315 */
134316 if( pSub->pPrior ){
134317 if( pSub->pOrderBy ){
134318 return 0; /* Restriction (20) */
134319 }
134320 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
134321 return 0; /* (17d1), (17d2), or (17d3) */
134322 }
134323 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
134324 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
134325 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
134326 assert( pSub->pSrc!=0 );
 
134327 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
134328 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
134329 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
134330 || pSub1->pSrc->nSrc<1 /* (17c) */
134331 #ifndef SQLITE_OMIT_WINDOWFUNC
@@ -134342,19 +134443,18 @@
134342 int ii;
134343 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
134344 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
134345 }
134346 }
134347 }
134348
134349 /* Ex-restriction (23):
134350 ** The only way that the recursive part of a CTE can contain a compound
134351 ** subquery is for the subquery to be one term of a join. But if the
134352 ** subquery is a join, then the flattening has already been stopped by
134353 ** restriction (17d3)
134354 */
134355 assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
134356
134357 /***** If we reach this point, flattening is permitted. *****/
134358 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
134359 pSub->selId, pSub, iFrom));
134360
@@ -134361,10 +134461,21 @@
134361 /* Authorize the subquery */
134362 pParse->zAuthContext = pSubitem->zName;
134363 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
134364 testcase( i==SQLITE_DENY );
134365 pParse->zAuthContext = zSavedAuthContext;
 
 
 
 
 
 
 
 
 
 
 
134366
134367 /* If the sub-query is a compound SELECT statement, then (by restrictions
134368 ** 17 and 18 above) it must be a UNION ALL and the parent query must
134369 ** be of the form:
134370 **
@@ -134400,47 +134511,40 @@
134400 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
134401 Select *pNew;
134402 ExprList *pOrderBy = p->pOrderBy;
134403 Expr *pLimit = p->pLimit;
134404 Select *pPrior = p->pPrior;
 
 
134405 p->pOrderBy = 0;
134406 p->pSrc = 0;
134407 p->pPrior = 0;
134408 p->pLimit = 0;
134409 pNew = sqlite3SelectDup(db, p, 0);
134410 p->pLimit = pLimit;
134411 p->pOrderBy = pOrderBy;
134412 p->pSrc = pSrc;
134413 p->op = TK_ALL;
 
134414 if( pNew==0 ){
134415 p->pPrior = pPrior;
134416 }else{
 
 
 
134417 pNew->pPrior = pPrior;
134418 if( pPrior ) pPrior->pNext = pNew;
134419 pNew->pNext = p;
134420 p->pPrior = pNew;
134421 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
134422 " creates %u as peer\n",pNew->selId));
134423 }
134424 if( db->mallocFailed ) return 1;
134425 }
134426
134427 /* Begin flattening the iFrom-th entry of the FROM clause
134428 ** in the outer query.
134429 */
134430 pSub = pSub1 = pSubitem->pSelect;
134431
134432 /* Delete the transient table structure associated with the
134433 ** subquery
134434 */
134435 sqlite3DbFree(db, pSubitem->zDatabase);
134436 sqlite3DbFree(db, pSubitem->zName);
134437 sqlite3DbFree(db, pSubitem->zAlias);
134438 pSubitem->zDatabase = 0;
134439 pSubitem->zName = 0;
134440 pSubitem->zAlias = 0;
134441 pSubitem->pSelect = 0;
134442
134443 /* Defer deleting the Table object associated with the
134444 ** subquery until code generation is
134445 ** complete, since there may still exist Expr.pTab entries that
134446 ** refer to the subquery even after flattening. Ticket #3346.
@@ -134470,26 +134574,21 @@
134470 ** iParent. The iParent cursor will never be used. Subsequent code
134471 ** will scan expressions looking for iParent references and replace
134472 ** those references with expressions that resolve to the subquery FROM
134473 ** elements we are now copying in.
134474 */
 
134475 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
134476 int nSubSrc;
134477 u8 jointype = 0;
134478 assert( pSub!=0 );
134479 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
134480 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
134481 pSrc = pParent->pSrc; /* FROM clause of the outer query */
134482
134483 if( pSrc ){
134484 assert( pParent==p ); /* First time through the loop */
134485 jointype = pSubitem->fg.jointype;
134486 }else{
134487 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
134488 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
134489 if( pSrc==0 ) break;
134490 pParent->pSrc = pSrc;
134491 }
134492
134493 /* The subquery uses a single slot of the FROM clause of the outer
134494 ** query. If the subquery has more than one element in its FROM clause,
134495 ** then expand the outer query to make space for it to hold all elements
@@ -136010,11 +136109,13 @@
136010 ** within the HAVING expression with a constant "1".
136011 */
136012 static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
136013 if( pExpr->op!=TK_AND ){
136014 Select *pS = pWalker->u.pSelect;
136015 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
 
 
136016 sqlite3 *db = pWalker->pParse->db;
136017 Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
136018 if( pNew ){
136019 Expr *pWhere = pS->pWhere;
136020 SWAP(Expr, *pNew, *pExpr);
@@ -137284,19 +137385,17 @@
137284 sqlite3ExprListDelete(db, pMinMaxOrderBy);
137285 #ifdef SQLITE_DEBUG
137286 if( pAggInfo && !db->mallocFailed ){
137287 for(i=0; i<pAggInfo->nColumn; i++){
137288 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
137289 assert( pExpr!=0 || db->mallocFailed );
137290 if( pExpr==0 ) continue;
137291 assert( pExpr->pAggInfo==pAggInfo );
137292 assert( pExpr->iAgg==i );
137293 }
137294 for(i=0; i<pAggInfo->nFunc; i++){
137295 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
137296 assert( pExpr!=0 || db->mallocFailed );
137297 if( pExpr==0 ) continue;
137298 assert( pExpr->pAggInfo==pAggInfo );
137299 assert( pExpr->iAgg==i );
137300 }
137301 }
137302 #endif
@@ -149427,11 +149526,11 @@
149427 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
149428 );
149429 pBtm = pTerm;
149430 pTop = 0;
149431 if( pTerm->wtFlags & TERM_LIKEOPT ){
149432 /* Range contraints that come from the LIKE optimization are
149433 ** always used in pairs. */
149434 pTop = &pTerm[1];
149435 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
149436 assert( pTop->wtFlags & TERM_LIKEOPT );
149437 assert( pTop->eOperator==WO_LT );
@@ -203997,10 +204096,11 @@
203997 int bIndirect; /* True if all changes are indirect */
203998 int bAutoAttach; /* True to auto-attach tables */
203999 int rc; /* Non-zero if an error has occurred */
204000 void *pFilterCtx; /* First argument to pass to xTableFilter */
204001 int (*xTableFilter)(void *pCtx, const char *zTab);
 
204002 sqlite3_value *pZeroBlob; /* Value containing X'' */
204003 sqlite3_session *pNext; /* Next session object on same db. */
204004 SessionTable *pTable; /* List of attached tables */
204005 SessionHook hook; /* APIs to grab new and old data with */
204006 };
@@ -204380,10 +204480,30 @@
204380
204381 if( pnWrite ) *pnWrite += nByte;
204382 return SQLITE_OK;
204383 }
204384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204385
204386 /*
204387 ** This macro is used to calculate hash key values for data structures. In
204388 ** order to use this macro, the entire data structure must be represented
204389 ** as a series of unsigned integers. In order to calculate a hash-key value
@@ -204847,17 +204967,23 @@
204847 ** It is possible that a non-fatal OOM error occurs in this function. In
204848 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
204849 ** Growing the hash table in this case is a performance optimization only,
204850 ** it is not required for correct operation.
204851 */
204852 static int sessionGrowHash(int bPatchset, SessionTable *pTab){
 
 
 
 
204853 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
204854 int i;
204855 SessionChange **apNew;
204856 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
204857
204858 apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew);
 
 
204859 if( apNew==0 ){
204860 if( pTab->nChange==0 ){
204861 return SQLITE_ERROR;
204862 }
204863 return SQLITE_OK;
@@ -204874,11 +205000,11 @@
204874 p->pNext = apNew[iHash];
204875 apNew[iHash] = p;
204876 }
204877 }
204878
204879 sqlite3_free(pTab->apChange);
204880 pTab->nChange = nNew;
204881 pTab->apChange = apNew;
204882 }
204883
204884 return SQLITE_OK;
@@ -204908,10 +205034,11 @@
204908 **
204909 ** All returned buffers are part of the same single allocation, which must
204910 ** be freed using sqlite3_free() by the caller
204911 */
204912 static int sessionTableInfo(
 
204913 sqlite3 *db, /* Database connection */
204914 const char *zDb, /* Name of attached database (e.g. "main") */
204915 const char *zThis, /* Table name */
204916 int *pnCol, /* OUT: number of columns */
204917 const char **pzTab, /* OUT: Copy of zThis */
@@ -204962,11 +205089,11 @@
204962 }
204963 rc = sqlite3_reset(pStmt);
204964
204965 if( rc==SQLITE_OK ){
204966 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
204967 pAlloc = sqlite3_malloc64(nByte);
204968 if( pAlloc==0 ){
204969 rc = SQLITE_NOMEM;
204970 }
204971 }
204972 if( rc==SQLITE_OK ){
@@ -205005,11 +205132,11 @@
205005 }else{
205006 *pazCol = 0;
205007 *pabPK = 0;
205008 *pnCol = 0;
205009 if( pzTab ) *pzTab = 0;
205010 sqlite3_free(azCol);
205011 }
205012 sqlite3_finalize(pStmt);
205013 return rc;
205014 }
205015
@@ -205027,11 +205154,11 @@
205027 */
205028 static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
205029 if( pTab->nCol==0 ){
205030 u8 *abPK;
205031 assert( pTab->azCol==0 || pTab->abPK==0 );
205032 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
205033 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
205034 );
205035 if( pSession->rc==SQLITE_OK ){
205036 int i;
205037 for(i=0; i<pTab->nCol; i++){
@@ -205118,11 +205245,11 @@
205118 pSession->rc = SQLITE_SCHEMA;
205119 return;
205120 }
205121
205122 /* Grow the hash table if required */
205123 if( sessionGrowHash(0, pTab) ){
205124 pSession->rc = SQLITE_NOMEM;
205125 return;
205126 }
205127
205128 if( pTab->bStat1 ){
@@ -205185,11 +205312,11 @@
205185 rc = sessionSerializeValue(0, p, &nByte);
205186 if( rc!=SQLITE_OK ) goto error_out;
205187 }
205188
205189 /* Allocate the change object */
205190 pChange = (SessionChange *)sqlite3_malloc64(nByte);
205191 if( !pChange ){
205192 rc = SQLITE_NOMEM;
205193 goto error_out;
205194 }else{
205195 memset(pChange, 0, sizeof(SessionChange));
@@ -205558,11 +205685,11 @@
205558 int bHasPk = 0;
205559 int bMismatch = 0;
205560 int nCol; /* Columns in zFrom.zTbl */
205561 u8 *abPK;
205562 const char **azCol = 0;
205563 rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
205564 if( rc==SQLITE_OK ){
205565 if( pTo->nCol!=nCol ){
205566 bMismatch = 1;
205567 }else{
205568 int i;
@@ -205656,11 +205783,11 @@
205656
205657 /*
205658 ** Free the list of table objects passed as the first argument. The contents
205659 ** of the changed-rows hash tables are also deleted.
205660 */
205661 static void sessionDeleteTable(SessionTable *pList){
205662 SessionTable *pNext;
205663 SessionTable *pTab;
205664
205665 for(pTab=pList; pTab; pTab=pNext){
205666 int i;
@@ -205668,16 +205795,16 @@
205668 for(i=0; i<pTab->nChange; i++){
205669 SessionChange *p;
205670 SessionChange *pNextChange;
205671 for(p=pTab->apChange[i]; p; p=pNextChange){
205672 pNextChange = p->pNext;
205673 sqlite3_free(p);
205674 }
205675 }
205676 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
205677 sqlite3_free(pTab->apChange);
205678 sqlite3_free(pTab);
205679 }
205680 }
205681
205682 /*
205683 ** Delete a session object previously allocated using sqlite3session_create().
@@ -205701,13 +205828,15 @@
205701 sqlite3_mutex_leave(sqlite3_db_mutex(db));
205702 sqlite3ValueFree(pSession->pZeroBlob);
205703
205704 /* Delete all attached table objects. And the contents of their
205705 ** associated hash-tables. */
205706 sessionDeleteTable(pSession->pTable);
205707
205708 /* Free the session object itself. */
 
 
205709 sqlite3_free(pSession);
205710 }
205711
205712 /*
205713 ** Set a table filter on a Session Object.
@@ -205750,11 +205879,12 @@
205750 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
205751 }
205752
205753 if( !pTab ){
205754 /* Allocate new SessionTable object. */
205755 pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1);
 
205756 if( !pTab ){
205757 rc = SQLITE_NOMEM;
205758 }else{
205759 /* Populate the new SessionTable object and link it into the list.
205760 ** The new object must be linked onto the end of the list, not
@@ -206347,11 +206477,11 @@
206347 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
206348 int nRewind = buf.nBuf; /* Initial size of write buffer */
206349 int nNoop; /* Size of buffer after writing tbl header */
206350
206351 /* Check the table schema is still Ok. */
206352 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
206353 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
206354 rc = SQLITE_SCHEMA;
206355 }
206356
206357 /* Write a table header */
@@ -206521,10 +206651,17 @@
206521 }
206522 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
206523
206524 return (ret==0);
206525 }
 
 
 
 
 
 
 
206526
206527 /*
206528 ** Do the work for either sqlite3changeset_start() or start_strm().
206529 */
206530 static int sessionChangesetStart(
@@ -208333,11 +208470,11 @@
208333 }else{
208334 int nMinCol = 0;
208335 int i;
208336
208337 sqlite3changeset_pk(pIter, &abPK, 0);
208338 rc = sessionTableInfo(
208339 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
208340 );
208341 if( rc!=SQLITE_OK ) break;
208342 for(i=0; i<sApply.nCol; i++){
208343 if( sApply.abPK[i] ) nMinCol = i+1;
@@ -208812,11 +208949,11 @@
208812 rc = SQLITE_SCHEMA;
208813 break;
208814 }
208815 }
208816
208817 if( sessionGrowHash(pIter->bPatchset, pTab) ){
208818 rc = SQLITE_NOMEM;
208819 break;
208820 }
208821 iHash = sessionChangeHash(
208822 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
@@ -208998,11 +209135,11 @@
208998 /*
208999 ** Delete a changegroup object.
209000 */
209001 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
209002 if( pGrp ){
209003 sessionDeleteTable(pGrp->pList);
209004 sqlite3_free(pGrp);
209005 }
209006 }
209007
209008 /*
@@ -209399,11 +209536,11 @@
209399 /*
209400 ** Destroy a rebaser object
209401 */
209402 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
209403 if( p ){
209404 sessionDeleteTable(p->grp.pList);
209405 sqlite3_free(p);
209406 }
209407 }
209408
209409 /*
@@ -220128,18 +220265,14 @@
220128 if( pbNewTerm ) *pbNewTerm = 1;
220129 }
220130 }else{
220131 /* The following could be done by calling fts5SegIterLoadNPos(). But
220132 ** this block is particularly performance critical, so equivalent
220133 ** code is inlined.
220134 **
220135 ** Later: Switched back to fts5SegIterLoadNPos() because it supports
220136 ** detail=none mode. Not ideal.
220137 */
220138 int nSz;
220139 assert( p->rc==SQLITE_OK );
220140 assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
220141 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
220142 pIter->bDel = (nSz & 0x0001);
220143 pIter->nPos = nSz>>1;
220144 assert_nc( pIter->nPos>=0 );
220145 }
@@ -227551,11 +227684,11 @@
227551 int nArg, /* Number of args */
227552 sqlite3_value **apUnused /* Function arguments */
227553 ){
227554 assert( nArg==0 );
227555 UNUSED_PARAM2(nArg, apUnused);
227556 sqlite3_result_text(pCtx, "fts5: 2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660", -1, SQLITE_TRANSIENT);
227557 }
227558
227559 /*
227560 ** Return true if zName is the extension on one of the shadow tables used
227561 ** by this module.
@@ -232477,12 +232610,12 @@
232477 }
232478 #endif /* SQLITE_CORE */
232479 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
232480
232481 /************** End of stmt.c ************************************************/
232482 #if __LINE__!=232482
232483 #undef SQLITE_SOURCE_ID
232484 #define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66alt2"
232485 #endif
232486 /* Return the source-id for this library */
232487 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
232488 /************************** End of sqlite3.c ******************************/
232489
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -283,11 +283,11 @@
283 #endif
284 #ifdef SQLITE_ENABLE_LOCKING_STYLE
285 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
286 #endif
287 #if SQLITE_ENABLE_MATH_FUNCTIONS
288 "ENABLE_MATH_FUNCTIONS",
289 #endif
290 #if SQLITE_ENABLE_MEMORY_MANAGEMENT
291 "ENABLE_MEMORY_MANAGEMENT",
292 #endif
293 #if SQLITE_ENABLE_MEMSYS3
@@ -1186,11 +1186,11 @@
1186 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1187 ** [sqlite_version()] and [sqlite_source_id()].
1188 */
1189 #define SQLITE_VERSION "3.35.0"
1190 #define SQLITE_VERSION_NUMBER 3035000
1191 #define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f"
1192
1193 /*
1194 ** CAPI3REF: Run-Time Library Version Numbers
1195 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1196 **
@@ -4560,10 +4560,11 @@
4560 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4561 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4562 ** that uses dot-files in place of posix advisory locking.
4563 ** <tr><td> file:data.db?mode=readonly <td>
4564 ** An error. "readonly" is not a valid option for the "mode" parameter.
4565 ** Use "ro" instead: "file:data.db?mode=ro".
4566 ** </table>
4567 **
4568 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4569 ** query components of a URI. A hexadecimal escape sequence consists of a
4570 ** percent sign - "%" - followed by exactly two hexadecimal digits
@@ -11500,10 +11501,18 @@
11501 ** guaranteed that a call to sqlite3session_changeset() will return a
11502 ** changeset containing zero changes.
11503 */
11504 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11505
11506 /*
11507 ** CAPI3REF: Query for the amount of heap memory used by a session object.
11508 **
11509 ** This API returns the total amount of heap memory in bytes currently
11510 ** used by the session object passed as the only argument.
11511 */
11512 SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
11513
11514 /*
11515 ** CAPI3REF: Create An Iterator To Traverse A Changeset
11516 ** CONSTRUCTOR: sqlite3_changeset_iter
11517 **
11518 ** Create an iterator used to iterate through the contents of a changeset.
@@ -14626,13 +14635,13 @@
14635 #endif
14636
14637 /*
14638 ** Macros for "wheretrace"
14639 */
14640 SQLITE_PRIVATE u32 sqlite3WhereTrace;
14641 #if defined(SQLITE_DEBUG) \
14642 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
 
14643 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14644 # define WHERETRACE_ENABLED 1
14645 #else
14646 # define WHERETRACE(K,X)
14647 #endif
@@ -18158,11 +18167,11 @@
18167 ** TK_SELECT: 1st register of result vector */
18168 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18169 ** TK_VARIABLE: variable number (always >= 1).
18170 ** TK_SELECT_COLUMN: column of the result vector */
18171 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18172 int iRightJoinTable; /* If EP_FromJoin, the right table of the join */
18173 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
18174 union {
18175 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
18176 ** for a column of an index on an expression */
18177 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
@@ -19361,11 +19370,10 @@
19370 SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
19371 SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*, int);
19372 SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
19373 SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
19374 SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
 
19375 SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
19376 SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
19377 SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
19378 SQLITE_PRIVATE void sqlite3WindowFunctions(void);
19379 SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
@@ -20098,10 +20106,11 @@
20106 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
20107 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
20108 SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
20109 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
20110 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
20111 SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
20112 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
20113 SQLITE_PRIVATE int sqlite3MatchEName(
20114 const struct ExprList_item*,
20115 const char*,
20116 const char*,
@@ -73754,10 +73763,13 @@
73763 getCellInfo(pSrc);
73764 aOut += putVarint32(aOut, pSrc->info.nPayload);
73765 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
73766 nIn = pSrc->info.nLocal;
73767 aIn = pSrc->info.pPayload;
73768 if( aIn+nIn>pSrc->pPage->aDataEnd ){
73769 return SQLITE_CORRUPT_BKPT;
73770 }
73771 nRem = pSrc->info.nPayload;
73772 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
73773 memcpy(aOut, aIn, nIn);
73774 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
73775 }else{
@@ -73774,10 +73786,13 @@
73786 pPgnoOut = &aOut[nOut];
73787 pBt->nPreformatSize += 4;
73788 }
73789
73790 if( nRem>nIn ){
73791 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
73792 return SQLITE_CORRUPT_BKPT;
73793 }
73794 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
73795 }
73796
73797 do {
73798 nRem -= nOut;
@@ -85613,11 +85628,11 @@
85628 testcase( zRawSql[0]=='#' );
85629 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
85630 assert( idx>0 );
85631 }
85632 zRawSql += nToken;
85633 nextIndex = MAX(idx + 1, nextIndex);
85634 assert( idx>0 && idx<=p->nVar );
85635 pVar = &p->aVar[idx-1];
85636 if( pVar->flags & MEM_Null ){
85637 sqlite3_str_append(&out, "NULL", 4);
85638 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
@@ -90934,10 +90949,12 @@
90949 case OP_RowCell: {
90950 VdbeCursor *pDest; /* Cursor to write to */
90951 VdbeCursor *pSrc; /* Cursor to read from */
90952 i64 iKey; /* Rowid value to insert with */
90953 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
90954 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
90955 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
90956 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
90957 pDest = p->apCsr[pOp->p1];
90958 pSrc = p->apCsr[pOp->p2];
90959 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
90960 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
@@ -98200,11 +98217,11 @@
98217 #if !defined(SQLITE_OMIT_WINDOWFUNC)
98218 /*
98219 ** Walk all expressions linked into the list of Window objects passed
98220 ** as the second argument.
98221 */
98222 static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
98223 Window *pWin;
98224 for(pWin=pList; pWin; pWin=pWin->pNextWin){
98225 int rc;
98226 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
98227 if( rc ) return WRC_Abort;
@@ -98219,10 +98236,11 @@
98236 ** not matter anyhow. */
98237 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
98238 if( NEVER(rc) ) return WRC_Abort;
98239 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
98240 if( NEVER(rc) ) return WRC_Abort;
98241 if( bOneOnly ) break;
98242 }
98243 return WRC_Continue;
98244 }
98245 #endif
98246
@@ -98266,11 +98284,11 @@
98284 if( pExpr->x.pList ){
98285 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
98286 }
98287 #ifndef SQLITE_OMIT_WINDOWFUNC
98288 if( ExprHasProperty(pExpr, EP_WinFunc) ){
98289 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
98290 }
98291 #endif
98292 }
98293 }
98294 break;
@@ -98313,11 +98331,11 @@
98331 {
98332 Parse *pParse = pWalker->pParse;
98333 if( pParse && IN_RENAME_OBJECT ){
98334 /* The following may return WRC_Abort if there are unresolvable
98335 ** symbols (e.g. a table that does not exist) in a window definition. */
98336 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
98337 return rc;
98338 }
98339 }
98340 #endif
98341 return WRC_Continue;
@@ -123431,11 +123449,11 @@
123449 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
123450 ** to defer the running of the rowid conflict checking until after
123451 ** the UNIQUE constraints have run.
123452 */
123453 if( onError==OE_Replace /* IPK rule is REPLACE */
123454 && onError!=overrideError /* Rules for other constraints are different */
123455 && pTab->pIndex /* There exist other constraints */
123456 ){
123457 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
123458 VdbeComment((v, "defer IPK REPLACE until last"));
123459 }
@@ -124484,11 +124502,11 @@
124502 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
124503 }
124504 if( i==pSrcIdx->nColumn ){
124505 idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
124506 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
124507 sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc);
124508 }
124509 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
124510 idxInsFlags |= OPFLAG_NCHANGE;
124511 }
124512 if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
@@ -130742,11 +130760,11 @@
130760 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
130761 if( pEq && isOuterJoin ){
130762 ExprSetProperty(pEq, EP_FromJoin);
130763 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
130764 ExprSetVVAProperty(pEq, EP_NoReduce);
130765 pEq->iRightJoinTable = pE2->iTable;
130766 }
130767 *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
130768 }
130769
130770 /*
@@ -130778,11 +130796,11 @@
130796 SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){
130797 while( p ){
130798 ExprSetProperty(p, EP_FromJoin);
130799 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
130800 ExprSetVVAProperty(p, EP_NoReduce);
130801 p->iRightJoinTable = iTable;
130802 if( p->op==TK_FUNCTION && p->x.pList ){
130803 int i;
130804 for(i=0; i<p->x.pList->nExpr; i++){
130805 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
130806 }
@@ -134044,10 +134062,92 @@
134062 pSrcItem->colUsed = 0;
134063 sqlite3WalkSelect(&w, pSelect);
134064 }
134065 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
134066
134067 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134068 /*
134069 ** Assign new cursor numbers to each of the items in pSrc. For each
134070 ** new cursor number assigned, set an entry in the aCsrMap[] array
134071 ** to map the old cursor number to the new:
134072 **
134073 ** aCsrMap[iOld] = iNew;
134074 **
134075 ** The array is guaranteed by the caller to be large enough for all
134076 ** existing cursor numbers in pSrc.
134077 **
134078 ** If pSrc contains any sub-selects, call this routine recursively
134079 ** on the FROM clause of each such sub-select, with iExcept set to -1.
134080 */
134081 static void srclistRenumberCursors(
134082 Parse *pParse, /* Parse context */
134083 int *aCsrMap, /* Array to store cursor mappings in */
134084 SrcList *pSrc, /* FROM clause to renumber */
134085 int iExcept /* FROM clause item to skip */
134086 ){
134087 int i;
134088 struct SrcList_item *pItem;
134089 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
134090 if( i!=iExcept ){
134091 Select *p;
134092 pItem->iCursor = aCsrMap[pItem->iCursor] = pParse->nTab++;
134093 for(p=pItem->pSelect; p; p=p->pPrior){
134094 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
134095 }
134096 }
134097 }
134098 }
134099
134100 /*
134101 ** Expression walker callback used by renumberCursors() to update
134102 ** Expr objects to match newly assigned cursor numbers.
134103 */
134104 static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
134105 int *aCsrMap = pWalker->u.aiCol;
134106 if( pExpr->op==TK_COLUMN && aCsrMap[pExpr->iTable] ){
134107 pExpr->iTable = aCsrMap[pExpr->iTable];
134108 }
134109 if( ExprHasProperty(pExpr, EP_FromJoin) && aCsrMap[pExpr->iRightJoinTable] ){
134110 pExpr->iRightJoinTable = aCsrMap[pExpr->iRightJoinTable];
134111 }
134112 return WRC_Continue;
134113 }
134114
134115 /*
134116 ** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
134117 ** of the SELECT statement passed as the second argument, and to each
134118 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
134119 ** Except, do not assign a new cursor number to the iExcept'th element in
134120 ** the FROM clause of (*p). Update all expressions and other references
134121 ** to refer to the new cursor numbers.
134122 **
134123 ** Argument aCsrMap is an array that may be used for temporary working
134124 ** space. Two guarantees are made by the caller:
134125 **
134126 ** * the array is larger than the largest cursor number used within the
134127 ** select statement passed as an argument, and
134128 **
134129 ** * the array entries for all cursor numbers that do *not* appear in
134130 ** FROM clauses of the select statement as described above are
134131 ** initialized to zero.
134132 */
134133 static void renumberCursors(
134134 Parse *pParse, /* Parse context */
134135 Select *p, /* Select to renumber cursors within */
134136 int iExcept, /* FROM clause item to skip */
134137 int *aCsrMap /* Working space */
134138 ){
134139 Walker w;
134140 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
134141 memset(&w, 0, sizeof(w));
134142 w.u.aiCol = aCsrMap;
134143 w.xExprCallback = renumberCursorsCb;
134144 w.xSelectCallback = sqlite3SelectWalkNoop;
134145 sqlite3WalkSelect(&w, p);
134146 }
134147 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
134148
134149 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134150 /*
134151 ** This routine attempts to flatten subqueries as a performance optimization.
134152 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
134153 **
@@ -134138,13 +134238,13 @@
134238 ** (17b) no terms within the subquery compound may be aggregate
134239 ** or DISTINCT, and
134240 ** (17c) every term within the subquery compound must have a FROM clause
134241 ** (17d) the outer query may not be
134242 ** (17d1) aggregate, or
134243 ** (17d2) DISTINCT
134244 ** (17e) the subquery may not contain window functions, and
134245 ** (17f) the subquery must not be the RHS of a LEFT JOIN.
134246 **
134247 ** The parent and sub-query may contain WHERE clauses. Subject to
134248 ** rules (11), (13) and (14), they may also contain ORDER BY,
134249 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
134250 ** operator other than UNION ALL because all the other compound
@@ -134156,12 +134256,12 @@
134256 ** SELECT statement, but all the code here does is make sure that no
134257 ** such (illegal) sub-query is flattened. The caller will detect the
134258 ** syntax error and return a detailed message.
134259 **
134260 ** (18) If the sub-query is a compound select, then all terms of the
134261 ** ORDER BY clause of the parent must be copies of a term returned
134262 ** by the parent query.
134263 **
134264 ** (19) If the subquery uses LIMIT then the outer query may not
134265 ** have a WHERE clause.
134266 **
134267 ** (20) If the sub-query is a compound select, then it must not use
@@ -134173,13 +134273,12 @@
134273 ** (21) If the subquery uses LIMIT then the outer query may not be
134274 ** DISTINCT. (See ticket [752e1646fc]).
134275 **
134276 ** (22) The subquery may not be a recursive CTE.
134277 **
134278 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
134279 ** a compound query. This restriction is because transforming the
 
134280 ** parent to a compound query confuses the code that handles
134281 ** recursive queries in multiSelect().
134282 **
134283 ** (**) We no longer attempt to flatten aggregate subqueries. Was:
134284 ** The subquery may not be an aggregate that uses the built-in min() or
@@ -134220,10 +134319,11 @@
134319 int i; /* Loop counter */
134320 Expr *pWhere; /* The WHERE clause */
134321 struct SrcList_item *pSubitem; /* The subquery */
134322 sqlite3 *db = pParse->db;
134323 Walker w; /* Walker to persist agginfo data */
134324 int *aCsrMap = 0;
134325
134326 /* Check to see if flattening is permitted. Return 0 if not.
134327 */
134328 assert( p!=0 );
134329 assert( p->pPrior==0 );
@@ -134315,17 +134415,18 @@
134415 */
134416 if( pSub->pPrior ){
134417 if( pSub->pOrderBy ){
134418 return 0; /* Restriction (20) */
134419 }
134420 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isLeftJoin>0 ){
134421 return 0; /* (17d1), (17d2), or (17f) */
134422 }
134423 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
134424 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
134425 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
134426 assert( pSub->pSrc!=0 );
134427 assert( (pSub->selFlags & SF_Recursive)==0 );
134428 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
134429 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
134430 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
134431 || pSub1->pSrc->nSrc<1 /* (17c) */
134432 #ifndef SQLITE_OMIT_WINDOWFUNC
@@ -134342,19 +134443,18 @@
134443 int ii;
134444 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
134445 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
134446 }
134447 }
134448
134449 /* Restriction (23) */
134450 if( (p->selFlags & SF_Recursive) ) return 0;
134451
134452 if( pSrc->nSrc>1 ){
134453 aCsrMap = sqlite3DbMallocZero(db, pParse->nTab*sizeof(int));
134454 }
134455 }
 
134456
134457 /***** If we reach this point, flattening is permitted. *****/
134458 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
134459 pSub->selId, pSub, iFrom));
134460
@@ -134361,10 +134461,21 @@
134461 /* Authorize the subquery */
134462 pParse->zAuthContext = pSubitem->zName;
134463 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
134464 testcase( i==SQLITE_DENY );
134465 pParse->zAuthContext = zSavedAuthContext;
134466
134467 /* Delete the transient structures associated with thesubquery */
134468 pSub1 = pSubitem->pSelect;
134469 sqlite3DbFree(db, pSubitem->zDatabase);
134470 sqlite3DbFree(db, pSubitem->zName);
134471 sqlite3DbFree(db, pSubitem->zAlias);
134472 pSubitem->zDatabase = 0;
134473 pSubitem->zName = 0;
134474 pSubitem->zAlias = 0;
134475 pSubitem->pSelect = 0;
134476 assert( pSubitem->pOn==0 );
134477
134478 /* If the sub-query is a compound SELECT statement, then (by restrictions
134479 ** 17 and 18 above) it must be a UNION ALL and the parent query must
134480 ** be of the form:
134481 **
@@ -134400,47 +134511,40 @@
134511 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
134512 Select *pNew;
134513 ExprList *pOrderBy = p->pOrderBy;
134514 Expr *pLimit = p->pLimit;
134515 Select *pPrior = p->pPrior;
134516 Table *pItemTab = pSubitem->pTab;
134517 pSubitem->pTab = 0;
134518 p->pOrderBy = 0;
 
134519 p->pPrior = 0;
134520 p->pLimit = 0;
134521 pNew = sqlite3SelectDup(db, p, 0);
134522 p->pLimit = pLimit;
134523 p->pOrderBy = pOrderBy;
 
134524 p->op = TK_ALL;
134525 pSubitem->pTab = pItemTab;
134526 if( pNew==0 ){
134527 p->pPrior = pPrior;
134528 }else{
134529 if( aCsrMap && db->mallocFailed==0 ){
134530 renumberCursors(pParse, pNew, iFrom, aCsrMap);
134531 }
134532 pNew->pPrior = pPrior;
134533 if( pPrior ) pPrior->pNext = pNew;
134534 pNew->pNext = p;
134535 p->pPrior = pNew;
134536 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
134537 " creates %u as peer\n",pNew->selId));
134538 }
134539 assert( pSubitem->pSelect==0 );
134540 }
134541 sqlite3DbFree(db, aCsrMap);
134542 if( db->mallocFailed ){
134543 pSubitem->pSelect = pSub1;
134544 return 1;
134545 }
 
 
 
 
 
 
 
 
 
 
 
134546
134547 /* Defer deleting the Table object associated with the
134548 ** subquery until code generation is
134549 ** complete, since there may still exist Expr.pTab entries that
134550 ** refer to the subquery even after flattening. Ticket #3346.
@@ -134470,26 +134574,21 @@
134574 ** iParent. The iParent cursor will never be used. Subsequent code
134575 ** will scan expressions looking for iParent references and replace
134576 ** those references with expressions that resolve to the subquery FROM
134577 ** elements we are now copying in.
134578 */
134579 pSub = pSub1;
134580 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
134581 int nSubSrc;
134582 u8 jointype = 0;
134583 assert( pSub!=0 );
134584 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
134585 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
134586 pSrc = pParent->pSrc; /* FROM clause of the outer query */
134587
134588 if( pParent==p ){
134589 jointype = pSubitem->fg.jointype; /* First time through the loop */
 
 
 
 
 
 
134590 }
134591
134592 /* The subquery uses a single slot of the FROM clause of the outer
134593 ** query. If the subquery has more than one element in its FROM clause,
134594 ** then expand the outer query to make space for it to hold all elements
@@ -136010,11 +136109,13 @@
136109 ** within the HAVING expression with a constant "1".
136110 */
136111 static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
136112 if( pExpr->op!=TK_AND ){
136113 Select *pS = pWalker->u.pSelect;
136114 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
136115 && ExprAlwaysFalse(pExpr)==0
136116 ){
136117 sqlite3 *db = pWalker->pParse->db;
136118 Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
136119 if( pNew ){
136120 Expr *pWhere = pS->pWhere;
136121 SWAP(Expr, *pNew, *pExpr);
@@ -137284,19 +137385,17 @@
137385 sqlite3ExprListDelete(db, pMinMaxOrderBy);
137386 #ifdef SQLITE_DEBUG
137387 if( pAggInfo && !db->mallocFailed ){
137388 for(i=0; i<pAggInfo->nColumn; i++){
137389 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
137390 assert( pExpr!=0 );
 
137391 assert( pExpr->pAggInfo==pAggInfo );
137392 assert( pExpr->iAgg==i );
137393 }
137394 for(i=0; i<pAggInfo->nFunc; i++){
137395 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
137396 assert( pExpr!=0 );
 
137397 assert( pExpr->pAggInfo==pAggInfo );
137398 assert( pExpr->iAgg==i );
137399 }
137400 }
137401 #endif
@@ -149427,11 +149526,11 @@
149526 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
149527 );
149528 pBtm = pTerm;
149529 pTop = 0;
149530 if( pTerm->wtFlags & TERM_LIKEOPT ){
149531 /* Range constraints that come from the LIKE optimization are
149532 ** always used in pairs. */
149533 pTop = &pTerm[1];
149534 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
149535 assert( pTop->wtFlags & TERM_LIKEOPT );
149536 assert( pTop->eOperator==WO_LT );
@@ -203997,10 +204096,11 @@
204096 int bIndirect; /* True if all changes are indirect */
204097 int bAutoAttach; /* True to auto-attach tables */
204098 int rc; /* Non-zero if an error has occurred */
204099 void *pFilterCtx; /* First argument to pass to xTableFilter */
204100 int (*xTableFilter)(void *pCtx, const char *zTab);
204101 i64 nMalloc; /* Number of bytes of data allocated */
204102 sqlite3_value *pZeroBlob; /* Value containing X'' */
204103 sqlite3_session *pNext; /* Next session object on same db. */
204104 SessionTable *pTable; /* List of attached tables */
204105 SessionHook hook; /* APIs to grab new and old data with */
204106 };
@@ -204380,10 +204480,30 @@
204480
204481 if( pnWrite ) *pnWrite += nByte;
204482 return SQLITE_OK;
204483 }
204484
204485 /*
204486 ** Allocate and return a pointer to a buffer nByte bytes in size. If
204487 ** pSession is not NULL, increase the sqlite3_session.nMalloc variable
204488 ** by the number of bytes allocated.
204489 */
204490 static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
204491 void *pRet = sqlite3_malloc64(nByte);
204492 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
204493 return pRet;
204494 }
204495
204496 /*
204497 ** Free buffer pFree, which must have been allocated by an earlier
204498 ** call to sessionMalloc64(). If pSession is not NULL, decrease the
204499 ** sqlite3_session.nMalloc counter by the number of bytes freed.
204500 */
204501 static void sessionFree(sqlite3_session *pSession, void *pFree){
204502 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
204503 sqlite3_free(pFree);
204504 }
204505
204506 /*
204507 ** This macro is used to calculate hash key values for data structures. In
204508 ** order to use this macro, the entire data structure must be represented
204509 ** as a series of unsigned integers. In order to calculate a hash-key value
@@ -204847,17 +204967,23 @@
204967 ** It is possible that a non-fatal OOM error occurs in this function. In
204968 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
204969 ** Growing the hash table in this case is a performance optimization only,
204970 ** it is not required for correct operation.
204971 */
204972 static int sessionGrowHash(
204973 sqlite3_session *pSession, /* For memory accounting. May be NULL */
204974 int bPatchset,
204975 SessionTable *pTab
204976 ){
204977 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
204978 int i;
204979 SessionChange **apNew;
204980 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
204981
204982 apNew = (SessionChange**)sessionMalloc64(
204983 pSession, sizeof(SessionChange*) * nNew
204984 );
204985 if( apNew==0 ){
204986 if( pTab->nChange==0 ){
204987 return SQLITE_ERROR;
204988 }
204989 return SQLITE_OK;
@@ -204874,11 +205000,11 @@
205000 p->pNext = apNew[iHash];
205001 apNew[iHash] = p;
205002 }
205003 }
205004
205005 sessionFree(pSession, pTab->apChange);
205006 pTab->nChange = nNew;
205007 pTab->apChange = apNew;
205008 }
205009
205010 return SQLITE_OK;
@@ -204908,10 +205034,11 @@
205034 **
205035 ** All returned buffers are part of the same single allocation, which must
205036 ** be freed using sqlite3_free() by the caller
205037 */
205038 static int sessionTableInfo(
205039 sqlite3_session *pSession, /* For memory accounting. May be NULL */
205040 sqlite3 *db, /* Database connection */
205041 const char *zDb, /* Name of attached database (e.g. "main") */
205042 const char *zThis, /* Table name */
205043 int *pnCol, /* OUT: number of columns */
205044 const char **pzTab, /* OUT: Copy of zThis */
@@ -204962,11 +205089,11 @@
205089 }
205090 rc = sqlite3_reset(pStmt);
205091
205092 if( rc==SQLITE_OK ){
205093 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
205094 pAlloc = sessionMalloc64(pSession, nByte);
205095 if( pAlloc==0 ){
205096 rc = SQLITE_NOMEM;
205097 }
205098 }
205099 if( rc==SQLITE_OK ){
@@ -205005,11 +205132,11 @@
205132 }else{
205133 *pazCol = 0;
205134 *pabPK = 0;
205135 *pnCol = 0;
205136 if( pzTab ) *pzTab = 0;
205137 sessionFree(pSession, azCol);
205138 }
205139 sqlite3_finalize(pStmt);
205140 return rc;
205141 }
205142
@@ -205027,11 +205154,11 @@
205154 */
205155 static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
205156 if( pTab->nCol==0 ){
205157 u8 *abPK;
205158 assert( pTab->azCol==0 || pTab->abPK==0 );
205159 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
205160 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
205161 );
205162 if( pSession->rc==SQLITE_OK ){
205163 int i;
205164 for(i=0; i<pTab->nCol; i++){
@@ -205118,11 +205245,11 @@
205245 pSession->rc = SQLITE_SCHEMA;
205246 return;
205247 }
205248
205249 /* Grow the hash table if required */
205250 if( sessionGrowHash(pSession, 0, pTab) ){
205251 pSession->rc = SQLITE_NOMEM;
205252 return;
205253 }
205254
205255 if( pTab->bStat1 ){
@@ -205185,11 +205312,11 @@
205312 rc = sessionSerializeValue(0, p, &nByte);
205313 if( rc!=SQLITE_OK ) goto error_out;
205314 }
205315
205316 /* Allocate the change object */
205317 pChange = (SessionChange *)sessionMalloc64(pSession, nByte);
205318 if( !pChange ){
205319 rc = SQLITE_NOMEM;
205320 goto error_out;
205321 }else{
205322 memset(pChange, 0, sizeof(SessionChange));
@@ -205558,11 +205685,11 @@
205685 int bHasPk = 0;
205686 int bMismatch = 0;
205687 int nCol; /* Columns in zFrom.zTbl */
205688 u8 *abPK;
205689 const char **azCol = 0;
205690 rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
205691 if( rc==SQLITE_OK ){
205692 if( pTo->nCol!=nCol ){
205693 bMismatch = 1;
205694 }else{
205695 int i;
@@ -205656,11 +205783,11 @@
205783
205784 /*
205785 ** Free the list of table objects passed as the first argument. The contents
205786 ** of the changed-rows hash tables are also deleted.
205787 */
205788 static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
205789 SessionTable *pNext;
205790 SessionTable *pTab;
205791
205792 for(pTab=pList; pTab; pTab=pNext){
205793 int i;
@@ -205668,16 +205795,16 @@
205795 for(i=0; i<pTab->nChange; i++){
205796 SessionChange *p;
205797 SessionChange *pNextChange;
205798 for(p=pTab->apChange[i]; p; p=pNextChange){
205799 pNextChange = p->pNext;
205800 sessionFree(pSession, p);
205801 }
205802 }
205803 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
205804 sessionFree(pSession, pTab->apChange);
205805 sessionFree(pSession, pTab);
205806 }
205807 }
205808
205809 /*
205810 ** Delete a session object previously allocated using sqlite3session_create().
@@ -205701,13 +205828,15 @@
205828 sqlite3_mutex_leave(sqlite3_db_mutex(db));
205829 sqlite3ValueFree(pSession->pZeroBlob);
205830
205831 /* Delete all attached table objects. And the contents of their
205832 ** associated hash-tables. */
205833 sessionDeleteTable(pSession, pSession->pTable);
205834
205835 /* Assert that all allocations have been freed and then free the
205836 ** session object itself. */
205837 assert( pSession->nMalloc==0 );
205838 sqlite3_free(pSession);
205839 }
205840
205841 /*
205842 ** Set a table filter on a Session Object.
@@ -205750,11 +205879,12 @@
205879 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
205880 }
205881
205882 if( !pTab ){
205883 /* Allocate new SessionTable object. */
205884 int nByte = sizeof(SessionTable) + nName + 1;
205885 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
205886 if( !pTab ){
205887 rc = SQLITE_NOMEM;
205888 }else{
205889 /* Populate the new SessionTable object and link it into the list.
205890 ** The new object must be linked onto the end of the list, not
@@ -206347,11 +206477,11 @@
206477 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
206478 int nRewind = buf.nBuf; /* Initial size of write buffer */
206479 int nNoop; /* Size of buffer after writing tbl header */
206480
206481 /* Check the table schema is still Ok. */
206482 rc = sessionTableInfo(0, db, pSession->zDb, zName, &nCol, 0,&azCol,&abPK);
206483 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
206484 rc = SQLITE_SCHEMA;
206485 }
206486
206487 /* Write a table header */
@@ -206521,10 +206651,17 @@
206651 }
206652 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
206653
206654 return (ret==0);
206655 }
206656
206657 /*
206658 ** Return the amount of heap memory in use.
206659 */
206660 SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
206661 return pSession->nMalloc;
206662 }
206663
206664 /*
206665 ** Do the work for either sqlite3changeset_start() or start_strm().
206666 */
206667 static int sessionChangesetStart(
@@ -208333,11 +208470,11 @@
208470 }else{
208471 int nMinCol = 0;
208472 int i;
208473
208474 sqlite3changeset_pk(pIter, &abPK, 0);
208475 rc = sessionTableInfo(0,
208476 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
208477 );
208478 if( rc!=SQLITE_OK ) break;
208479 for(i=0; i<sApply.nCol; i++){
208480 if( sApply.abPK[i] ) nMinCol = i+1;
@@ -208812,11 +208949,11 @@
208949 rc = SQLITE_SCHEMA;
208950 break;
208951 }
208952 }
208953
208954 if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
208955 rc = SQLITE_NOMEM;
208956 break;
208957 }
208958 iHash = sessionChangeHash(
208959 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
@@ -208998,11 +209135,11 @@
209135 /*
209136 ** Delete a changegroup object.
209137 */
209138 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
209139 if( pGrp ){
209140 sessionDeleteTable(0, pGrp->pList);
209141 sqlite3_free(pGrp);
209142 }
209143 }
209144
209145 /*
@@ -209399,11 +209536,11 @@
209536 /*
209537 ** Destroy a rebaser object
209538 */
209539 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
209540 if( p ){
209541 sessionDeleteTable(0, p->grp.pList);
209542 sqlite3_free(p);
209543 }
209544 }
209545
209546 /*
@@ -220128,18 +220265,14 @@
220265 if( pbNewTerm ) *pbNewTerm = 1;
220266 }
220267 }else{
220268 /* The following could be done by calling fts5SegIterLoadNPos(). But
220269 ** this block is particularly performance critical, so equivalent
220270 ** code is inlined. */
 
 
 
 
220271 int nSz;
220272 assert( p->rc==SQLITE_OK );
220273 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn );
220274 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
220275 pIter->bDel = (nSz & 0x0001);
220276 pIter->nPos = nSz>>1;
220277 assert_nc( pIter->nPos>=0 );
220278 }
@@ -227551,11 +227684,11 @@
227684 int nArg, /* Number of args */
227685 sqlite3_value **apUnused /* Function arguments */
227686 ){
227687 assert( nArg==0 );
227688 UNUSED_PARAM2(nArg, apUnused);
227689 sqlite3_result_text(pCtx, "fts5: 2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f", -1, SQLITE_TRANSIENT);
227690 }
227691
227692 /*
227693 ** Return true if zName is the extension on one of the shadow tables used
227694 ** by this module.
@@ -232477,12 +232610,12 @@
232610 }
232611 #endif /* SQLITE_CORE */
232612 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
232613
232614 /************** End of stmt.c ************************************************/
232615 #if __LINE__!=232615
232616 #undef SQLITE_SOURCE_ID
232617 #define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124dalt2"
232618 #endif
232619 /* Return the source-id for this library */
232620 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
232621 /************************** End of sqlite3.c ******************************/
232622
+10 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126126
#define SQLITE_VERSION "3.35.0"
127127
#define SQLITE_VERSION_NUMBER 3035000
128
-#define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660"
128
+#define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
@@ -3497,10 +3497,11 @@
34973497
** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
34983498
** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
34993499
** that uses dot-files in place of posix advisory locking.
35003500
** <tr><td> file:data.db?mode=readonly <td>
35013501
** An error. "readonly" is not a valid option for the "mode" parameter.
3502
+** Use "ro" instead: "file:data.db?mode=ro".
35023503
** </table>
35033504
**
35043505
** ^URI hexadecimal escape sequences (%HH) are supported within the path and
35053506
** query components of a URI. A hexadecimal escape sequence consists of a
35063507
** percent sign - "%" - followed by exactly two hexadecimal digits
@@ -10437,10 +10438,18 @@
1043710438
** guaranteed that a call to sqlite3session_changeset() will return a
1043810439
** changeset containing zero changes.
1043910440
*/
1044010441
SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
1044110442
10443
+/*
10444
+** CAPI3REF: Query for the amount of heap memory used by a session object.
10445
+**
10446
+** This API returns the total amount of heap memory in bytes currently
10447
+** used by the session object passed as the only argument.
10448
+*/
10449
+SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
10450
+
1044210451
/*
1044310452
** CAPI3REF: Create An Iterator To Traverse A Changeset
1044410453
** CONSTRUCTOR: sqlite3_changeset_iter
1044510454
**
1044610455
** Create an iterator used to iterate through the contents of a changeset.
1044710456
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.35.0"
127 #define SQLITE_VERSION_NUMBER 3035000
128 #define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -3497,10 +3497,11 @@
3497 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3498 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3499 ** that uses dot-files in place of posix advisory locking.
3500 ** <tr><td> file:data.db?mode=readonly <td>
3501 ** An error. "readonly" is not a valid option for the "mode" parameter.
 
3502 ** </table>
3503 **
3504 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3505 ** query components of a URI. A hexadecimal escape sequence consists of a
3506 ** percent sign - "%" - followed by exactly two hexadecimal digits
@@ -10437,10 +10438,18 @@
10437 ** guaranteed that a call to sqlite3session_changeset() will return a
10438 ** changeset containing zero changes.
10439 */
10440 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10441
 
 
 
 
 
 
 
 
10442 /*
10443 ** CAPI3REF: Create An Iterator To Traverse A Changeset
10444 ** CONSTRUCTOR: sqlite3_changeset_iter
10445 **
10446 ** Create an iterator used to iterate through the contents of a changeset.
10447
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.35.0"
127 #define SQLITE_VERSION_NUMBER 3035000
128 #define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -3497,10 +3497,11 @@
3497 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3498 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3499 ** that uses dot-files in place of posix advisory locking.
3500 ** <tr><td> file:data.db?mode=readonly <td>
3501 ** An error. "readonly" is not a valid option for the "mode" parameter.
3502 ** Use "ro" instead: "file:data.db?mode=ro".
3503 ** </table>
3504 **
3505 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3506 ** query components of a URI. A hexadecimal escape sequence consists of a
3507 ** percent sign - "%" - followed by exactly two hexadecimal digits
@@ -10437,10 +10438,18 @@
10438 ** guaranteed that a call to sqlite3session_changeset() will return a
10439 ** changeset containing zero changes.
10440 */
10441 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10442
10443 /*
10444 ** CAPI3REF: Query for the amount of heap memory used by a session object.
10445 **
10446 ** This API returns the total amount of heap memory in bytes currently
10447 ** used by the session object passed as the only argument.
10448 */
10449 SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
10450
10451 /*
10452 ** CAPI3REF: Create An Iterator To Traverse A Changeset
10453 ** CONSTRUCTOR: sqlite3_changeset_iter
10454 **
10455 ** Create an iterator used to iterate through the contents of a changeset.
10456

Keyboard Shortcuts

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