Fossil SCM

Update the built-in SQLite to the latest 3.35 alpha version that uses the EXISTS-to-IN optimization. EXISTS operators are used a lot in Fossil, so Fossil should give the new optimization a good real-world test.

drh 2021-01-18 15:02 trunk
Commit 74dd3187a9d53f6300d42f069d4e8d4eb28a570712b4a4629d20c63b638e6c29
3 files changed +2 -1 +430 -53 +1 -1
+2 -1
--- src/shell.c
+++ src/shell.c
@@ -12069,10 +12069,11 @@
1206912069
}
1207012070
}
1207112071
if( azArg==0 ) break;
1207212072
for(i=0; i<nArg; i++){
1207312073
int w = aExplainWidth[i];
12074
+ if( i==nArg-1 ) w = 0;
1207412075
if( azArg[i] && strlenChar(azArg[i])>w ){
1207512076
w = strlenChar(azArg[i]);
1207612077
}
1207712078
if( i==1 && p->aiIndent && p->pStmt ){
1207812079
if( p->iIndent<p->nIndent ){
@@ -20036,11 +20037,11 @@
2003620037
switch(testctrl){
2003720038
2003820039
/* sqlite3_test_control(int, db, int) */
2003920040
case SQLITE_TESTCTRL_OPTIMIZATIONS:
2004020041
if( nArg==3 ){
20041
- int opt = (int)strtol(azArg[2], 0, 0);
20042
+ unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
2004220043
rc2 = sqlite3_test_control(testctrl, p->db, opt);
2004320044
isOk = 3;
2004420045
}
2004520046
break;
2004620047
2004720048
--- src/shell.c
+++ src/shell.c
@@ -12069,10 +12069,11 @@
12069 }
12070 }
12071 if( azArg==0 ) break;
12072 for(i=0; i<nArg; i++){
12073 int w = aExplainWidth[i];
 
12074 if( azArg[i] && strlenChar(azArg[i])>w ){
12075 w = strlenChar(azArg[i]);
12076 }
12077 if( i==1 && p->aiIndent && p->pStmt ){
12078 if( p->iIndent<p->nIndent ){
@@ -20036,11 +20037,11 @@
20036 switch(testctrl){
20037
20038 /* sqlite3_test_control(int, db, int) */
20039 case SQLITE_TESTCTRL_OPTIMIZATIONS:
20040 if( nArg==3 ){
20041 int opt = (int)strtol(azArg[2], 0, 0);
20042 rc2 = sqlite3_test_control(testctrl, p->db, opt);
20043 isOk = 3;
20044 }
20045 break;
20046
20047
--- src/shell.c
+++ src/shell.c
@@ -12069,10 +12069,11 @@
12069 }
12070 }
12071 if( azArg==0 ) break;
12072 for(i=0; i<nArg; i++){
12073 int w = aExplainWidth[i];
12074 if( i==nArg-1 ) w = 0;
12075 if( azArg[i] && strlenChar(azArg[i])>w ){
12076 w = strlenChar(azArg[i]);
12077 }
12078 if( i==1 && p->aiIndent && p->pStmt ){
12079 if( p->iIndent<p->nIndent ){
@@ -20036,11 +20037,11 @@
20037 switch(testctrl){
20038
20039 /* sqlite3_test_control(int, db, int) */
20040 case SQLITE_TESTCTRL_OPTIMIZATIONS:
20041 if( nArg==3 ){
20042 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
20043 rc2 = sqlite3_test_control(testctrl, p->db, opt);
20044 isOk = 3;
20045 }
20046 break;
20047
20048
+430 -53
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -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 "2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553b19f"
1191
+#define SQLITE_SOURCE_ID "2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18df9a04"
11921192
11931193
/*
11941194
** CAPI3REF: Run-Time Library Version Numbers
11951195
** KEYWORDS: sqlite3_version sqlite3_sourceid
11961196
**
@@ -14775,10 +14775,11 @@
1477514775
typedef struct Lookaside Lookaside;
1477614776
typedef struct LookasideSlot LookasideSlot;
1477714777
typedef struct Module Module;
1477814778
typedef struct NameContext NameContext;
1477914779
typedef struct Parse Parse;
14780
+typedef struct ParseCleanup ParseCleanup;
1478014781
typedef struct PreUpdate PreUpdate;
1478114782
typedef struct PrintfArguments PrintfArguments;
1478214783
typedef struct RenameToken RenameToken;
1478314784
typedef struct RowSet RowSet;
1478414785
typedef struct Savepoint Savepoint;
@@ -16947,11 +16948,11 @@
1694716948
u32 nSchemaLock; /* Do not reset the schema when non-zero */
1694816949
unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
1694916950
int errCode; /* Most recent error code (SQLITE_*) */
1695016951
int errMask; /* & result codes with this before returning */
1695116952
int iSysErrno; /* Errno value from last system error */
16952
- u16 dbOptFlags; /* Flags to enable/disable optimizations */
16953
+ u32 dbOptFlags; /* Flags to enable/disable optimizations */
1695316954
u8 enc; /* Text encoding */
1695416955
u8 autoCommit; /* The auto-commit flag. */
1695516956
u8 temp_store; /* 1: file 2: memory 0: default */
1695616957
u8 mallocFailed; /* True if we have seen a malloc failure */
1695716958
u8 bBenignMalloc; /* Do not require OOMs if true */
@@ -17154,28 +17155,30 @@
1715417155
/*
1715517156
** Bits of the sqlite3.dbOptFlags field that are used by the
1715617157
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
1715717158
** selectively disable various optimizations.
1715817159
*/
17159
-#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
17160
-#define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */
17161
-#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
17162
-#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
17163
-#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
17164
-#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
17165
-#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
17166
-#define SQLITE_Transitive 0x0080 /* Transitive constraints */
17167
-#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
17168
-#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
17169
-#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
17170
-#define SQLITE_Stat4 0x0800 /* Use STAT4 data */
17171
- /* TH3 expects the Stat4 ^^^^^^ value to be 0x0800. Don't change it */
17172
-#define SQLITE_PushDown 0x1000 /* The push-down optimization */
17173
-#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
17174
-#define SQLITE_SkipScan 0x4000 /* Skip-scans */
17175
-#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
17176
-#define SQLITE_AllOpts 0xffff /* All optimizations */
17160
+#define SQLITE_QueryFlattener 0x00000001 /* Query flattening */
17161
+#define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */
17162
+#define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */
17163
+#define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */
17164
+#define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */
17165
+#define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */
17166
+#define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */
17167
+#define SQLITE_Transitive 0x00000080 /* Transitive constraints */
17168
+#define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */
17169
+#define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17170
+#define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */
17171
+#define SQLITE_Stat4 0x00000800 /* Use STAT4 data */
17172
+ /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
17173
+#define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17174
+#define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */
17175
+#define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17176
+#define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */
17177
+#define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */
17178
+#define SQLITE_ExistsToIN 0x00020000 /* The EXISTS-to-IN optimization */
17179
+#define SQLITE_AllOpts 0xffffffff /* All optimizations */
1717717180
1717817181
/*
1717917182
** Macros for testing whether or not optimizations are enabled or disabled.
1718017183
*/
1718117184
#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
@@ -17638,11 +17641,10 @@
1763817641
char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
1763917642
VTable *pVTable; /* List of VTable objects. */
1764017643
#endif
1764117644
Trigger *pTrigger; /* List of triggers stored in pSchema */
1764217645
Schema *pSchema; /* Schema that contains this table */
17643
- Table *pNextZombie; /* Next on the Parse.pZombieTab list */
1764417646
};
1764517647
1764617648
/*
1764717649
** Allowed values for Table.tabFlags.
1764817650
**
@@ -18800,10 +18802,21 @@
1880018802
# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
1880118803
# define DbMaskAllZero(M) (M)==0
1880218804
# define DbMaskNonZero(M) (M)!=0
1880318805
#endif
1880418806
18807
+/*
18808
+** An instance of the ParseCleanup object specifies an operation that
18809
+** should be performed after parsing to deallocation resources obtained
18810
+** during the parse and which are no longer needed.
18811
+*/
18812
+struct ParseCleanup {
18813
+ ParseCleanup *pNext; /* Next cleanup task */
18814
+ void *pPtr; /* Pointer to object to deallocate */
18815
+ void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
18816
+};
18817
+
1880518818
/*
1880618819
** An SQL parser context. A copy of this structure is passed through
1880718820
** the parser and down into all the parser action routine in order to
1880818821
** carry around information that is global to the entire parse.
1880918822
**
@@ -18831,10 +18844,13 @@
1883118844
u8 mayAbort; /* True if statement may throw an ABORT exception */
1883218845
u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
1883318846
u8 okConstFactor; /* OK to factor out constants */
1883418847
u8 disableLookaside; /* Number of times lookaside has been disabled */
1883518848
u8 disableVtab; /* Disable all virtual tables for this parse */
18849
+#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
18850
+ u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
18851
+#endif
1883618852
int nRangeReg; /* Size of the temporary register block */
1883718853
int iRangeReg; /* First register in temporary register block */
1883818854
int nErr; /* Number of errors seen */
1883918855
int nTab; /* Number of previously allocated VDBE cursors */
1884018856
int nMem; /* Number of memory cells used so far */
@@ -18909,14 +18925,13 @@
1890918925
const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
1891018926
#ifndef SQLITE_OMIT_VIRTUALTABLE
1891118927
Token sArg; /* Complete text of a module argument */
1891218928
Table **apVtabLock; /* Pointer to virtual tables needing locking */
1891318929
#endif
18914
- Table *pZombieTab; /* List of Table objects to delete after code gen */
1891518930
TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
1891618931
With *pWith; /* Current WITH clause, or NULL */
18917
- With *pWithToFree; /* Free this WITH object at the end of the parse */
18932
+ ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
1891818933
#ifndef SQLITE_OMIT_ALTERTABLE
1891918934
RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
1892018935
#endif
1892118936
};
1892218937
@@ -19780,10 +19795,11 @@
1978019795
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
1978119796
SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
1978219797
SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
1978319798
SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
1978419799
SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
19800
+SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
1978519801
SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
1978619802
SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
1978719803
SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
1978819804
SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
1978919805
#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
@@ -20279,10 +20295,11 @@
2027920295
SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
2028020296
SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
2028120297
SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
2028220298
SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
2028320299
SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
20300
+SQLITE_PRIVATE void sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
2028420301
#ifdef SQLITE_ENABLE_NORMALIZE
2028520302
SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
2028620303
#endif
2028720304
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
2028820305
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
@@ -100513,11 +100530,22 @@
100513100530
Parse *pParse, /* Parsing context */
100514100531
Expr *pExpr, /* Add the "COLLATE" clause to this expression */
100515100532
const Token *pCollName, /* Name of collating sequence */
100516100533
int dequote /* True to dequote pCollName */
100517100534
){
100518
- if( pCollName->n>0 ){
100535
+ assert( pExpr!=0 || pParse->db->mallocFailed );
100536
+ if( pExpr==0 ) return 0;
100537
+ if( pExpr->op==TK_VECTOR ){
100538
+ ExprList *pList = pExpr->x.pList;
100539
+ if( ALWAYS(pList!=0) ){
100540
+ int i;
100541
+ for(i=0; i<pList->nExpr; i++){
100542
+ pList->a[i].pExpr = sqlite3ExprAddCollateToken(pParse,pList->a[i].pExpr,
100543
+ pCollName, dequote);
100544
+ }
100545
+ }
100546
+ }else if( pCollName->n>0 ){
100519100547
Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
100520100548
if( pNew ){
100521100549
pNew->pLeft = pExpr;
100522100550
pNew->flags |= EP_Collate|EP_Skip;
100523100551
pExpr = pNew;
@@ -121907,11 +121935,13 @@
121907121935
121908121936
pInfo = pToplevel->pAinc;
121909121937
while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
121910121938
if( pInfo==0 ){
121911121939
pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
121912
- if( pInfo==0 ) return 0;
121940
+ sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
121941
+ testcase( pParse->earlyCleanup );
121942
+ if( pParse->db->mallocFailed ) return 0;
121913121943
pInfo->pNext = pToplevel->pAinc;
121914121944
pToplevel->pAinc = pInfo;
121915121945
pInfo->pTab = pTab;
121916121946
pInfo->iDb = iDb;
121917121947
pToplevel->nMem++; /* Register to hold name of table */
@@ -130048,19 +130078,66 @@
130048130078
while( pThis ){
130049130079
AggInfo *pNext = pThis->pNext;
130050130080
agginfoFree(db, pThis);
130051130081
pThis = pNext;
130052130082
}
130083
+ while( pParse->pCleanup ){
130084
+ ParseCleanup *pCleanup = pParse->pCleanup;
130085
+ pParse->pCleanup = pCleanup->pNext;
130086
+ pCleanup->xCleanup(db, pCleanup->pPtr);
130087
+ sqlite3DbFree(db, pCleanup);
130088
+ }
130053130089
sqlite3DbFree(db, pParse->aLabel);
130054
- sqlite3ExprListDelete(db, pParse->pConstExpr);
130090
+ if( pParse->pConstExpr ){
130091
+ sqlite3ExprListDelete(db, pParse->pConstExpr);
130092
+ }
130055130093
if( db ){
130056130094
assert( db->lookaside.bDisable >= pParse->disableLookaside );
130057130095
db->lookaside.bDisable -= pParse->disableLookaside;
130058130096
db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
130059130097
}
130060130098
pParse->disableLookaside = 0;
130061130099
}
130100
+
130101
+/*
130102
+** Add a new cleanup operation to a Parser. The cleanup should happen when
130103
+** the parser object is destroyed. But, beware: the cleanup might happen
130104
+** immediately.
130105
+**
130106
+** Use this mechanism for uncommon cleanups. There is a higher setup
130107
+** cost for this mechansim (an extra malloc), so it should not be used
130108
+** for common cleanups that happen on most calls. But for less
130109
+** common cleanups, we save a single NULL-pointer comparison in
130110
+** sqlite3ParserReset(), which reduces the total CPU cycle count.
130111
+**
130112
+** If a memory allocation error occurs, then the cleanup happens immediately.
130113
+** When eithr SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
130114
+** pParse->earlyCleanup flag is set in that case. Calling code show verify
130115
+** that test cases exist for which this happens, to guard against possible
130116
+** use-after-free errors following an OOM. The preferred way to do this is
130117
+** to immediately follow the call to this routine with:
130118
+**
130119
+** testcase( pParse->earlyCleanup );
130120
+*/
130121
+SQLITE_PRIVATE void sqlite3ParserAddCleanup(
130122
+ Parse *pParse, /* Destroy when this Parser finishes */
130123
+ void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
130124
+ void *pPtr /* Pointer to object to be cleaned up */
130125
+){
130126
+ ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
130127
+ if( pCleanup ){
130128
+ pCleanup->pNext = pParse->pCleanup;
130129
+ pParse->pCleanup = pCleanup;
130130
+ pCleanup->pPtr = pPtr;
130131
+ pCleanup->xCleanup = xCleanup;
130132
+ }else{
130133
+ xCleanup(pParse->db, pPtr);
130134
+#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
130135
+ pParse->earlyCleanup = 1;
130136
+#endif
130137
+ }
130138
+}
130062130139
130063130140
/*
130064130141
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
130065130142
*/
130066130143
static int sqlite3Prepare(
@@ -134588,12 +134665,14 @@
134588134665
*/
134589134666
if( ALWAYS(pSubitem->pTab!=0) ){
134590134667
Table *pTabToDel = pSubitem->pTab;
134591134668
if( pTabToDel->nTabRef==1 ){
134592134669
Parse *pToplevel = sqlite3ParseToplevel(pParse);
134593
- pTabToDel->pNextZombie = pToplevel->pZombieTab;
134594
- pToplevel->pZombieTab = pTabToDel;
134670
+ sqlite3ParserAddCleanup(pToplevel,
134671
+ (void(*)(sqlite3*,void*))sqlite3DeleteTable,
134672
+ pTabToDel);
134673
+ testcase( pToplevel->earlyCleanup );
134595134674
}else{
134596134675
pTabToDel->nTabRef--;
134597134676
}
134598134677
pSubitem->pTab = 0;
134599134678
}
@@ -135086,11 +135165,15 @@
135086135165
u8 sortFlags = 0;
135087135166
135088135167
assert( *ppMinMax==0 );
135089135168
assert( pFunc->op==TK_AGG_FUNCTION );
135090135169
assert( !IsWindowFunc(pFunc) );
135091
- if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
135170
+ if( pEList==0
135171
+ || pEList->nExpr!=1
135172
+ || ExprHasProperty(pFunc, EP_WinFunc)
135173
+ || OptimizationDisabled(db, SQLITE_MinMaxOpt)
135174
+ ){
135092135175
return eRet;
135093135176
}
135094135177
zFunc = pFunc->u.zToken;
135095135178
if( sqlite3StrICmp(zFunc, "min")==0 ){
135096135179
eRet = WHERE_ORDERBY_MIN;
@@ -135304,16 +135387,20 @@
135304135387
** should be freed along with the Parse object. In other cases, when
135305135388
** bFree==0, the With object will be freed along with the SELECT
135306135389
** statement with which it is associated.
135307135390
*/
135308135391
SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
135309
- assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
135310135392
if( pWith ){
135311135393
assert( pParse->pWith!=pWith );
135312135394
pWith->pOuter = pParse->pWith;
135313135395
pParse->pWith = pWith;
135314
- if( bFree ) pParse->pWithToFree = pWith;
135396
+ if( bFree ){
135397
+ sqlite3ParserAddCleanup(pParse,
135398
+ (void(*)(sqlite3*,void*))sqlite3WithDelete,
135399
+ pWith);
135400
+ testcase( pParse->earlyCleanup );
135401
+ }
135315135402
}
135316135403
}
135317135404
135318135405
/*
135319135406
** This function checks if argument pFrom refers to a CTE declared by
@@ -137022,10 +137109,14 @@
137022137109
#if SELECTTRACE_ENABLED
137023137110
if( sqlite3SelectTrace & 0x400 ){
137024137111
int ii;
137025137112
SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
137026137113
sqlite3TreeViewSelect(0, p, 0);
137114
+ if( minMaxFlag ){
137115
+ sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
137116
+ sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
137117
+ }
137027137118
for(ii=0; ii<pAggInfo->nColumn; ii++){
137028137119
sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
137029137120
ii, pAggInfo->aCol[ii].iMem);
137030137121
sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
137031137122
}
@@ -137211,11 +137302,11 @@
137211137302
VdbeComment((v, "indicate data in accumulator"));
137212137303
137213137304
/* End of the loop
137214137305
*/
137215137306
if( groupBySort ){
137216
- sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx, addrTopOfLoop);
137307
+ sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
137217137308
VdbeCoverage(v);
137218137309
}else{
137219137310
sqlite3WhereEnd(pWInfo);
137220137311
sqlite3VdbeChangeToNoop(v, addrSortingIdx);
137221137312
}
@@ -137323,11 +137414,10 @@
137323137414
sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
137324137415
sqlite3VdbeAddOp1(v, OP_Close, iCsr);
137325137416
explainSimpleCount(pParse, pTab, pBest);
137326137417
}else{
137327137418
int regAcc = 0; /* "populate accumulators" flag */
137328
- int addrSkip;
137329137419
137330137420
/* If there are accumulator registers but no min() or max() functions
137331137421
** without FILTER clauses, allocate register regAcc. Register regAcc
137332137422
** will contain 0 the first time the inner loop runs, and 1 thereafter.
137333137423
** The code generated by updateAccumulator() uses this to ensure
@@ -137372,13 +137462,12 @@
137372137462
if( pWInfo==0 ){
137373137463
goto select_end;
137374137464
}
137375137465
updateAccumulator(pParse, regAcc, pAggInfo);
137376137466
if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
137377
- addrSkip = sqlite3WhereOrderByLimitOptLabel(pWInfo);
137378
- if( addrSkip!=sqlite3WhereContinueLabel(pWInfo) ){
137379
- sqlite3VdbeGoto(v, addrSkip);
137467
+ if( minMaxFlag ){
137468
+ sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
137380137469
}
137381137470
sqlite3WhereEnd(pWInfo);
137382137471
finalizeAggFunctions(pParse, pAggInfo);
137383137472
}
137384137473
@@ -138648,11 +138737,10 @@
138648138737
pPrg->aColmask[0] = pSubParse->oldmask;
138649138738
pPrg->aColmask[1] = pSubParse->newmask;
138650138739
sqlite3VdbeDelete(v);
138651138740
}
138652138741
138653
- assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
138654138742
assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
138655138743
sqlite3ParserReset(pSubParse);
138656138744
sqlite3StackFree(db, pSubParse);
138657138745
138658138746
return pPrg;
@@ -146346,10 +146434,275 @@
146346146434
}
146347146435
if( mPrereq==0 ) return 0; /* No table references */
146348146436
if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
146349146437
return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
146350146438
}
146439
+
146440
+/*
146441
+** Expression callback for exprUsesSrclist().
146442
+*/
146443
+static int exprUsesSrclistCb(Walker *p, Expr *pExpr){
146444
+ if( pExpr->op==TK_COLUMN ){
146445
+ SrcList *pSrc = p->u.pSrcList;
146446
+ int iCsr = pExpr->iTable;
146447
+ int ii;
146448
+ for(ii=0; ii<pSrc->nSrc; ii++){
146449
+ if( pSrc->a[ii].iCursor==iCsr ){
146450
+ return p->eCode ? WRC_Abort : WRC_Continue;
146451
+ }
146452
+ }
146453
+ return p->eCode ? WRC_Continue : WRC_Abort;
146454
+ }
146455
+ return WRC_Continue;
146456
+}
146457
+
146458
+/*
146459
+** Select callback for exprUsesSrclist().
146460
+*/
146461
+static int exprUsesSrclistSelectCb(Walker *p, Select *pSelect){
146462
+ return WRC_Abort;
146463
+}
146464
+
146465
+/*
146466
+** This function always returns true if expression pExpr contains
146467
+** a sub-select.
146468
+**
146469
+** If there is no sub-select in pExpr, then return true if pExpr
146470
+** contains a TK_COLUMN node for a table that is (bUses==1)
146471
+** or is not (bUses==0) in pSrc.
146472
+**
146473
+** Said another way:
146474
+**
146475
+** bUses Return Meaning
146476
+** -------- ------ ------------------------------------------------
146477
+**
146478
+** bUses==1 true pExpr contains either a sub-select or a
146479
+** TK_COLUMN referencing pSrc.
146480
+**
146481
+** bUses==1 false pExpr contains no sub-selects and all TK_COLUMN
146482
+** nodes reference tables not found in pSrc
146483
+**
146484
+** bUses==0 true pExpr contains either a sub-select or a TK_COLUMN
146485
+** that references a table not in pSrc.
146486
+**
146487
+** bUses==0 false pExpr contains no sub-selects and all TK_COLUMN
146488
+** nodes reference pSrc
146489
+*/
146490
+static int exprUsesSrclist(SrcList *pSrc, Expr *pExpr, int bUses){
146491
+ Walker sWalker;
146492
+ memset(&sWalker, 0, sizeof(Walker));
146493
+ sWalker.eCode = bUses;
146494
+ sWalker.u.pSrcList = pSrc;
146495
+ sWalker.xExprCallback = exprUsesSrclistCb;
146496
+ sWalker.xSelectCallback = exprUsesSrclistSelectCb;
146497
+ return (sqlite3WalkExpr(&sWalker, pExpr)==WRC_Abort);
146498
+}
146499
+
146500
+/*
146501
+** Context object used by exprExistsToInIter() as it iterates through an
146502
+** expression tree.
146503
+*/
146504
+struct ExistsToInCtx {
146505
+ SrcList *pSrc; /* The tables in an EXISTS(SELECT ... FROM <here> ...) */
146506
+ Expr *pInLhs; /* OUT: Use this as the LHS of the IN operator */
146507
+ Expr *pEq; /* OUT: The == term that include pInLhs */
146508
+ Expr **ppAnd; /* OUT: The AND operator that includes pEq as a child */
146509
+ Expr **ppParent; /* The AND operator currently being examined */
146510
+};
146511
+
146512
+/*
146513
+** Iterate through all AND connected nodes in the expression tree
146514
+** headed by (*ppExpr), populating the structure passed as the first
146515
+** argument with the values required by exprAnalyzeExistsFindEq().
146516
+**
146517
+** This function returns non-zero if the expression tree does not meet
146518
+** the two conditions described by the header comment for
146519
+** exprAnalyzeExistsFindEq(), or zero if it does.
146520
+*/
146521
+static int exprExistsToInIter(struct ExistsToInCtx *p, Expr **ppExpr){
146522
+ Expr *pExpr = *ppExpr;
146523
+ switch( pExpr->op ){
146524
+ case TK_AND:
146525
+ p->ppParent = ppExpr;
146526
+ if( exprExistsToInIter(p, &pExpr->pLeft) ) return 1;
146527
+ p->ppParent = ppExpr;
146528
+ if( exprExistsToInIter(p, &pExpr->pRight) ) return 1;
146529
+ break;
146530
+ case TK_EQ: {
146531
+ int bLeft = exprUsesSrclist(p->pSrc, pExpr->pLeft, 0);
146532
+ int bRight = exprUsesSrclist(p->pSrc, pExpr->pRight, 0);
146533
+ if( bLeft || bRight ){
146534
+ if( (bLeft && bRight) || p->pInLhs ) return 1;
146535
+ p->pInLhs = bLeft ? pExpr->pLeft : pExpr->pRight;
146536
+ if( exprUsesSrclist(p->pSrc, p->pInLhs, 1) ) return 1;
146537
+ p->pEq = pExpr;
146538
+ p->ppAnd = p->ppParent;
146539
+ }
146540
+ break;
146541
+ }
146542
+ default:
146543
+ if( exprUsesSrclist(p->pSrc, pExpr, 0) ){
146544
+ return 1;
146545
+ }
146546
+ break;
146547
+ }
146548
+
146549
+ return 0;
146550
+}
146551
+
146552
+/*
146553
+** This function is used by exprAnalyzeExists() when creating virtual IN(...)
146554
+** terms equivalent to user-supplied EXIST(...) clauses. It splits the WHERE
146555
+** clause of the Select object passed as the first argument into one or more
146556
+** expressions joined by AND operators, and then tests if the following are
146557
+** true:
146558
+**
146559
+** 1. Exactly one of the AND separated terms refers to the outer
146560
+** query, and it is an == (TK_EQ) expression.
146561
+**
146562
+** 2. Only one side of the == expression refers to the outer query, and
146563
+** it does not refer to any columns from the inner query.
146564
+**
146565
+** If both these conditions are true, then a pointer to the side of the ==
146566
+** expression that refers to the outer query is returned. The caller will
146567
+** use this expression as the LHS of the IN(...) virtual term. Or, if one
146568
+** or both of the above conditions are not true, NULL is returned.
146569
+**
146570
+** If non-NULL is returned and ppEq is non-NULL, *ppEq is set to point
146571
+** to the == expression node before returning. If pppAnd is non-NULL and
146572
+** the == node is not the root of the WHERE clause, then *pppAnd is set
146573
+** to point to the pointer to the AND node that is the parent of the ==
146574
+** node within the WHERE expression tree.
146575
+*/
146576
+static Expr *exprAnalyzeExistsFindEq(
146577
+ Select *pSel, /* The SELECT of the EXISTS */
146578
+ Expr **ppEq, /* OUT: == node from WHERE clause */
146579
+ Expr ***pppAnd /* OUT: Pointer to parent of ==, if any */
146580
+){
146581
+ struct ExistsToInCtx ctx;
146582
+ memset(&ctx, 0, sizeof(ctx));
146583
+ ctx.pSrc = pSel->pSrc;
146584
+ if( exprExistsToInIter(&ctx, &pSel->pWhere) ){
146585
+ return 0;
146586
+ }
146587
+ if( ppEq ) *ppEq = ctx.pEq;
146588
+ if( pppAnd ) *pppAnd = ctx.ppAnd;
146589
+ return ctx.pInLhs;
146590
+}
146591
+
146592
+/*
146593
+** Term idxTerm of the WHERE clause passed as the second argument is an
146594
+** EXISTS expression with a correlated SELECT statement on the RHS.
146595
+** This function analyzes the SELECT statement, and if possible adds an
146596
+** equivalent "? IN(SELECT...)" virtual term to the WHERE clause.
146597
+**
146598
+** For an EXISTS term such as the following:
146599
+**
146600
+** EXISTS (SELECT ... FROM <srclist> WHERE <e1> = <e2> AND <e3>)
146601
+**
146602
+** The virtual IN() term added is:
146603
+**
146604
+** <e1> IN (SELECT <e2> FROM <srclist> WHERE <e3>)
146605
+**
146606
+** The virtual term is only added if the following conditions are met:
146607
+**
146608
+** 1. The sub-select must not be an aggregate or use window functions,
146609
+**
146610
+** 2. The sub-select must not be a compound SELECT,
146611
+**
146612
+** 3. Expression <e1> must refer to at least one column from the outer
146613
+** query, and must not refer to any column from the inner query
146614
+** (i.e. from <srclist>).
146615
+**
146616
+** 4. <e2> and <e3> must not refer to any values from the outer query.
146617
+** In other words, once <e1> has been removed, the inner query
146618
+** must not be correlated.
146619
+**
146620
+*/
146621
+static void exprAnalyzeExists(
146622
+ SrcList *pSrc, /* the FROM clause */
146623
+ WhereClause *pWC, /* the WHERE clause */
146624
+ int idxTerm /* Index of the term to be analyzed */
146625
+){
146626
+ Parse *pParse = pWC->pWInfo->pParse;
146627
+ WhereTerm *pTerm = &pWC->a[idxTerm];
146628
+ Expr *pExpr = pTerm->pExpr;
146629
+ Select *pSel = pExpr->x.pSelect;
146630
+ Expr *pDup = 0;
146631
+ Expr *pEq = 0;
146632
+ Expr *pRet = 0;
146633
+ Expr *pInLhs = 0;
146634
+ Expr **ppAnd = 0;
146635
+ int idxNew;
146636
+ sqlite3 *db = pParse->db;
146637
+
146638
+ assert( pExpr->op==TK_EXISTS );
146639
+ assert( (pExpr->flags & EP_VarSelect) && (pExpr->flags & EP_xIsSelect) );
146640
+
146641
+ if( (pSel->selFlags & SF_Aggregate) || pSel->pWin ) return;
146642
+ if( pSel->pPrior ) return;
146643
+ if( pSel->pWhere==0 ) return;
146644
+ if( 0==exprAnalyzeExistsFindEq(pSel, 0, 0) ) return;
146645
+
146646
+ pDup = sqlite3ExprDup(db, pExpr, 0);
146647
+ if( db->mallocFailed ){
146648
+ sqlite3ExprDelete(db, pDup);
146649
+ return;
146650
+ }
146651
+ pSel = pDup->x.pSelect;
146652
+ sqlite3ExprListDelete(db, pSel->pEList);
146653
+ pSel->pEList = 0;
146654
+
146655
+ pInLhs = exprAnalyzeExistsFindEq(pSel, &pEq, &ppAnd);
146656
+ assert( pInLhs && pEq );
146657
+ assert( pEq==pSel->pWhere || ppAnd );
146658
+ if( pInLhs==pEq->pLeft ){
146659
+ pRet = pEq->pRight;
146660
+ }else{
146661
+ CollSeq *p = sqlite3ExprCompareCollSeq(pParse, pEq);
146662
+ pInLhs = sqlite3ExprAddCollateString(pParse, pInLhs, p?p->zName:"BINARY");
146663
+ pRet = pEq->pLeft;
146664
+ }
146665
+
146666
+ assert( pDup->pLeft==0 );
146667
+ pDup->op = TK_IN;
146668
+ pDup->pLeft = pInLhs;
146669
+ pDup->flags &= ~EP_VarSelect;
146670
+ if( pRet->op==TK_VECTOR ){
146671
+ pSel->pEList = pRet->x.pList;
146672
+ pRet->x.pList = 0;
146673
+ sqlite3ExprDelete(db, pRet);
146674
+ }else{
146675
+ pSel->pEList = sqlite3ExprListAppend(pParse, 0, pRet);
146676
+ }
146677
+ pEq->pLeft = 0;
146678
+ pEq->pRight = 0;
146679
+ if( ppAnd ){
146680
+ Expr *pAnd = *ppAnd;
146681
+ Expr *pOther = (pAnd->pLeft==pEq) ? pAnd->pRight : pAnd->pLeft;
146682
+ pAnd->pLeft = pAnd->pRight = 0;
146683
+ sqlite3ExprDelete(db, pAnd);
146684
+ *ppAnd = pOther;
146685
+ }else{
146686
+ assert( pSel->pWhere==pEq );
146687
+ pSel->pWhere = 0;
146688
+ }
146689
+ sqlite3ExprDelete(db, pEq);
146690
+
146691
+#ifdef WHERETRACE_ENABLED /* 0x20 */
146692
+ if( sqlite3WhereTrace & 0x20 ){
146693
+ sqlite3DebugPrintf("Convert EXISTS:\n");
146694
+ sqlite3TreeViewExpr(0, pExpr, 0);
146695
+ sqlite3DebugPrintf("into IN:\n");
146696
+ sqlite3TreeViewExpr(0, pDup, 0);
146697
+ }
146698
+#endif
146699
+ idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
146700
+ exprAnalyze(pSrc, pWC, idxNew);
146701
+ markTermAsChild(pWC, idxNew, idxTerm);
146702
+ pWC->a[idxTerm].wtFlags |= TERM_COPIED;
146703
+}
146351146704
146352146705
/*
146353146706
** The input to this routine is an WhereTerm structure with only the
146354146707
** "pExpr" field filled in. The job of this routine is to analyze the
146355146708
** subexpression and populate all the other fields of the WhereTerm
@@ -146531,10 +146884,20 @@
146531146884
assert( pWC->op==TK_AND );
146532146885
exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
146533146886
pTerm = &pWC->a[idxTerm];
146534146887
}
146535146888
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
146889
+
146890
+ else if( pExpr->op==TK_EXISTS ){
146891
+ /* Perhaps treat an EXISTS operator as an IN operator */
146892
+ if( (pExpr->flags & EP_VarSelect)!=0
146893
+ && OptimizationEnabled(db, SQLITE_ExistsToIN)
146894
+ ){
146895
+ exprAnalyzeExists(pSrc, pWC, idxTerm);
146896
+ }
146897
+ }
146898
+
146536146899
146537146900
#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
146538146901
/* Add constraints to reduce the search space on a LIKE or GLOB
146539146902
** operator.
146540146903
**
@@ -147054,10 +147417,36 @@
147054147417
}
147055147418
pInner = &pWInfo->a[pWInfo->nLevel-1];
147056147419
assert( pInner->addrNxt!=0 );
147057147420
return pInner->addrNxt;
147058147421
}
147422
+
147423
+/*
147424
+** While generating code for the min/max optimization, after handling
147425
+** the aggregate-step call to min() or max(), check to see if any
147426
+** additional looping is required. If the output order is such that
147427
+** we are certain that the correct answer has already been found, then
147428
+** code an OP_Goto to by pass subsequent processing.
147429
+**
147430
+** Any extra OP_Goto that is coded here is an optimization. The
147431
+** correct answer should be obtained regardless. This OP_Goto just
147432
+** makes the answer appear faster.
147433
+*/
147434
+SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
147435
+ WhereLevel *pInner;
147436
+ int i;
147437
+ if( !pWInfo->bOrderedInnerLoop ) return;
147438
+ if( pWInfo->nOBSat==0 ) return;
147439
+ for(i=pWInfo->nLevel-1; i>=0; i--){
147440
+ pInner = &pWInfo->a[i];
147441
+ if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
147442
+ sqlite3VdbeGoto(v, pInner->addrNxt);
147443
+ return;
147444
+ }
147445
+ }
147446
+ sqlite3VdbeGoto(v, pWInfo->iBreak);
147447
+}
147059147448
147060147449
/*
147061147450
** Return the VDBE address or label to jump to in order to continue
147062147451
** immediately with the next row of a WHERE clause.
147063147452
*/
@@ -162028,23 +162417,11 @@
162028162417
sqlite3DeleteTable(db, pParse->pNewTable);
162029162418
}
162030162419
if( !IN_RENAME_OBJECT ){
162031162420
sqlite3DeleteTrigger(db, pParse->pNewTrigger);
162032162421
}
162033
-
162034
- if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
162035162422
sqlite3DbFree(db, pParse->pVList);
162036
- while( pParse->pAinc ){
162037
- AutoincInfo *p = pParse->pAinc;
162038
- pParse->pAinc = p->pNext;
162039
- sqlite3DbFreeNN(db, p);
162040
- }
162041
- while( pParse->pZombieTab ){
162042
- Table *p = pParse->pZombieTab;
162043
- pParse->pZombieTab = p->pNextZombie;
162044
- sqlite3DeleteTable(db, p);
162045
- }
162046162423
db->pParse = pParse->pParentParse;
162047162424
pParse->pParentParse = 0;
162048162425
assert( nErr==0 || pParse->rc!=SQLITE_OK );
162049162426
return nErr;
162050162427
}
@@ -166653,11 +167030,11 @@
166653167030
** with various optimizations disabled to verify that the same answer
166654167031
** is obtained in every case.
166655167032
*/
166656167033
case SQLITE_TESTCTRL_OPTIMIZATIONS: {
166657167034
sqlite3 *db = va_arg(ap, sqlite3*);
166658
- db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
167035
+ db->dbOptFlags = va_arg(ap, u32);
166659167036
break;
166660167037
}
166661167038
166662167039
/* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
166663167040
**
@@ -227709,11 +228086,11 @@
227709228086
int nArg, /* Number of args */
227710228087
sqlite3_value **apUnused /* Function arguments */
227711228088
){
227712228089
assert( nArg==0 );
227713228090
UNUSED_PARAM2(nArg, apUnused);
227714
- sqlite3_result_text(pCtx, "fts5: 2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553b19f", -1, SQLITE_TRANSIENT);
228091
+ sqlite3_result_text(pCtx, "fts5: 2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18df9a04", -1, SQLITE_TRANSIENT);
227715228092
}
227716228093
227717228094
/*
227718228095
** Return true if zName is the extension on one of the shadow tables used
227719228096
** by this module.
@@ -232635,12 +233012,12 @@
232635233012
}
232636233013
#endif /* SQLITE_CORE */
232637233014
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
232638233015
232639233016
/************** End of stmt.c ************************************************/
232640
-#if __LINE__!=232640
233017
+#if __LINE__!=233017
232641233018
#undef SQLITE_SOURCE_ID
232642
-#define SQLITE_SOURCE_ID "2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553alt2"
233019
+#define SQLITE_SOURCE_ID "2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18dfalt2"
232643233020
#endif
232644233021
/* Return the source-id for this library */
232645233022
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
232646233023
/************************** End of sqlite3.c ******************************/
232647233024
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -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 "2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553b19f"
1192
1193 /*
1194 ** CAPI3REF: Run-Time Library Version Numbers
1195 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1196 **
@@ -14775,10 +14775,11 @@
14775 typedef struct Lookaside Lookaside;
14776 typedef struct LookasideSlot LookasideSlot;
14777 typedef struct Module Module;
14778 typedef struct NameContext NameContext;
14779 typedef struct Parse Parse;
 
14780 typedef struct PreUpdate PreUpdate;
14781 typedef struct PrintfArguments PrintfArguments;
14782 typedef struct RenameToken RenameToken;
14783 typedef struct RowSet RowSet;
14784 typedef struct Savepoint Savepoint;
@@ -16947,11 +16948,11 @@
16947 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16948 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16949 int errCode; /* Most recent error code (SQLITE_*) */
16950 int errMask; /* & result codes with this before returning */
16951 int iSysErrno; /* Errno value from last system error */
16952 u16 dbOptFlags; /* Flags to enable/disable optimizations */
16953 u8 enc; /* Text encoding */
16954 u8 autoCommit; /* The auto-commit flag. */
16955 u8 temp_store; /* 1: file 2: memory 0: default */
16956 u8 mallocFailed; /* True if we have seen a malloc failure */
16957 u8 bBenignMalloc; /* Do not require OOMs if true */
@@ -17154,28 +17155,30 @@
17154 /*
17155 ** Bits of the sqlite3.dbOptFlags field that are used by the
17156 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
17157 ** selectively disable various optimizations.
17158 */
17159 #define SQLITE_QueryFlattener 0x0001 /* Query flattening */
17160 #define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */
17161 #define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
17162 #define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
17163 #define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
17164 #define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
17165 #define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
17166 #define SQLITE_Transitive 0x0080 /* Transitive constraints */
17167 #define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
17168 #define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
17169 #define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
17170 #define SQLITE_Stat4 0x0800 /* Use STAT4 data */
17171 /* TH3 expects the Stat4 ^^^^^^ value to be 0x0800. Don't change it */
17172 #define SQLITE_PushDown 0x1000 /* The push-down optimization */
17173 #define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
17174 #define SQLITE_SkipScan 0x4000 /* Skip-scans */
17175 #define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
17176 #define SQLITE_AllOpts 0xffff /* All optimizations */
 
 
17177
17178 /*
17179 ** Macros for testing whether or not optimizations are enabled or disabled.
17180 */
17181 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
@@ -17638,11 +17641,10 @@
17638 char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
17639 VTable *pVTable; /* List of VTable objects. */
17640 #endif
17641 Trigger *pTrigger; /* List of triggers stored in pSchema */
17642 Schema *pSchema; /* Schema that contains this table */
17643 Table *pNextZombie; /* Next on the Parse.pZombieTab list */
17644 };
17645
17646 /*
17647 ** Allowed values for Table.tabFlags.
17648 **
@@ -18800,10 +18802,21 @@
18800 # define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
18801 # define DbMaskAllZero(M) (M)==0
18802 # define DbMaskNonZero(M) (M)!=0
18803 #endif
18804
 
 
 
 
 
 
 
 
 
 
 
18805 /*
18806 ** An SQL parser context. A copy of this structure is passed through
18807 ** the parser and down into all the parser action routine in order to
18808 ** carry around information that is global to the entire parse.
18809 **
@@ -18831,10 +18844,13 @@
18831 u8 mayAbort; /* True if statement may throw an ABORT exception */
18832 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18833 u8 okConstFactor; /* OK to factor out constants */
18834 u8 disableLookaside; /* Number of times lookaside has been disabled */
18835 u8 disableVtab; /* Disable all virtual tables for this parse */
 
 
 
18836 int nRangeReg; /* Size of the temporary register block */
18837 int iRangeReg; /* First register in temporary register block */
18838 int nErr; /* Number of errors seen */
18839 int nTab; /* Number of previously allocated VDBE cursors */
18840 int nMem; /* Number of memory cells used so far */
@@ -18909,14 +18925,13 @@
18909 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18910 #ifndef SQLITE_OMIT_VIRTUALTABLE
18911 Token sArg; /* Complete text of a module argument */
18912 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18913 #endif
18914 Table *pZombieTab; /* List of Table objects to delete after code gen */
18915 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18916 With *pWith; /* Current WITH clause, or NULL */
18917 With *pWithToFree; /* Free this WITH object at the end of the parse */
18918 #ifndef SQLITE_OMIT_ALTERTABLE
18919 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18920 #endif
18921 };
18922
@@ -19780,10 +19795,11 @@
19780 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19781 SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19782 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19783 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19784 SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
 
