16c1f4ef2Sdrh /*
26c1f4ef2Sdrh ** 2015-06-08
36c1f4ef2Sdrh **
46c1f4ef2Sdrh ** The author disclaims copyright to this source code. In place of
56c1f4ef2Sdrh ** a legal notice, here is a blessing:
66c1f4ef2Sdrh **
76c1f4ef2Sdrh ** May you do good and not evil.
86c1f4ef2Sdrh ** May you find forgiveness for yourself and forgive others.
96c1f4ef2Sdrh ** May you share freely, never taking more than you give.
106c1f4ef2Sdrh **
116c1f4ef2Sdrh *************************************************************************
126c1f4ef2Sdrh ** This module contains C code that generates VDBE code used to process
136c1f4ef2Sdrh ** the WHERE clause of SQL statements.
146c1f4ef2Sdrh **
156c1f4ef2Sdrh ** This file was originally part of where.c but was split out to improve
166c1f4ef2Sdrh ** readability and editabiliity. This file contains utility routines for
176c1f4ef2Sdrh ** analyzing Expr objects in the WHERE clause.
186c1f4ef2Sdrh */
196c1f4ef2Sdrh #include "sqliteInt.h"
206c1f4ef2Sdrh #include "whereInt.h"
216c1f4ef2Sdrh
226c1f4ef2Sdrh /* Forward declarations */
236c1f4ef2Sdrh static void exprAnalyze(SrcList*, WhereClause*, int);
246c1f4ef2Sdrh
256c1f4ef2Sdrh /*
266c1f4ef2Sdrh ** Deallocate all memory associated with a WhereOrInfo object.
276c1f4ef2Sdrh */
whereOrInfoDelete(sqlite3 * db,WhereOrInfo * p)286c1f4ef2Sdrh static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
296c1f4ef2Sdrh sqlite3WhereClauseClear(&p->wc);
306c1f4ef2Sdrh sqlite3DbFree(db, p);
316c1f4ef2Sdrh }
326c1f4ef2Sdrh
336c1f4ef2Sdrh /*
346c1f4ef2Sdrh ** Deallocate all memory associated with a WhereAndInfo object.
356c1f4ef2Sdrh */
whereAndInfoDelete(sqlite3 * db,WhereAndInfo * p)366c1f4ef2Sdrh static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
376c1f4ef2Sdrh sqlite3WhereClauseClear(&p->wc);
386c1f4ef2Sdrh sqlite3DbFree(db, p);
396c1f4ef2Sdrh }
406c1f4ef2Sdrh
416c1f4ef2Sdrh /*
426c1f4ef2Sdrh ** Add a single new WhereTerm entry to the WhereClause object pWC.
436c1f4ef2Sdrh ** The new WhereTerm object is constructed from Expr p and with wtFlags.
446c1f4ef2Sdrh ** The index in pWC->a[] of the new WhereTerm is returned on success.
456c1f4ef2Sdrh ** 0 is returned if the new WhereTerm could not be added due to a memory
466c1f4ef2Sdrh ** allocation error. The memory allocation failure will be recorded in
476c1f4ef2Sdrh ** the db->mallocFailed flag so that higher-level functions can detect it.
486c1f4ef2Sdrh **
496c1f4ef2Sdrh ** This routine will increase the size of the pWC->a[] array as necessary.
506c1f4ef2Sdrh **
516c1f4ef2Sdrh ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
526c1f4ef2Sdrh ** for freeing the expression p is assumed by the WhereClause object pWC.
536c1f4ef2Sdrh ** This is true even if this routine fails to allocate a new WhereTerm.
546c1f4ef2Sdrh **
556c1f4ef2Sdrh ** WARNING: This routine might reallocate the space used to store
566c1f4ef2Sdrh ** WhereTerms. All pointers to WhereTerms should be invalidated after
576c1f4ef2Sdrh ** calling this routine. Such pointers may be reinitialized by referencing
586c1f4ef2Sdrh ** the pWC->a[] array.
596c1f4ef2Sdrh */
whereClauseInsert(WhereClause * pWC,Expr * p,u16 wtFlags)606c1f4ef2Sdrh static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
616c1f4ef2Sdrh WhereTerm *pTerm;
626c1f4ef2Sdrh int idx;
636c1f4ef2Sdrh testcase( wtFlags & TERM_VIRTUAL );
646c1f4ef2Sdrh if( pWC->nTerm>=pWC->nSlot ){
656c1f4ef2Sdrh WhereTerm *pOld = pWC->a;
666c1f4ef2Sdrh sqlite3 *db = pWC->pWInfo->pParse->db;
67f8bdcfa3Sdrh pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
686c1f4ef2Sdrh if( pWC->a==0 ){
696c1f4ef2Sdrh if( wtFlags & TERM_DYNAMIC ){
706c1f4ef2Sdrh sqlite3ExprDelete(db, p);
716c1f4ef2Sdrh }
726c1f4ef2Sdrh pWC->a = pOld;
736c1f4ef2Sdrh return 0;
746c1f4ef2Sdrh }
756c1f4ef2Sdrh memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
76f8bdcfa3Sdrh pWC->nSlot = pWC->nSlot*2;
776c1f4ef2Sdrh }
786c1f4ef2Sdrh pTerm = &pWC->a[idx = pWC->nTerm++];
79132f96fcSdrh if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
806c1f4ef2Sdrh if( p && ExprHasProperty(p, EP_Unlikely) ){
816c1f4ef2Sdrh pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
826c1f4ef2Sdrh }else{
836c1f4ef2Sdrh pTerm->truthProb = 1;
846c1f4ef2Sdrh }
850d950af3Sdrh pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
866c1f4ef2Sdrh pTerm->wtFlags = wtFlags;
876c1f4ef2Sdrh pTerm->pWC = pWC;
886c1f4ef2Sdrh pTerm->iParent = -1;
8987c05f0cSdrh memset(&pTerm->eOperator, 0,
9087c05f0cSdrh sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
916c1f4ef2Sdrh return idx;
926c1f4ef2Sdrh }
936c1f4ef2Sdrh
946c1f4ef2Sdrh /*
956c1f4ef2Sdrh ** Return TRUE if the given operator is one of the operators that is
966c1f4ef2Sdrh ** allowed for an indexable WHERE clause term. The allowed operators are
9771c57db0Sdan ** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
986c1f4ef2Sdrh */
allowedOp(int op)996c1f4ef2Sdrh static int allowedOp(int op){
1006c1f4ef2Sdrh assert( TK_GT>TK_EQ && TK_GT<TK_GE );
1016c1f4ef2Sdrh assert( TK_LT>TK_EQ && TK_LT<TK_GE );
1026c1f4ef2Sdrh assert( TK_LE>TK_EQ && TK_LE<TK_GE );
1036c1f4ef2Sdrh assert( TK_GE==TK_EQ+4 );
1046c1f4ef2Sdrh return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
1056c1f4ef2Sdrh }
1066c1f4ef2Sdrh
1076c1f4ef2Sdrh /*
1086c1f4ef2Sdrh ** Commute a comparison operator. Expressions of the form "X op Y"
1096c1f4ef2Sdrh ** are converted into "Y op X".
1106c1f4ef2Sdrh */
exprCommute(Parse * pParse,Expr * pExpr)111c7d12f4aSdrh static u16 exprCommute(Parse *pParse, Expr *pExpr){
11298c94e60Sdrh if( pExpr->pLeft->op==TK_VECTOR
11398c94e60Sdrh || pExpr->pRight->op==TK_VECTOR
11498c94e60Sdrh || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
11598c94e60Sdrh sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
11698c94e60Sdrh ){
117898c527eSdrh pExpr->flags ^= EP_Commuted;
1186c1f4ef2Sdrh }
1196c1f4ef2Sdrh SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
1206c1f4ef2Sdrh if( pExpr->op>=TK_GT ){
1216c1f4ef2Sdrh assert( TK_LT==TK_GT+2 );
1226c1f4ef2Sdrh assert( TK_GE==TK_LE+2 );
1236c1f4ef2Sdrh assert( TK_GT>TK_EQ );
1246c1f4ef2Sdrh assert( TK_GT<TK_LE );
1256c1f4ef2Sdrh assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
1266c1f4ef2Sdrh pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
1276c1f4ef2Sdrh }
128898c527eSdrh return 0;
1296c1f4ef2Sdrh }
1306c1f4ef2Sdrh
1316c1f4ef2Sdrh /*
1326c1f4ef2Sdrh ** Translate from TK_xx operator to WO_xx bitmask.
1336c1f4ef2Sdrh */
operatorMask(int op)1346c1f4ef2Sdrh static u16 operatorMask(int op){
1356c1f4ef2Sdrh u16 c;
1366c1f4ef2Sdrh assert( allowedOp(op) );
1376c1f4ef2Sdrh if( op==TK_IN ){
1386c1f4ef2Sdrh c = WO_IN;
1396c1f4ef2Sdrh }else if( op==TK_ISNULL ){
1406c1f4ef2Sdrh c = WO_ISNULL;
1416c1f4ef2Sdrh }else if( op==TK_IS ){
1426c1f4ef2Sdrh c = WO_IS;
1436c1f4ef2Sdrh }else{
1446c1f4ef2Sdrh assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
1456c1f4ef2Sdrh c = (u16)(WO_EQ<<(op-TK_EQ));
1466c1f4ef2Sdrh }
1476c1f4ef2Sdrh assert( op!=TK_ISNULL || c==WO_ISNULL );
1486c1f4ef2Sdrh assert( op!=TK_IN || c==WO_IN );
1496c1f4ef2Sdrh assert( op!=TK_EQ || c==WO_EQ );
1506c1f4ef2Sdrh assert( op!=TK_LT || c==WO_LT );
1516c1f4ef2Sdrh assert( op!=TK_LE || c==WO_LE );
1526c1f4ef2Sdrh assert( op!=TK_GT || c==WO_GT );
1536c1f4ef2Sdrh assert( op!=TK_GE || c==WO_GE );
1546c1f4ef2Sdrh assert( op!=TK_IS || c==WO_IS );
1556c1f4ef2Sdrh return c;
1566c1f4ef2Sdrh }
1576c1f4ef2Sdrh
1586c1f4ef2Sdrh
1596c1f4ef2Sdrh #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1606c1f4ef2Sdrh /*
1616c1f4ef2Sdrh ** Check to see if the given expression is a LIKE or GLOB operator that
1626c1f4ef2Sdrh ** can be optimized using inequality constraints. Return TRUE if it is
1636c1f4ef2Sdrh ** so and false if not.
1646c1f4ef2Sdrh **
1656c1f4ef2Sdrh ** In order for the operator to be optimizible, the RHS must be a string
1666c1f4ef2Sdrh ** literal that does not begin with a wildcard. The LHS must be a column
1676c1f4ef2Sdrh ** that may only be NULL, a string, or a BLOB, never a number. (This means
1686c1f4ef2Sdrh ** that virtual tables cannot participate in the LIKE optimization.) The
1696c1f4ef2Sdrh ** collating sequence for the column on the LHS must be appropriate for
1706c1f4ef2Sdrh ** the operator.
1716c1f4ef2Sdrh */
isLikeOrGlob(Parse * pParse,Expr * pExpr,Expr ** ppPrefix,int * pisComplete,int * pnoCase)1726c1f4ef2Sdrh static int isLikeOrGlob(
1736c1f4ef2Sdrh Parse *pParse, /* Parsing and code generating context */
1746c1f4ef2Sdrh Expr *pExpr, /* Test this expression */
1756c1f4ef2Sdrh Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
1766c1f4ef2Sdrh int *pisComplete, /* True if the only wildcard is % in the last character */
1776c1f4ef2Sdrh int *pnoCase /* True if uppercase is equivalent to lowercase */
1786c1f4ef2Sdrh ){
179b8313cc9Sdrh const u8 *z = 0; /* String on RHS of LIKE operator */
1806c1f4ef2Sdrh Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
1816c1f4ef2Sdrh ExprList *pList; /* List of operands to the LIKE operator */
182ad9f515fSdrh u8 c; /* One character in z[] */
1836c1f4ef2Sdrh int cnt; /* Number of non-wildcard prefix characters */
184ad9f515fSdrh u8 wc[4]; /* Wildcard characters */
1856c1f4ef2Sdrh sqlite3 *db = pParse->db; /* Database connection */
1866c1f4ef2Sdrh sqlite3_value *pVal = 0;
1876c1f4ef2Sdrh int op; /* Opcode of pRight */
188b8763639Sdrh int rc; /* Result code to return */
1896c1f4ef2Sdrh
190ad9f515fSdrh if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
1916c1f4ef2Sdrh return 0;
1926c1f4ef2Sdrh }
1936c1f4ef2Sdrh #ifdef SQLITE_EBCDIC
1946c1f4ef2Sdrh if( *pnoCase ) return 0;
1956c1f4ef2Sdrh #endif
196a4eeccdfSdrh assert( ExprUseXList(pExpr) );
1976c1f4ef2Sdrh pList = pExpr->x.pList;
1986c1f4ef2Sdrh pLeft = pList->a[1].pExpr;
1996c1f4ef2Sdrh
2006c1f4ef2Sdrh pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
2016c1f4ef2Sdrh op = pRight->op;
2027df7475dSdrh if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
2036c1f4ef2Sdrh Vdbe *pReprepare = pParse->pReprepare;
2046c1f4ef2Sdrh int iCol = pRight->iColumn;
2056c1f4ef2Sdrh pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
2066c1f4ef2Sdrh if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
207b8313cc9Sdrh z = sqlite3_value_text(pVal);
2086c1f4ef2Sdrh }
2096c1f4ef2Sdrh sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
2106c1f4ef2Sdrh assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
2116c1f4ef2Sdrh }else if( op==TK_STRING ){
212f9751074Sdrh assert( !ExprHasProperty(pRight, EP_IntValue) );
213b8313cc9Sdrh z = (u8*)pRight->u.zToken;
2146c1f4ef2Sdrh }
2156c1f4ef2Sdrh if( z ){
2161c84bd47Sdrh
2171d42ea71Sdrh /* Count the number of prefix characters prior to the first wildcard */
2186c1f4ef2Sdrh cnt = 0;
2196c1f4ef2Sdrh while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
2206c1f4ef2Sdrh cnt++;
221f41a8d3dSdrh if( c==wc[3] && z[cnt]!=0 ) cnt++;
2221d42ea71Sdrh }
2231d42ea71Sdrh
2241d42ea71Sdrh /* The optimization is possible only if (1) the pattern does not begin
2251d42ea71Sdrh ** with a wildcard and if (2) the non-wildcard prefix does not end with
2266b4b8820Sdan ** an (illegal 0xff) character, or (3) the pattern does not consist of
2276b4b8820Sdan ** a single escape character. The second condition is necessary so
2281d42ea71Sdrh ** that we can increment the prefix key to find an upper bound for the
2296b4b8820Sdan ** range search. The third is because the caller assumes that the pattern
2306b4b8820Sdan ** consists of at least one character after all escapes have been
2316b4b8820Sdan ** removed. */
2326b4b8820Sdan if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
2336c1f4ef2Sdrh Expr *pPrefix;
2341d42ea71Sdrh
2351d42ea71Sdrh /* A "complete" match if the pattern ends with "*" or "%" */
2366c1f4ef2Sdrh *pisComplete = c==wc[0] && z[cnt+1]==0;
2371d42ea71Sdrh
2381d42ea71Sdrh /* Get the pattern prefix. Remove all escapes from the prefix. */
239b8313cc9Sdrh pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
2401d42ea71Sdrh if( pPrefix ){
2411d42ea71Sdrh int iFrom, iTo;
242f9751074Sdrh char *zNew;
243f9751074Sdrh assert( !ExprHasProperty(pPrefix, EP_IntValue) );
244f9751074Sdrh zNew = pPrefix->u.zToken;
2451d42ea71Sdrh zNew[cnt] = 0;
2461d42ea71Sdrh for(iFrom=iTo=0; iFrom<cnt; iFrom++){
2471d42ea71Sdrh if( zNew[iFrom]==wc[3] ) iFrom++;
2481d42ea71Sdrh zNew[iTo++] = zNew[iFrom];
2491d42ea71Sdrh }
2501d42ea71Sdrh zNew[iTo] = 0;
2516f7f5d95Sdrh assert( iTo>0 );
252b7a002f8Sdrh
253060b7fa9Sdrh /* If the LHS is not an ordinary column with TEXT affinity, then the
254060b7fa9Sdrh ** pattern prefix boundaries (both the start and end boundaries) must
255060b7fa9Sdrh ** not look like a number. Otherwise the pattern might be treated as
256060b7fa9Sdrh ** a number, which will invalidate the LIKE optimization.
257b7a002f8Sdrh **
258060b7fa9Sdrh ** Getting this right has been a persistent source of bugs in the
259060b7fa9Sdrh ** LIKE optimization. See, for example:
260060b7fa9Sdrh ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
261060b7fa9Sdrh ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
262060b7fa9Sdrh ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
263060b7fa9Sdrh ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
26491f473b5Sdrh ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
265b7a002f8Sdrh */
266b7a002f8Sdrh if( pLeft->op!=TK_COLUMN
267b7a002f8Sdrh || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
268477572b9Sdrh || (ALWAYS( ExprUseYTab(pLeft) )
26963b3a64cSdrh && ALWAYS(pLeft->y.pTab)
270477572b9Sdrh && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
271b7a002f8Sdrh ){
272060b7fa9Sdrh int isNum;
273060b7fa9Sdrh double rDummy;
274060b7fa9Sdrh isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
275060b7fa9Sdrh if( isNum<=0 ){
27691f473b5Sdrh if( iTo==1 && zNew[0]=='-' ){
27791f473b5Sdrh isNum = +1;
27891f473b5Sdrh }else{
279060b7fa9Sdrh zNew[iTo-1]++;
280060b7fa9Sdrh isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
281060b7fa9Sdrh zNew[iTo-1]--;
282060b7fa9Sdrh }
28391f473b5Sdrh }
284060b7fa9Sdrh if( isNum>0 ){
285b7a002f8Sdrh sqlite3ExprDelete(db, pPrefix);
286b7a002f8Sdrh sqlite3ValueFree(pVal);
287b7a002f8Sdrh return 0;
288b7a002f8Sdrh }
289b7a002f8Sdrh }
2901d42ea71Sdrh }
2916c1f4ef2Sdrh *ppPrefix = pPrefix;
2921d42ea71Sdrh
2931d42ea71Sdrh /* If the RHS pattern is a bound parameter, make arrangements to
2941d42ea71Sdrh ** reprepare the statement when that parameter is rebound */
2956c1f4ef2Sdrh if( op==TK_VARIABLE ){
2966c1f4ef2Sdrh Vdbe *v = pParse->pVdbe;
2976c1f4ef2Sdrh sqlite3VdbeSetVarmask(v, pRight->iColumn);
298f9751074Sdrh assert( !ExprHasProperty(pRight, EP_IntValue) );
2996c1f4ef2Sdrh if( *pisComplete && pRight->u.zToken[1] ){
3006c1f4ef2Sdrh /* If the rhs of the LIKE expression is a variable, and the current
3016c1f4ef2Sdrh ** value of the variable means there is no need to invoke the LIKE
3026c1f4ef2Sdrh ** function, then no OP_Variable will be added to the program.
3036c1f4ef2Sdrh ** This causes problems for the sqlite3_bind_parameter_name()
3046c1f4ef2Sdrh ** API. To work around them, add a dummy OP_Variable here.
3056c1f4ef2Sdrh */
3066c1f4ef2Sdrh int r1 = sqlite3GetTempReg(pParse);
3076c1f4ef2Sdrh sqlite3ExprCodeTarget(pParse, pRight, r1);
3086c1f4ef2Sdrh sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
3096c1f4ef2Sdrh sqlite3ReleaseTempReg(pParse, r1);
3106c1f4ef2Sdrh }
3116c1f4ef2Sdrh }
3126c1f4ef2Sdrh }else{
3136c1f4ef2Sdrh z = 0;
3146c1f4ef2Sdrh }
3156c1f4ef2Sdrh }
3166c1f4ef2Sdrh
317b8763639Sdrh rc = (z!=0);
3186c1f4ef2Sdrh sqlite3ValueFree(pVal);
319b8763639Sdrh return rc;
3206c1f4ef2Sdrh }
3216c1f4ef2Sdrh #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
3226c1f4ef2Sdrh
3236c1f4ef2Sdrh
3246c1f4ef2Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
3256c1f4ef2Sdrh /*
326303a69b5Sdrh ** Check to see if the pExpr expression is a form that needs to be passed
327303a69b5Sdrh ** to the xBestIndex method of virtual tables. Forms of interest include:
3286c1f4ef2Sdrh **
329303a69b5Sdrh ** Expression Virtual Table Operator
330303a69b5Sdrh ** ----------------------- ---------------------------------
331303a69b5Sdrh ** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
332303a69b5Sdrh ** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
333303a69b5Sdrh ** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
334303a69b5Sdrh ** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
335303a69b5Sdrh ** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
336303a69b5Sdrh ** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
337303a69b5Sdrh ** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
338303a69b5Sdrh ** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
339303a69b5Sdrh ** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
34043970dd7Sdan **
341303a69b5Sdrh ** In every case, "column" must be a column of a virtual table. If there
342303a69b5Sdrh ** is a match, set *ppLeft to the "column" expression, set *ppRight to the
343303a69b5Sdrh ** "expr" expression (even though in forms (6) and (8) the column is on the
344303a69b5Sdrh ** right and the expression is on the left). Also set *peOp2 to the
345303a69b5Sdrh ** appropriate virtual table operator. The return value is 1 or 2 if there
346303a69b5Sdrh ** is a match. The usual return is 1, but if the RHS is also a column
347303a69b5Sdrh ** of virtual table in forms (5) or (7) then return 2.
348d03024d8Sdan **
349d03024d8Sdan ** If the expression matches none of the patterns above, return 0.
3506c1f4ef2Sdrh */
isAuxiliaryVtabOperator(sqlite3 * db,Expr * pExpr,unsigned char * peOp2,Expr ** ppLeft,Expr ** ppRight)351303a69b5Sdrh static int isAuxiliaryVtabOperator(
35259155065Sdrh sqlite3 *db, /* Parsing context */
35307bdba86Sdan Expr *pExpr, /* Test this expression */
354d03024d8Sdan unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
355d03024d8Sdan Expr **ppLeft, /* Column expression to left of MATCH/op2 */
356d03024d8Sdan Expr **ppRight /* Expression to left of MATCH/op2 */
3576c1f4ef2Sdrh ){
358d03024d8Sdan if( pExpr->op==TK_FUNCTION ){
359e104dd3cSdrh static const struct Op2 {
36007bdba86Sdan const char *zOp;
36107bdba86Sdan unsigned char eOp2;
36207bdba86Sdan } aOp[] = {
36307bdba86Sdan { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
36407bdba86Sdan { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
36507bdba86Sdan { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
36643970dd7Sdan { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
36707bdba86Sdan };
3686c1f4ef2Sdrh ExprList *pList;
36943970dd7Sdan Expr *pCol; /* Column reference */
37007bdba86Sdan int i;
3716c1f4ef2Sdrh
372a4eeccdfSdrh assert( ExprUseXList(pExpr) );
3736c1f4ef2Sdrh pList = pExpr->x.pList;
374ff7b22b7Sdan if( pList==0 || pList->nExpr!=2 ){
3756c1f4ef2Sdrh return 0;
3766c1f4ef2Sdrh }
37759155065Sdrh
37859155065Sdrh /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
37959155065Sdrh ** virtual table on their second argument, which is the same as
38059155065Sdrh ** the left-hand side operand in their in-fix form.
38159155065Sdrh **
38259155065Sdrh ** vtab_column MATCH expression
38359155065Sdrh ** MATCH(expression,vtab_column)
38459155065Sdrh */
38543970dd7Sdan pCol = pList->a[1].pExpr;
38663b3a64cSdrh assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
38778d1d225Sdrh if( ExprIsVtab(pCol) ){
38807bdba86Sdan for(i=0; i<ArraySize(aOp); i++){
389f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) );
39007bdba86Sdan if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
39107bdba86Sdan *peOp2 = aOp[i].eOp2;
392d03024d8Sdan *ppRight = pList->a[0].pExpr;
393d03024d8Sdan *ppLeft = pCol;
3946c1f4ef2Sdrh return 1;
3956c1f4ef2Sdrh }
39607bdba86Sdan }
39759155065Sdrh }
39859155065Sdrh
39959155065Sdrh /* We can also match against the first column of overloaded
40059155065Sdrh ** functions where xFindFunction returns a value of at least
40159155065Sdrh ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
40259155065Sdrh **
40359155065Sdrh ** OVERLOADED(vtab_column,expression)
40459155065Sdrh **
40559155065Sdrh ** Historically, xFindFunction expected to see lower-case function
40659155065Sdrh ** names. But for this use case, xFindFunction is expected to deal
40759155065Sdrh ** with function names in an arbitrary case.
40859155065Sdrh */
40959155065Sdrh pCol = pList->a[0].pExpr;
410477572b9Sdrh assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
41163b3a64cSdrh assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
41278d1d225Sdrh if( ExprIsVtab(pCol) ){
41359155065Sdrh sqlite3_vtab *pVtab;
41459155065Sdrh sqlite3_module *pMod;
41559155065Sdrh void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
41659155065Sdrh void *pNotUsed;
417eda079cdSdrh pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
41859155065Sdrh assert( pVtab!=0 );
41959155065Sdrh assert( pVtab->pModule!=0 );
420f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) );
42159155065Sdrh pMod = (sqlite3_module *)pVtab->pModule;
42259155065Sdrh if( pMod->xFindFunction!=0 ){
42359155065Sdrh i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
42459155065Sdrh if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
42559155065Sdrh *peOp2 = i;
42659155065Sdrh *ppRight = pList->a[1].pExpr;
42759155065Sdrh *ppLeft = pCol;
42859155065Sdrh return 1;
42959155065Sdrh }
43059155065Sdrh }
43159155065Sdrh }
432d03024d8Sdan }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
433d03024d8Sdan int res = 0;
434d03024d8Sdan Expr *pLeft = pExpr->pLeft;
435d03024d8Sdan Expr *pRight = pExpr->pRight;
43663b3a64cSdrh assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
43778d1d225Sdrh if( ExprIsVtab(pLeft) ){
438d03024d8Sdan res++;
439d03024d8Sdan }
44063b3a64cSdrh assert( pRight==0 || pRight->op!=TK_COLUMN
44163b3a64cSdrh || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
44278d1d225Sdrh if( pRight && ExprIsVtab(pRight) ){
443d03024d8Sdan res++;
444d03024d8Sdan SWAP(Expr*, pLeft, pRight);
445d03024d8Sdan }
446d03024d8Sdan *ppLeft = pLeft;
447d03024d8Sdan *ppRight = pRight;
448d03024d8Sdan if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
449d03024d8Sdan if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
450d03024d8Sdan if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
451d03024d8Sdan return res;
452d03024d8Sdan }
45307bdba86Sdan return 0;
45407bdba86Sdan }
4556c1f4ef2Sdrh #endif /* SQLITE_OMIT_VIRTUALTABLE */
4566c1f4ef2Sdrh
4576c1f4ef2Sdrh /*
4586c1f4ef2Sdrh ** If the pBase expression originated in the ON or USING clause of
4596c1f4ef2Sdrh ** a join, then transfer the appropriate markings over to derived.
4606c1f4ef2Sdrh */
transferJoinMarkings(Expr * pDerived,Expr * pBase)4616c1f4ef2Sdrh static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
462a6e8ee12Sdrh if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON) ){
463a6e8ee12Sdrh pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
464d1985262Sdrh pDerived->w.iJoin = pBase->w.iJoin;
4656c1f4ef2Sdrh }
4666c1f4ef2Sdrh }
4676c1f4ef2Sdrh
4686c1f4ef2Sdrh /*
4696c1f4ef2Sdrh ** Mark term iChild as being a child of term iParent
4706c1f4ef2Sdrh */
markTermAsChild(WhereClause * pWC,int iChild,int iParent)4716c1f4ef2Sdrh static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
4726c1f4ef2Sdrh pWC->a[iChild].iParent = iParent;
4736c1f4ef2Sdrh pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
4746c1f4ef2Sdrh pWC->a[iParent].nChild++;
4756c1f4ef2Sdrh }
4766c1f4ef2Sdrh
4776c1f4ef2Sdrh /*
4786c1f4ef2Sdrh ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
4796c1f4ef2Sdrh ** a conjunction, then return just pTerm when N==0. If N is exceeds
4806c1f4ef2Sdrh ** the number of available subterms, return NULL.
4816c1f4ef2Sdrh */
whereNthSubterm(WhereTerm * pTerm,int N)4826c1f4ef2Sdrh static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
4836c1f4ef2Sdrh if( pTerm->eOperator!=WO_AND ){
4846c1f4ef2Sdrh return N==0 ? pTerm : 0;
4856c1f4ef2Sdrh }
4866c1f4ef2Sdrh if( N<pTerm->u.pAndInfo->wc.nTerm ){
4876c1f4ef2Sdrh return &pTerm->u.pAndInfo->wc.a[N];
4886c1f4ef2Sdrh }
4896c1f4ef2Sdrh return 0;
4906c1f4ef2Sdrh }
4916c1f4ef2Sdrh
4926c1f4ef2Sdrh /*
4936c1f4ef2Sdrh ** Subterms pOne and pTwo are contained within WHERE clause pWC. The
4946c1f4ef2Sdrh ** two subterms are in disjunction - they are OR-ed together.
4956c1f4ef2Sdrh **
4966c1f4ef2Sdrh ** If these two terms are both of the form: "A op B" with the same
4976c1f4ef2Sdrh ** A and B values but different operators and if the operators are
4986c1f4ef2Sdrh ** compatible (if one is = and the other is <, for example) then
4996c1f4ef2Sdrh ** add a new virtual AND term to pWC that is the combination of the
5006c1f4ef2Sdrh ** two.
5016c1f4ef2Sdrh **
5026c1f4ef2Sdrh ** Some examples:
5036c1f4ef2Sdrh **
5046c1f4ef2Sdrh ** x<y OR x=y --> x<=y
5056c1f4ef2Sdrh ** x=y OR x=y --> x=y
5066c1f4ef2Sdrh ** x<=y OR x<y --> x<=y
5076c1f4ef2Sdrh **
5086c1f4ef2Sdrh ** The following is NOT generated:
5096c1f4ef2Sdrh **
5106c1f4ef2Sdrh ** x<y OR x>y --> x!=y
5116c1f4ef2Sdrh */
whereCombineDisjuncts(SrcList * pSrc,WhereClause * pWC,WhereTerm * pOne,WhereTerm * pTwo)5126c1f4ef2Sdrh static void whereCombineDisjuncts(
5136c1f4ef2Sdrh SrcList *pSrc, /* the FROM clause */
5146c1f4ef2Sdrh WhereClause *pWC, /* The complete WHERE clause */
5156c1f4ef2Sdrh WhereTerm *pOne, /* First disjunct */
5166c1f4ef2Sdrh WhereTerm *pTwo /* Second disjunct */
5176c1f4ef2Sdrh ){
5186c1f4ef2Sdrh u16 eOp = pOne->eOperator | pTwo->eOperator;
5196c1f4ef2Sdrh sqlite3 *db; /* Database connection (for malloc) */
5206c1f4ef2Sdrh Expr *pNew; /* New virtual expression */
5216c1f4ef2Sdrh int op; /* Operator for the combined expression */
5226c1f4ef2Sdrh int idxNew; /* Index in pWC of the next virtual term */
5236c1f4ef2Sdrh
524677e62aaSdan if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
5256c1f4ef2Sdrh if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
5266c1f4ef2Sdrh if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
5276c1f4ef2Sdrh if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
5286c1f4ef2Sdrh && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
5296c1f4ef2Sdrh assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
5306c1f4ef2Sdrh assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
5315aa550cfSdan if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
5325aa550cfSdan if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
5336c1f4ef2Sdrh /* If we reach this point, it means the two subterms can be combined */
5346c1f4ef2Sdrh if( (eOp & (eOp-1))!=0 ){
5356c1f4ef2Sdrh if( eOp & (WO_LT|WO_LE) ){
5366c1f4ef2Sdrh eOp = WO_LE;
5376c1f4ef2Sdrh }else{
5386c1f4ef2Sdrh assert( eOp & (WO_GT|WO_GE) );
5396c1f4ef2Sdrh eOp = WO_GE;
5406c1f4ef2Sdrh }
5416c1f4ef2Sdrh }
5426c1f4ef2Sdrh db = pWC->pWInfo->pParse->db;
5436c1f4ef2Sdrh pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
5446c1f4ef2Sdrh if( pNew==0 ) return;
5456c1f4ef2Sdrh for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
5466c1f4ef2Sdrh pNew->op = op;
5476c1f4ef2Sdrh idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
5486c1f4ef2Sdrh exprAnalyze(pSrc, pWC, idxNew);
5496c1f4ef2Sdrh }
5506c1f4ef2Sdrh
5516c1f4ef2Sdrh #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
5526c1f4ef2Sdrh /*
5536c1f4ef2Sdrh ** Analyze a term that consists of two or more OR-connected
5546c1f4ef2Sdrh ** subterms. So in:
5556c1f4ef2Sdrh **
5566c1f4ef2Sdrh ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
5576c1f4ef2Sdrh ** ^^^^^^^^^^^^^^^^^^^^
5586c1f4ef2Sdrh **
5596c1f4ef2Sdrh ** This routine analyzes terms such as the middle term in the above example.
5606c1f4ef2Sdrh ** A WhereOrTerm object is computed and attached to the term under
5616c1f4ef2Sdrh ** analysis, regardless of the outcome of the analysis. Hence:
5626c1f4ef2Sdrh **
5636c1f4ef2Sdrh ** WhereTerm.wtFlags |= TERM_ORINFO
5646c1f4ef2Sdrh ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
5656c1f4ef2Sdrh **
5666c1f4ef2Sdrh ** The term being analyzed must have two or more of OR-connected subterms.
5676c1f4ef2Sdrh ** A single subterm might be a set of AND-connected sub-subterms.
5686c1f4ef2Sdrh ** Examples of terms under analysis:
5696c1f4ef2Sdrh **
5706c1f4ef2Sdrh ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
5716c1f4ef2Sdrh ** (B) x=expr1 OR expr2=x OR x=expr3
5726c1f4ef2Sdrh ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
5736c1f4ef2Sdrh ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
5746c1f4ef2Sdrh ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
5756c1f4ef2Sdrh ** (F) x>A OR (x=A AND y>=B)
5766c1f4ef2Sdrh **
5776c1f4ef2Sdrh ** CASE 1:
5786c1f4ef2Sdrh **
5796c1f4ef2Sdrh ** If all subterms are of the form T.C=expr for some single column of C and
5806c1f4ef2Sdrh ** a single table T (as shown in example B above) then create a new virtual
5816c1f4ef2Sdrh ** term that is an equivalent IN expression. In other words, if the term
5826c1f4ef2Sdrh ** being analyzed is:
5836c1f4ef2Sdrh **
5846c1f4ef2Sdrh ** x = expr1 OR expr2 = x OR x = expr3
5856c1f4ef2Sdrh **
5866c1f4ef2Sdrh ** then create a new virtual term like this:
5876c1f4ef2Sdrh **
5886c1f4ef2Sdrh ** x IN (expr1,expr2,expr3)
5896c1f4ef2Sdrh **
5906c1f4ef2Sdrh ** CASE 2:
5916c1f4ef2Sdrh **
5926c1f4ef2Sdrh ** If there are exactly two disjuncts and one side has x>A and the other side
5936c1f4ef2Sdrh ** has x=A (for the same x and A) then add a new virtual conjunct term to the
5946c1f4ef2Sdrh ** WHERE clause of the form "x>=A". Example:
5956c1f4ef2Sdrh **
5966c1f4ef2Sdrh ** x>A OR (x=A AND y>B) adds: x>=A
5976c1f4ef2Sdrh **
5986c1f4ef2Sdrh ** The added conjunct can sometimes be helpful in query planning.
5996c1f4ef2Sdrh **
6006c1f4ef2Sdrh ** CASE 3:
6016c1f4ef2Sdrh **
6026c1f4ef2Sdrh ** If all subterms are indexable by a single table T, then set
6036c1f4ef2Sdrh **
6046c1f4ef2Sdrh ** WhereTerm.eOperator = WO_OR
6056c1f4ef2Sdrh ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
6066c1f4ef2Sdrh **
6076c1f4ef2Sdrh ** A subterm is "indexable" if it is of the form
6086c1f4ef2Sdrh ** "T.C <op> <expr>" where C is any column of table T and
6096c1f4ef2Sdrh ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
6106c1f4ef2Sdrh ** A subterm is also indexable if it is an AND of two or more
6116c1f4ef2Sdrh ** subsubterms at least one of which is indexable. Indexable AND
6126c1f4ef2Sdrh ** subterms have their eOperator set to WO_AND and they have
6136c1f4ef2Sdrh ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
6146c1f4ef2Sdrh **
6156c1f4ef2Sdrh ** From another point of view, "indexable" means that the subterm could
6166c1f4ef2Sdrh ** potentially be used with an index if an appropriate index exists.
6176c1f4ef2Sdrh ** This analysis does not consider whether or not the index exists; that
6186c1f4ef2Sdrh ** is decided elsewhere. This analysis only looks at whether subterms
6196c1f4ef2Sdrh ** appropriate for indexing exist.
6206c1f4ef2Sdrh **
6216c1f4ef2Sdrh ** All examples A through E above satisfy case 3. But if a term
6226c1f4ef2Sdrh ** also satisfies case 1 (such as B) we know that the optimizer will
6236c1f4ef2Sdrh ** always prefer case 1, so in that case we pretend that case 3 is not
6246c1f4ef2Sdrh ** satisfied.
6256c1f4ef2Sdrh **
6266c1f4ef2Sdrh ** It might be the case that multiple tables are indexable. For example,
6276c1f4ef2Sdrh ** (E) above is indexable on tables P, Q, and R.
6286c1f4ef2Sdrh **
6296c1f4ef2Sdrh ** Terms that satisfy case 3 are candidates for lookup by using
6306c1f4ef2Sdrh ** separate indices to find rowids for each subterm and composing
6316c1f4ef2Sdrh ** the union of all rowids using a RowSet object. This is similar
6326c1f4ef2Sdrh ** to "bitmap indices" in other database engines.
6336c1f4ef2Sdrh **
6346c1f4ef2Sdrh ** OTHERWISE:
6356c1f4ef2Sdrh **
6366c1f4ef2Sdrh ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
6376c1f4ef2Sdrh ** zero. This term is not useful for search.
6386c1f4ef2Sdrh */
exprAnalyzeOrTerm(SrcList * pSrc,WhereClause * pWC,int idxTerm)6396c1f4ef2Sdrh static void exprAnalyzeOrTerm(
6406c1f4ef2Sdrh SrcList *pSrc, /* the FROM clause */
6416c1f4ef2Sdrh WhereClause *pWC, /* the complete WHERE clause */
6426c1f4ef2Sdrh int idxTerm /* Index of the OR-term to be analyzed */
6436c1f4ef2Sdrh ){
6446c1f4ef2Sdrh WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
6456c1f4ef2Sdrh Parse *pParse = pWInfo->pParse; /* Parser context */
6466c1f4ef2Sdrh sqlite3 *db = pParse->db; /* Database connection */
6476c1f4ef2Sdrh WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
6486c1f4ef2Sdrh Expr *pExpr = pTerm->pExpr; /* The expression of the term */
6496c1f4ef2Sdrh int i; /* Loop counters */
6506c1f4ef2Sdrh WhereClause *pOrWc; /* Breakup of pTerm into subterms */
6516c1f4ef2Sdrh WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
6526c1f4ef2Sdrh WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
6536c1f4ef2Sdrh Bitmask chngToIN; /* Tables that might satisfy case 1 */
6546c1f4ef2Sdrh Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
6556c1f4ef2Sdrh
6566c1f4ef2Sdrh /*
6576c1f4ef2Sdrh ** Break the OR clause into its separate subterms. The subterms are
6586c1f4ef2Sdrh ** stored in a WhereClause structure containing within the WhereOrInfo
6596c1f4ef2Sdrh ** object that is attached to the original OR clause term.
6606c1f4ef2Sdrh */
6616c1f4ef2Sdrh assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
6626c1f4ef2Sdrh assert( pExpr->op==TK_OR );
6636c1f4ef2Sdrh pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
6646c1f4ef2Sdrh if( pOrInfo==0 ) return;
6656c1f4ef2Sdrh pTerm->wtFlags |= TERM_ORINFO;
6666c1f4ef2Sdrh pOrWc = &pOrInfo->wc;
66781fd3497Sdrh memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
6686c1f4ef2Sdrh sqlite3WhereClauseInit(pOrWc, pWInfo);
6696c1f4ef2Sdrh sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
6706c1f4ef2Sdrh sqlite3WhereExprAnalyze(pSrc, pOrWc);
6716c1f4ef2Sdrh if( db->mallocFailed ) return;
6726c1f4ef2Sdrh assert( pOrWc->nTerm>=2 );
6736c1f4ef2Sdrh
6746c1f4ef2Sdrh /*
6756c1f4ef2Sdrh ** Compute the set of tables that might satisfy cases 1 or 3.
6766c1f4ef2Sdrh */
6776c1f4ef2Sdrh indexable = ~(Bitmask)0;
6786c1f4ef2Sdrh chngToIN = ~(Bitmask)0;
6796c1f4ef2Sdrh for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
6806c1f4ef2Sdrh if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
6816c1f4ef2Sdrh WhereAndInfo *pAndInfo;
6826c1f4ef2Sdrh assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
6836c1f4ef2Sdrh chngToIN = 0;
684575fad65Sdrh pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
6856c1f4ef2Sdrh if( pAndInfo ){
6866c1f4ef2Sdrh WhereClause *pAndWC;
6876c1f4ef2Sdrh WhereTerm *pAndTerm;
6886c1f4ef2Sdrh int j;
6896c1f4ef2Sdrh Bitmask b = 0;
6906c1f4ef2Sdrh pOrTerm->u.pAndInfo = pAndInfo;
6916c1f4ef2Sdrh pOrTerm->wtFlags |= TERM_ANDINFO;
6926c1f4ef2Sdrh pOrTerm->eOperator = WO_AND;
693220f0d6fSdrh pOrTerm->leftCursor = -1;
6946c1f4ef2Sdrh pAndWC = &pAndInfo->wc;
69581fd3497Sdrh memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
6966c1f4ef2Sdrh sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
6976c1f4ef2Sdrh sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
6986c1f4ef2Sdrh sqlite3WhereExprAnalyze(pSrc, pAndWC);
6996c1f4ef2Sdrh pAndWC->pOuter = pWC;
7006c1f4ef2Sdrh if( !db->mallocFailed ){
7016c1f4ef2Sdrh for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
7026c1f4ef2Sdrh assert( pAndTerm->pExpr );
703dbd2dcbdSdan if( allowedOp(pAndTerm->pExpr->op)
704303a69b5Sdrh || pAndTerm->eOperator==WO_AUX
705dbd2dcbdSdan ){
7066c1f4ef2Sdrh b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
7076c1f4ef2Sdrh }
7086c1f4ef2Sdrh }
7096c1f4ef2Sdrh }
7106c1f4ef2Sdrh indexable &= b;
7116c1f4ef2Sdrh }
7126c1f4ef2Sdrh }else if( pOrTerm->wtFlags & TERM_COPIED ){
7136c1f4ef2Sdrh /* Skip this term for now. We revisit it when we process the
7146c1f4ef2Sdrh ** corresponding TERM_VIRTUAL term */
7156c1f4ef2Sdrh }else{
7166c1f4ef2Sdrh Bitmask b;
7176c1f4ef2Sdrh b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
7186c1f4ef2Sdrh if( pOrTerm->wtFlags & TERM_VIRTUAL ){
7196c1f4ef2Sdrh WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
7206c1f4ef2Sdrh b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
7216c1f4ef2Sdrh }
7226c1f4ef2Sdrh indexable &= b;
7236c1f4ef2Sdrh if( (pOrTerm->eOperator & WO_EQ)==0 ){
7246c1f4ef2Sdrh chngToIN = 0;
7256c1f4ef2Sdrh }else{
7266c1f4ef2Sdrh chngToIN &= b;
7276c1f4ef2Sdrh }
7286c1f4ef2Sdrh }
7296c1f4ef2Sdrh }
7306c1f4ef2Sdrh
7316c1f4ef2Sdrh /*
7326c1f4ef2Sdrh ** Record the set of tables that satisfy case 3. The set might be
7336c1f4ef2Sdrh ** empty.
7346c1f4ef2Sdrh */
7356c1f4ef2Sdrh pOrInfo->indexable = indexable;
736220f0d6fSdrh pTerm->eOperator = WO_OR;
737220f0d6fSdrh pTerm->leftCursor = -1;
738da230bd4Sdrh if( indexable ){
739da230bd4Sdrh pWC->hasOr = 1;
740da230bd4Sdrh }
7416c1f4ef2Sdrh
7426c1f4ef2Sdrh /* For a two-way OR, attempt to implementation case 2.
7436c1f4ef2Sdrh */
7446c1f4ef2Sdrh if( indexable && pOrWc->nTerm==2 ){
7456c1f4ef2Sdrh int iOne = 0;
7466c1f4ef2Sdrh WhereTerm *pOne;
7476c1f4ef2Sdrh while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
7486c1f4ef2Sdrh int iTwo = 0;
7496c1f4ef2Sdrh WhereTerm *pTwo;
7506c1f4ef2Sdrh while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
7516c1f4ef2Sdrh whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
7526c1f4ef2Sdrh }
7536c1f4ef2Sdrh }
7546c1f4ef2Sdrh }
7556c1f4ef2Sdrh
7566c1f4ef2Sdrh /*
7576c1f4ef2Sdrh ** chngToIN holds a set of tables that *might* satisfy case 1. But
7586c1f4ef2Sdrh ** we have to do some additional checking to see if case 1 really
7596c1f4ef2Sdrh ** is satisfied.
7606c1f4ef2Sdrh **
7616c1f4ef2Sdrh ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
7626c1f4ef2Sdrh ** that there is no possibility of transforming the OR clause into an
7636c1f4ef2Sdrh ** IN operator because one or more terms in the OR clause contain
7646c1f4ef2Sdrh ** something other than == on a column in the single table. The 1-bit
7656c1f4ef2Sdrh ** case means that every term of the OR clause is of the form
7666c1f4ef2Sdrh ** "table.column=expr" for some single table. The one bit that is set
7676c1f4ef2Sdrh ** will correspond to the common table. We still need to check to make
7686c1f4ef2Sdrh ** sure the same column is used on all terms. The 2-bit case is when
7696c1f4ef2Sdrh ** the all terms are of the form "table1.column=table2.column". It
7706c1f4ef2Sdrh ** might be possible to form an IN operator with either table1.column
7716c1f4ef2Sdrh ** or table2.column as the LHS if either is common to every term of
7726c1f4ef2Sdrh ** the OR clause.
7736c1f4ef2Sdrh **
7746c1f4ef2Sdrh ** Note that terms of the form "table.column1=table.column2" (the
7756c1f4ef2Sdrh ** same table on both sizes of the ==) cannot be optimized.
7766c1f4ef2Sdrh */
7776c1f4ef2Sdrh if( chngToIN ){
7786c1f4ef2Sdrh int okToChngToIN = 0; /* True if the conversion to IN is valid */
7796c1f4ef2Sdrh int iColumn = -1; /* Column index on lhs of IN operator */
7806c1f4ef2Sdrh int iCursor = -1; /* Table cursor common to all terms */
7816c1f4ef2Sdrh int j = 0; /* Loop counter */
7826c1f4ef2Sdrh
7836c1f4ef2Sdrh /* Search for a table and column that appears on one side or the
7846c1f4ef2Sdrh ** other of the == operator in every subterm. That table and column
7856c1f4ef2Sdrh ** will be recorded in iCursor and iColumn. There might not be any
7866c1f4ef2Sdrh ** such table and column. Set okToChngToIN if an appropriate table
7876c1f4ef2Sdrh ** and column is found but leave okToChngToIN false if not found.
7886c1f4ef2Sdrh */
7896c1f4ef2Sdrh for(j=0; j<2 && !okToChngToIN; j++){
7907525b87bSdan Expr *pLeft = 0;
7913c2db5deSdrh pOrTerm = pOrWc->a;
7926c1f4ef2Sdrh for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
7936c1f4ef2Sdrh assert( pOrTerm->eOperator & WO_EQ );
7948a95d3d4Sdrh pOrTerm->wtFlags &= ~TERM_OK;
7956c1f4ef2Sdrh if( pOrTerm->leftCursor==iCursor ){
7966c1f4ef2Sdrh /* This is the 2-bit case and we are on the second iteration and
7976c1f4ef2Sdrh ** current term is from the first iteration. So skip this term. */
7986c1f4ef2Sdrh assert( j==1 );
7996c1f4ef2Sdrh continue;
8006c1f4ef2Sdrh }
8016c1f4ef2Sdrh if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
8026c1f4ef2Sdrh pOrTerm->leftCursor))==0 ){
8036c1f4ef2Sdrh /* This term must be of the form t1.a==t2.b where t2 is in the
8046c1f4ef2Sdrh ** chngToIN set but t1 is not. This term will be either preceded
8056c1f4ef2Sdrh ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
8066c1f4ef2Sdrh ** and use its inversion. */
8076c1f4ef2Sdrh testcase( pOrTerm->wtFlags & TERM_COPIED );
8086c1f4ef2Sdrh testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
8096c1f4ef2Sdrh assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
8106c1f4ef2Sdrh continue;
8116c1f4ef2Sdrh }
812220f0d6fSdrh assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
81375fa2663Sdrh iColumn = pOrTerm->u.x.leftColumn;
8146c1f4ef2Sdrh iCursor = pOrTerm->leftCursor;
8157525b87bSdan pLeft = pOrTerm->pExpr->pLeft;
8166c1f4ef2Sdrh break;
8176c1f4ef2Sdrh }
8186c1f4ef2Sdrh if( i<0 ){
8196c1f4ef2Sdrh /* No candidate table+column was found. This can only occur
8206c1f4ef2Sdrh ** on the second iteration */
8216c1f4ef2Sdrh assert( j==1 );
8226c1f4ef2Sdrh assert( IsPowerOfTwo(chngToIN) );
8236c1f4ef2Sdrh assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
8246c1f4ef2Sdrh break;
8256c1f4ef2Sdrh }
8266c1f4ef2Sdrh testcase( j==1 );
8276c1f4ef2Sdrh
8286c1f4ef2Sdrh /* We have found a candidate table and column. Check to see if that
8296c1f4ef2Sdrh ** table and column is common to every term in the OR clause */
8306c1f4ef2Sdrh okToChngToIN = 1;
8316c1f4ef2Sdrh for(; i>=0 && okToChngToIN; i--, pOrTerm++){
8326c1f4ef2Sdrh assert( pOrTerm->eOperator & WO_EQ );
833220f0d6fSdrh assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
8346c1f4ef2Sdrh if( pOrTerm->leftCursor!=iCursor ){
8358a95d3d4Sdrh pOrTerm->wtFlags &= ~TERM_OK;
83675fa2663Sdrh }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
8377525b87bSdan && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
8387525b87bSdan )){
8396c1f4ef2Sdrh okToChngToIN = 0;
8406c1f4ef2Sdrh }else{
8416c1f4ef2Sdrh int affLeft, affRight;
8426c1f4ef2Sdrh /* If the right-hand side is also a column, then the affinities
8436c1f4ef2Sdrh ** of both right and left sides must be such that no type
8446c1f4ef2Sdrh ** conversions are required on the right. (Ticket #2249)
8456c1f4ef2Sdrh */
8466c1f4ef2Sdrh affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
8476c1f4ef2Sdrh affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
8486c1f4ef2Sdrh if( affRight!=0 && affRight!=affLeft ){
8496c1f4ef2Sdrh okToChngToIN = 0;
8506c1f4ef2Sdrh }else{
8518a95d3d4Sdrh pOrTerm->wtFlags |= TERM_OK;
8526c1f4ef2Sdrh }
8536c1f4ef2Sdrh }
8546c1f4ef2Sdrh }
8556c1f4ef2Sdrh }
8566c1f4ef2Sdrh
8576c1f4ef2Sdrh /* At this point, okToChngToIN is true if original pTerm satisfies
8586c1f4ef2Sdrh ** case 1. In that case, construct a new virtual term that is
8596c1f4ef2Sdrh ** pTerm converted into an IN operator.
8606c1f4ef2Sdrh */
8616c1f4ef2Sdrh if( okToChngToIN ){
8626c1f4ef2Sdrh Expr *pDup; /* A transient duplicate expression */
8636c1f4ef2Sdrh ExprList *pList = 0; /* The RHS of the IN operator */
8646c1f4ef2Sdrh Expr *pLeft = 0; /* The LHS of the IN operator */
8656c1f4ef2Sdrh Expr *pNew; /* The complete IN operator */
8666c1f4ef2Sdrh
8676c1f4ef2Sdrh for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
8688a95d3d4Sdrh if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
8696c1f4ef2Sdrh assert( pOrTerm->eOperator & WO_EQ );
870220f0d6fSdrh assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
8716c1f4ef2Sdrh assert( pOrTerm->leftCursor==iCursor );
87275fa2663Sdrh assert( pOrTerm->u.x.leftColumn==iColumn );
8736c1f4ef2Sdrh pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
8746c1f4ef2Sdrh pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
8756c1f4ef2Sdrh pLeft = pOrTerm->pExpr->pLeft;
8766c1f4ef2Sdrh }
8776c1f4ef2Sdrh assert( pLeft!=0 );
8786c1f4ef2Sdrh pDup = sqlite3ExprDup(db, pLeft, 0);
879abfd35eaSdrh pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
8806c1f4ef2Sdrh if( pNew ){
8816c1f4ef2Sdrh int idxNew;
8826c1f4ef2Sdrh transferJoinMarkings(pNew, pExpr);
883a4eeccdfSdrh assert( ExprUseXList(pNew) );
8846c1f4ef2Sdrh pNew->x.pList = pList;
8856c1f4ef2Sdrh idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
8866c1f4ef2Sdrh testcase( idxNew==0 );
8876c1f4ef2Sdrh exprAnalyze(pSrc, pWC, idxNew);
88811c87895Sdrh /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
8896c1f4ef2Sdrh markTermAsChild(pWC, idxNew, idxTerm);
8906c1f4ef2Sdrh }else{
8916c1f4ef2Sdrh sqlite3ExprListDelete(db, pList);
8926c1f4ef2Sdrh }
8936c1f4ef2Sdrh }
8946c1f4ef2Sdrh }
8956c1f4ef2Sdrh }
8966c1f4ef2Sdrh #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
8976c1f4ef2Sdrh
8986c1f4ef2Sdrh /*
8996c1f4ef2Sdrh ** We already know that pExpr is a binary operator where both operands are
9006c1f4ef2Sdrh ** column references. This routine checks to see if pExpr is an equivalence
9016c1f4ef2Sdrh ** relation:
9026c1f4ef2Sdrh ** 1. The SQLITE_Transitive optimization must be enabled
9036c1f4ef2Sdrh ** 2. Must be either an == or an IS operator
9046c1f4ef2Sdrh ** 3. Not originating in the ON clause of an OUTER JOIN
9056c1f4ef2Sdrh ** 4. The affinities of A and B must be compatible
9066c1f4ef2Sdrh ** 5a. Both operands use the same collating sequence OR
9076c1f4ef2Sdrh ** 5b. The overall collating sequence is BINARY
9086c1f4ef2Sdrh ** If this routine returns TRUE, that means that the RHS can be substituted
9096c1f4ef2Sdrh ** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
9106c1f4ef2Sdrh ** This is an optimization. No harm comes from returning 0. But if 1 is
9116c1f4ef2Sdrh ** returned when it should not be, then incorrect answers might result.
9126c1f4ef2Sdrh */
termIsEquivalence(Parse * pParse,Expr * pExpr)9136c1f4ef2Sdrh static int termIsEquivalence(Parse *pParse, Expr *pExpr){
9146c1f4ef2Sdrh char aff1, aff2;
9156c1f4ef2Sdrh CollSeq *pColl;
9166c1f4ef2Sdrh if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
9176c1f4ef2Sdrh if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
91867a99dbeSdrh if( ExprHasProperty(pExpr, EP_OuterON) ) return 0;
9196c1f4ef2Sdrh aff1 = sqlite3ExprAffinity(pExpr->pLeft);
9206c1f4ef2Sdrh aff2 = sqlite3ExprAffinity(pExpr->pRight);
9216c1f4ef2Sdrh if( aff1!=aff2
9226c1f4ef2Sdrh && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
9236c1f4ef2Sdrh ){
9246c1f4ef2Sdrh return 0;
9256c1f4ef2Sdrh }
926898c527eSdrh pColl = sqlite3ExprCompareCollSeq(pParse, pExpr);
927efad2e23Sdrh if( sqlite3IsBinary(pColl) ) return 1;
92870efa84dSdrh return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
9296c1f4ef2Sdrh }
9306c1f4ef2Sdrh
9316c1f4ef2Sdrh /*
9326c1f4ef2Sdrh ** Recursively walk the expressions of a SELECT statement and generate
9336c1f4ef2Sdrh ** a bitmask indicating which tables are used in that expression
9346c1f4ef2Sdrh ** tree.
9356c1f4ef2Sdrh */
exprSelectUsage(WhereMaskSet * pMaskSet,Select * pS)9366c1f4ef2Sdrh static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
9376c1f4ef2Sdrh Bitmask mask = 0;
9386c1f4ef2Sdrh while( pS ){
9396c1f4ef2Sdrh SrcList *pSrc = pS->pSrc;
9406c1f4ef2Sdrh mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
9416c1f4ef2Sdrh mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
9426c1f4ef2Sdrh mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
9436c1f4ef2Sdrh mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
9446c1f4ef2Sdrh mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
9456c1f4ef2Sdrh if( ALWAYS(pSrc!=0) ){
9466c1f4ef2Sdrh int i;
9476c1f4ef2Sdrh for(i=0; i<pSrc->nSrc; i++){
9486c1f4ef2Sdrh mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
949d44f8b23Sdrh if( pSrc->a[i].fg.isUsing==0 ){
950d44f8b23Sdrh mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
951d44f8b23Sdrh }
95233f763d1Sdrh if( pSrc->a[i].fg.isTabFunc ){
95333f763d1Sdrh mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
95433f763d1Sdrh }
9556c1f4ef2Sdrh }
9566c1f4ef2Sdrh }
9576c1f4ef2Sdrh pS = pS->pPrior;
9586c1f4ef2Sdrh }
9596c1f4ef2Sdrh return mask;
9606c1f4ef2Sdrh }
9616c1f4ef2Sdrh
9626c1f4ef2Sdrh /*
96347991425Sdrh ** Expression pExpr is one operand of a comparison operator that might
96447991425Sdrh ** be useful for indexing. This routine checks to see if pExpr appears
96547991425Sdrh ** in any index. Return TRUE (1) if pExpr is an indexed term and return
966e97c9ff4Sdrh ** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
967e97c9ff4Sdrh ** number of the table that is indexed and aiCurCol[1] to the column number
9688d25cb90Sdrh ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
9698d25cb90Sdrh ** indexed.
97047991425Sdrh **
97147991425Sdrh ** If pExpr is a TK_COLUMN column reference, then this routine always returns
97247991425Sdrh ** true even if that particular column is not indexed, because the column
97347991425Sdrh ** might be added to an automatic index later.
97447991425Sdrh */
exprMightBeIndexed2(SrcList * pFrom,Bitmask mPrereq,int * aiCurCol,Expr * pExpr)975e97c9ff4Sdrh static SQLITE_NOINLINE int exprMightBeIndexed2(
97647991425Sdrh SrcList *pFrom, /* The FROM clause */
97747991425Sdrh Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
978e97c9ff4Sdrh int *aiCurCol, /* Write the referenced table cursor and column here */
979e97c9ff4Sdrh Expr *pExpr /* An operand of a comparison operator */
98047991425Sdrh ){
98147991425Sdrh Index *pIdx;
98247991425Sdrh int i;
98347991425Sdrh int iCur;
984e97c9ff4Sdrh for(i=0; mPrereq>1; i++, mPrereq>>=1){}
985e97c9ff4Sdrh iCur = pFrom->a[i].iCursor;
986e97c9ff4Sdrh for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
987e97c9ff4Sdrh if( pIdx->aColExpr==0 ) continue;
988e97c9ff4Sdrh for(i=0; i<pIdx->nKeyCol; i++){
989e97c9ff4Sdrh if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
9904bc1cc18Sdrh assert( pIdx->bHasExpr );
991e97c9ff4Sdrh if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
992e97c9ff4Sdrh aiCurCol[0] = iCur;
993e97c9ff4Sdrh aiCurCol[1] = XN_EXPR;
994e97c9ff4Sdrh return 1;
995e97c9ff4Sdrh }
996e97c9ff4Sdrh }
997e97c9ff4Sdrh }
998e97c9ff4Sdrh return 0;
999e97c9ff4Sdrh }
exprMightBeIndexed(SrcList * pFrom,Bitmask mPrereq,int * aiCurCol,Expr * pExpr,int op)1000e97c9ff4Sdrh static int exprMightBeIndexed(
1001e97c9ff4Sdrh SrcList *pFrom, /* The FROM clause */
1002e97c9ff4Sdrh Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
1003e97c9ff4Sdrh int *aiCurCol, /* Write the referenced table cursor & column here */
1004e97c9ff4Sdrh Expr *pExpr, /* An operand of a comparison operator */
1005e97c9ff4Sdrh int op /* The specific comparison operator */
1006e97c9ff4Sdrh ){
100771c57db0Sdan /* If this expression is a vector to the left or right of a
100871c57db0Sdan ** inequality constraint (>, <, >= or <=), perform the processing
100971c57db0Sdan ** on the first element of the vector. */
101071c57db0Sdan assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
101164bcb8cfSdrh assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
101264bcb8cfSdrh assert( op<=TK_GE );
101364bcb8cfSdrh if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
1014a4eeccdfSdrh assert( ExprUseXList(pExpr) );
101571c57db0Sdan pExpr = pExpr->x.pList->a[0].pExpr;
10166a9595a7Sdrh
101771c57db0Sdan }
101871c57db0Sdan
101947991425Sdrh if( pExpr->op==TK_COLUMN ){
1020e97c9ff4Sdrh aiCurCol[0] = pExpr->iTable;
1021e97c9ff4Sdrh aiCurCol[1] = pExpr->iColumn;
102247991425Sdrh return 1;
102347991425Sdrh }
102447991425Sdrh if( mPrereq==0 ) return 0; /* No table references */
102547991425Sdrh if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
1026e97c9ff4Sdrh return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
102747991425Sdrh }
102847991425Sdrh
10296bfc167aSdan
10306bfc167aSdan /*
10316c1f4ef2Sdrh ** The input to this routine is an WhereTerm structure with only the
10326c1f4ef2Sdrh ** "pExpr" field filled in. The job of this routine is to analyze the
10336c1f4ef2Sdrh ** subexpression and populate all the other fields of the WhereTerm
10346c1f4ef2Sdrh ** structure.
10356c1f4ef2Sdrh **
10366c1f4ef2Sdrh ** If the expression is of the form "<expr> <op> X" it gets commuted
10376c1f4ef2Sdrh ** to the standard form of "X <op> <expr>".
10386c1f4ef2Sdrh **
10396c1f4ef2Sdrh ** If the expression is of the form "X <op> Y" where both X and Y are
10406c1f4ef2Sdrh ** columns, then the original expression is unchanged and a new virtual
10416c1f4ef2Sdrh ** term of the form "Y <op> X" is added to the WHERE clause and
10426c1f4ef2Sdrh ** analyzed separately. The original term is marked with TERM_COPIED
10436c1f4ef2Sdrh ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
10446c1f4ef2Sdrh ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
10456c1f4ef2Sdrh ** is a commuted copy of a prior term.) The original term has nChild=1
10466c1f4ef2Sdrh ** and the copy has idxParent set to the index of the original term.
10476c1f4ef2Sdrh */
exprAnalyze(SrcList * pSrc,WhereClause * pWC,int idxTerm)10486c1f4ef2Sdrh static void exprAnalyze(
10496c1f4ef2Sdrh SrcList *pSrc, /* the FROM clause */
10506c1f4ef2Sdrh WhereClause *pWC, /* the WHERE clause */
10516c1f4ef2Sdrh int idxTerm /* Index of the term to be analyzed */
10526c1f4ef2Sdrh ){
10536c1f4ef2Sdrh WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
10546c1f4ef2Sdrh WhereTerm *pTerm; /* The term to be analyzed */
10556c1f4ef2Sdrh WhereMaskSet *pMaskSet; /* Set of table index masks */
10566c1f4ef2Sdrh Expr *pExpr; /* The expression to be analyzed */
10576c1f4ef2Sdrh Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
10586c1f4ef2Sdrh Bitmask prereqAll; /* Prerequesites of pExpr */
10596c1f4ef2Sdrh Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
10606c1f4ef2Sdrh Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
10616c1f4ef2Sdrh int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
10626c1f4ef2Sdrh int noCase = 0; /* uppercase equivalent to lowercase */
10636c1f4ef2Sdrh int op; /* Top-level operator. pExpr->op */
10646c1f4ef2Sdrh Parse *pParse = pWInfo->pParse; /* Parsing context */
10656c1f4ef2Sdrh sqlite3 *db = pParse->db; /* Database connection */
106653be36b0Smistachkin unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
1067d9bcb32eSdrh int nLeft; /* Number of elements on left side vector */
10686c1f4ef2Sdrh
10696c1f4ef2Sdrh if( db->mallocFailed ){
10706c1f4ef2Sdrh return;
10716c1f4ef2Sdrh }
1072cbb7746cSdrh assert( pWC->nTerm > idxTerm );
10736c1f4ef2Sdrh pTerm = &pWC->a[idxTerm];
10746c1f4ef2Sdrh pMaskSet = &pWInfo->sMaskSet;
10756c1f4ef2Sdrh pExpr = pTerm->pExpr;
1076cbb7746cSdrh assert( pExpr!=0 ); /* Because malloc() has not failed */
10776c1f4ef2Sdrh assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
107865a3c850Sdrh pMaskSet->bVarSelect = 0;
10796c1f4ef2Sdrh prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
10806c1f4ef2Sdrh op = pExpr->op;
10816c1f4ef2Sdrh if( op==TK_IN ){
10826c1f4ef2Sdrh assert( pExpr->pRight==0 );
10837b35a77bSdan if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
1084a4eeccdfSdrh if( ExprUseXSelect(pExpr) ){
10856c1f4ef2Sdrh pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
10866c1f4ef2Sdrh }else{
10876c1f4ef2Sdrh pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
10886c1f4ef2Sdrh }
108965a3c850Sdrh prereqAll = prereqLeft | pTerm->prereqRight;
10906c1f4ef2Sdrh }else{
10916c1f4ef2Sdrh pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
109260a8e1b4Sdrh if( pExpr->pLeft==0
109360a8e1b4Sdrh || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow)
109460a8e1b4Sdrh || pExpr->x.pList!=0
109560a8e1b4Sdrh ){
1096ccf6db5eSdrh prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
109765a3c850Sdrh }else{
109865a3c850Sdrh prereqAll = prereqLeft | pTerm->prereqRight;
109965a3c850Sdrh }
110065a3c850Sdrh }
1101d3930b12Sdan if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
110265a3c850Sdrh
110365a3c850Sdrh #ifdef SQLITE_DEBUG
110465a3c850Sdrh if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){
110565a3c850Sdrh printf("\n*** Incorrect prereqAll computed for:\n");
110665a3c850Sdrh sqlite3TreeViewExpr(0,pExpr,0);
1107bf154369Smistachkin assert( 0 );
110865a3c850Sdrh }
110965a3c850Sdrh #endif
111065a3c850Sdrh
11116af03b46Sdrh if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) ){
1112d1985262Sdrh Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
11136af03b46Sdrh if( ExprHasProperty(pExpr, EP_OuterON) ){
11146c1f4ef2Sdrh prereqAll |= x;
11156c1f4ef2Sdrh extraRight = x-1; /* ON clause terms may not be used with an index
11166c1f4ef2Sdrh ** on left table of a LEFT JOIN. Ticket #3015 */
11178e36ddd3Sdrh if( (prereqAll>>1)>=x ){
11188e36ddd3Sdrh sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
11198e36ddd3Sdrh return;
11208e36ddd3Sdrh }
11216af03b46Sdrh }else if( (prereqAll>>1)>=x ){
11226af03b46Sdrh /* The ON clause of an INNER JOIN references a table to its right.
1123d7480403Sdrh ** Most other SQL database engines raise an error. But SQLite versions
1124d7480403Sdrh ** 3.0 through 3.38 just put the ON clause constraint into the WHERE
1125d7480403Sdrh ** clause and carried on. Beginning with 3.39, raise an error only
1126cf5cab01Sdrh ** if there is a RIGHT or FULL JOIN in the query. This makes SQLite
1127d7480403Sdrh ** more like other systems, and also preserves legacy. */
11284af6462fSdrh if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
1129d7480403Sdrh sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
1130d7480403Sdrh return;
1131d7480403Sdrh }
11326af03b46Sdrh ExprClearProperty(pExpr, EP_InnerON);
11336af03b46Sdrh }
11346c1f4ef2Sdrh }
11356c1f4ef2Sdrh pTerm->prereqAll = prereqAll;
11366c1f4ef2Sdrh pTerm->leftCursor = -1;
11376c1f4ef2Sdrh pTerm->iParent = -1;
11386c1f4ef2Sdrh pTerm->eOperator = 0;
11396c1f4ef2Sdrh if( allowedOp(op) ){
1140e97c9ff4Sdrh int aiCurCol[2];
11416c1f4ef2Sdrh Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
11426c1f4ef2Sdrh Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
11436c1f4ef2Sdrh u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
11448da209b1Sdan
114575fa2663Sdrh if( pTerm->u.x.iField>0 ){
1146145b4ea5Sdan assert( op==TK_IN );
11478da209b1Sdan assert( pLeft->op==TK_VECTOR );
1148a4eeccdfSdrh assert( ExprUseXList(pLeft) );
114975fa2663Sdrh pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
11508da209b1Sdan }
11518da209b1Sdan
1152e97c9ff4Sdrh if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
1153e97c9ff4Sdrh pTerm->leftCursor = aiCurCol[0];
1154220f0d6fSdrh assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
115575fa2663Sdrh pTerm->u.x.leftColumn = aiCurCol[1];
11566860e6faSdrh pTerm->eOperator = operatorMask(op) & opMask;
11576c1f4ef2Sdrh }
11586c1f4ef2Sdrh if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
115947991425Sdrh if( pRight
1160e97c9ff4Sdrh && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
11616a9595a7Sdrh && !ExprHasProperty(pRight, EP_FixedCol)
116247991425Sdrh ){
11636c1f4ef2Sdrh WhereTerm *pNew;
11646c1f4ef2Sdrh Expr *pDup;
11656c1f4ef2Sdrh u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
116675fa2663Sdrh assert( pTerm->u.x.iField==0 );
11676c1f4ef2Sdrh if( pTerm->leftCursor>=0 ){
11686c1f4ef2Sdrh int idxNew;
11696c1f4ef2Sdrh pDup = sqlite3ExprDup(db, pExpr, 0);
11706c1f4ef2Sdrh if( db->mallocFailed ){
11716c1f4ef2Sdrh sqlite3ExprDelete(db, pDup);
11726c1f4ef2Sdrh return;
11736c1f4ef2Sdrh }
11746c1f4ef2Sdrh idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
11756c1f4ef2Sdrh if( idxNew==0 ) return;
11766c1f4ef2Sdrh pNew = &pWC->a[idxNew];
11776c1f4ef2Sdrh markTermAsChild(pWC, idxNew, idxTerm);
11786c1f4ef2Sdrh if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
11796c1f4ef2Sdrh pTerm = &pWC->a[idxTerm];
11806c1f4ef2Sdrh pTerm->wtFlags |= TERM_COPIED;
11816c1f4ef2Sdrh
11826c1f4ef2Sdrh if( termIsEquivalence(pParse, pDup) ){
11836c1f4ef2Sdrh pTerm->eOperator |= WO_EQUIV;
11846c1f4ef2Sdrh eExtraOp = WO_EQUIV;
11856c1f4ef2Sdrh }
11866c1f4ef2Sdrh }else{
11876c1f4ef2Sdrh pDup = pExpr;
11886c1f4ef2Sdrh pNew = pTerm;
11896c1f4ef2Sdrh }
1190c7d12f4aSdrh pNew->wtFlags |= exprCommute(pParse, pDup);
1191e97c9ff4Sdrh pNew->leftCursor = aiCurCol[0];
1192220f0d6fSdrh assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
119375fa2663Sdrh pNew->u.x.leftColumn = aiCurCol[1];
11946c1f4ef2Sdrh testcase( (prereqLeft | extraRight) != prereqLeft );
11956c1f4ef2Sdrh pNew->prereqRight = prereqLeft | extraRight;
11966c1f4ef2Sdrh pNew->prereqAll = prereqAll;
11976c1f4ef2Sdrh pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
1198348e002eSdrh }else
1199348e002eSdrh if( op==TK_ISNULL
120067a99dbeSdrh && !ExprHasProperty(pExpr,EP_OuterON)
1201348e002eSdrh && 0==sqlite3ExprCanBeNull(pLeft)
1202348e002eSdrh ){
1203f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) );
12048ddf6862Sdan pExpr->op = TK_TRUEFALSE;
120515de3ce9Sdan pExpr->u.zToken = "false";
12068ddf6862Sdan ExprSetProperty(pExpr, EP_IsFalse);
12078ddf6862Sdan pTerm->prereqAll = 0;
12088ddf6862Sdan pTerm->eOperator = 0;
12096c1f4ef2Sdrh }
12106c1f4ef2Sdrh }
12116c1f4ef2Sdrh
12126c1f4ef2Sdrh #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
12136c1f4ef2Sdrh /* If a term is the BETWEEN operator, create two new virtual terms
12146c1f4ef2Sdrh ** that define the range that the BETWEEN implements. For example:
12156c1f4ef2Sdrh **
12166c1f4ef2Sdrh ** a BETWEEN b AND c
12176c1f4ef2Sdrh **
12186c1f4ef2Sdrh ** is converted into:
12196c1f4ef2Sdrh **
12206c1f4ef2Sdrh ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
12216c1f4ef2Sdrh **
12226c1f4ef2Sdrh ** The two new terms are added onto the end of the WhereClause object.
12236c1f4ef2Sdrh ** The new terms are "dynamic" and are children of the original BETWEEN
12246c1f4ef2Sdrh ** term. That means that if the BETWEEN term is coded, the children are
12256c1f4ef2Sdrh ** skipped. Or, if the children are satisfied by an index, the original
12266c1f4ef2Sdrh ** BETWEEN term is skipped.
12276c1f4ef2Sdrh */
12286c1f4ef2Sdrh else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
1229a4eeccdfSdrh ExprList *pList;
12306c1f4ef2Sdrh int i;
12316c1f4ef2Sdrh static const u8 ops[] = {TK_GE, TK_LE};
1232a4eeccdfSdrh assert( ExprUseXList(pExpr) );
1233a4eeccdfSdrh pList = pExpr->x.pList;
12346c1f4ef2Sdrh assert( pList!=0 );
12356c1f4ef2Sdrh assert( pList->nExpr==2 );
12366c1f4ef2Sdrh for(i=0; i<2; i++){
12376c1f4ef2Sdrh Expr *pNewExpr;
12386c1f4ef2Sdrh int idxNew;
12396c1f4ef2Sdrh pNewExpr = sqlite3PExpr(pParse, ops[i],
12406c1f4ef2Sdrh sqlite3ExprDup(db, pExpr->pLeft, 0),
1241abfd35eaSdrh sqlite3ExprDup(db, pList->a[i].pExpr, 0));
12426c1f4ef2Sdrh transferJoinMarkings(pNewExpr, pExpr);
12436c1f4ef2Sdrh idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
12446c1f4ef2Sdrh testcase( idxNew==0 );
12456c1f4ef2Sdrh exprAnalyze(pSrc, pWC, idxNew);
12466c1f4ef2Sdrh pTerm = &pWC->a[idxTerm];
12476c1f4ef2Sdrh markTermAsChild(pWC, idxNew, idxTerm);
12486c1f4ef2Sdrh }
12496c1f4ef2Sdrh }
12506c1f4ef2Sdrh #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
12516c1f4ef2Sdrh
12526c1f4ef2Sdrh #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
12536c1f4ef2Sdrh /* Analyze a term that is composed of two or more subterms connected by
12546c1f4ef2Sdrh ** an OR operator.
12556c1f4ef2Sdrh */
12566c1f4ef2Sdrh else if( pExpr->op==TK_OR ){
12576c1f4ef2Sdrh assert( pWC->op==TK_AND );
12586c1f4ef2Sdrh exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
12596c1f4ef2Sdrh pTerm = &pWC->a[idxTerm];
12606c1f4ef2Sdrh }
12616c1f4ef2Sdrh #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
12626cca0aa9Sdrh /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently
12636cca0aa9Sdrh ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
12646cca0aa9Sdrh ** virtual term of that form.
12656cca0aa9Sdrh **
12666cca0aa9Sdrh ** The virtual term must be tagged with TERM_VNULL.
12676cca0aa9Sdrh */
12686cca0aa9Sdrh else if( pExpr->op==TK_NOTNULL ){
12696cca0aa9Sdrh if( pExpr->pLeft->op==TK_COLUMN
12706cca0aa9Sdrh && pExpr->pLeft->iColumn>=0
127167a99dbeSdrh && !ExprHasProperty(pExpr, EP_OuterON)
12726cca0aa9Sdrh ){
12736cca0aa9Sdrh Expr *pNewExpr;
12746cca0aa9Sdrh Expr *pLeft = pExpr->pLeft;
12756cca0aa9Sdrh int idxNew;
12766cca0aa9Sdrh WhereTerm *pNewTerm;
12776cca0aa9Sdrh
12786cca0aa9Sdrh pNewExpr = sqlite3PExpr(pParse, TK_GT,
12796cca0aa9Sdrh sqlite3ExprDup(db, pLeft, 0),
12806cca0aa9Sdrh sqlite3ExprAlloc(db, TK_NULL, 0, 0));
12816cca0aa9Sdrh
12826cca0aa9Sdrh idxNew = whereClauseInsert(pWC, pNewExpr,
12836cca0aa9Sdrh TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
12846cca0aa9Sdrh if( idxNew ){
12856cca0aa9Sdrh pNewTerm = &pWC->a[idxNew];
12866cca0aa9Sdrh pNewTerm->prereqRight = 0;
12876cca0aa9Sdrh pNewTerm->leftCursor = pLeft->iTable;
12886cca0aa9Sdrh pNewTerm->u.x.leftColumn = pLeft->iColumn;
12896cca0aa9Sdrh pNewTerm->eOperator = WO_GT;
12906cca0aa9Sdrh markTermAsChild(pWC, idxNew, idxTerm);
12916cca0aa9Sdrh pTerm = &pWC->a[idxTerm];
12926cca0aa9Sdrh pTerm->wtFlags |= TERM_COPIED;
12936cca0aa9Sdrh pNewTerm->prereqAll = pTerm->prereqAll;
12946cca0aa9Sdrh }
12956cca0aa9Sdrh }
12966cca0aa9Sdrh }
129710c9ef65Sdrh
129871aff855Sdrh
12996c1f4ef2Sdrh #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
13006c1f4ef2Sdrh /* Add constraints to reduce the search space on a LIKE or GLOB
13016c1f4ef2Sdrh ** operator.
13026c1f4ef2Sdrh **
13036c1f4ef2Sdrh ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
13046c1f4ef2Sdrh **
13056c1f4ef2Sdrh ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
13066c1f4ef2Sdrh **
13076c1f4ef2Sdrh ** The last character of the prefix "abc" is incremented to form the
13086c1f4ef2Sdrh ** termination condition "abd". If case is not significant (the default
13096c1f4ef2Sdrh ** for LIKE) then the lower-bound is made all uppercase and the upper-
13106c1f4ef2Sdrh ** bound is made all lowercase so that the bounds also work when comparing
13116c1f4ef2Sdrh ** BLOBs.
13126c1f4ef2Sdrh */
131371aff855Sdrh else if( pExpr->op==TK_FUNCTION
131471aff855Sdrh && pWC->op==TK_AND
13156c1f4ef2Sdrh && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
13166c1f4ef2Sdrh ){
13176c1f4ef2Sdrh Expr *pLeft; /* LHS of LIKE/GLOB operator */
13186c1f4ef2Sdrh Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
13196c1f4ef2Sdrh Expr *pNewExpr1;
13206c1f4ef2Sdrh Expr *pNewExpr2;
13216c1f4ef2Sdrh int idxNew1;
13226c1f4ef2Sdrh int idxNew2;
13236c1f4ef2Sdrh const char *zCollSeqName; /* Name of collating sequence */
13246c1f4ef2Sdrh const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
13256c1f4ef2Sdrh
1326a4eeccdfSdrh assert( ExprUseXList(pExpr) );
13276c1f4ef2Sdrh pLeft = pExpr->x.pList->a[1].pExpr;
13286c1f4ef2Sdrh pStr2 = sqlite3ExprDup(db, pStr1, 0);
1329f9751074Sdrh assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) );
1330f9751074Sdrh assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) );
1331f9751074Sdrh
13326c1f4ef2Sdrh
13336c1f4ef2Sdrh /* Convert the lower bound to upper-case and the upper bound to
13346c1f4ef2Sdrh ** lower-case (upper-case is less than lower-case in ASCII) so that
13356c1f4ef2Sdrh ** the range constraints also work for BLOBs
13366c1f4ef2Sdrh */
13376c1f4ef2Sdrh if( noCase && !pParse->db->mallocFailed ){
13386c1f4ef2Sdrh int i;
13396c1f4ef2Sdrh char c;
13406c1f4ef2Sdrh pTerm->wtFlags |= TERM_LIKE;
13416c1f4ef2Sdrh for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
13426c1f4ef2Sdrh pStr1->u.zToken[i] = sqlite3Toupper(c);
13436c1f4ef2Sdrh pStr2->u.zToken[i] = sqlite3Tolower(c);
13446c1f4ef2Sdrh }
13456c1f4ef2Sdrh }
13466c1f4ef2Sdrh
13476c1f4ef2Sdrh if( !db->mallocFailed ){
13486c1f4ef2Sdrh u8 c, *pC; /* Last character before the first wildcard */
13496c1f4ef2Sdrh pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
13506c1f4ef2Sdrh c = *pC;
13516c1f4ef2Sdrh if( noCase ){
13526c1f4ef2Sdrh /* The point is to increment the last character before the first
13536c1f4ef2Sdrh ** wildcard. But if we increment '@', that will push it into the
13546c1f4ef2Sdrh ** alphabetic range where case conversions will mess up the
13556c1f4ef2Sdrh ** inequality. To avoid this, make sure to also run the full
13566c1f4ef2Sdrh ** LIKE on all candidate expressions by clearing the isComplete flag
13576c1f4ef2Sdrh */
13586c1f4ef2Sdrh if( c=='A'-1 ) isComplete = 0;
13596c1f4ef2Sdrh c = sqlite3UpperToLower[c];
13606c1f4ef2Sdrh }
13616c1f4ef2Sdrh *pC = c + 1;
13626c1f4ef2Sdrh }
13637810ab64Sdrh zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
13646c1f4ef2Sdrh pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
13656c1f4ef2Sdrh pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
13666c1f4ef2Sdrh sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
1367abfd35eaSdrh pStr1);
13686c1f4ef2Sdrh transferJoinMarkings(pNewExpr1, pExpr);
13696c1f4ef2Sdrh idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
13706c1f4ef2Sdrh testcase( idxNew1==0 );
13716c1f4ef2Sdrh exprAnalyze(pSrc, pWC, idxNew1);
13726c1f4ef2Sdrh pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
13736c1f4ef2Sdrh pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
13746c1f4ef2Sdrh sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
1375abfd35eaSdrh pStr2);
13766c1f4ef2Sdrh transferJoinMarkings(pNewExpr2, pExpr);
13776c1f4ef2Sdrh idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
13786c1f4ef2Sdrh testcase( idxNew2==0 );
13796c1f4ef2Sdrh exprAnalyze(pSrc, pWC, idxNew2);
13806c1f4ef2Sdrh pTerm = &pWC->a[idxTerm];
13816c1f4ef2Sdrh if( isComplete ){
13826c1f4ef2Sdrh markTermAsChild(pWC, idxNew1, idxTerm);
13836c1f4ef2Sdrh markTermAsChild(pWC, idxNew2, idxTerm);
13846c1f4ef2Sdrh }
13856c1f4ef2Sdrh }
13866c1f4ef2Sdrh #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
13876c1f4ef2Sdrh
138871aff855Sdrh /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
138971aff855Sdrh ** new terms for each component comparison - "a = ?" and "b = ?". The
139071aff855Sdrh ** new terms completely replace the original vector comparison, which is
139171aff855Sdrh ** no longer used.
139271aff855Sdrh **
139371aff855Sdrh ** This is only required if at least one side of the comparison operation
13940ae56fbdSdrh ** is not a sub-select.
13950ae56fbdSdrh **
13960ae56fbdSdrh ** tag-20220128a
13970ae56fbdSdrh */
139871aff855Sdrh if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
139971aff855Sdrh && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
140071aff855Sdrh && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
140171aff855Sdrh && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
140271aff855Sdrh || (pExpr->pRight->flags & EP_xIsSelect)==0)
140371aff855Sdrh && pWC->op==TK_AND
140471aff855Sdrh ){
140571aff855Sdrh int i;
140671aff855Sdrh for(i=0; i<nLeft; i++){
140771aff855Sdrh int idxNew;
140871aff855Sdrh Expr *pNew;
140910f08270Sdrh Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
141010f08270Sdrh Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
141171aff855Sdrh
141271aff855Sdrh pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
141371aff855Sdrh transferJoinMarkings(pNew, pExpr);
141402e3e041Sdrh idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_SLICE);
141571aff855Sdrh exprAnalyze(pSrc, pWC, idxNew);
141671aff855Sdrh }
141771aff855Sdrh pTerm = &pWC->a[idxTerm];
141871aff855Sdrh pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
14190f4b534bSdrh pTerm->eOperator = WO_ROWVAL;
142071aff855Sdrh }
142171aff855Sdrh
142271aff855Sdrh /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
142371aff855Sdrh ** a virtual term for each vector component. The expression object
142471aff855Sdrh ** used by each such virtual term is pExpr (the full vector IN(...)
142571aff855Sdrh ** expression). The WhereTerm.u.x.iField variable identifies the index within
142671aff855Sdrh ** the vector on the LHS that the virtual term represents.
142771aff855Sdrh **
142871aff855Sdrh ** This only works if the RHS is a simple SELECT (not a compound) that does
142971aff855Sdrh ** not use window functions.
143071aff855Sdrh */
143171aff855Sdrh else if( pExpr->op==TK_IN
143271aff855Sdrh && pTerm->u.x.iField==0
143371aff855Sdrh && pExpr->pLeft->op==TK_VECTOR
1434a4eeccdfSdrh && ALWAYS( ExprUseXSelect(pExpr) )
143571aff855Sdrh && pExpr->x.pSelect->pPrior==0
143671aff855Sdrh #ifndef SQLITE_OMIT_WINDOWFUNC
143771aff855Sdrh && pExpr->x.pSelect->pWin==0
143871aff855Sdrh #endif
143971aff855Sdrh && pWC->op==TK_AND
144071aff855Sdrh ){
144171aff855Sdrh int i;
144271aff855Sdrh for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
144371aff855Sdrh int idxNew;
144451896e6fSdrh idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL|TERM_SLICE);
144571aff855Sdrh pWC->a[idxNew].u.x.iField = i+1;
144671aff855Sdrh exprAnalyze(pSrc, pWC, idxNew);
144771aff855Sdrh markTermAsChild(pWC, idxNew, idxTerm);
144871aff855Sdrh }
144971aff855Sdrh }
145071aff855Sdrh
14516c1f4ef2Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
1452303a69b5Sdrh /* Add a WO_AUX auxiliary term to the constraint set if the
1453303a69b5Sdrh ** current expression is of the form "column OP expr" where OP
1454303a69b5Sdrh ** is an operator that gets passed into virtual tables but which is
1455303a69b5Sdrh ** not normally optimized for ordinary tables. In other words, OP
1456303a69b5Sdrh ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
14576c1f4ef2Sdrh ** This information is used by the xBestIndex methods of
14586c1f4ef2Sdrh ** virtual tables. The native query optimizer does not attempt
14596c1f4ef2Sdrh ** to do anything with MATCH functions.
14606c1f4ef2Sdrh */
146171aff855Sdrh else if( pWC->op==TK_AND ){
146253be36b0Smistachkin Expr *pRight = 0, *pLeft = 0;
146359155065Sdrh int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
1464303a69b5Sdrh while( res-- > 0 ){
1465d03024d8Sdan int idxNew;
14666c1f4ef2Sdrh WhereTerm *pNewTerm;
14676c1f4ef2Sdrh Bitmask prereqColumn, prereqExpr;
14686c1f4ef2Sdrh
14696c1f4ef2Sdrh prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
14706c1f4ef2Sdrh prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
14716c1f4ef2Sdrh if( (prereqExpr & prereqColumn)==0 ){
14726c1f4ef2Sdrh Expr *pNewExpr;
14736c1f4ef2Sdrh pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
1474abfd35eaSdrh 0, sqlite3ExprDup(db, pRight, 0));
147567a99dbeSdrh if( ExprHasProperty(pExpr, EP_OuterON) && pNewExpr ){
147667a99dbeSdrh ExprSetProperty(pNewExpr, EP_OuterON);
1477d1985262Sdrh pNewExpr->w.iJoin = pExpr->w.iJoin;
1478210ec4c8Sdan }
14796c1f4ef2Sdrh idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
14806c1f4ef2Sdrh testcase( idxNew==0 );
14816c1f4ef2Sdrh pNewTerm = &pWC->a[idxNew];
14826c1f4ef2Sdrh pNewTerm->prereqRight = prereqExpr;
14836c1f4ef2Sdrh pNewTerm->leftCursor = pLeft->iTable;
148475fa2663Sdrh pNewTerm->u.x.leftColumn = pLeft->iColumn;
1485303a69b5Sdrh pNewTerm->eOperator = WO_AUX;
148607bdba86Sdan pNewTerm->eMatchOp = eOp2;
14876c1f4ef2Sdrh markTermAsChild(pWC, idxNew, idxTerm);
14886c1f4ef2Sdrh pTerm = &pWC->a[idxTerm];
14896c1f4ef2Sdrh pTerm->wtFlags |= TERM_COPIED;
14906c1f4ef2Sdrh pNewTerm->prereqAll = pTerm->prereqAll;
14916c1f4ef2Sdrh }
1492d03024d8Sdan SWAP(Expr*, pLeft, pRight);
1493d03024d8Sdan }
14946c1f4ef2Sdrh }
14956c1f4ef2Sdrh #endif /* SQLITE_OMIT_VIRTUALTABLE */
14966c1f4ef2Sdrh
14976c1f4ef2Sdrh /* Prevent ON clause terms of a LEFT JOIN from being used to drive
14986c1f4ef2Sdrh ** an index for tables to the left of the join.
14996c1f4ef2Sdrh */
15000f85b2ffSdrh testcase( pTerm!=&pWC->a[idxTerm] );
15010f85b2ffSdrh pTerm = &pWC->a[idxTerm];
15026c1f4ef2Sdrh pTerm->prereqRight |= extraRight;
15036c1f4ef2Sdrh }
15046c1f4ef2Sdrh
15056c1f4ef2Sdrh /***************************************************************************
15066c1f4ef2Sdrh ** Routines with file scope above. Interface to the rest of the where.c
15076c1f4ef2Sdrh ** subsystem follows.
15086c1f4ef2Sdrh ***************************************************************************/
15096c1f4ef2Sdrh
15106c1f4ef2Sdrh /*
15116c1f4ef2Sdrh ** This routine identifies subexpressions in the WHERE clause where
15126c1f4ef2Sdrh ** each subexpression is separated by the AND operator or some other
15136c1f4ef2Sdrh ** operator specified in the op parameter. The WhereClause structure
15146c1f4ef2Sdrh ** is filled with pointers to subexpressions. For example:
15156c1f4ef2Sdrh **
15166c1f4ef2Sdrh ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
15176c1f4ef2Sdrh ** \________/ \_______________/ \________________/
15186c1f4ef2Sdrh ** slot[0] slot[1] slot[2]
15196c1f4ef2Sdrh **
15206c1f4ef2Sdrh ** The original WHERE clause in pExpr is unaltered. All this routine
15216c1f4ef2Sdrh ** does is make slot[] entries point to substructure within pExpr.
15226c1f4ef2Sdrh **
15236c1f4ef2Sdrh ** In the previous sentence and in the diagram, "slot[]" refers to
15246c1f4ef2Sdrh ** the WhereClause.a[] array. The slot[] array grows as needed to contain
15256c1f4ef2Sdrh ** all terms of the WHERE clause.
15266c1f4ef2Sdrh */
sqlite3WhereSplit(WhereClause * pWC,Expr * pExpr,u8 op)15276c1f4ef2Sdrh void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
15280d950af3Sdrh Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
15296c1f4ef2Sdrh pWC->op = op;
1530235667a8Sdrh assert( pE2!=0 || pExpr==0 );
15316c1f4ef2Sdrh if( pE2==0 ) return;
15326c1f4ef2Sdrh if( pE2->op!=op ){
15336c1f4ef2Sdrh whereClauseInsert(pWC, pExpr, 0);
15346c1f4ef2Sdrh }else{
15356c1f4ef2Sdrh sqlite3WhereSplit(pWC, pE2->pLeft, op);
15366c1f4ef2Sdrh sqlite3WhereSplit(pWC, pE2->pRight, op);
15376c1f4ef2Sdrh }
15386c1f4ef2Sdrh }
15396c1f4ef2Sdrh
15406c1f4ef2Sdrh /*
1541895bab33Sdrh ** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
1542895bab33Sdrh ** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
1543895bab33Sdrh ** where-clause passed as the first argument. The value for the term
1544895bab33Sdrh ** is found in register iReg.
1545233ba1b8Sdrh **
1546233ba1b8Sdrh ** In the common case where the value is a simple integer
1547233ba1b8Sdrh ** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
1548233ba1b8Sdrh ** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
1549233ba1b8Sdrh ** If not, then it codes as a TK_REGISTER expression.
1550895bab33Sdrh */
whereAddLimitExpr(WhereClause * pWC,int iReg,Expr * pExpr,int iCsr,int eMatchOp)15516e85b27cSlarrybr static void whereAddLimitExpr(
1552233ba1b8Sdrh WhereClause *pWC, /* Add the constraint to this WHERE clause */
1553233ba1b8Sdrh int iReg, /* Register that will hold value of the limit/offset */
1554233ba1b8Sdrh Expr *pExpr, /* Expression that defines the limit/offset */
1555233ba1b8Sdrh int iCsr, /* Cursor to which the constraint applies */
1556233ba1b8Sdrh int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
1557233ba1b8Sdrh ){
1558895bab33Sdrh Parse *pParse = pWC->pWInfo->pParse;
1559895bab33Sdrh sqlite3 *db = pParse->db;
1560233ba1b8Sdrh Expr *pNew;
1561233ba1b8Sdrh int iVal = 0;
1562895bab33Sdrh
15630ae56fbdSdrh if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){
1564233ba1b8Sdrh Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0);
1565233ba1b8Sdrh if( pVal==0 ) return;
1566233ba1b8Sdrh ExprSetProperty(pVal, EP_IntValue);
1567233ba1b8Sdrh pVal->u.iValue = iVal;
1568233ba1b8Sdrh pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal);
1569233ba1b8Sdrh }else{
1570233ba1b8Sdrh Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0);
1571233ba1b8Sdrh if( pVal==0 ) return;
1572233ba1b8Sdrh pVal->iTable = iReg;
1573233ba1b8Sdrh pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal);
1574233ba1b8Sdrh }
1575233ba1b8Sdrh if( pNew ){
1576895bab33Sdrh WhereTerm *pTerm;
1577895bab33Sdrh int idx;
1578233ba1b8Sdrh idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL);
1579895bab33Sdrh pTerm = &pWC->a[idx];
1580895bab33Sdrh pTerm->leftCursor = iCsr;
1581895bab33Sdrh pTerm->eOperator = WO_AUX;
1582895bab33Sdrh pTerm->eMatchOp = eMatchOp;
1583895bab33Sdrh }
1584895bab33Sdrh }
1585895bab33Sdrh
1586895bab33Sdrh /*
1587895bab33Sdrh ** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
1588895bab33Sdrh ** SELECT statement passed as the second argument. These terms are only
1589895bab33Sdrh ** added if:
1590895bab33Sdrh **
1591895bab33Sdrh ** 1. The SELECT statement has a LIMIT clause, and
1592895bab33Sdrh ** 2. The SELECT statement is not an aggregate or DISTINCT query, and
1593895bab33Sdrh ** 3. The SELECT statement has exactly one object in its from clause, and
1594895bab33Sdrh ** that object is a virtual table, and
1595895bab33Sdrh ** 4. There are no terms in the WHERE clause that will not be passed
1596895bab33Sdrh ** to the virtual table xBestIndex method.
1597895bab33Sdrh ** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
1598895bab33Sdrh ** method.
1599895bab33Sdrh **
1600895bab33Sdrh ** LIMIT and OFFSET terms are ignored by most of the planner code. They
1601895bab33Sdrh ** exist only so that they may be passed to the xBestIndex method of the
1602895bab33Sdrh ** single virtual table in the FROM clause of the SELECT.
1603895bab33Sdrh */
sqlite3WhereAddLimit(WhereClause * pWC,Select * p)1604f55a7dadSdrh void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
1605f55a7dadSdrh assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
1606*fb643592Sdrh if( p->pGroupBy==0
1607*fb643592Sdrh && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
1608895bab33Sdrh && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
1609895bab33Sdrh ){
1610895bab33Sdrh ExprList *pOrderBy = p->pOrderBy;
1611895bab33Sdrh int iCsr = p->pSrc->a[0].iCursor;
1612895bab33Sdrh int ii;
1613895bab33Sdrh
1614895bab33Sdrh /* Check condition (4). Return early if it is not met. */
1615895bab33Sdrh for(ii=0; ii<pWC->nTerm; ii++){
16160ae56fbdSdrh if( pWC->a[ii].wtFlags & TERM_CODED ){
16170ae56fbdSdrh /* This term is a vector operation that has been decomposed into
16180ae56fbdSdrh ** other, subsequent terms. It can be ignored. See tag-20220128a */
16190ae56fbdSdrh assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
16200f4b534bSdrh assert( pWC->a[ii].eOperator==WO_ROWVAL );
16210ae56fbdSdrh continue;
16220ae56fbdSdrh }
1623895bab33Sdrh if( pWC->a[ii].leftCursor!=iCsr ) return;
1624895bab33Sdrh }
1625895bab33Sdrh
1626895bab33Sdrh /* Check condition (5). Return early if it is not met. */
1627895bab33Sdrh if( pOrderBy ){
1628895bab33Sdrh for(ii=0; ii<pOrderBy->nExpr; ii++){
1629895bab33Sdrh Expr *pExpr = pOrderBy->a[ii].pExpr;
1630f6f78886Sdrh if( pExpr->op!=TK_COLUMN ) return;
1631ddd166a3Sdrh if( pExpr->iTable!=iCsr ) return;
1632d88fd539Sdrh if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
1633895bab33Sdrh }
1634895bab33Sdrh }
1635895bab33Sdrh
1636895bab33Sdrh /* All conditions are met. Add the terms to the where-clause object. */
1637233ba1b8Sdrh assert( p->pLimit->op==TK_LIMIT );
1638233ba1b8Sdrh whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
1639233ba1b8Sdrh iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
1640895bab33Sdrh if( p->iOffset>0 ){
1641233ba1b8Sdrh whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
1642233ba1b8Sdrh iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
1643895bab33Sdrh }
1644895bab33Sdrh }
1645895bab33Sdrh }
1646895bab33Sdrh
1647895bab33Sdrh /*
16486c1f4ef2Sdrh ** Initialize a preallocated WhereClause structure.
16496c1f4ef2Sdrh */
sqlite3WhereClauseInit(WhereClause * pWC,WhereInfo * pWInfo)16506c1f4ef2Sdrh void sqlite3WhereClauseInit(
16516c1f4ef2Sdrh WhereClause *pWC, /* The WhereClause to be initialized */
16526c1f4ef2Sdrh WhereInfo *pWInfo /* The WHERE processing context */
16536c1f4ef2Sdrh ){
16546c1f4ef2Sdrh pWC->pWInfo = pWInfo;
16559c3549aaSdrh pWC->hasOr = 0;
16566c1f4ef2Sdrh pWC->pOuter = 0;
16576c1f4ef2Sdrh pWC->nTerm = 0;
1658132f96fcSdrh pWC->nBase = 0;
16596c1f4ef2Sdrh pWC->nSlot = ArraySize(pWC->aStatic);
16606c1f4ef2Sdrh pWC->a = pWC->aStatic;
16616c1f4ef2Sdrh }
16626c1f4ef2Sdrh
16636c1f4ef2Sdrh /*
16646c1f4ef2Sdrh ** Deallocate a WhereClause structure. The WhereClause structure
166562aaa6caSdrh ** itself is not freed. This routine is the inverse of
166662aaa6caSdrh ** sqlite3WhereClauseInit().
16676c1f4ef2Sdrh */
sqlite3WhereClauseClear(WhereClause * pWC)16686c1f4ef2Sdrh void sqlite3WhereClauseClear(WhereClause *pWC){
16696c1f4ef2Sdrh sqlite3 *db = pWC->pWInfo->pParse->db;
1670132f96fcSdrh assert( pWC->nTerm>=pWC->nBase );
1671cdd90503Sdrh if( pWC->nTerm>0 ){
1672cdd90503Sdrh WhereTerm *a = pWC->a;
1673cdd90503Sdrh WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
1674132f96fcSdrh #ifdef SQLITE_DEBUG
1675132f96fcSdrh int i;
1676132f96fcSdrh /* Verify that every term past pWC->nBase is virtual */
1677132f96fcSdrh for(i=pWC->nBase; i<pWC->nTerm; i++){
1678132f96fcSdrh assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
1679132f96fcSdrh }
1680132f96fcSdrh #endif
1681cdd90503Sdrh while(1){
16828f2c0b59Sdrh assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
16836c1f4ef2Sdrh if( a->wtFlags & TERM_DYNAMIC ){
16846c1f4ef2Sdrh sqlite3ExprDelete(db, a->pExpr);
16856c1f4ef2Sdrh }
1686cdd90503Sdrh if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
16876c1f4ef2Sdrh if( a->wtFlags & TERM_ORINFO ){
1688cdd90503Sdrh assert( (a->wtFlags & TERM_ANDINFO)==0 );
16896c1f4ef2Sdrh whereOrInfoDelete(db, a->u.pOrInfo);
1690cdd90503Sdrh }else{
1691cdd90503Sdrh assert( (a->wtFlags & TERM_ANDINFO)!=0 );
16926c1f4ef2Sdrh whereAndInfoDelete(db, a->u.pAndInfo);
16936c1f4ef2Sdrh }
16946c1f4ef2Sdrh }
1695cdd90503Sdrh if( a==aLast ) break;
1696cdd90503Sdrh a++;
1697cdd90503Sdrh }
1698cdd90503Sdrh }
16996c1f4ef2Sdrh }
17006c1f4ef2Sdrh
17016c1f4ef2Sdrh
17026c1f4ef2Sdrh /*
17036c1f4ef2Sdrh ** These routines walk (recursively) an expression tree and generate
17046c1f4ef2Sdrh ** a bitmask indicating which tables are used in that expression
17056c1f4ef2Sdrh ** tree.
170665a3c850Sdrh **
170765a3c850Sdrh ** sqlite3WhereExprUsage(MaskSet, Expr) ->
170865a3c850Sdrh **
170965a3c850Sdrh ** Return a Bitmask of all tables referenced by Expr. Expr can be
171065a3c850Sdrh ** be NULL, in which case 0 is returned.
171165a3c850Sdrh **
171265a3c850Sdrh ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
171365a3c850Sdrh **
171465a3c850Sdrh ** Same as sqlite3WhereExprUsage() except that Expr must not be
171565a3c850Sdrh ** NULL. The "NN" suffix on the name stands for "Not Null".
171665a3c850Sdrh **
171765a3c850Sdrh ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
171865a3c850Sdrh **
171965a3c850Sdrh ** Return a Bitmask of all tables referenced by every expression
172065a3c850Sdrh ** in the expression list ExprList. ExprList can be NULL, in which
172165a3c850Sdrh ** case 0 is returned.
172265a3c850Sdrh **
172365a3c850Sdrh ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
172465a3c850Sdrh **
172565a3c850Sdrh ** Internal use only. Called only by sqlite3WhereExprUsageNN() for
172665a3c850Sdrh ** complex expressions that require pushing register values onto
172765a3c850Sdrh ** the stack. Many calls to sqlite3WhereExprUsageNN() do not need
172865a3c850Sdrh ** the more complex analysis done by this routine. Hence, the
172965a3c850Sdrh ** computations done by this routine are broken out into a separate
173065a3c850Sdrh ** "no-inline" function to avoid the stack push overhead in the
173165a3c850Sdrh ** common case where it is not needed.
17326c1f4ef2Sdrh */
sqlite3WhereExprUsageFull(WhereMaskSet * pMaskSet,Expr * p)173365a3c850Sdrh static SQLITE_NOINLINE Bitmask sqlite3WhereExprUsageFull(
173465a3c850Sdrh WhereMaskSet *pMaskSet,
173565a3c850Sdrh Expr *p
173665a3c850Sdrh ){
173793ca3933Sdrh Bitmask mask;
1738f43ce0b4Sdrh mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
1739ccf6db5eSdrh if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
1740e24b92bcSdrh if( p->pRight ){
1741ccf6db5eSdrh mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
1742e24b92bcSdrh assert( p->x.pList==0 );
1743a4eeccdfSdrh }else if( ExprUseXSelect(p) ){
1744d3930b12Sdan if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
17456c1f4ef2Sdrh mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
1746926957f0Sdrh }else if( p->x.pList ){
17476c1f4ef2Sdrh mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
17486c1f4ef2Sdrh }
17499e24439cSdan #ifndef SQLITE_OMIT_WINDOWFUNC
1750477572b9Sdrh if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
1751477572b9Sdrh assert( p->y.pWin!=0 );
17529e24439cSdan mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
17539e24439cSdan mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
17548cc8feaeSdrh mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
17559e24439cSdan }
17569e24439cSdan #endif
17576c1f4ef2Sdrh return mask;
17586c1f4ef2Sdrh }
sqlite3WhereExprUsageNN(WhereMaskSet * pMaskSet,Expr * p)175965a3c850Sdrh Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
176065a3c850Sdrh if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
176165a3c850Sdrh return sqlite3WhereGetMask(pMaskSet, p->iTable);
176265a3c850Sdrh }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
176365a3c850Sdrh assert( p->op!=TK_IF_NULL_ROW );
176465a3c850Sdrh return 0;
176565a3c850Sdrh }
176665a3c850Sdrh return sqlite3WhereExprUsageFull(pMaskSet, p);
176765a3c850Sdrh }
sqlite3WhereExprUsage(WhereMaskSet * pMaskSet,Expr * p)1768ccf6db5eSdrh Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
1769ccf6db5eSdrh return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
1770ccf6db5eSdrh }
sqlite3WhereExprListUsage(WhereMaskSet * pMaskSet,ExprList * pList)17716c1f4ef2Sdrh Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
17726c1f4ef2Sdrh int i;
17736c1f4ef2Sdrh Bitmask mask = 0;
17746c1f4ef2Sdrh if( pList ){
17756c1f4ef2Sdrh for(i=0; i<pList->nExpr; i++){
17766c1f4ef2Sdrh mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
17776c1f4ef2Sdrh }
17786c1f4ef2Sdrh }
17796c1f4ef2Sdrh return mask;
17806c1f4ef2Sdrh }
17816c1f4ef2Sdrh
17826c1f4ef2Sdrh
17836c1f4ef2Sdrh /*
17846c1f4ef2Sdrh ** Call exprAnalyze on all terms in a WHERE clause.
17856c1f4ef2Sdrh **
17866c1f4ef2Sdrh ** Note that exprAnalyze() might add new virtual terms onto the
17876c1f4ef2Sdrh ** end of the WHERE clause. We do not want to analyze these new
17886c1f4ef2Sdrh ** virtual terms, so start analyzing at the end and work forward
17896c1f4ef2Sdrh ** so that the added virtual terms are never processed.
17906c1f4ef2Sdrh */
sqlite3WhereExprAnalyze(SrcList * pTabList,WhereClause * pWC)17916c1f4ef2Sdrh void sqlite3WhereExprAnalyze(
17926c1f4ef2Sdrh SrcList *pTabList, /* the FROM clause */
17936c1f4ef2Sdrh WhereClause *pWC /* the WHERE clause to be analyzed */
17946c1f4ef2Sdrh ){
17956c1f4ef2Sdrh int i;
17966c1f4ef2Sdrh for(i=pWC->nTerm-1; i>=0; i--){
17976c1f4ef2Sdrh exprAnalyze(pTabList, pWC, i);
17986c1f4ef2Sdrh }
17996c1f4ef2Sdrh }
180001d230ceSdrh
180101d230ceSdrh /*
180201d230ceSdrh ** For table-valued-functions, transform the function arguments into
180301d230ceSdrh ** new WHERE clause terms.
180401d230ceSdrh **
180501d230ceSdrh ** Each function argument translates into an equality constraint against
180601d230ceSdrh ** a HIDDEN column in the table.
180701d230ceSdrh */
sqlite3WhereTabFuncArgs(Parse * pParse,SrcItem * pItem,WhereClause * pWC)180801d230ceSdrh void sqlite3WhereTabFuncArgs(
180901d230ceSdrh Parse *pParse, /* Parsing context */
18107601294aSdrh SrcItem *pItem, /* The FROM clause term to process */
181101d230ceSdrh WhereClause *pWC /* Xfer function arguments to here */
181201d230ceSdrh ){
181301d230ceSdrh Table *pTab;
181401d230ceSdrh int j, k;
181501d230ceSdrh ExprList *pArgs;
181601d230ceSdrh Expr *pColRef;
181701d230ceSdrh Expr *pTerm;
181801d230ceSdrh if( pItem->fg.isTabFunc==0 ) return;
181901d230ceSdrh pTab = pItem->pTab;
182001d230ceSdrh assert( pTab!=0 );
182101d230ceSdrh pArgs = pItem->u1.pFuncArg;
182220292310Sdrh if( pArgs==0 ) return;
182301d230ceSdrh for(j=k=0; j<pArgs->nExpr; j++){
1824f689400dSdan Expr *pRhs;
18253a6e4c59Sdrh u32 joinType;
182601d230ceSdrh while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
182701d230ceSdrh if( k>=pTab->nCol ){
1828d8b1bfc6Sdrh sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
182901d230ceSdrh pTab->zName, j);
183001d230ceSdrh return;
183101d230ceSdrh }
1832e1c03b62Sdrh pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
183301d230ceSdrh if( pColRef==0 ) return;
183401d230ceSdrh pColRef->iTable = pItem->iCursor;
183501d230ceSdrh pColRef->iColumn = k++;
1836477572b9Sdrh assert( ExprUseYTab(pColRef) );
1837eda079cdSdrh pColRef->y.pTab = pTab;
1838cee5cb48Sdrh pItem->colUsed |= sqlite3ExprColUsed(pColRef);
1839f689400dSdan pRhs = sqlite3PExpr(pParse, TK_UPLUS,
1840f689400dSdan sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
1841f689400dSdan pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
1842a76ac88aSdrh if( pItem->fg.jointype & (JT_LEFT|JT_LTORJ) ){
184367a99dbeSdrh joinType = EP_OuterON;
18443a6e4c59Sdrh }else{
184567a99dbeSdrh joinType = EP_InnerON;
18468103a036Sdrh }
18473a6e4c59Sdrh sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
184801d230ceSdrh whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
184901d230ceSdrh }
185001d230ceSdrh }
1851