19785 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
19786 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
19787 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
19788 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
19789 #define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
@@ -20279,10 +20295,11 @@
20279 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20280 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20281 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20282 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
20283 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
 
20284 #ifdef SQLITE_ENABLE_NORMALIZE
20285 SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
20286 #endif
20287 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
20288 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
@@ -100513,11 +100530,22 @@
100513 Parse *pParse, /* Parsing context */
100514 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
100515 const Token *pCollName, /* Name of collating sequence */
100516 int dequote /* True to dequote pCollName */
100517 ){
100518 if( pCollName->n>0 ){
 
 
 
 
 
 
 
 
 
 
 
100519 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
100520 if( pNew ){
100521 pNew->pLeft = pExpr;
100522 pNew->flags |= EP_Collate|EP_Skip;
100523 pExpr = pNew;
@@ -121907,11 +121935,13 @@
121907
121908 pInfo = pToplevel->pAinc;
121909 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
121910 if( pInfo==0 ){
121911 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
121912 if( pInfo==0 ) return 0;
 
 
121913 pInfo->pNext = pToplevel->pAinc;
121914 pToplevel->pAinc = pInfo;
121915 pInfo->pTab = pTab;
121916 pInfo->iDb = iDb;
121917 pToplevel->nMem++; /* Register to hold name of table */
@@ -130048,19 +130078,66 @@
130048 while( pThis ){
130049 AggInfo *pNext = pThis->pNext;
130050 agginfoFree(db, pThis);
130051 pThis = pNext;
130052 }
 
 
 
 
 
 
130053 sqlite3DbFree(db, pParse->aLabel);
130054 sqlite3ExprListDelete(db, pParse->pConstExpr);
 
 
130055 if( db ){
130056 assert( db->lookaside.bDisable >= pParse->disableLookaside );
130057 db->lookaside.bDisable -= pParse->disableLookaside;
130058 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
130059 }
130060 pParse->disableLookaside = 0;
130061 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130062
130063 /*
130064 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
130065 */
130066 static int sqlite3Prepare(
@@ -134588,12 +134665,14 @@
134588 */
134589 if( ALWAYS(pSubitem->pTab!=0) ){
134590 Table *pTabToDel = pSubitem->pTab;
134591 if( pTabToDel->nTabRef==1 ){
134592 Parse *pToplevel = sqlite3ParseToplevel(pParse);
134593 pTabToDel->pNextZombie = pToplevel->pZombieTab;
134594 pToplevel->pZombieTab = pTabToDel;
 
 
134595 }else{
134596 pTabToDel->nTabRef--;
134597 }
134598 pSubitem->pTab = 0;
134599 }
@@ -135086,11 +135165,15 @@
135086 u8 sortFlags = 0;
135087
135088 assert( *ppMinMax==0 );
135089 assert( pFunc->op==TK_AGG_FUNCTION );
135090 assert( !IsWindowFunc(pFunc) );
135091 if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
 
 
 
 
135092 return eRet;
135093 }
135094 zFunc = pFunc->u.zToken;
135095 if( sqlite3StrICmp(zFunc, "min")==0 ){
135096 eRet = WHERE_ORDERBY_MIN;
@@ -135304,16 +135387,20 @@
135304 ** should be freed along with the Parse object. In other cases, when
135305 ** bFree==0, the With object will be freed along with the SELECT
135306 ** statement with which it is associated.
135307 */
135308 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
135309 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
135310 if( pWith ){
135311 assert( pParse->pWith!=pWith );
135312 pWith->pOuter = pParse->pWith;
135313 pParse->pWith = pWith;
135314 if( bFree ) pParse->pWithToFree = pWith;
 
 
 
 
 
135315 }
135316 }
135317
135318 /*
135319 ** This function checks if argument pFrom refers to a CTE declared by
@@ -137022,10 +137109,14 @@
137022 #if SELECTTRACE_ENABLED
137023 if( sqlite3SelectTrace & 0x400 ){
137024 int ii;
137025 SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
137026 sqlite3TreeViewSelect(0, p, 0);
 
 
 
 
137027 for(ii=0; ii<pAggInfo->nColumn; ii++){
137028 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
137029 ii, pAggInfo->aCol[ii].iMem);
137030 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
137031 }
@@ -137211,11 +137302,11 @@
137211 VdbeComment((v, "indicate data in accumulator"));
137212
137213 /* End of the loop
137214 */
137215 if( groupBySort ){
137216 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx, addrTopOfLoop);
137217 VdbeCoverage(v);
137218 }else{
137219 sqlite3WhereEnd(pWInfo);
137220 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
137221 }
@@ -137323,11 +137414,10 @@
137323 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
137324 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
137325 explainSimpleCount(pParse, pTab, pBest);
137326 }else{
137327 int regAcc = 0; /* "populate accumulators" flag */
137328 int addrSkip;
137329
137330 /* If there are accumulator registers but no min() or max() functions
137331 ** without FILTER clauses, allocate register regAcc. Register regAcc
137332 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
137333 ** The code generated by updateAccumulator() uses this to ensure
@@ -137372,13 +137462,12 @@
137372 if( pWInfo==0 ){
137373 goto select_end;
137374 }
137375 updateAccumulator(pParse, regAcc, pAggInfo);
137376 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
137377 addrSkip = sqlite3WhereOrderByLimitOptLabel(pWInfo);
137378 if( addrSkip!=sqlite3WhereContinueLabel(pWInfo) ){
137379 sqlite3VdbeGoto(v, addrSkip);
137380 }
137381 sqlite3WhereEnd(pWInfo);
137382 finalizeAggFunctions(pParse, pAggInfo);
137383 }
137384
@@ -138648,11 +138737,10 @@
138648 pPrg->aColmask[0] = pSubParse->oldmask;
138649 pPrg->aColmask[1] = pSubParse->newmask;
138650 sqlite3VdbeDelete(v);
138651 }
138652
138653 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
138654 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
138655 sqlite3ParserReset(pSubParse);
138656 sqlite3StackFree(db, pSubParse);
138657
138658 return pPrg;
@@ -146346,10 +146434,275 @@
146346 }
146347 if( mPrereq==0 ) return 0; /* No table references */
146348 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
146349 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
146350 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146351
146352 /*
146353 ** The input to this routine is an WhereTerm structure with only the
146354 ** "pExpr" field filled in. The job of this routine is to analyze the
146355 ** subexpression and populate all the other fields of the WhereTerm
@@ -146531,10 +146884,20 @@
146531 assert( pWC->op==TK_AND );
146532 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
146533 pTerm = &pWC->a[idxTerm];
146534 }
146535 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
 
 
 
 
 
 
 
 
 
 
146536
146537 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
146538 /* Add constraints to reduce the search space on a LIKE or GLOB
146539 ** operator.
146540 **
@@ -147054,10 +147417,36 @@
147054 }
147055 pInner = &pWInfo->a[pWInfo->nLevel-1];
147056 assert( pInner->addrNxt!=0 );
147057 return pInner->addrNxt;
147058 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147059
147060 /*
147061 ** Return the VDBE address or label to jump to in order to continue
147062 ** immediately with the next row of a WHERE clause.
147063 */
@@ -162028,23 +162417,11 @@
162028 sqlite3DeleteTable(db, pParse->pNewTable);
162029 }
162030 if( !IN_RENAME_OBJECT ){
162031 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
162032 }
162033
162034 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
162035 sqlite3DbFree(db, pParse->pVList);
162036 while( pParse->pAinc ){
162037 AutoincInfo *p = pParse->pAinc;
162038 pParse->pAinc = p->pNext;
162039 sqlite3DbFreeNN(db, p);
162040 }
162041 while( pParse->pZombieTab ){
162042 Table *p = pParse->pZombieTab;
162043 pParse->pZombieTab = p->pNextZombie;
162044 sqlite3DeleteTable(db, p);
162045 }
162046 db->pParse = pParse->pParentParse;
162047 pParse->pParentParse = 0;
162048 assert( nErr==0 || pParse->rc!=SQLITE_OK );
162049 return nErr;
162050 }
@@ -166653,11 +167030,11 @@
166653 ** with various optimizations disabled to verify that the same answer
166654 ** is obtained in every case.
166655 */
166656 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
166657 sqlite3 *db = va_arg(ap, sqlite3*);
166658 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
166659 break;
166660 }
166661
166662 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
166663 **
@@ -227709,11 +228086,11 @@
227709 int nArg, /* Number of args */
227710 sqlite3_value **apUnused /* Function arguments */
227711 ){
227712 assert( nArg==0 );
227713 UNUSED_PARAM2(nArg, apUnused);
227714 sqlite3_result_text(pCtx, "fts5: 2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553b19f", -1, SQLITE_TRANSIENT);
227715 }
227716
227717 /*
227718 ** Return true if zName is the extension on one of the shadow tables used
227719 ** by this module.
@@ -232635,12 +233012,12 @@
232635 }
232636 #endif /* SQLITE_CORE */
232637 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
232638
232639 /************** End of stmt.c ************************************************/
232640 #if __LINE__!=232640
232641 #undef SQLITE_SOURCE_ID
232642 #define SQLITE_SOURCE_ID "2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553alt2"
232643 #endif
232644 /* Return the source-id for this library */
232645 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
232646 /************************** End of sqlite3.c ******************************/
232647
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -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 "2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18df9a04"
1192
1193 /*
1194 ** CAPI3REF: Run-Time Library Version Numbers
1195 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1196 **
@@ -14775,10 +14775,11 @@
14775 typedef struct Lookaside Lookaside;
14776 typedef struct LookasideSlot LookasideSlot;
14777 typedef struct Module Module;
14778 typedef struct NameContext NameContext;
14779 typedef struct Parse Parse;
14780 typedef struct ParseCleanup ParseCleanup;
14781 typedef struct PreUpdate PreUpdate;
14782 typedef struct PrintfArguments PrintfArguments;
14783 typedef struct RenameToken RenameToken;
14784 typedef struct RowSet RowSet;
14785 typedef struct Savepoint Savepoint;
@@ -16947,11 +16948,11 @@
16948 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16949 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16950 int errCode; /* Most recent error code (SQLITE_*) */
16951 int errMask; /* & result codes with this before returning */
16952 int iSysErrno; /* Errno value from last system error */
16953 u32 dbOptFlags; /* Flags to enable/disable optimizations */
16954 u8 enc; /* Text encoding */
16955 u8 autoCommit; /* The auto-commit flag. */
16956 u8 temp_store; /* 1: file 2: memory 0: default */
16957 u8 mallocFailed; /* True if we have seen a malloc failure */
16958 u8 bBenignMalloc; /* Do not require OOMs if true */
@@ -17154,28 +17155,30 @@
17155 /*
17156 ** Bits of the sqlite3.dbOptFlags field that are used by the
17157 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
17158 ** selectively disable various optimizations.
17159 */
17160 #define SQLITE_QueryFlattener 0x00000001 /* Query flattening */
17161 #define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */
17162 #define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */
17163 #define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */
17164 #define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */
17165 #define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */
17166 #define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */
17167 #define SQLITE_Transitive 0x00000080 /* Transitive constraints */
17168 #define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */
17169 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17170 #define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */
17171 #define SQLITE_Stat4 0x00000800 /* Use STAT4 data */
17172 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
17173 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17174 #define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */
17175 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17176 #define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */
17177 #define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */
17178 #define SQLITE_ExistsToIN 0x00020000 /* The EXISTS-to-IN optimization */
17179 #define SQLITE_AllOpts 0xffffffff /* All optimizations */
17180
17181 /*
17182 ** Macros for testing whether or not optimizations are enabled or disabled.
17183 */
17184 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
@@ -17638,11 +17641,10 @@
17641 char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
17642 VTable *pVTable; /* List of VTable objects. */
17643 #endif
17644 Trigger *pTrigger; /* List of triggers stored in pSchema */
17645 Schema *pSchema; /* Schema that contains this table */
 
17646 };
17647
17648 /*
17649 ** Allowed values for Table.tabFlags.
17650 **
@@ -18800,10 +18802,21 @@
18802 # define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
18803 # define DbMaskAllZero(M) (M)==0
18804 # define DbMaskNonZero(M) (M)!=0
18805 #endif
18806
18807 /*
18808 ** An instance of the ParseCleanup object specifies an operation that
18809 ** should be performed after parsing to deallocation resources obtained
18810 ** during the parse and which are no longer needed.
18811 */
18812 struct ParseCleanup {
18813 ParseCleanup *pNext; /* Next cleanup task */
18814 void *pPtr; /* Pointer to object to deallocate */
18815 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
18816 };
18817
18818 /*
18819 ** An SQL parser context. A copy of this structure is passed through
18820 ** the parser and down into all the parser action routine in order to
18821 ** carry around information that is global to the entire parse.
18822 **
@@ -18831,10 +18844,13 @@
18844 u8 mayAbort; /* True if statement may throw an ABORT exception */
18845 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18846 u8 okConstFactor; /* OK to factor out constants */
18847 u8 disableLookaside; /* Number of times lookaside has been disabled */
18848 u8 disableVtab; /* Disable all virtual tables for this parse */
18849 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
18850 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
18851 #endif
18852 int nRangeReg; /* Size of the temporary register block */
18853 int iRangeReg; /* First register in temporary register block */
18854 int nErr; /* Number of errors seen */
18855 int nTab; /* Number of previously allocated VDBE cursors */
18856 int nMem; /* Number of memory cells used so far */
@@ -18909,14 +18925,13 @@
18925 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18926 #ifndef SQLITE_OMIT_VIRTUALTABLE
18927 Token sArg; /* Complete text of a module argument */
18928 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18929 #endif
 
18930 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18931 With *pWith; /* Current WITH clause, or NULL */
18932 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
18933 #ifndef SQLITE_OMIT_ALTERTABLE
18934 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18935 #endif
18936 };
18937
@@ -19780,10 +19795,11 @@
19795 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19796 SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19797 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19798 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19799 SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
19800 SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
19801 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
19802 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
19803 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
19804 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
19805 #define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
@@ -20279,10 +20295,11 @@
20295 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20296 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20297 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20298 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
20299 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
20300 SQLITE_PRIVATE void sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
20301 #ifdef SQLITE_ENABLE_NORMALIZE
20302 SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
20303 #endif
20304 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
20305 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
@@ -100513,11 +100530,22 @@
100530 Parse *pParse, /* Parsing context */
100531 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
100532 const Token *pCollName, /* Name of collating sequence */
100533 int dequote /* True to dequote pCollName */
100534 ){
100535 assert( pExpr!=0 || pParse->db->mallocFailed );
100536 if( pExpr==0 ) return 0;
100537 if( pExpr->op==TK_VECTOR ){
100538 ExprList *pList = pExpr->x.pList;
100539 if( ALWAYS(pList!=0) ){
100540 int i;
100541 for(i=0; i<pList->nExpr; i++){
100542 pList->a[i].pExpr = sqlite3ExprAddCollateToken(pParse,pList->a[i].pExpr,
100543 pCollName, dequote);
100544 }
100545 }
100546 }else if( pCollName->n>0 ){
100547 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
100548 if( pNew ){
100549 pNew->pLeft = pExpr;
100550 pNew->flags |= EP_Collate|EP_Skip;
100551 pExpr = pNew;
@@ -121907,11 +121935,13 @@
121935
121936 pInfo = pToplevel->pAinc;
121937 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
121938 if( pInfo==0 ){
121939 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
121940 sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
121941 testcase( pParse->earlyCleanup );
121942 if( pParse->db->mallocFailed ) return 0;
121943 pInfo->pNext = pToplevel->pAinc;
121944 pToplevel->pAinc = pInfo;
121945 pInfo->pTab = pTab;
121946 pInfo->iDb = iDb;
121947 pToplevel->nMem++; /* Register to hold name of table */
@@ -130048,19 +130078,66 @@
130078 while( pThis ){
130079 AggInfo *pNext = pThis->pNext;
130080 agginfoFree(db, pThis);
130081 pThis = pNext;
130082 }
130083 while( pParse->pCleanup ){
130084 ParseCleanup *pCleanup = pParse->pCleanup;
130085 pParse->pCleanup = pCleanup->pNext;
130086 pCleanup->xCleanup(db, pCleanup->pPtr);
130087 sqlite3DbFree(db, pCleanup);
130088 }
130089 sqlite3DbFree(db, pParse->aLabel);
130090 if( pParse->pConstExpr ){
130091 sqlite3ExprListDelete(db, pParse->pConstExpr);
130092 }
130093 if( db ){
130094 assert( db->lookaside.bDisable >= pParse->disableLookaside );
130095 db->lookaside.bDisable -= pParse->disableLookaside;
130096 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
130097 }
130098 pParse->disableLookaside = 0;
130099 }
130100
130101 /*
130102 ** Add a new cleanup operation to a Parser. The cleanup should happen when
130103 ** the parser object is destroyed. But, beware: the cleanup might happen
130104 ** immediately.
130105 **
130106 ** Use this mechanism for uncommon cleanups. There is a higher setup
130107 ** cost for this mechansim (an extra malloc), so it should not be used
130108 ** for common cleanups that happen on most calls. But for less
130109 ** common cleanups, we save a single NULL-pointer comparison in
130110 ** sqlite3ParserReset(), which reduces the total CPU cycle count.
130111 **
130112 ** If a memory allocation error occurs, then the cleanup happens immediately.
130113 ** When eithr SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
130114 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
130115 ** that test cases exist for which this happens, to guard against possible
130116 ** use-after-free errors following an OOM. The preferred way to do this is
130117 ** to immediately follow the call to this routine with:
130118 **
130119 ** testcase( pParse->earlyCleanup );
130120 */
130121 SQLITE_PRIVATE void sqlite3ParserAddCleanup(
130122 Parse *pParse, /* Destroy when this Parser finishes */
130123 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
130124 void *pPtr /* Pointer to object to be cleaned up */
130125 ){
130126 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
130127 if( pCleanup ){
130128 pCleanup->pNext = pParse->pCleanup;
130129 pParse->pCleanup = pCleanup;
130130 pCleanup->pPtr = pPtr;
130131 pCleanup->xCleanup = xCleanup;
130132 }else{
130133 xCleanup(pParse->db, pPtr);
130134 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
130135 pParse->earlyCleanup = 1;
130136 #endif
130137 }
130138 }
130139
130140 /*
130141 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
130142 */
130143 static int sqlite3Prepare(
@@ -134588,12 +134665,14 @@
134665 */
134666 if( ALWAYS(pSubitem->pTab!=0) ){
134667 Table *pTabToDel = pSubitem->pTab;
134668 if( pTabToDel->nTabRef==1 ){
134669 Parse *pToplevel = sqlite3ParseToplevel(pParse);
134670 sqlite3ParserAddCleanup(pToplevel,
134671 (void(*)(sqlite3*,void*))sqlite3DeleteTable,
134672 pTabToDel);
134673 testcase( pToplevel->earlyCleanup );
134674 }else{
134675 pTabToDel->nTabRef--;
134676 }
134677 pSubitem->pTab = 0;
134678 }
@@ -135086,11 +135165,15 @@
135165 u8 sortFlags = 0;
135166
135167 assert( *ppMinMax==0 );
135168 assert( pFunc->op==TK_AGG_FUNCTION );
135169 assert( !IsWindowFunc(pFunc) );
135170 if( pEList==0
135171 || pEList->nExpr!=1
135172 || ExprHasProperty(pFunc, EP_WinFunc)
135173 || OptimizationDisabled(db, SQLITE_MinMaxOpt)
135174 ){
135175 return eRet;
135176 }
135177 zFunc = pFunc->u.zToken;
135178 if( sqlite3StrICmp(zFunc, "min")==0 ){
135179 eRet = WHERE_ORDERBY_MIN;
@@ -135304,16 +135387,20 @@
135387 ** should be freed along with the Parse object. In other cases, when
135388 ** bFree==0, the With object will be freed along with the SELECT
135389 ** statement with which it is associated.
135390 */
135391 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
 
135392 if( pWith ){
135393 assert( pParse->pWith!=pWith );
135394 pWith->pOuter = pParse->pWith;
135395 pParse->pWith = pWith;
135396 if( bFree ){
135397 sqlite3ParserAddCleanup(pParse,
135398 (void(*)(sqlite3*,void*))sqlite3WithDelete,
135399 pWith);
135400 testcase( pParse->earlyCleanup );
135401 }
135402 }
135403 }
135404
135405 /*
135406 ** This function checks if argument pFrom refers to a CTE declared by
@@ -137022,10 +137109,14 @@
137109 #if SELECTTRACE_ENABLED
137110 if( sqlite3SelectTrace & 0x400 ){
137111 int ii;
137112 SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
137113 sqlite3TreeViewSelect(0, p, 0);
137114 if( minMaxFlag ){
137115 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
137116 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
137117 }
137118 for(ii=0; ii<pAggInfo->nColumn; ii++){
137119 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
137120 ii, pAggInfo->aCol[ii].iMem);
137121 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
137122 }
@@ -137211,11 +137302,11 @@
137302 VdbeComment((v, "indicate data in accumulator"));
137303
137304 /* End of the loop
137305 */
137306 if( groupBySort ){
137307 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
137308 VdbeCoverage(v);
137309 }else{
137310 sqlite3WhereEnd(pWInfo);
137311 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
137312 }
@@ -137323,11 +137414,10 @@
137414 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
137415 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
137416 explainSimpleCount(pParse, pTab, pBest);
137417 }else{
137418 int regAcc = 0; /* "populate accumulators" flag */
 
137419
137420 /* If there are accumulator registers but no min() or max() functions
137421 ** without FILTER clauses, allocate register regAcc. Register regAcc
137422 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
137423 ** The code generated by updateAccumulator() uses this to ensure
@@ -137372,13 +137462,12 @@
137462 if( pWInfo==0 ){
137463 goto select_end;
137464 }
137465 updateAccumulator(pParse, regAcc, pAggInfo);
137466 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
137467 if( minMaxFlag ){
137468 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
 
137469 }
137470 sqlite3WhereEnd(pWInfo);
137471 finalizeAggFunctions(pParse, pAggInfo);
137472 }
137473
@@ -138648,11 +138737,10 @@
138737 pPrg->aColmask[0] = pSubParse->oldmask;
138738 pPrg->aColmask[1] = pSubParse->newmask;
138739 sqlite3VdbeDelete(v);
138740 }
138741
 
138742 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
138743 sqlite3ParserReset(pSubParse);
138744 sqlite3StackFree(db, pSubParse);
138745
138746 return pPrg;
@@ -146346,10 +146434,275 @@
146434 }
146435 if( mPrereq==0 ) return 0; /* No table references */
146436 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
146437 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
146438 }
146439
146440 /*
146441 ** Expression callback for exprUsesSrclist().
146442 */
146443 static int exprUsesSrclistCb(Walker *p, Expr *pExpr){
146444 if( pExpr->op==TK_COLUMN ){
146445 SrcList *pSrc = p->u.pSrcList;
146446 int iCsr = pExpr->iTable;
146447 int ii;
146448 for(ii=0; ii<pSrc->nSrc; ii++){
146449 if( pSrc->a[ii].iCursor==iCsr ){
146450 return p->eCode ? WRC_Abort : WRC_Continue;
146451 }
146452 }
146453 return p->eCode ? WRC_Continue : WRC_Abort;
146454 }
146455 return WRC_Continue;
146456 }
146457
146458 /*
146459 ** Select callback for exprUsesSrclist().
146460 */
146461 static int exprUsesSrclistSelectCb(Walker *p, Select *pSelect){
146462 return WRC_Abort;
146463 }
146464
146465 /*
146466 ** This function always returns true if expression pExpr contains
146467 ** a sub-select.
146468 **
146469 ** If there is no sub-select in pExpr, then return true if pExpr
146470 ** contains a TK_COLUMN node for a table that is (bUses==1)
146471 ** or is not (bUses==0) in pSrc.
146472 **
146473 ** Said another way:
146474 **
146475 ** bUses Return Meaning
146476 ** -------- ------ ------------------------------------------------
146477 **
146478 ** bUses==1 true pExpr contains either a sub-select or a
146479 ** TK_COLUMN referencing pSrc.
146480 **
146481 ** bUses==1 false pExpr contains no sub-selects and all TK_COLUMN
146482 ** nodes reference tables not found in pSrc
146483 **
146484 ** bUses==0 true pExpr contains either a sub-select or a TK_COLUMN
146485 ** that references a table not in pSrc.
146486 **
146487 ** bUses==0 false pExpr contains no sub-selects and all TK_COLUMN
146488 ** nodes reference pSrc
146489 */
146490 static int exprUsesSrclist(SrcList *pSrc, Expr *pExpr, int bUses){
146491 Walker sWalker;
146492 memset(&sWalker, 0, sizeof(Walker));
146493 sWalker.eCode = bUses;
146494 sWalker.u.pSrcList = pSrc;
146495 sWalker.xExprCallback = exprUsesSrclistCb;
146496 sWalker.xSelectCallback = exprUsesSrclistSelectCb;
146497 return (sqlite3WalkExpr(&sWalker, pExpr)==WRC_Abort);
146498 }
146499
146500 /*
146501 ** Context object used by exprExistsToInIter() as it iterates through an
146502 ** expression tree.
146503 */
146504 struct ExistsToInCtx {
146505 SrcList *pSrc; /* The tables in an EXISTS(SELECT ... FROM <here> ...) */
146506 Expr *pInLhs; /* OUT: Use this as the LHS of the IN operator */
146507 Expr *pEq; /* OUT: The == term that include pInLhs */
146508 Expr **ppAnd; /* OUT: The AND operator that includes pEq as a child */
146509 Expr **ppParent; /* The AND operator currently being examined */
146510 };
146511
146512 /*
146513 ** Iterate through all AND connected nodes in the expression tree
146514 ** headed by (*ppExpr), populating the structure passed as the first
146515 ** argument with the values required by exprAnalyzeExistsFindEq().
146516 **
146517 ** This function returns non-zero if the expression tree does not meet
146518 ** the two conditions described by the header comment for
146519 ** exprAnalyzeExistsFindEq(), or zero if it does.
146520 */
146521 static int exprExistsToInIter(struct ExistsToInCtx *p, Expr **ppExpr){
146522 Expr *pExpr = *ppExpr;
146523 switch( pExpr->op ){
146524 case TK_AND:
146525 p->ppParent = ppExpr;
146526 if( exprExistsToInIter(p, &pExpr->pLeft) ) return 1;
146527 p->ppParent = ppExpr;
146528 if( exprExistsToInIter(p, &pExpr->pRight) ) return 1;
146529 break;
146530 case TK_EQ: {
146531 int bLeft = exprUsesSrclist(p->pSrc, pExpr->pLeft, 0);
146532 int bRight = exprUsesSrclist(p->pSrc, pExpr->pRight, 0);
146533 if( bLeft || bRight ){
146534 if( (bLeft && bRight) || p->pInLhs ) return 1;
146535 p->pInLhs = bLeft ? pExpr->pLeft : pExpr->pRight;
146536 if( exprUsesSrclist(p->pSrc, p->pInLhs, 1) ) return 1;
146537 p->pEq = pExpr;
146538 p->ppAnd = p->ppParent;
146539 }
146540 break;
146541 }
146542 default:
146543 if( exprUsesSrclist(p->pSrc, pExpr, 0) ){
146544 return 1;
146545 }
146546 break;
146547 }
146548
146549 return 0;
146550 }
146551
146552 /*
146553 ** This function is used by exprAnalyzeExists() when creating virtual IN(...)
146554 ** terms equivalent to user-supplied EXIST(...) clauses. It splits the WHERE
146555 ** clause of the Select object passed as the first argument into one or more
146556 ** expressions joined by AND operators, and then tests if the following are
146557 ** true:
146558 **
146559 ** 1. Exactly one of the AND separated terms refers to the outer
146560 ** query, and it is an == (TK_EQ) expression.
146561 **
146562 ** 2. Only one side of the == expression refers to the outer query, and
146563 ** it does not refer to any columns from the inner query.
146564 **
146565 ** If both these conditions are true, then a pointer to the side of the ==
146566 ** expression that refers to the outer query is returned. The caller will
146567 ** use this expression as the LHS of the IN(...) virtual term. Or, if one
146568 ** or both of the above conditions are not true, NULL is returned.
146569 **
146570 ** If non-NULL is returned and ppEq is non-NULL, *ppEq is set to point
146571 ** to the == expression node before returning. If pppAnd is non-NULL and
146572 ** the == node is not the root of the WHERE clause, then *pppAnd is set
146573 ** to point to the pointer to the AND node that is the parent of the ==
146574 ** node within the WHERE expression tree.
146575 */
146576 static Expr *exprAnalyzeExistsFindEq(
146577 Select *pSel, /* The SELECT of the EXISTS */
146578 Expr **ppEq, /* OUT: == node from WHERE clause */
146579 Expr ***pppAnd /* OUT: Pointer to parent of ==, if any */
146580 ){
146581 struct ExistsToInCtx ctx;
146582 memset(&ctx, 0, sizeof(ctx));
146583 ctx.pSrc = pSel->pSrc;
146584 if( exprExistsToInIter(&ctx, &pSel->pWhere) ){
146585 return 0;
146586 }
146587 if( ppEq ) *ppEq = ctx.pEq;
146588 if( pppAnd ) *pppAnd = ctx.ppAnd;
146589 return ctx.pInLhs;
146590 }
146591
146592 /*
146593 ** Term idxTerm of the WHERE clause passed as the second argument is an
146594 ** EXISTS expression with a correlated SELECT statement on the RHS.
146595 ** This function analyzes the SELECT statement, and if possible adds an
146596 ** equivalent "? IN(SELECT...)" virtual term to the WHERE clause.
146597 **
146598 ** For an EXISTS term such as the following:
146599 **
146600 ** EXISTS (SELECT ... FROM <srclist> WHERE <e1> = <e2> AND <e3>)
146601 **
146602 ** The virtual IN() term added is:
146603 **
146604 ** <e1> IN (SELECT <e2> FROM <srclist> WHERE <e3>)
146605 **
146606 ** The virtual term is only added if the following conditions are met:
146607 **
146608 ** 1. The sub-select must not be an aggregate or use window functions,
146609 **
146610 ** 2. The sub-select must not be a compound SELECT,
146611 **
146612 ** 3. Expression <e1> must refer to at least one column from the outer
146613 ** query, and must not refer to any column from the inner query
146614 ** (i.e. from <srclist>).
146615 **
146616 ** 4. <e2> and <e3> must not refer to any values from the outer query.
146617 ** In other words, once <e1> has been removed, the inner query
146618 ** must not be correlated.
146619 **
146620 */
146621 static void exprAnalyzeExists(
146622 SrcList *pSrc, /* the FROM clause */
146623 WhereClause *pWC, /* the WHERE clause */
146624 int idxTerm /* Index of the term to be analyzed */
146625 ){
146626 Parse *pParse = pWC->pWInfo->pParse;
146627 WhereTerm *pTerm = &pWC->a[idxTerm];
146628 Expr *pExpr = pTerm->pExpr;
146629 Select *pSel = pExpr->x.pSelect;
146630 Expr *pDup = 0;
146631 Expr *pEq = 0;
146632 Expr *pRet = 0;
146633 Expr *pInLhs = 0;
146634 Expr **ppAnd = 0;
146635 int idxNew;
146636 sqlite3 *db = pParse->db;
146637
146638 assert( pExpr->op==TK_EXISTS );
146639 assert( (pExpr->flags & EP_VarSelect) && (pExpr->flags & EP_xIsSelect) );
146640
146641 if( (pSel->selFlags & SF_Aggregate) || pSel->pWin ) return;
146642 if( pSel->pPrior ) return;
146643 if( pSel->pWhere==0 ) return;
146644 if( 0==exprAnalyzeExistsFindEq(pSel, 0, 0) ) return;
146645
146646 pDup = sqlite3ExprDup(db, pExpr, 0);
146647 if( db->mallocFailed ){
146648 sqlite3ExprDelete(db, pDup);
146649 return;
146650 }
146651 pSel = pDup->x.pSelect;
146652 sqlite3ExprListDelete(db, pSel->pEList);
146653 pSel->pEList = 0;
146654
146655 pInLhs = exprAnalyzeExistsFindEq(pSel, &pEq, &ppAnd);
146656 assert( pInLhs && pEq );
146657 assert( pEq==pSel->pWhere || ppAnd );
146658 if( pInLhs==pEq->pLeft ){
146659 pRet = pEq->pRight;
146660 }else{
146661 CollSeq *p = sqlite3ExprCompareCollSeq(pParse, pEq);
146662 pInLhs = sqlite3ExprAddCollateString(pParse, pInLhs, p?p->zName:"BINARY");
146663 pRet = pEq->pLeft;
146664 }
146665
146666 assert( pDup->pLeft==0 );
146667 pDup->op = TK_IN;
146668 pDup->pLeft = pInLhs;
146669 pDup->flags &= ~EP_VarSelect;
146670 if( pRet->op==TK_VECTOR ){
146671 pSel->pEList = pRet->x.pList;
146672 pRet->x.pList = 0;
146673 sqlite3ExprDelete(db, pRet);
146674 }else{
146675 pSel->pEList = sqlite3ExprListAppend(pParse, 0, pRet);
146676 }
146677 pEq->pLeft = 0;
146678 pEq->pRight = 0;
146679 if( ppAnd ){
146680 Expr *pAnd = *ppAnd;
146681 Expr *pOther = (pAnd->pLeft==pEq) ? pAnd->pRight : pAnd->pLeft;
146682 pAnd->pLeft = pAnd->pRight = 0;
146683 sqlite3ExprDelete(db, pAnd);
146684 *ppAnd = pOther;
146685 }else{
146686 assert( pSel->pWhere==pEq );
146687 pSel->pWhere = 0;
146688 }
146689 sqlite3ExprDelete(db, pEq);
146690
146691 #ifdef WHERETRACE_ENABLED /* 0x20 */
146692 if( sqlite3WhereTrace & 0x20 ){
146693 sqlite3DebugPrintf("Convert EXISTS:\n");
146694 sqlite3TreeViewExpr(0, pExpr, 0);
146695 sqlite3DebugPrintf("into IN:\n");
146696 sqlite3TreeViewExpr(0, pDup, 0);
146697 }
146698 #endif
146699 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
146700 exprAnalyze(pSrc, pWC, idxNew);
146701 markTermAsChild(pWC, idxNew, idxTerm);
146702 pWC->a[idxTerm].wtFlags |= TERM_COPIED;
146703 }
146704
146705 /*
146706 ** The input to this routine is an WhereTerm structure with only the
146707 ** "pExpr" field filled in. The job of this routine is to analyze the
146708 ** subexpression and populate all the other fields of the WhereTerm
@@ -146531,10 +146884,20 @@
146884 assert( pWC->op==TK_AND );
146885 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
146886 pTerm = &pWC->a[idxTerm];
146887 }
146888 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
146889
146890 else if( pExpr->op==TK_EXISTS ){
146891 /* Perhaps treat an EXISTS operator as an IN operator */
146892 if( (pExpr->flags & EP_VarSelect)!=0
146893 && OptimizationEnabled(db, SQLITE_ExistsToIN)
146894 ){
146895 exprAnalyzeExists(pSrc, pWC, idxTerm);
146896 }
146897 }
146898
146899
146900 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
146901 /* Add constraints to reduce the search space on a LIKE or GLOB
146902 ** operator.
146903 **
@@ -147054,10 +147417,36 @@
147417 }
147418 pInner = &pWInfo->a[pWInfo->nLevel-1];
147419 assert( pInner->addrNxt!=0 );
147420 return pInner->addrNxt;
147421 }
147422
147423 /*
147424 ** While generating code for the min/max optimization, after handling
147425 ** the aggregate-step call to min() or max(), check to see if any
147426 ** additional looping is required. If the output order is such that
147427 ** we are certain that the correct answer has already been found, then
147428 ** code an OP_Goto to by pass subsequent processing.
147429 **
147430 ** Any extra OP_Goto that is coded here is an optimization. The
147431 ** correct answer should be obtained regardless. This OP_Goto just
147432 ** makes the answer appear faster.
147433 */
147434 SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
147435 WhereLevel *pInner;
147436 int i;
147437 if( !pWInfo->bOrderedInnerLoop ) return;
147438 if( pWInfo->nOBSat==0 ) return;
147439 for(i=pWInfo->nLevel-1; i>=0; i--){
147440 pInner = &pWInfo->a[i];
147441 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
147442 sqlite3VdbeGoto(v, pInner->addrNxt);
147443 return;
147444 }
147445 }
147446 sqlite3VdbeGoto(v, pWInfo->iBreak);
147447 }
147448
147449 /*
147450 ** Return the VDBE address or label to jump to in order to continue
147451 ** immediately with the next row of a WHERE clause.
147452 */
@@ -162028,23 +162417,11 @@
162417 sqlite3DeleteTable(db, pParse->pNewTable);
162418 }
162419 if( !IN_RENAME_OBJECT ){
162420 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
162421 }
 
 
162422 sqlite3DbFree(db, pParse->pVList);
 
 
 
 
 
 
 
 
 
 
162423 db->pParse = pParse->pParentParse;
162424 pParse->pParentParse = 0;
162425 assert( nErr==0 || pParse->rc!=SQLITE_OK );
162426 return nErr;
162427 }
@@ -166653,11 +167030,11 @@
167030 ** with various optimizations disabled to verify that the same answer
167031 ** is obtained in every case.
167032 */
167033 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
167034 sqlite3 *db = va_arg(ap, sqlite3*);
167035 db->dbOptFlags = va_arg(ap, u32);
167036 break;
167037 }
167038
167039 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
167040 **
@@ -227709,11 +228086,11 @@
228086 int nArg, /* Number of args */
228087 sqlite3_value **apUnused /* Function arguments */
228088 ){
228089 assert( nArg==0 );
228090 UNUSED_PARAM2(nArg, apUnused);
228091 sqlite3_result_text(pCtx, "fts5: 2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18df9a04", -1, SQLITE_TRANSIENT);
228092 }
228093
228094 /*
228095 ** Return true if zName is the extension on one of the shadow tables used
228096 ** by this module.
@@ -232635,12 +233012,12 @@
233012 }
233013 #endif /* SQLITE_CORE */
233014 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
233015
233016 /************** End of stmt.c ************************************************/
233017 #if __LINE__!=233017
233018 #undef SQLITE_SOURCE_ID
233019 #define SQLITE_SOURCE_ID "2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18dfalt2"
233020 #endif
233021 /* Return the source-id for this library */
233022 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
233023 /************************** End of sqlite3.c ******************************/
233024
+1 -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 "2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553b19f"
128
+#define SQLITE_SOURCE_ID "2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18df9a04"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
134134
--- 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 "2021-01-09 19:10:04 49dfce469e6a17111b349e53578479daf783064200bf0eec5bf8a91d3553b19f"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
134
--- 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 "2021-01-18 12:35:16 c1862abb44873f06ec0d772469d8a2d128ae4670b1e98c2d97b0e2da18df9a04"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
134

Keyboard Shortcuts

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