drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This module contains C code that generates VDBE code used to process |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 13 | ** the WHERE clause of SQL statements. This module is responsible for |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 14 | ** generating the code that loops through a table looking for applicable |
| 15 | ** rows. Indices are selected and used to speed the search when doing |
| 16 | ** so is applicable. Because this module is responsible for selecting |
| 17 | ** indices, you might also think of this module as the "query optimizer". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 20 | #include "whereInt.h" |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 23 | ** Return the estimated number of output rows from a WHERE clause |
| 24 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 25 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 26 | return sqlite3LogEstToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* |
| 30 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 31 | ** WHERE clause returns outputs for DISTINCT processing. |
| 32 | */ |
| 33 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 34 | return pWInfo->eDistinct; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 39 | ** Return FALSE if the output needs to be sorted. |
| 40 | */ |
| 41 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 42 | return pWInfo->nOBSat; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | ** Return the VDBE address or label to jump to in order to continue |
| 47 | ** immediately with the next row of a WHERE clause. |
| 48 | */ |
| 49 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 50 | assert( pWInfo->iContinue!=0 ); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 51 | return pWInfo->iContinue; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | ** Return the VDBE address or label to jump to in order to break |
| 56 | ** out of a WHERE loop. |
| 57 | */ |
| 58 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 59 | return pWInfo->iBreak; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 64 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 65 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 66 | ** |
| 67 | ** If the ONEPASS optimization is used (if this routine returns true) |
| 68 | ** then also write the indices of open cursors used by ONEPASS |
| 69 | ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 70 | ** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 71 | ** Either value may be -1, indicating that cursor is not used. |
| 72 | ** Any cursors returned will have been opened for writing. |
| 73 | ** |
| 74 | ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 75 | ** unable to use the ONEPASS optimization. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 76 | */ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 77 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 78 | memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 79 | return pWInfo->okOnePass; |
| 80 | } |
| 81 | |
| 82 | /* |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 83 | ** Move the content of pSrc into pDest |
| 84 | */ |
| 85 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 86 | pDest->n = pSrc->n; |
| 87 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 92 | ** |
| 93 | ** The new entry might overwrite an existing entry, or it might be |
| 94 | ** appended, or it might be discarded. Do whatever is the right thing |
| 95 | ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 96 | */ |
| 97 | static int whereOrInsert( |
| 98 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 99 | Bitmask prereq, /* Prerequisites of the new entry */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 100 | LogEst rRun, /* Run-cost of the new entry */ |
| 101 | LogEst nOut /* Number of outputs for the new entry */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 102 | ){ |
| 103 | u16 i; |
| 104 | WhereOrCost *p; |
| 105 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 106 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 107 | goto whereOrInsert_done; |
| 108 | } |
| 109 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | if( pSet->n<N_OR_COST ){ |
| 114 | p = &pSet->a[pSet->n++]; |
| 115 | p->nOut = nOut; |
| 116 | }else{ |
| 117 | p = pSet->a; |
| 118 | for(i=1; i<pSet->n; i++){ |
| 119 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 120 | } |
| 121 | if( p->rRun<=rRun ) return 0; |
| 122 | } |
| 123 | whereOrInsert_done: |
| 124 | p->prereq = prereq; |
| 125 | p->rRun = rRun; |
| 126 | if( p->nOut>nOut ) p->nOut = nOut; |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 131 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 132 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 133 | static void whereClauseInit( |
| 134 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 135 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 136 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 137 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 138 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 139 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 140 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 141 | pWC->a = pWC->aStatic; |
| 142 | } |
| 143 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 144 | /* Forward reference */ |
| 145 | static void whereClauseClear(WhereClause*); |
| 146 | |
| 147 | /* |
| 148 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 149 | */ |
| 150 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 151 | whereClauseClear(&p->wc); |
| 152 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 157 | */ |
| 158 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 159 | whereClauseClear(&p->wc); |
| 160 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 163 | /* |
| 164 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 165 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 166 | */ |
| 167 | static void whereClauseClear(WhereClause *pWC){ |
| 168 | int i; |
| 169 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 170 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 171 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 172 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 173 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 174 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 175 | if( a->wtFlags & TERM_ORINFO ){ |
| 176 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 177 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 178 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 179 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 180 | } |
| 181 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 182 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 187 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 188 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 189 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 190 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 191 | ** allocation error. The memory allocation failure will be recorded in |
| 192 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 193 | ** |
| 194 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 195 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 196 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 197 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 198 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 199 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 200 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 201 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 202 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 203 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 204 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 205 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 206 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 207 | int idx; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 208 | testcase( wtFlags & TERM_VIRTUAL ); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 209 | if( pWC->nTerm>=pWC->nSlot ){ |
| 210 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 211 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 212 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 213 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 214 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 215 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 216 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 217 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 218 | return 0; |
| 219 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 220 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 221 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 222 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 223 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 224 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 225 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 226 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 227 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 228 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 99; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 229 | }else{ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 230 | pTerm->truthProb = 1; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 231 | } |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 232 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 233 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 234 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 235 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 236 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 237 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 238 | |
| 239 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 240 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 241 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 242 | ** operator specified in the op parameter. The WhereClause structure |
| 243 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 244 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 245 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 246 | ** \________/ \_______________/ \________________/ |
| 247 | ** slot[0] slot[1] slot[2] |
| 248 | ** |
| 249 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 250 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 251 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 252 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 253 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 254 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 255 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 256 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 257 | pWC->op = op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 258 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 259 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 260 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 261 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 262 | whereSplit(pWC, pExpr->pLeft, op); |
| 263 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 264 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 268 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 269 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 270 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 271 | |
| 272 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 273 | ** Return the bitmask for the given cursor number. Return 0 if |
| 274 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 275 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 276 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 277 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 278 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 279 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 280 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 281 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 282 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 283 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 288 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 289 | ** |
| 290 | ** There is one cursor per table in the FROM clause. The number of |
| 291 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 292 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 293 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 294 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 295 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 296 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 297 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 301 | ** These routines walk (recursively) an expression tree and generate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 303 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 305 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 306 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 307 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 308 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 309 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 310 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 311 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 312 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 313 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 314 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 315 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 316 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 317 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 318 | }else{ |
| 319 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 320 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 321 | return mask; |
| 322 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 323 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 324 | int i; |
| 325 | Bitmask mask = 0; |
| 326 | if( pList ){ |
| 327 | for(i=0; i<pList->nExpr; i++){ |
| 328 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 331 | return mask; |
| 332 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 333 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 334 | Bitmask mask = 0; |
| 335 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 336 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 337 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 338 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 339 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 340 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 341 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 342 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 343 | int i; |
| 344 | for(i=0; i<pSrc->nSrc; i++){ |
| 345 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 346 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 347 | } |
| 348 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 349 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 350 | } |
| 351 | return mask; |
| 352 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 353 | |
| 354 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 355 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 356 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 357 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 358 | */ |
| 359 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 360 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 361 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 362 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 363 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 364 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 368 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 369 | */ |
| 370 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 371 | |
| 372 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 373 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 374 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 375 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 376 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 377 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 378 | ** that the collating sequence does not change. For example: |
| 379 | ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 380 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 381 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 382 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 383 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 384 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 385 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 386 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 387 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 388 | if( expRight==expLeft ){ |
| 389 | /* Either X and Y both have COLLATE operator or neither do */ |
| 390 | if( expRight ){ |
| 391 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 392 | ** used by clearing the EP_Collate flag from Y. */ |
| 393 | pExpr->pRight->flags &= ~EP_Collate; |
| 394 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 395 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 396 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 397 | ** it to be searched first. */ |
| 398 | pExpr->pLeft->flags |= EP_Collate; |
| 399 | } |
| 400 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 401 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 402 | if( pExpr->op>=TK_GT ){ |
| 403 | assert( TK_LT==TK_GT+2 ); |
| 404 | assert( TK_GE==TK_LE+2 ); |
| 405 | assert( TK_GT>TK_EQ ); |
| 406 | assert( TK_GT<TK_LE ); |
| 407 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 408 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 409 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 413 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 414 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 415 | static u16 operatorMask(int op){ |
| 416 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 417 | assert( allowedOp(op) ); |
| 418 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 419 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 420 | }else if( op==TK_ISNULL ){ |
| 421 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 422 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 423 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 424 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 425 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 426 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 427 | assert( op!=TK_IN || c==WO_IN ); |
| 428 | assert( op!=TK_EQ || c==WO_EQ ); |
| 429 | assert( op!=TK_LT || c==WO_LT ); |
| 430 | assert( op!=TK_LE || c==WO_LE ); |
| 431 | assert( op!=TK_GT || c==WO_GT ); |
| 432 | assert( op!=TK_GE || c==WO_GE ); |
| 433 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 437 | ** Advance to the next WhereTerm that matches according to the criteria |
| 438 | ** established when the pScan object was initialized by whereScanInit(). |
| 439 | ** Return NULL if there are no more matching WhereTerms. |
| 440 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 441 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 442 | int iCur; /* The cursor on the LHS of the term */ |
| 443 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 444 | Expr *pX; /* An expression being tested */ |
| 445 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 446 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 447 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 448 | |
| 449 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 450 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 451 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 452 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 453 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | e1a086e | 2013-10-28 20:15:56 +0000 | [diff] [blame] | 454 | if( pTerm->leftCursor==iCur |
| 455 | && pTerm->u.leftColumn==iColumn |
| 456 | && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 457 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 458 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 459 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 460 | ){ |
| 461 | int j; |
| 462 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 463 | assert( pX->op==TK_COLUMN ); |
| 464 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 465 | if( pScan->aEquiv[j]==pX->iTable |
| 466 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | if( j==pScan->nEquiv ){ |
| 471 | pScan->aEquiv[j] = pX->iTable; |
| 472 | pScan->aEquiv[j+1] = pX->iColumn; |
| 473 | pScan->nEquiv += 2; |
| 474 | } |
| 475 | } |
| 476 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 477 | /* Verify the affinity and collating sequence match */ |
| 478 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 479 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 480 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 481 | pX = pTerm->pExpr; |
| 482 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 483 | continue; |
| 484 | } |
| 485 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 486 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 487 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 488 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 489 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 490 | continue; |
| 491 | } |
| 492 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 493 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 494 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 495 | && pX->iTable==pScan->aEquiv[0] |
| 496 | && pX->iColumn==pScan->aEquiv[1] |
| 497 | ){ |
| 498 | continue; |
| 499 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 500 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 501 | return pTerm; |
| 502 | } |
| 503 | } |
| 504 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 505 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 506 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 507 | } |
| 508 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 509 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 510 | pScan->iEquiv += 2; |
| 511 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 517 | ** first match. Return NULL if there are no matches. |
| 518 | ** |
| 519 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 520 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 521 | ** iCur. The <op> must be one of the operators described by opMask. |
| 522 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 523 | ** If the search is for X and the WHERE clause contains terms of the |
| 524 | ** form X=Y then this routine might also return terms of the form |
| 525 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 526 | ** but is enough to handle most commonly occurring SQL statements. |
| 527 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 528 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 529 | ** index pIdx. |
| 530 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 531 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 532 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 533 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 534 | int iCur, /* Cursor to scan for */ |
| 535 | int iColumn, /* Column to scan for */ |
| 536 | u32 opMask, /* Operator(s) to scan for */ |
| 537 | Index *pIdx /* Must be compatible with this index */ |
| 538 | ){ |
| 539 | int j; |
| 540 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 541 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 542 | pScan->pOrigWC = pWC; |
| 543 | pScan->pWC = pWC; |
| 544 | if( pIdx && iColumn>=0 ){ |
| 545 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 546 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 547 | if( NEVER(j>=pIdx->nKeyCol) ) return 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 548 | } |
| 549 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 550 | }else{ |
| 551 | pScan->idxaff = 0; |
| 552 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 553 | } |
| 554 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 555 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 556 | pScan->aEquiv[0] = iCur; |
| 557 | pScan->aEquiv[1] = iColumn; |
| 558 | pScan->nEquiv = 2; |
| 559 | pScan->iEquiv = 2; |
| 560 | return whereScanNext(pScan); |
| 561 | } |
| 562 | |
| 563 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 564 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 565 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 566 | ** the WO_xx operator codes specified by the op parameter. |
| 567 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 568 | ** |
| 569 | ** The term returned might by Y=<expr> if there is another constraint in |
| 570 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 571 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 572 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 573 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 574 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 575 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 576 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 577 | ** and ... and A9=A10 and A10=<expr>. |
| 578 | ** |
| 579 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 580 | ** then try for the one with no dependencies on <expr> - in other words where |
| 581 | ** <expr> is a constant expression of some kind. Only return entries of |
| 582 | ** the form "X <op> Y" where Y is a column in another table if no terms of |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 583 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 584 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 585 | */ |
| 586 | static WhereTerm *findTerm( |
| 587 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 588 | int iCur, /* Cursor number of LHS */ |
| 589 | int iColumn, /* Column number of LHS */ |
| 590 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 591 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 592 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 593 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 594 | WhereTerm *pResult = 0; |
| 595 | WhereTerm *p; |
| 596 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 597 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 598 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 599 | while( p ){ |
| 600 | if( (p->prereqRight & notReady)==0 ){ |
| 601 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 602 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 603 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 604 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 605 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 606 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 607 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 608 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 609 | } |
| 610 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 611 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 612 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 616 | */ |
| 617 | static void exprAnalyzeAll( |
| 618 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 619 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 620 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 621 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 622 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 623 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 627 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 628 | /* |
| 629 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 630 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 631 | ** so and false if not. |
| 632 | ** |
| 633 | ** In order for the operator to be optimizible, the RHS must be a string |
| 634 | ** literal that does not begin with a wildcard. |
| 635 | */ |
| 636 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 637 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 638 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 639 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 640 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 641 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 642 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 643 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 644 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 645 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 646 | int c; /* One character in z[] */ |
| 647 | int cnt; /* Number of non-wildcard prefix characters */ |
| 648 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 649 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 650 | sqlite3_value *pVal = 0; |
| 651 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 652 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 653 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 654 | return 0; |
| 655 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 656 | #ifdef SQLITE_EBCDIC |
| 657 | if( *pnoCase ) return 0; |
| 658 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 659 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 660 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 661 | if( pLeft->op!=TK_COLUMN |
| 662 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 663 | || IsVirtual(pLeft->pTab) |
| 664 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 665 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 666 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 667 | return 0; |
| 668 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 669 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 670 | |
drh | 6ade453 | 2014-01-16 15:31:41 +0000 | [diff] [blame] | 671 | pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 672 | op = pRight->op; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 673 | if( op==TK_VARIABLE ){ |
| 674 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 675 | int iCol = pRight->iColumn; |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 676 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 677 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 678 | z = (char *)sqlite3_value_text(pVal); |
| 679 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 680 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 681 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 682 | }else if( op==TK_STRING ){ |
| 683 | z = pRight->u.zToken; |
| 684 | } |
| 685 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 686 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 687 | while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 688 | cnt++; |
| 689 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 690 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 691 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 692 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 693 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 694 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 695 | *ppPrefix = pPrefix; |
| 696 | if( op==TK_VARIABLE ){ |
| 697 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 698 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 699 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 700 | /* If the rhs of the LIKE expression is a variable, and the current |
| 701 | ** value of the variable means there is no need to invoke the LIKE |
| 702 | ** function, then no OP_Variable will be added to the program. |
| 703 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 704 | ** API. To workaround them, add a dummy OP_Variable here. |
| 705 | */ |
| 706 | int r1 = sqlite3GetTempReg(pParse); |
| 707 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 708 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 709 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | }else{ |
| 713 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 714 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 715 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 716 | |
| 717 | sqlite3ValueFree(pVal); |
| 718 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 719 | } |
| 720 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 721 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 722 | |
| 723 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 724 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 725 | ** Check to see if the given expression is of the form |
| 726 | ** |
| 727 | ** column MATCH expr |
| 728 | ** |
| 729 | ** If it is then return TRUE. If not, return FALSE. |
| 730 | */ |
| 731 | static int isMatchOfColumn( |
| 732 | Expr *pExpr /* Test this expression */ |
| 733 | ){ |
| 734 | ExprList *pList; |
| 735 | |
| 736 | if( pExpr->op!=TK_FUNCTION ){ |
| 737 | return 0; |
| 738 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 739 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 740 | return 0; |
| 741 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 742 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 743 | if( pList->nExpr!=2 ){ |
| 744 | return 0; |
| 745 | } |
| 746 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 747 | return 0; |
| 748 | } |
| 749 | return 1; |
| 750 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 751 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 752 | |
| 753 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 754 | ** If the pBase expression originated in the ON or USING clause of |
| 755 | ** a join, then transfer the appropriate markings over to derived. |
| 756 | */ |
| 757 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 758 | if( pDerived ){ |
| 759 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 760 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 761 | } |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 762 | } |
| 763 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 764 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 765 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 766 | ** Analyze a term that consists of two or more OR-connected |
| 767 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 768 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 769 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 770 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 771 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 772 | ** This routine analyzes terms such as the middle term in the above example. |
| 773 | ** A WhereOrTerm object is computed and attached to the term under |
| 774 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 775 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 776 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 777 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 778 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 779 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 780 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 781 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 782 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 783 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 784 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 785 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 786 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 787 | ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 788 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 789 | ** CASE 1: |
| 790 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 791 | ** If all subterms are of the form T.C=expr for some single column of C and |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 792 | ** a single table T (as shown in example B above) then create a new virtual |
| 793 | ** term that is an equivalent IN expression. In other words, if the term |
| 794 | ** being analyzed is: |
| 795 | ** |
| 796 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 797 | ** |
| 798 | ** then create a new virtual term like this: |
| 799 | ** |
| 800 | ** x IN (expr1,expr2,expr3) |
| 801 | ** |
| 802 | ** CASE 2: |
| 803 | ** |
| 804 | ** If all subterms are indexable by a single table T, then set |
| 805 | ** |
| 806 | ** WhereTerm.eOperator = WO_OR |
| 807 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 808 | ** |
| 809 | ** A subterm is "indexable" if it is of the form |
| 810 | ** "T.C <op> <expr>" where C is any column of table T and |
| 811 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 812 | ** A subterm is also indexable if it is an AND of two or more |
| 813 | ** subsubterms at least one of which is indexable. Indexable AND |
| 814 | ** subterms have their eOperator set to WO_AND and they have |
| 815 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 816 | ** |
| 817 | ** From another point of view, "indexable" means that the subterm could |
| 818 | ** potentially be used with an index if an appropriate index exists. |
| 819 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 820 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 821 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 822 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 823 | ** All examples A through E above satisfy case 2. But if a term |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 824 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 825 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 826 | ** satisfied. |
| 827 | ** |
| 828 | ** It might be the case that multiple tables are indexable. For example, |
| 829 | ** (E) above is indexable on tables P, Q, and R. |
| 830 | ** |
| 831 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 832 | ** separate indices to find rowids for each subterm and composing |
| 833 | ** the union of all rowids using a RowSet object. This is similar |
| 834 | ** to "bitmap indices" in other database engines. |
| 835 | ** |
| 836 | ** OTHERWISE: |
| 837 | ** |
| 838 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 839 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 840 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 841 | static void exprAnalyzeOrTerm( |
| 842 | SrcList *pSrc, /* the FROM clause */ |
| 843 | WhereClause *pWC, /* the complete WHERE clause */ |
| 844 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 845 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 846 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 847 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 848 | sqlite3 *db = pParse->db; /* Database connection */ |
| 849 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 850 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 851 | int i; /* Loop counters */ |
| 852 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 853 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 854 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 855 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 856 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 857 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 858 | /* |
| 859 | ** Break the OR clause into its separate subterms. The subterms are |
| 860 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 861 | ** object that is attached to the original OR clause term. |
| 862 | */ |
| 863 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 864 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 865 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 866 | if( pOrInfo==0 ) return; |
| 867 | pTerm->wtFlags |= TERM_ORINFO; |
| 868 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 869 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 870 | whereSplit(pOrWc, pExpr, TK_OR); |
| 871 | exprAnalyzeAll(pSrc, pOrWc); |
| 872 | if( db->mallocFailed ) return; |
| 873 | assert( pOrWc->nTerm>=2 ); |
| 874 | |
| 875 | /* |
| 876 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 877 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 878 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 879 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 880 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 881 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 882 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 883 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 884 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 885 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 886 | if( pAndInfo ){ |
| 887 | WhereClause *pAndWC; |
| 888 | WhereTerm *pAndTerm; |
| 889 | int j; |
| 890 | Bitmask b = 0; |
| 891 | pOrTerm->u.pAndInfo = pAndInfo; |
| 892 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 893 | pOrTerm->eOperator = WO_AND; |
| 894 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 895 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 896 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 897 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 898 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 899 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 900 | if( !db->mallocFailed ){ |
| 901 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 902 | assert( pAndTerm->pExpr ); |
| 903 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 904 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 905 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | indexable &= b; |
| 909 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 910 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 911 | /* Skip this term for now. We revisit it when we process the |
| 912 | ** corresponding TERM_VIRTUAL term */ |
| 913 | }else{ |
| 914 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 915 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 916 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 917 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 918 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 919 | } |
| 920 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 921 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 922 | chngToIN = 0; |
| 923 | }else{ |
| 924 | chngToIN &= b; |
| 925 | } |
| 926 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 927 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 931 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 932 | */ |
| 933 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 934 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 935 | |
| 936 | /* |
| 937 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 938 | ** we have to do some additional checking to see if case 1 really |
| 939 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 940 | ** |
| 941 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 942 | ** that there is no possibility of transforming the OR clause into an |
| 943 | ** IN operator because one or more terms in the OR clause contain |
| 944 | ** something other than == on a column in the single table. The 1-bit |
| 945 | ** case means that every term of the OR clause is of the form |
| 946 | ** "table.column=expr" for some single table. The one bit that is set |
| 947 | ** will correspond to the common table. We still need to check to make |
| 948 | ** sure the same column is used on all terms. The 2-bit case is when |
| 949 | ** the all terms are of the form "table1.column=table2.column". It |
| 950 | ** might be possible to form an IN operator with either table1.column |
| 951 | ** or table2.column as the LHS if either is common to every term of |
| 952 | ** the OR clause. |
| 953 | ** |
| 954 | ** Note that terms of the form "table.column1=table.column2" (the |
| 955 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 956 | */ |
| 957 | if( chngToIN ){ |
| 958 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 959 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 960 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 961 | int j = 0; /* Loop counter */ |
| 962 | |
| 963 | /* Search for a table and column that appears on one side or the |
| 964 | ** other of the == operator in every subterm. That table and column |
| 965 | ** will be recorded in iCursor and iColumn. There might not be any |
| 966 | ** such table and column. Set okToChngToIN if an appropriate table |
| 967 | ** and column is found but leave okToChngToIN false if not found. |
| 968 | */ |
| 969 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 970 | pOrTerm = pOrWc->a; |
| 971 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 972 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 973 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 974 | if( pOrTerm->leftCursor==iCursor ){ |
| 975 | /* This is the 2-bit case and we are on the second iteration and |
| 976 | ** current term is from the first iteration. So skip this term. */ |
| 977 | assert( j==1 ); |
| 978 | continue; |
| 979 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 980 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 981 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 982 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 983 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 984 | ** and use its inversion. */ |
| 985 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 986 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 987 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 988 | continue; |
| 989 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 990 | iColumn = pOrTerm->u.leftColumn; |
| 991 | iCursor = pOrTerm->leftCursor; |
| 992 | break; |
| 993 | } |
| 994 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 995 | /* No candidate table+column was found. This can only occur |
| 996 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 997 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 998 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 999 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1000 | break; |
| 1001 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1002 | testcase( j==1 ); |
| 1003 | |
| 1004 | /* We have found a candidate table and column. Check to see if that |
| 1005 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1006 | okToChngToIN = 1; |
| 1007 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1008 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1009 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1010 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1011 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1012 | okToChngToIN = 0; |
| 1013 | }else{ |
| 1014 | int affLeft, affRight; |
| 1015 | /* If the right-hand side is also a column, then the affinities |
| 1016 | ** of both right and left sides must be such that no type |
| 1017 | ** conversions are required on the right. (Ticket #2249) |
| 1018 | */ |
| 1019 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1020 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1021 | if( affRight!=0 && affRight!=affLeft ){ |
| 1022 | okToChngToIN = 0; |
| 1023 | }else{ |
| 1024 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1031 | ** case 1. In that case, construct a new virtual term that is |
| 1032 | ** pTerm converted into an IN operator. |
| 1033 | */ |
| 1034 | if( okToChngToIN ){ |
| 1035 | Expr *pDup; /* A transient duplicate expression */ |
| 1036 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1037 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1038 | Expr *pNew; /* The complete IN operator */ |
| 1039 | |
| 1040 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1041 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1042 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1043 | assert( pOrTerm->leftCursor==iCursor ); |
| 1044 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1045 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1046 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1047 | pLeft = pOrTerm->pExpr->pLeft; |
| 1048 | } |
| 1049 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1050 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1051 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1052 | if( pNew ){ |
| 1053 | int idxNew; |
| 1054 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1055 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1056 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1057 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1058 | testcase( idxNew==0 ); |
| 1059 | exprAnalyze(pSrc, pWC, idxNew); |
| 1060 | pTerm = &pWC->a[idxTerm]; |
| 1061 | pWC->a[idxNew].iParent = idxTerm; |
| 1062 | pTerm->nChild = 1; |
| 1063 | }else{ |
| 1064 | sqlite3ExprListDelete(db, pList); |
| 1065 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1066 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1067 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1068 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1069 | } |
| 1070 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1071 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1072 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1073 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1074 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1075 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1076 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1077 | ** |
| 1078 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1079 | ** to the standard form of "X <op> <expr>". |
| 1080 | ** |
| 1081 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1082 | ** columns, then the original expression is unchanged and a new virtual |
| 1083 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1084 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1085 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1086 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1087 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1088 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1089 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1090 | static void exprAnalyze( |
| 1091 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1092 | WhereClause *pWC, /* the WHERE clause */ |
| 1093 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1094 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1095 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1096 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1097 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1098 | Expr *pExpr; /* The expression to be analyzed */ |
| 1099 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1100 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1101 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1102 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1103 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1104 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1105 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1106 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1107 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1108 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1109 | if( db->mallocFailed ){ |
| 1110 | return; |
| 1111 | } |
| 1112 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1113 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1114 | pExpr = pTerm->pExpr; |
| 1115 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1116 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1117 | op = pExpr->op; |
| 1118 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1119 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1120 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1121 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1122 | }else{ |
| 1123 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1124 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1125 | }else if( op==TK_ISNULL ){ |
| 1126 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1127 | }else{ |
| 1128 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1129 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1130 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1131 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1132 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1133 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1134 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1135 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1136 | } |
| 1137 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1138 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1139 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1140 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1141 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1142 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1143 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1144 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1145 | if( pLeft->op==TK_COLUMN ){ |
| 1146 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1147 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1148 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1149 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1150 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1151 | WhereTerm *pNew; |
| 1152 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1153 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1154 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1155 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1156 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1157 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1158 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1159 | return; |
| 1160 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1161 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1162 | if( idxNew==0 ) return; |
| 1163 | pNew = &pWC->a[idxNew]; |
| 1164 | pNew->iParent = idxTerm; |
| 1165 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1166 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1167 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1168 | if( pExpr->op==TK_EQ |
| 1169 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1170 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1171 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1172 | pTerm->eOperator |= WO_EQUIV; |
| 1173 | eExtraOp = WO_EQUIV; |
| 1174 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1175 | }else{ |
| 1176 | pDup = pExpr; |
| 1177 | pNew = pTerm; |
| 1178 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1179 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1180 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1181 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1182 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1183 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1184 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1185 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1186 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1189 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1190 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1191 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1192 | ** that define the range that the BETWEEN implements. For example: |
| 1193 | ** |
| 1194 | ** a BETWEEN b AND c |
| 1195 | ** |
| 1196 | ** is converted into: |
| 1197 | ** |
| 1198 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1199 | ** |
| 1200 | ** The two new terms are added onto the end of the WhereClause object. |
| 1201 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1202 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1203 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1204 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1205 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1206 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1207 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1208 | int i; |
| 1209 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1210 | assert( pList!=0 ); |
| 1211 | assert( pList->nExpr==2 ); |
| 1212 | for(i=0; i<2; i++){ |
| 1213 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1214 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1215 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1216 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1217 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1218 | transferJoinMarkings(pNewExpr, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1219 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1220 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1221 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1222 | pTerm = &pWC->a[idxTerm]; |
| 1223 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1224 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1225 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1226 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1227 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1228 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1229 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1230 | /* Analyze a term that is composed of two or more subterms connected by |
| 1231 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1232 | */ |
| 1233 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1234 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1235 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1236 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1237 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1238 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1239 | |
| 1240 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1241 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1242 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1243 | ** |
| 1244 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1245 | ** |
| 1246 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1247 | ** |
| 1248 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1249 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1250 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1251 | if( pWC->op==TK_AND |
| 1252 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1253 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1254 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1255 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1256 | Expr *pNewExpr1; |
| 1257 | Expr *pNewExpr2; |
| 1258 | int idxNew1; |
| 1259 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1260 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1261 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1262 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1263 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1264 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1265 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1266 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1267 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1268 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1269 | /* The point is to increment the last character before the first |
| 1270 | ** wildcard. But if we increment '@', that will push it into the |
| 1271 | ** alphabetic range where case conversions will mess up the |
| 1272 | ** inequality. To avoid this, make sure to also run the full |
| 1273 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1274 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1275 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1276 | c = sqlite3UpperToLower[c]; |
| 1277 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1278 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1279 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1280 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1281 | sCollSeqName.n = 6; |
| 1282 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1283 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1284 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1285 | pStr1, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1286 | transferJoinMarkings(pNewExpr1, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1287 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1288 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1289 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1290 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1291 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1292 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1293 | pStr2, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1294 | transferJoinMarkings(pNewExpr2, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1295 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1296 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1297 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1298 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1299 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1300 | pWC->a[idxNew1].iParent = idxTerm; |
| 1301 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1302 | pTerm->nChild = 2; |
| 1303 | } |
| 1304 | } |
| 1305 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1306 | |
| 1307 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1308 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1309 | ** current expression is of the form: column MATCH expr. |
| 1310 | ** This information is used by the xBestIndex methods of |
| 1311 | ** virtual tables. The native query optimizer does not attempt |
| 1312 | ** to do anything with MATCH functions. |
| 1313 | */ |
| 1314 | if( isMatchOfColumn(pExpr) ){ |
| 1315 | int idxNew; |
| 1316 | Expr *pRight, *pLeft; |
| 1317 | WhereTerm *pNewTerm; |
| 1318 | Bitmask prereqColumn, prereqExpr; |
| 1319 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1320 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1321 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1322 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1323 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1324 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1325 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1326 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1327 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1328 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1329 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1330 | pNewTerm = &pWC->a[idxNew]; |
| 1331 | pNewTerm->prereqRight = prereqExpr; |
| 1332 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1333 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1334 | pNewTerm->eOperator = WO_MATCH; |
| 1335 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1336 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1337 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1338 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1339 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1340 | } |
| 1341 | } |
| 1342 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1343 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1344 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1345 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1346 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1347 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1348 | ** virtual term of that form. |
| 1349 | ** |
| 1350 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1351 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1352 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1353 | ** the start of the loop will prevent any results from being returned. |
| 1354 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1355 | if( pExpr->op==TK_NOTNULL |
| 1356 | && pExpr->pLeft->op==TK_COLUMN |
| 1357 | && pExpr->pLeft->iColumn>=0 |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 1358 | && OptimizationEnabled(db, SQLITE_Stat3) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1359 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1360 | Expr *pNewExpr; |
| 1361 | Expr *pLeft = pExpr->pLeft; |
| 1362 | int idxNew; |
| 1363 | WhereTerm *pNewTerm; |
| 1364 | |
| 1365 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1366 | sqlite3ExprDup(db, pLeft, 0), |
| 1367 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1368 | |
| 1369 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1370 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1371 | if( idxNew ){ |
| 1372 | pNewTerm = &pWC->a[idxNew]; |
| 1373 | pNewTerm->prereqRight = 0; |
| 1374 | pNewTerm->leftCursor = pLeft->iTable; |
| 1375 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1376 | pNewTerm->eOperator = WO_GT; |
| 1377 | pNewTerm->iParent = idxTerm; |
| 1378 | pTerm = &pWC->a[idxTerm]; |
| 1379 | pTerm->nChild = 1; |
| 1380 | pTerm->wtFlags |= TERM_COPIED; |
| 1381 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1382 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1383 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1384 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1385 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1386 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1387 | ** an index for tables to the left of the join. |
| 1388 | */ |
| 1389 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1392 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1393 | ** This function searches pList for a entry that matches the iCol-th column |
| 1394 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1395 | ** |
| 1396 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1397 | ** no expression is found, -1 is returned. |
| 1398 | */ |
| 1399 | static int findIndexCol( |
| 1400 | Parse *pParse, /* Parse context */ |
| 1401 | ExprList *pList, /* Expression list to search */ |
| 1402 | int iBase, /* Cursor for table associated with pIdx */ |
| 1403 | Index *pIdx, /* Index to match column of */ |
| 1404 | int iCol /* Column of index to match */ |
| 1405 | ){ |
| 1406 | int i; |
| 1407 | const char *zColl = pIdx->azColl[iCol]; |
| 1408 | |
| 1409 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1410 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1411 | if( p->op==TK_COLUMN |
| 1412 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1413 | && p->iTable==iBase |
| 1414 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1415 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1416 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1417 | return i; |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
| 1425 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1426 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1427 | ** is redundant. |
| 1428 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1429 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1430 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1431 | */ |
| 1432 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1433 | Parse *pParse, /* Parsing context */ |
| 1434 | SrcList *pTabList, /* The FROM clause */ |
| 1435 | WhereClause *pWC, /* The WHERE clause */ |
| 1436 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1437 | ){ |
| 1438 | Table *pTab; |
| 1439 | Index *pIdx; |
| 1440 | int i; |
| 1441 | int iBase; |
| 1442 | |
| 1443 | /* If there is more than one table or sub-select in the FROM clause of |
| 1444 | ** this query, then it will not be possible to show that the DISTINCT |
| 1445 | ** clause is redundant. */ |
| 1446 | if( pTabList->nSrc!=1 ) return 0; |
| 1447 | iBase = pTabList->a[0].iCursor; |
| 1448 | pTab = pTabList->a[0].pTab; |
| 1449 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1450 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1451 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1452 | ** current SELECT is a correlated sub-query. |
| 1453 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1454 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1455 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1456 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /* Loop through all indices on the table, checking each to see if it makes |
| 1460 | ** the DISTINCT qualifier redundant. It does so if: |
| 1461 | ** |
| 1462 | ** 1. The index is itself UNIQUE, and |
| 1463 | ** |
| 1464 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1465 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1466 | ** where X is a constant value. The collation sequences of the |
| 1467 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1468 | ** |
| 1469 | ** 3. All of those index columns for which the WHERE clause does not |
| 1470 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1471 | */ |
| 1472 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1473 | if( pIdx->onError==OE_None ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1474 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 1475 | i16 iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1476 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1477 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1478 | if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){ |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1479 | break; |
| 1480 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1483 | if( i==pIdx->nKeyCol ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1484 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1485 | return 1; |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | return 0; |
| 1490 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1491 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1492 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1493 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1494 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1495 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 1496 | static LogEst estLog(LogEst N){ |
drh | 696964d | 2014-06-12 15:46:46 +0000 | [diff] [blame] | 1497 | return N<=10 ? 0 : sqlite3LogEst(N) - 33; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1500 | /* |
| 1501 | ** Two routines for printing the content of an sqlite3_index_info |
| 1502 | ** structure. Used for testing and debugging only. If neither |
| 1503 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1504 | ** are no-ops. |
| 1505 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1506 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1507 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1508 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1509 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1510 | for(i=0; i<p->nConstraint; i++){ |
| 1511 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1512 | i, |
| 1513 | p->aConstraint[i].iColumn, |
| 1514 | p->aConstraint[i].iTermOffset, |
| 1515 | p->aConstraint[i].op, |
| 1516 | p->aConstraint[i].usable); |
| 1517 | } |
| 1518 | for(i=0; i<p->nOrderBy; i++){ |
| 1519 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1520 | i, |
| 1521 | p->aOrderBy[i].iColumn, |
| 1522 | p->aOrderBy[i].desc); |
| 1523 | } |
| 1524 | } |
| 1525 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1526 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1527 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1528 | for(i=0; i<p->nConstraint; i++){ |
| 1529 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1530 | i, |
| 1531 | p->aConstraintUsage[i].argvIndex, |
| 1532 | p->aConstraintUsage[i].omit); |
| 1533 | } |
| 1534 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1535 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1536 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1537 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 1538 | sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1539 | } |
| 1540 | #else |
| 1541 | #define TRACE_IDX_INPUTS(A) |
| 1542 | #define TRACE_IDX_OUTPUTS(A) |
| 1543 | #endif |
| 1544 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1545 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1546 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1547 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1548 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1549 | ** index existed. |
| 1550 | */ |
| 1551 | static int termCanDriveIndex( |
| 1552 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1553 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1554 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1555 | ){ |
| 1556 | char aff; |
| 1557 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1558 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1559 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1560 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1561 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1562 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 1563 | return 1; |
| 1564 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1565 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1566 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1567 | |
| 1568 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1569 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1570 | ** Generate code to construct the Index object for an automatic index |
| 1571 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1572 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1573 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1574 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1575 | Parse *pParse, /* The parsing context */ |
| 1576 | WhereClause *pWC, /* The WHERE clause */ |
| 1577 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1578 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1579 | WhereLevel *pLevel /* Write new index here */ |
| 1580 | ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1581 | int nKeyCol; /* Number of columns in the constructed index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1582 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1583 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1584 | Index *pIdx; /* Object describing the transient index */ |
| 1585 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1586 | int addrInit; /* Address of the initialization bypass jump */ |
| 1587 | Table *pTable; /* The table being indexed */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1588 | int addrTop; /* Top of the index fill loop */ |
| 1589 | int regRecord; /* Register holding an index record */ |
| 1590 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1591 | int i; /* Loop counter */ |
| 1592 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1593 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1594 | WhereLoop *pLoop; /* The Loop object */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 1595 | char *zNotUsed; /* Extra space on the end of pIdx */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1596 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 1597 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1598 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1599 | |
| 1600 | /* Generate code to skip over the creation and initialization of the |
| 1601 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 1602 | v = pParse->pVdbe; |
| 1603 | assert( v!=0 ); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 1604 | addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1605 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1606 | /* Count the number of columns that will be added to the index |
| 1607 | ** and used to match WHERE clause constraints */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1608 | nKeyCol = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1609 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1610 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1611 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1612 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 1613 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1614 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 1615 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1616 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1617 | testcase( iCol==BMS ); |
| 1618 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1619 | if( !sentWarning ){ |
| 1620 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 1621 | "automatic index on %s(%s)", pTable->zName, |
| 1622 | pTable->aCol[iCol].zName); |
| 1623 | sentWarning = 1; |
| 1624 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1625 | if( (idxCols & cMask)==0 ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1626 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return; |
| 1627 | pLoop->aLTerm[nKeyCol++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1628 | idxCols |= cMask; |
| 1629 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1632 | assert( nKeyCol>0 ); |
| 1633 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1634 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 1635 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1636 | |
| 1637 | /* Count the number of additional columns needed to create a |
| 1638 | ** covering index. A "covering index" is an index that contains all |
| 1639 | ** columns that are needed by the query. With a covering index, the |
| 1640 | ** original table never needs to be accessed. Automatic indices must |
| 1641 | ** be a covering index because the index will not be updated if the |
| 1642 | ** original table changes and the index and table cannot both be used |
| 1643 | ** if they go out of sync. |
| 1644 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1645 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1646 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1647 | testcase( pTable->nCol==BMS-1 ); |
| 1648 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1649 | for(i=0; i<mxBitCol; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1650 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1651 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1652 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1653 | nKeyCol += pTable->nCol - BMS + 1; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1654 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1655 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1656 | |
| 1657 | /* Construct the Index object to describe this index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1658 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1659 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1660 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1661 | pIdx->zName = "auto-index"; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1662 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1663 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1664 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1665 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1666 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1667 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1668 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 1669 | testcase( iCol==BMS-1 ); |
| 1670 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1671 | if( (idxCols & cMask)==0 ){ |
| 1672 | Expr *pX = pTerm->pExpr; |
| 1673 | idxCols |= cMask; |
| 1674 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 1675 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 1676 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1677 | n++; |
| 1678 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1681 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1682 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1683 | /* Add additional columns needed to make the automatic index into |
| 1684 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1685 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1686 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1687 | pIdx->aiColumn[n] = i; |
| 1688 | pIdx->azColl[n] = "BINARY"; |
| 1689 | n++; |
| 1690 | } |
| 1691 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1692 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1693 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 1694 | pIdx->aiColumn[n] = i; |
| 1695 | pIdx->azColl[n] = "BINARY"; |
| 1696 | n++; |
| 1697 | } |
| 1698 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1699 | assert( n==nKeyCol ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1700 | pIdx->aiColumn[n] = -1; |
| 1701 | pIdx->azColl[n] = "BINARY"; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1702 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1703 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1704 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 1705 | pLevel->iIdxCur = pParse->nTab++; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1706 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 1707 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1708 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1709 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1710 | /* Fill the automatic index with content */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1711 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1712 | regRecord = sqlite3GetTempReg(pParse); |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 1713 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1714 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 1715 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1716 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1717 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1718 | sqlite3VdbeJumpHere(v, addrTop); |
| 1719 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 1720 | |
| 1721 | /* Jump here when skipping the initialization */ |
| 1722 | sqlite3VdbeJumpHere(v, addrInit); |
| 1723 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1724 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1725 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1726 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1727 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1728 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 1729 | ** responsibility of the caller to eventually release the structure |
| 1730 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 1731 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 1732 | static sqlite3_index_info *allocateIndexInfo( |
| 1733 | Parse *pParse, |
| 1734 | WhereClause *pWC, |
| 1735 | struct SrcList_item *pSrc, |
| 1736 | ExprList *pOrderBy |
| 1737 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1738 | int i, j; |
| 1739 | int nTerm; |
| 1740 | struct sqlite3_index_constraint *pIdxCons; |
| 1741 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 1742 | struct sqlite3_index_constraint_usage *pUsage; |
| 1743 | WhereTerm *pTerm; |
| 1744 | int nOrderBy; |
| 1745 | sqlite3_index_info *pIdxInfo; |
| 1746 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1747 | /* Count the number of possible WHERE clause constraints referring |
| 1748 | ** to this virtual table */ |
| 1749 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 1750 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1751 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1752 | testcase( pTerm->eOperator & WO_IN ); |
| 1753 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 1754 | testcase( pTerm->eOperator & WO_ALL ); |
| 1755 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 1756 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1757 | nTerm++; |
| 1758 | } |
| 1759 | |
| 1760 | /* If the ORDER BY clause contains only columns in the current |
| 1761 | ** virtual table then allocate space for the aOrderBy part of |
| 1762 | ** the sqlite3_index_info structure. |
| 1763 | */ |
| 1764 | nOrderBy = 0; |
| 1765 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1766 | int n = pOrderBy->nExpr; |
| 1767 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1768 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 1769 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 1770 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1771 | if( i==n){ |
| 1772 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /* Allocate the sqlite3_index_info structure |
| 1777 | */ |
| 1778 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 1779 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 1780 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 1781 | if( pIdxInfo==0 ){ |
| 1782 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1783 | return 0; |
| 1784 | } |
| 1785 | |
| 1786 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 1787 | ** many fields that are declared "const" to prevent xBestIndex from |
| 1788 | ** changing them. We have to do some funky casting in order to |
| 1789 | ** initialize those fields. |
| 1790 | */ |
| 1791 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 1792 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 1793 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 1794 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 1795 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 1796 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 1797 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 1798 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 1799 | pUsage; |
| 1800 | |
| 1801 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1802 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1803 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1804 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1805 | testcase( pTerm->eOperator & WO_IN ); |
| 1806 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 1807 | testcase( pTerm->eOperator & WO_ALL ); |
| 1808 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 1809 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1810 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 1811 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1812 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1813 | if( op==WO_IN ) op = WO_EQ; |
| 1814 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1815 | /* The direct assignment in the previous line is possible only because |
| 1816 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 1817 | ** following asserts verify this fact. */ |
| 1818 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 1819 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 1820 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 1821 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 1822 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 1823 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1824 | assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1825 | j++; |
| 1826 | } |
| 1827 | for(i=0; i<nOrderBy; i++){ |
| 1828 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 1829 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 1830 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 1831 | } |
| 1832 | |
| 1833 | return pIdxInfo; |
| 1834 | } |
| 1835 | |
| 1836 | /* |
| 1837 | ** The table object reference passed as the second argument to this function |
| 1838 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1839 | ** method of the virtual table with the sqlite3_index_info object that |
| 1840 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1841 | ** |
| 1842 | ** If an error occurs, pParse is populated with an error message and a |
| 1843 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 1844 | ** part of the sqlite3_index_info structure is left populated. |
| 1845 | ** |
| 1846 | ** Whether or not an error is returned, it is the responsibility of the |
| 1847 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 1848 | ** that this is required. |
| 1849 | */ |
| 1850 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 1851 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1852 | int i; |
| 1853 | int rc; |
| 1854 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1855 | TRACE_IDX_INPUTS(p); |
| 1856 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 1857 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1858 | |
| 1859 | if( rc!=SQLITE_OK ){ |
| 1860 | if( rc==SQLITE_NOMEM ){ |
| 1861 | pParse->db->mallocFailed = 1; |
| 1862 | }else if( !pVtab->zErrMsg ){ |
| 1863 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 1864 | }else{ |
| 1865 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 1866 | } |
| 1867 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 1868 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1869 | pVtab->zErrMsg = 0; |
| 1870 | |
| 1871 | for(i=0; i<p->nConstraint; i++){ |
| 1872 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 1873 | sqlite3ErrorMsg(pParse, |
| 1874 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | return pParse->nErr; |
| 1879 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1880 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1881 | |
| 1882 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1883 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1884 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1885 | ** Estimate the location of a particular key among all keys in an |
| 1886 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 1887 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1888 | ** aStat[0] Est. number of rows less than pVal |
| 1889 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1890 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1891 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1892 | */ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 1893 | static void whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1894 | Parse *pParse, /* Database connection */ |
| 1895 | Index *pIdx, /* Index to consider domain of */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 1896 | UnpackedRecord *pRec, /* Vector of values to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1897 | int roundUp, /* Round up if true. Round down if false */ |
| 1898 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1899 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1900 | IndexSample *aSample = pIdx->aSample; |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 1901 | int iCol; /* Index of required stats in anEq[] etc. */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1902 | int iMin = 0; /* Smallest sample not yet tested */ |
| 1903 | int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */ |
| 1904 | int iTest; /* Next sample to test */ |
| 1905 | int res; /* Result of comparison operation */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1906 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 1907 | #ifndef SQLITE_DEBUG |
| 1908 | UNUSED_PARAMETER( pParse ); |
| 1909 | #endif |
drh | 7f59475 | 2013-12-03 19:49:55 +0000 | [diff] [blame] | 1910 | assert( pRec!=0 ); |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 1911 | iCol = pRec->nField - 1; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 1912 | assert( pIdx->nSample>0 ); |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 1913 | assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1914 | do{ |
| 1915 | iTest = (iMin+i)/2; |
dan | 3833e93 | 2014-03-01 19:44:56 +0000 | [diff] [blame] | 1916 | res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1917 | if( res<0 ){ |
| 1918 | iMin = iTest+1; |
| 1919 | }else{ |
| 1920 | i = iTest; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1921 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1922 | }while( res && iMin<i ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1923 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1924 | #ifdef SQLITE_DEBUG |
| 1925 | /* The following assert statements check that the binary search code |
| 1926 | ** above found the right answer. This block serves no purpose other |
| 1927 | ** than to invoke the asserts. */ |
| 1928 | if( res==0 ){ |
| 1929 | /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 1930 | assert( i<pIdx->nSample ); |
dan | 3833e93 | 2014-03-01 19:44:56 +0000 | [diff] [blame] | 1931 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0) |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 1932 | || pParse->db->mallocFailed ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1933 | }else{ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1934 | /* Otherwise, pRec must be smaller than sample $i and larger than |
| 1935 | ** sample ($i-1). */ |
| 1936 | assert( i==pIdx->nSample |
dan | 3833e93 | 2014-03-01 19:44:56 +0000 | [diff] [blame] | 1937 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0 |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 1938 | || pParse->db->mallocFailed ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1939 | assert( i==0 |
dan | 3833e93 | 2014-03-01 19:44:56 +0000 | [diff] [blame] | 1940 | || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0 |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 1941 | || pParse->db->mallocFailed ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1942 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1943 | #endif /* ifdef SQLITE_DEBUG */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1944 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1945 | /* At this point, aSample[i] is the first sample that is greater than |
| 1946 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1947 | ** than pVal. If aSample[i]==pVal, then res==0. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1948 | */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1949 | if( res==0 ){ |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 1950 | aStat[0] = aSample[i].anLt[iCol]; |
| 1951 | aStat[1] = aSample[i].anEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1952 | }else{ |
| 1953 | tRowcnt iLower, iUpper, iGap; |
| 1954 | if( i==0 ){ |
| 1955 | iLower = 0; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 1956 | iUpper = aSample[0].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1957 | }else{ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1958 | i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 1959 | iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 1960 | iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1961 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1962 | aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1963 | if( iLower>=iUpper ){ |
| 1964 | iGap = 0; |
| 1965 | }else{ |
| 1966 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1967 | } |
| 1968 | if( roundUp ){ |
| 1969 | iGap = (iGap*2)/3; |
| 1970 | }else{ |
| 1971 | iGap = iGap/3; |
| 1972 | } |
| 1973 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1974 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1975 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1976 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1977 | |
| 1978 | /* |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 1979 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| 1980 | ** bound on a range scan. Without considering pTerm, it is estimated |
| 1981 | ** that the scan will visit nNew rows. This function returns the number |
| 1982 | ** estimated to be visited after taking pTerm into account. |
| 1983 | ** |
| 1984 | ** If the user explicitly specified a likelihood() value for this term, |
| 1985 | ** then the return value is the likelihood multiplied by the number of |
| 1986 | ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term |
| 1987 | ** has a likelihood of 0.50, and any other term a likelihood of 0.25. |
| 1988 | */ |
| 1989 | static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ |
| 1990 | LogEst nRet = nNew; |
| 1991 | if( pTerm ){ |
| 1992 | if( pTerm->truthProb<=0 ){ |
| 1993 | nRet += pTerm->truthProb; |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 1994 | }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 1995 | nRet -= 20; assert( 20==sqlite3LogEst(4) ); |
| 1996 | } |
| 1997 | } |
| 1998 | return nRet; |
| 1999 | } |
| 2000 | |
| 2001 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2002 | ** This function is used to estimate the number of rows that will be visited |
| 2003 | ** by scanning an index for a range of values. The range may have an upper |
| 2004 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2005 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2006 | ** example, assuming that index p is on t1(a): |
| 2007 | ** |
| 2008 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2009 | ** |_____| |_____| |
| 2010 | ** | | |
| 2011 | ** pLower pUpper |
| 2012 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2013 | ** If either of the upper or lower bound is not present, then NULL is passed in |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2014 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2015 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2016 | ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 2017 | ** column subject to the range constraint. Or, equivalently, the number of |
| 2018 | ** equality constraints optimized by the proposed index scan. For example, |
| 2019 | ** assuming index p is on t1(a, b), and the SQL query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2020 | ** |
| 2021 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2022 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2023 | ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 2024 | ** left-most column of the index). Or, if the query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2025 | ** |
| 2026 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2027 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2028 | ** then nEq is set to 0. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2029 | ** |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2030 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2031 | ** number of rows that the index scan is expected to visit without |
| 2032 | ** considering the range constraints. If nEq is 0, this is the number of |
| 2033 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 2034 | ** to account for the range contraints pLower and pUpper. |
| 2035 | ** |
| 2036 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
drh | 94aa7e0 | 2014-06-06 17:09:52 +0000 | [diff] [blame] | 2037 | ** used, a single range inequality reduces the search space by a factor of 4. |
| 2038 | ** and a pair of constraints (x>? AND x<?) reduces the expected number of |
| 2039 | ** rows visited by a factor of 64. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2040 | */ |
| 2041 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2042 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2043 | WhereLoopBuilder *pBuilder, |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2044 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2045 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2046 | WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2047 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2048 | int rc = SQLITE_OK; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2049 | int nOut = pLoop->nOut; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2050 | LogEst nNew; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2051 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2052 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2053 | Index *p = pLoop->u.btree.pIndex; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2054 | int nEq = pLoop->u.btree.nEq; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2055 | |
drh | 74dade2 | 2013-09-04 18:14:53 +0000 | [diff] [blame] | 2056 | if( p->nSample>0 |
| 2057 | && nEq==pBuilder->nRecValid |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 2058 | && nEq<p->nSampleCol |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2059 | && OptimizationEnabled(pParse->db, SQLITE_Stat3) |
| 2060 | ){ |
| 2061 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2062 | tRowcnt a[2]; |
dan | 575ab2f | 2013-09-02 07:16:40 +0000 | [diff] [blame] | 2063 | u8 aff; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2064 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2065 | /* Variable iLower will be set to the estimate of the number of rows in |
| 2066 | ** the index that are less than the lower bound of the range query. The |
| 2067 | ** lower bound being the concatenation of $P and $L, where $P is the |
| 2068 | ** key-prefix formed by the nEq values matched against the nEq left-most |
| 2069 | ** columns of the index, and $L is the value in pLower. |
| 2070 | ** |
| 2071 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 2072 | ** is not a simple variable or literal value), the lower bound of the |
| 2073 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 2074 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 2075 | ** ($P:$L) and the larger of the two returned values used. |
| 2076 | ** |
| 2077 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 2078 | ** less than the upper bound of the range query. Where the upper bound |
| 2079 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 2080 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 2081 | */ |
| 2082 | tRowcnt iLower; |
| 2083 | tRowcnt iUpper; |
| 2084 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2085 | if( nEq==p->nKeyCol ){ |
mistachkin | c2cfb51 | 2013-09-04 04:04:08 +0000 | [diff] [blame] | 2086 | aff = SQLITE_AFF_INTEGER; |
| 2087 | }else{ |
| 2088 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 2089 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2090 | /* Determine iLower and iUpper using ($P) only. */ |
| 2091 | if( nEq==0 ){ |
| 2092 | iLower = 0; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2093 | iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2094 | }else{ |
| 2095 | /* Note: this call could be optimized away - since the same values must |
| 2096 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 2097 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2098 | iLower = a[0]; |
| 2099 | iUpper = a[0] + a[1]; |
| 2100 | } |
| 2101 | |
| 2102 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2103 | if( pLower ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2104 | int bOk; /* True if value is extracted from pExpr */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2105 | Expr *pExpr = pLower->pExpr->pRight; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2106 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2107 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2108 | if( rc==SQLITE_OK && bOk ){ |
| 2109 | tRowcnt iNew; |
| 2110 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2111 | iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0); |
| 2112 | if( iNew>iLower ) iLower = iNew; |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2113 | nOut--; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2114 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2115 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 2118 | if( pUpper ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2119 | int bOk; /* True if value is extracted from pExpr */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2120 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2121 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2122 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2123 | if( rc==SQLITE_OK && bOk ){ |
| 2124 | tRowcnt iNew; |
| 2125 | whereKeyStats(pParse, p, pRec, 1, a); |
| 2126 | iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0); |
| 2127 | if( iNew<iUpper ) iUpper = iNew; |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2128 | nOut--; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2131 | |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2132 | pBuilder->pRec = pRec; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2133 | if( rc==SQLITE_OK ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2134 | if( iUpper>iLower ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2135 | nNew = sqlite3LogEst(iUpper - iLower); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2136 | }else{ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2137 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2138 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2139 | if( nNew<nOut ){ |
| 2140 | nOut = nNew; |
| 2141 | } |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2142 | pLoop->nOut = (LogEst)nOut; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2143 | WHERETRACE(0x10, ("range scan regions: %u..%u est=%d\n", |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2144 | (u32)iLower, (u32)iUpper, nOut)); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2145 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2146 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2147 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2148 | #else |
| 2149 | UNUSED_PARAMETER(pParse); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2150 | UNUSED_PARAMETER(pBuilder); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2151 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2152 | assert( pLower || pUpper ); |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2153 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2154 | nNew = whereRangeAdjust(pLower, nOut); |
| 2155 | nNew = whereRangeAdjust(pUpper, nNew); |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2156 | |
dan | 42685f2 | 2014-04-28 19:34:06 +0000 | [diff] [blame] | 2157 | /* TUNING: If there is both an upper and lower limit, assume the range is |
| 2158 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 2159 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 2160 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 2161 | ** match 1/64 of the index. */ |
| 2162 | if( pLower && pUpper ) nNew -= 20; |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2163 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2164 | nOut -= (pLower!=0) + (pUpper!=0); |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2165 | if( nNew<10 ) nNew = 10; |
| 2166 | if( nNew<nOut ) nOut = nNew; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2167 | pLoop->nOut = (LogEst)nOut; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2168 | return rc; |
| 2169 | } |
| 2170 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2171 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2172 | /* |
| 2173 | ** Estimate the number of rows that will be returned based on |
| 2174 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2175 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2176 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2177 | ** for that index. When pExpr==NULL that means the constraint is |
| 2178 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2179 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2180 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2181 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2182 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2183 | ** |
| 2184 | ** This routine can fail if it is unable to load a collating sequence |
| 2185 | ** required for string comparison, or if unable to allocate memory |
| 2186 | ** for a UTF conversion required for comparison. The error is stored |
| 2187 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2188 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2189 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2190 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2191 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2192 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2193 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2194 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2195 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2196 | int nEq = pBuilder->pNew->u.btree.nEq; |
| 2197 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2198 | u8 aff; /* Column affinity */ |
| 2199 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2200 | tRowcnt a[2]; /* Statistics */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2201 | int bOk; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2202 | |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2203 | assert( nEq>=1 ); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2204 | assert( nEq<=(p->nKeyCol+1) ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2205 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2206 | assert( p->nSample>0 ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2207 | assert( pBuilder->nRecValid<nEq ); |
| 2208 | |
| 2209 | /* If values are not available for all fields of the index to the left |
| 2210 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 2211 | if( pBuilder->nRecValid<(nEq-1) ){ |
| 2212 | return SQLITE_NOTFOUND; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2213 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2214 | |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 2215 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 2216 | ** below would return the same value. */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2217 | if( nEq>p->nKeyCol ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2218 | *pnRow = 1; |
| 2219 | return SQLITE_OK; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2220 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2221 | |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2222 | aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity; |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2223 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); |
| 2224 | pBuilder->pRec = pRec; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2225 | if( rc!=SQLITE_OK ) return rc; |
| 2226 | if( bOk==0 ) return SQLITE_NOTFOUND; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2227 | pBuilder->nRecValid = nEq; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2228 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2229 | whereKeyStats(pParse, p, pRec, 0, a); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2230 | WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2231 | *pnRow = a[1]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2232 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2233 | return rc; |
| 2234 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2235 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2236 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2237 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2238 | /* |
| 2239 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2240 | ** an IN constraint where the right-hand side of the IN operator |
| 2241 | ** is a list of values. Example: |
| 2242 | ** |
| 2243 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2244 | ** |
| 2245 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2246 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2247 | ** non-zero. |
| 2248 | ** |
| 2249 | ** This routine can fail if it is unable to load a collating sequence |
| 2250 | ** required for string comparison, or if unable to allocate memory |
| 2251 | ** for a UTF conversion required for comparison. The error is stored |
| 2252 | ** in the pParse structure. |
| 2253 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2254 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2255 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2256 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2257 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2258 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2259 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2260 | Index *p = pBuilder->pNew->u.btree.pIndex; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2261 | i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2262 | int nRecValid = pBuilder->nRecValid; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2263 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2264 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2265 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2266 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2267 | |
| 2268 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2269 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2270 | nEst = nRow0; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2271 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2272 | nRowEst += nEst; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2273 | pBuilder->nRecValid = nRecValid; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2274 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2275 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2276 | if( rc==SQLITE_OK ){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2277 | if( nRowEst > nRow0 ) nRowEst = nRow0; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2278 | *pnRow = nRowEst; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2279 | WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2280 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2281 | assert( pBuilder->nRecValid==nRecValid ); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2282 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2283 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2284 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2285 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2286 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2287 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2288 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2289 | ** or USING clause of that join. |
| 2290 | ** |
| 2291 | ** Consider the term t2.z='ok' in the following queries: |
| 2292 | ** |
| 2293 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2294 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2295 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2296 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2297 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2298 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2299 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2300 | ** |
| 2301 | ** Disabling a term causes that term to not be tested in the inner loop |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 2302 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2303 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2304 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2305 | ** but joins might run a little slower. The trick is to disable as much |
| 2306 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2307 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2308 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2309 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2310 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2311 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2312 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 2313 | && (pLevel->notReady & pTerm->prereqAll)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2314 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2315 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2316 | if( pTerm->iParent>=0 ){ |
| 2317 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2318 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2319 | disableTerm(pLevel, pOther); |
| 2320 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2321 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2326 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2327 | ** to the n registers starting at base. |
| 2328 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2329 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2330 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2331 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2332 | ** |
| 2333 | ** This routine makes its own copy of zAff so that the caller is free |
| 2334 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2335 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2336 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2337 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2338 | if( zAff==0 ){ |
| 2339 | assert( pParse->db->mallocFailed ); |
| 2340 | return; |
| 2341 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2342 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2343 | |
| 2344 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2345 | ** and end of the affinity string. |
| 2346 | */ |
| 2347 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2348 | n--; |
| 2349 | base++; |
| 2350 | zAff++; |
| 2351 | } |
| 2352 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2353 | n--; |
| 2354 | } |
| 2355 | |
| 2356 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2357 | if( n>0 ){ |
| 2358 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2359 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2360 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2361 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2366 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2367 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2368 | ** coded. |
| 2369 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2370 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2371 | ** |
| 2372 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2373 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2374 | ** this routine sets up a loop that will iterate over all values of X. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2375 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2376 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2377 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2378 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2379 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2380 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2381 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2382 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2383 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2384 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2385 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2386 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2387 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2388 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2389 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2390 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2391 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2392 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2393 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2394 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2395 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2396 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2397 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2398 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2399 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2400 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2401 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2402 | && pLoop->u.btree.pIndex!=0 |
| 2403 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2404 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2405 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2406 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2407 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2408 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2409 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2410 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2411 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2412 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2413 | testcase( bRev ); |
| 2414 | bRev = !bRev; |
| 2415 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2416 | iTab = pX->iTable; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2417 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 2418 | VdbeCoverageIf(v, bRev); |
| 2419 | VdbeCoverageIf(v, !bRev); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2420 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2421 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2422 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2423 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2424 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2425 | pLevel->u.in.nIn++; |
| 2426 | pLevel->u.in.aInLoop = |
| 2427 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2428 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2429 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2430 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2431 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2432 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2433 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2434 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2435 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2436 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2437 | } |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 2438 | pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2439 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2440 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2441 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2442 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2443 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2444 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2445 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2446 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2449 | /* |
| 2450 | ** Generate code that will evaluate all == and IN constraints for an |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2451 | ** index scan. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2452 | ** |
| 2453 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2454 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2455 | ** The index has as many as three equality constraints, but in this |
| 2456 | ** example, the third "c" value is an inequality. So only two |
| 2457 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2458 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2459 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2460 | ** |
| 2461 | ** In the example above nEq==2. But this subroutine works for any value |
| 2462 | ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2463 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2464 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2465 | ** |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2466 | ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 2467 | ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 2468 | ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 2469 | ** occurs after the nEq quality constraints. |
| 2470 | ** |
| 2471 | ** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 2472 | ** the index of the first memory cell in that range. The code that |
| 2473 | ** calls this routine will use that memory range to store keys for |
| 2474 | ** start and termination conditions of the loop. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2475 | ** key value of the loop. If one or more IN operators appear, then |
| 2476 | ** this routine allocates an additional nEq memory cells for internal |
| 2477 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2478 | ** |
| 2479 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2480 | ** copy of the column affinity string of the index allocated using |
| 2481 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2482 | ** with equality constraints that use NONE affinity are set to |
| 2483 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2484 | ** |
| 2485 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2486 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2487 | ** |
| 2488 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2489 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2490 | ** no conversion should be attempted before using a t2.b value as part of |
| 2491 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2492 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2493 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2494 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2495 | Parse *pParse, /* Parsing context */ |
| 2496 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2497 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2498 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2499 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2500 | ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2501 | u16 nEq; /* The number of == or IN constraints to code */ |
| 2502 | u16 nSkip; /* Number of left-most columns to skip */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2503 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2504 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2505 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2506 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2507 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2508 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2509 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2510 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2511 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2512 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2513 | pLoop = pLevel->pWLoop; |
| 2514 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2515 | nEq = pLoop->u.btree.nEq; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2516 | nSkip = pLoop->u.btree.nSkip; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2517 | pIdx = pLoop->u.btree.pIndex; |
| 2518 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2519 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2520 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2521 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2522 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2523 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2524 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2525 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2526 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2527 | if( !zAff ){ |
| 2528 | pParse->db->mallocFailed = 1; |
| 2529 | } |
| 2530 | |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2531 | if( nSkip ){ |
| 2532 | int iIdxCur = pLevel->iIdxCur; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2533 | sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 2534 | VdbeCoverageIf(v, bRev==0); |
| 2535 | VdbeCoverageIf(v, bRev!=0); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 2536 | VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 2537 | j = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 2538 | pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2539 | iIdxCur, 0, regBase, nSkip); |
| 2540 | VdbeCoverageIf(v, bRev==0); |
| 2541 | VdbeCoverageIf(v, bRev!=0); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 2542 | sqlite3VdbeJumpHere(v, j); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2543 | for(j=0; j<nSkip; j++){ |
| 2544 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 2545 | assert( pIdx->aiColumn[j]>=0 ); |
| 2546 | VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); |
| 2547 | } |
| 2548 | } |
| 2549 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2550 | /* Evaluate the equality constraints |
| 2551 | */ |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 2552 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2553 | for(j=nSkip; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2554 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2555 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2556 | assert( pTerm!=0 ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2557 | /* The following testcase is true for indices with redundant columns. |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2558 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2559 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 2560 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2561 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2562 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2563 | if( nReg==1 ){ |
| 2564 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2565 | regBase = r1; |
| 2566 | }else{ |
| 2567 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2568 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2569 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2570 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2571 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2572 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2573 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2574 | if( sqlite3ExprCanBeNull(pRight) ){ |
| 2575 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 2576 | VdbeCoverage(v); |
| 2577 | } |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2578 | if( zAff ){ |
| 2579 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2580 | zAff[j] = SQLITE_AFF_NONE; |
| 2581 | } |
| 2582 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2583 | zAff[j] = SQLITE_AFF_NONE; |
| 2584 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2585 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2586 | } |
| 2587 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2588 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2589 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2592 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2593 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2594 | ** This routine is a helper for explainIndexRange() below |
| 2595 | ** |
| 2596 | ** pStr holds the text of an expression that we are building up one term |
| 2597 | ** at a time. This routine adds a new term to the end of the expression. |
| 2598 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 2599 | ** terms only. |
| 2600 | */ |
| 2601 | static void explainAppendTerm( |
| 2602 | StrAccum *pStr, /* The text expression being built */ |
| 2603 | int iTerm, /* Index of this term. First is zero */ |
| 2604 | const char *zColumn, /* Name of the column */ |
| 2605 | const char *zOp /* Name of the operator */ |
| 2606 | ){ |
| 2607 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 2608 | sqlite3StrAccumAppendAll(pStr, zColumn); |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2609 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 2610 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 2611 | } |
| 2612 | |
| 2613 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2614 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 2615 | ** function returns a pointer to a string buffer containing a description |
| 2616 | ** of the subset of table rows scanned by the strategy in the form of an |
| 2617 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 2618 | ** |
| 2619 | ** For example, if the query: |
| 2620 | ** |
| 2621 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 2622 | ** |
| 2623 | ** is run and there is an index on (a, b), then this function returns a |
| 2624 | ** string similar to: |
| 2625 | ** |
| 2626 | ** "a=? AND b>?" |
| 2627 | ** |
| 2628 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 2629 | ** It is the responsibility of the caller to free the buffer when it is |
| 2630 | ** no longer required. |
| 2631 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2632 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 2633 | Index *pIndex = pLoop->u.btree.pIndex; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2634 | u16 nEq = pLoop->u.btree.nEq; |
| 2635 | u16 nSkip = pLoop->u.btree.nSkip; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2636 | int i, j; |
| 2637 | Column *aCol = pTab->aCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2638 | i16 *aiColumn = pIndex->aiColumn; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2639 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2640 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2641 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2642 | return 0; |
| 2643 | } |
| 2644 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 2645 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2646 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2647 | for(i=0; i<nEq; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2648 | char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2649 | if( i>=nSkip ){ |
| 2650 | explainAppendTerm(&txt, i, z, "="); |
| 2651 | }else{ |
| 2652 | if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5); |
| 2653 | sqlite3StrAccumAppend(&txt, "ANY(", 4); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 2654 | sqlite3StrAccumAppendAll(&txt, z); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2655 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 2656 | } |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2659 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2660 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2661 | char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 2662 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2663 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2664 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2665 | char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 2666 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2667 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2668 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 2669 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2672 | /* |
| 2673 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 2674 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 2675 | ** record is added to the output to describe the table scan strategy in |
| 2676 | ** pLevel. |
| 2677 | */ |
| 2678 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2679 | Parse *pParse, /* Parse context */ |
| 2680 | SrcList *pTabList, /* Table list this loop refers to */ |
| 2681 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 2682 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2683 | int iFrom, /* Value for "from" column of output */ |
| 2684 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2685 | ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 2686 | #ifndef SQLITE_DEBUG |
| 2687 | if( pParse->explain==2 ) |
| 2688 | #endif |
| 2689 | { |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2690 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2691 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 2692 | sqlite3 *db = pParse->db; /* Database handle */ |
| 2693 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2694 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2695 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2696 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 2697 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2698 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2699 | pLoop = pLevel->pWLoop; |
| 2700 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2701 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2702 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2703 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 2704 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 2705 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2706 | |
| 2707 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2708 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2709 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2710 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2711 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2714 | if( pItem->zAlias ){ |
| 2715 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 2716 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2717 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 2718 | && ALWAYS(pLoop->u.btree.pIndex!=0) |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2719 | ){ |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2720 | const char *zFmt; |
| 2721 | Index *pIdx = pLoop->u.btree.pIndex; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2722 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2723 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2724 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2725 | zFmt = zWhere ? "%s USING PRIMARY KEY%.0s%s" : "%s%.0s%s"; |
| 2726 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 2727 | zFmt = "%s USING AUTOMATIC COVERING INDEX%.0s%s"; |
| 2728 | }else if( flags & WHERE_IDX_ONLY ){ |
| 2729 | zFmt = "%s USING COVERING INDEX %s%s"; |
| 2730 | }else{ |
| 2731 | zFmt = "%s USING INDEX %s%s"; |
| 2732 | } |
| 2733 | zMsg = sqlite3MAppendf(db, zMsg, zFmt, zMsg, pIdx->zName, zWhere); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2734 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 2735 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2736 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2737 | |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 2738 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2739 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 2740 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2741 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 2742 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 2743 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 2744 | }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2745 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 2746 | } |
| 2747 | } |
| 2748 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2749 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2750 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2751 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2752 | } |
| 2753 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2754 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2755 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2759 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2760 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 2761 | |
| 2762 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2763 | /* |
| 2764 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 2765 | ** implementation described by pWInfo. |
| 2766 | */ |
| 2767 | static Bitmask codeOneLoopStart( |
| 2768 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 2769 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 2770 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2771 | ){ |
| 2772 | int j, k; /* Loop counters */ |
| 2773 | int iCur; /* The VDBE cursor for the table */ |
| 2774 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 2775 | int omitTable; /* True if we use the index only */ |
| 2776 | int bRev; /* True if we need to scan in reverse order */ |
| 2777 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2778 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2779 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 2780 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 2781 | Parse *pParse; /* Parsing context */ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 2782 | sqlite3 *db; /* Database connection */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2783 | Vdbe *v; /* The prepared stmt under constructions */ |
| 2784 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 2785 | int addrBrk; /* Jump here to break out of the loop */ |
| 2786 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 2787 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 2788 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2789 | |
| 2790 | pParse = pWInfo->pParse; |
| 2791 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 2792 | pWC = &pWInfo->sWC; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 2793 | db = pParse->db; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2794 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2795 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2796 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 2797 | iCur = pTabItem->iCursor; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 2798 | pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2799 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 2800 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 2801 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 2802 | VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2803 | |
| 2804 | /* Create labels for the "break" and "continue" instructions |
| 2805 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 2806 | ** Jump to cont to go immediately to the next iteration of the |
| 2807 | ** loop. |
| 2808 | ** |
| 2809 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 2810 | ** means to continue with the next IN value combination. When |
| 2811 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 2812 | ** is the same as "addrBrk". |
| 2813 | */ |
| 2814 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 2815 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 2816 | |
| 2817 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 2818 | ** initialize a memory cell that records if this table matches any |
| 2819 | ** row of the left table of the join. |
| 2820 | */ |
| 2821 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 2822 | pLevel->iLeftJoin = ++pParse->nMem; |
| 2823 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 2824 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 2825 | } |
| 2826 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 2827 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 2828 | if( pTabItem->viaCoroutine ){ |
| 2829 | int regYield = pTabItem->regReturn; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 2830 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 2831 | pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2832 | VdbeCoverage(v); |
drh | 725de29 | 2014-02-08 13:12:19 +0000 | [diff] [blame] | 2833 | VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 2834 | pLevel->op = OP_Goto; |
| 2835 | }else |
| 2836 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2837 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2838 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 2839 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2840 | ** to access the data. |
| 2841 | */ |
| 2842 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2843 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2844 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2845 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 2846 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2847 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2848 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2849 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 2850 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2851 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 2852 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2853 | if( pTerm->eOperator & WO_IN ){ |
| 2854 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 2855 | addrNotFound = pLevel->addrNxt; |
| 2856 | }else{ |
| 2857 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 2858 | } |
| 2859 | } |
| 2860 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 2861 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2862 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 2863 | pLoop->u.vtab.idxStr, |
| 2864 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2865 | VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2866 | pLoop->u.vtab.needFree = 0; |
| 2867 | for(j=0; j<nConstraint && j<16; j++){ |
| 2868 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2869 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2870 | } |
| 2871 | } |
| 2872 | pLevel->op = OP_VNext; |
| 2873 | pLevel->p1 = iCur; |
| 2874 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 2875 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | d249090 | 2014-04-13 19:28:15 +0000 | [diff] [blame] | 2876 | sqlite3ExprCachePop(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2877 | }else |
| 2878 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2879 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2880 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 2881 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 2882 | ){ |
| 2883 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2884 | ** equality comparison against the ROWID field. Or |
| 2885 | ** we reference multiple rows using a "rowid IN (...)" |
| 2886 | ** construct. |
| 2887 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2888 | assert( pLoop->u.btree.nEq==1 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2889 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2890 | assert( pTerm!=0 ); |
| 2891 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2892 | assert( omitTable==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 2893 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 2894 | iReleaseReg = ++pParse->nMem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2895 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 2896 | if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2897 | addrNxt = pLevel->addrNxt; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2898 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2899 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2900 | VdbeCoverage(v); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 2901 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 2902 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2903 | VdbeComment((v, "pk")); |
| 2904 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2905 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 2906 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 2907 | ){ |
| 2908 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2909 | */ |
| 2910 | int testOp = OP_Noop; |
| 2911 | int start; |
| 2912 | int memEndValue = 0; |
| 2913 | WhereTerm *pStart, *pEnd; |
| 2914 | |
| 2915 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2916 | j = 0; |
| 2917 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2918 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 2919 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 2920 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2921 | if( bRev ){ |
| 2922 | pTerm = pStart; |
| 2923 | pStart = pEnd; |
| 2924 | pEnd = pTerm; |
| 2925 | } |
| 2926 | if( pStart ){ |
| 2927 | Expr *pX; /* The expression that defines the start bound */ |
| 2928 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 2929 | |
| 2930 | /* The following constant maps TK_xx codes into corresponding |
| 2931 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 2932 | */ |
| 2933 | const u8 aMoveOp[] = { |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 2934 | /* TK_GT */ OP_SeekGT, |
| 2935 | /* TK_LE */ OP_SeekLE, |
| 2936 | /* TK_LT */ OP_SeekLT, |
| 2937 | /* TK_GE */ OP_SeekGE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2938 | }; |
| 2939 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 2940 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 2941 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 2942 | |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 2943 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 2944 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2945 | pX = pStart->pExpr; |
| 2946 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 2947 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2948 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 2949 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2950 | VdbeComment((v, "pk")); |
| 2951 | VdbeCoverageIf(v, pX->op==TK_GT); |
| 2952 | VdbeCoverageIf(v, pX->op==TK_LE); |
| 2953 | VdbeCoverageIf(v, pX->op==TK_LT); |
| 2954 | VdbeCoverageIf(v, pX->op==TK_GE); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2955 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 2956 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 2957 | disableTerm(pLevel, pStart); |
| 2958 | }else{ |
| 2959 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2960 | VdbeCoverageIf(v, bRev==0); |
| 2961 | VdbeCoverageIf(v, bRev!=0); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2962 | } |
| 2963 | if( pEnd ){ |
| 2964 | Expr *pX; |
| 2965 | pX = pEnd->pExpr; |
| 2966 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 2967 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 2968 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 2969 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2970 | memEndValue = ++pParse->nMem; |
| 2971 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 2972 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 2973 | testOp = bRev ? OP_Le : OP_Ge; |
| 2974 | }else{ |
| 2975 | testOp = bRev ? OP_Lt : OP_Gt; |
| 2976 | } |
| 2977 | disableTerm(pLevel, pEnd); |
| 2978 | } |
| 2979 | start = sqlite3VdbeCurrentAddr(v); |
| 2980 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 2981 | pLevel->p1 = iCur; |
| 2982 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 2983 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2984 | if( testOp!=OP_Noop ){ |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 2985 | iRowidReg = ++pParse->nMem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2986 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 2987 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2988 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2989 | VdbeCoverageIf(v, testOp==OP_Le); |
| 2990 | VdbeCoverageIf(v, testOp==OP_Lt); |
| 2991 | VdbeCoverageIf(v, testOp==OP_Ge); |
| 2992 | VdbeCoverageIf(v, testOp==OP_Gt); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2993 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2994 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 2995 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2996 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2997 | ** |
| 2998 | ** The WHERE clause may contain zero or more equality |
| 2999 | ** terms ("==" or "IN" operators) that refer to the N |
| 3000 | ** left-most columns of the index. It may also contain |
| 3001 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3002 | ** column that immediately follows the N equalities. Only |
| 3003 | ** the right-most column can be an inequality - the rest must |
| 3004 | ** use the "==" and "IN" operators. For example, if the |
| 3005 | ** index is on (x,y,z), then the following clauses are all |
| 3006 | ** optimized: |
| 3007 | ** |
| 3008 | ** x=5 |
| 3009 | ** x=5 AND y=10 |
| 3010 | ** x=5 AND y<10 |
| 3011 | ** x=5 AND y>5 AND y<10 |
| 3012 | ** x=5 AND y=5 AND z<=10 |
| 3013 | ** |
| 3014 | ** The z<10 term of the following cannot be used, only |
| 3015 | ** the x=5 term: |
| 3016 | ** |
| 3017 | ** x=5 AND z<10 |
| 3018 | ** |
| 3019 | ** N may be zero if there are inequality constraints. |
| 3020 | ** If there are no inequality constraints, then N is at |
| 3021 | ** least one. |
| 3022 | ** |
| 3023 | ** This case is also used when there are no WHERE clause |
| 3024 | ** constraints but an index is selected anyway, in order |
| 3025 | ** to force the output order to conform to an ORDER BY. |
| 3026 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3027 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3028 | 0, |
| 3029 | 0, |
| 3030 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3031 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3032 | OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3033 | OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 3034 | OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 3035 | OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3036 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3037 | static const u8 aEndOp[] = { |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3038 | OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 3039 | OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 3040 | OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 3041 | OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3042 | }; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3043 | u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3044 | int regBase; /* Base register holding constraint values */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3045 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3046 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3047 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3048 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3049 | int start_constraints; /* Start of range is constrained */ |
| 3050 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3051 | Index *pIdx; /* The index we will be using */ |
| 3052 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3053 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3054 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3055 | char *zStartAff; /* Affinity for start of range constraint */ |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3056 | char cEndAff = 0; /* Affinity for end of range constraint */ |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3057 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 3058 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3059 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3060 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3061 | iIdxCur = pLevel->iIdxCur; |
drh | 052e6a8 | 2013-11-14 19:34:10 +0000 | [diff] [blame] | 3062 | assert( nEq>=pLoop->u.btree.nSkip ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3063 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3064 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3065 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3066 | ** query, then the caller will only allow the loop to run for |
| 3067 | ** a single iteration. This means that the first row returned |
| 3068 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3069 | ** the first one after the nEq equality constraints in the index, |
| 3070 | ** this requires some special handling. |
| 3071 | */ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 3072 | assert( pWInfo->pOrderBy==0 |
| 3073 | || pWInfo->pOrderBy->nExpr==1 |
| 3074 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3075 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 3076 | && pWInfo->nOBSat>0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3077 | && (pIdx->nKeyCol>nEq) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3078 | ){ |
drh | 052e6a8 | 2013-11-14 19:34:10 +0000 | [diff] [blame] | 3079 | assert( pLoop->u.btree.nSkip==0 ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3080 | bSeekPastNull = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3081 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | /* Find any inequality constraint terms for the start and end |
| 3085 | ** of the range. |
| 3086 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3087 | j = nEq; |
| 3088 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3089 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3090 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3091 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3092 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3093 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3094 | nExtraReg = 1; |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3095 | if( pRangeStart==0 |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3096 | && (j = pIdx->aiColumn[nEq])>=0 |
| 3097 | && pIdx->pTable->aCol[j].notNull==0 |
| 3098 | ){ |
| 3099 | bSeekPastNull = 1; |
| 3100 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3101 | } |
dan | 0df163a | 2014-03-06 12:36:26 +0000 | [diff] [blame] | 3102 | assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3103 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3104 | /* Generate code to evaluate all constraint terms using == or IN |
| 3105 | ** and store the values of those terms in an array of registers |
| 3106 | ** starting at regBase. |
| 3107 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3108 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3109 | assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 3110 | if( zStartAff ) cEndAff = zStartAff[nEq]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3111 | addrNxt = pLevel->addrNxt; |
| 3112 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3113 | /* If we are doing a reverse order scan on an ascending index, or |
| 3114 | ** a forward order scan on a descending index, interchange the |
| 3115 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3116 | */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3117 | if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3118 | || (bRev && pIdx->nKeyCol==nEq) |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3119 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3120 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3121 | SWAP(u8, bSeekPastNull, bStopAtNull); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3124 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3125 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3126 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3127 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3128 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3129 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3130 | start_constraints = pRangeStart || nEq>0; |
| 3131 | |
| 3132 | /* Seek the index cursor to the start of the range. */ |
| 3133 | nConstraint = nEq; |
| 3134 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3135 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3136 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3137 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 3138 | && sqlite3ExprCanBeNull(pRight) |
| 3139 | ){ |
| 3140 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 3141 | VdbeCoverage(v); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3142 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3143 | if( zStartAff ){ |
| 3144 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3145 | /* Since the comparison is to be performed with no conversions |
| 3146 | ** applied to the operands, set the affinity to apply to pRight to |
| 3147 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3148 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3149 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3150 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3151 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3152 | } |
| 3153 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3154 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3155 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3156 | }else if( bSeekPastNull ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3157 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3158 | nConstraint++; |
| 3159 | startEq = 0; |
| 3160 | start_constraints = 1; |
| 3161 | } |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3162 | codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3163 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3164 | assert( op!=0 ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3165 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3166 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3167 | VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 3168 | VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 3169 | VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 3170 | VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 3171 | VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 3172 | VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3173 | |
| 3174 | /* Load the value for the inequality constraint at the end of the |
| 3175 | ** range (if any). |
| 3176 | */ |
| 3177 | nConstraint = nEq; |
| 3178 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3179 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3180 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3181 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3182 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 3183 | && sqlite3ExprCanBeNull(pRight) |
| 3184 | ){ |
| 3185 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 3186 | VdbeCoverage(v); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3187 | } |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3188 | if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE |
| 3189 | && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) |
| 3190 | ){ |
| 3191 | codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); |
| 3192 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3193 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3194 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3195 | }else if( bStopAtNull ){ |
| 3196 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3197 | endEq = 0; |
| 3198 | nConstraint++; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3199 | } |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3200 | sqlite3DbFree(db, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3201 | |
| 3202 | /* Top of the loop body */ |
| 3203 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3204 | |
| 3205 | /* Check if the index cursor is past the end of the range. */ |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3206 | if( nConstraint ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3207 | op = aEndOp[bRev*2 + endEq]; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3208 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3209 | testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 3210 | testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 3211 | testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 3212 | testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3213 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3214 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3215 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3216 | disableTerm(pLevel, pRangeStart); |
| 3217 | disableTerm(pLevel, pRangeEnd); |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3218 | if( omitTable ){ |
| 3219 | /* pIdx is a covering index. No need to access the main table. */ |
| 3220 | }else if( HasRowid(pIdx->pTable) ){ |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3221 | iRowidReg = ++pParse->nMem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3222 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3223 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3224 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 3225 | }else if( iCur!=iIdxCur ){ |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3226 | Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 3227 | iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 3228 | for(j=0; j<pPk->nKeyCol; j++){ |
| 3229 | k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 3230 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 3231 | } |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 3232 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3233 | iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3234 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3235 | |
| 3236 | /* Record the instruction used to terminate the loop. Disable |
| 3237 | ** WHERE clause terms made redundant by the index range scan. |
| 3238 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3239 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3240 | pLevel->op = OP_Noop; |
| 3241 | }else if( bRev ){ |
| 3242 | pLevel->op = OP_Prev; |
| 3243 | }else{ |
| 3244 | pLevel->op = OP_Next; |
| 3245 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3246 | pLevel->p1 = iIdxCur; |
drh | 0c8a934 | 2014-03-20 12:17:35 +0000 | [diff] [blame] | 3247 | pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3248 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3249 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3250 | }else{ |
| 3251 | assert( pLevel->p5==0 ); |
| 3252 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3253 | }else |
| 3254 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3255 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3256 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3257 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3258 | ** |
| 3259 | ** Example: |
| 3260 | ** |
| 3261 | ** CREATE TABLE t1(a,b,c,d); |
| 3262 | ** CREATE INDEX i1 ON t1(a); |
| 3263 | ** CREATE INDEX i2 ON t1(b); |
| 3264 | ** CREATE INDEX i3 ON t1(c); |
| 3265 | ** |
| 3266 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3267 | ** |
| 3268 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3269 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3270 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3271 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3272 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3273 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3274 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3275 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3276 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3277 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3278 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3279 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3280 | ** Gosub 2 A |
| 3281 | ** sqlite3WhereEnd() |
| 3282 | ** |
| 3283 | ** Following the above, code to terminate the loop. Label A, the target |
| 3284 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3285 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3286 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3287 | ** Goto B # The loop is finished. |
| 3288 | ** |
| 3289 | ** A: <loop body> # Return data, whatever. |
| 3290 | ** |
| 3291 | ** Return 2 # Jump back to the Gosub |
| 3292 | ** |
| 3293 | ** B: <after the loop> |
| 3294 | ** |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3295 | ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
| 3296 | ** use an ephermeral index instead of a RowSet to record the primary |
| 3297 | ** keys of the rows we have already seen. |
| 3298 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3299 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3300 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3301 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3302 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3303 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3304 | |
| 3305 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3306 | int regRowset = 0; /* Register for RowSet object */ |
| 3307 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3308 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3309 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3310 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3311 | int ii; /* Loop counter */ |
| 3312 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3313 | Table *pTab = pTabItem->pTab; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3314 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3315 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3316 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3317 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3318 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3319 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3320 | pLevel->op = OP_Return; |
| 3321 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3322 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3323 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3324 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3325 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3326 | */ |
| 3327 | if( pWInfo->nLevel>1 ){ |
| 3328 | int nNotReady; /* The number of notReady tables */ |
| 3329 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3330 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3331 | pOrTab = sqlite3StackAllocRaw(db, |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3332 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3333 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3334 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3335 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3336 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3337 | origSrc = pWInfo->pTabList->a; |
| 3338 | for(k=1; k<=nNotReady; k++){ |
| 3339 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3340 | } |
| 3341 | }else{ |
| 3342 | pOrTab = pWInfo->pTabList; |
| 3343 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3344 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3345 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3346 | ** equivalent to an empty rowset. Or, create an ephermeral index |
| 3347 | ** capable of holding primary keys in the case of a WITHOUT ROWID. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3348 | ** |
| 3349 | ** Also initialize regReturn to contain the address of the instruction |
| 3350 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3351 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3352 | ** over the top of the loop into the body of it. In this case the |
| 3353 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3354 | ** fall through to the next instruction, just as an OP_Next does if |
| 3355 | ** called on an uninitialized cursor. |
| 3356 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3357 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3358 | if( HasRowid(pTab) ){ |
| 3359 | regRowset = ++pParse->nMem; |
| 3360 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3361 | }else{ |
| 3362 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 3363 | regRowset = pParse->nTab++; |
| 3364 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 3365 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 3366 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3367 | regRowid = ++pParse->nMem; |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3368 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3369 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3370 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3371 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3372 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3373 | ** That way, terms in y that are factored into the disjunction will |
| 3374 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3375 | ** |
| 3376 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3377 | ** the "interesting" terms of z - terms that did not originate in the |
| 3378 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3379 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3380 | ** |
| 3381 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3382 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3383 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3384 | */ |
| 3385 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3386 | int iTerm; |
| 3387 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3388 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3389 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3390 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 7c32806 | 2014-02-11 01:50:29 +0000 | [diff] [blame] | 3391 | testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 3392 | testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 3393 | if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3394 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3395 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 3396 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3397 | } |
| 3398 | if( pAndExpr ){ |
| 3399 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3400 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3403 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 3404 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 3405 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 3406 | */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3407 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3408 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3409 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3410 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 3411 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| 3412 | int j1 = 0; /* Address of jump operation */ |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3413 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3414 | pAndExpr->pLeft = pOrExpr; |
| 3415 | pOrExpr = pAndExpr; |
| 3416 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3417 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3418 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3419 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3420 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3421 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3422 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3423 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3424 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3425 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3426 | ); |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3427 | /* This is the sub-WHERE clause body. First skip over |
| 3428 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 3429 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 3430 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 3431 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3432 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3433 | int r; |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3434 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3435 | if( HasRowid(pTab) ){ |
| 3436 | r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3437 | j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet); |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3438 | VdbeCoverage(v); |
| 3439 | }else{ |
| 3440 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 3441 | int nPk = pPk->nKeyCol; |
| 3442 | int iPk; |
| 3443 | |
| 3444 | /* Read the PK into an array of temp registers. */ |
| 3445 | r = sqlite3GetTempRange(pParse, nPk); |
| 3446 | for(iPk=0; iPk<nPk; iPk++){ |
| 3447 | int iCol = pPk->aiColumn[iPk]; |
| 3448 | sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0); |
| 3449 | } |
| 3450 | |
| 3451 | /* Check if the temp table already contains this key. If so, |
| 3452 | ** the row has already been included in the result set and |
| 3453 | ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 3454 | ** insert the key into the temp table and proceed with processing |
| 3455 | ** the row. |
| 3456 | ** |
| 3457 | ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 3458 | ** is zero, assume that the key cannot already be present in |
| 3459 | ** the temp table. And if iSet is -1, assume that there is no |
| 3460 | ** need to insert the key into the temp table, as it will never |
| 3461 | ** be tested for. */ |
| 3462 | if( iSet ){ |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3463 | j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
drh | 68c1215 | 2014-05-26 20:25:34 +0000 | [diff] [blame] | 3464 | VdbeCoverage(v); |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3465 | } |
| 3466 | if( iSet>=0 ){ |
| 3467 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 3468 | sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); |
| 3469 | if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 3470 | } |
| 3471 | |
| 3472 | /* Release the array of temp registers */ |
| 3473 | sqlite3ReleaseTempRange(pParse, r, nPk); |
| 3474 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3475 | } |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3476 | |
| 3477 | /* Invoke the main loop body as a subroutine */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3478 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3479 | |
| 3480 | /* Jump here (skipping the main loop body subroutine) if the |
| 3481 | ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3482 | if( j1 ) sqlite3VdbeJumpHere(v, j1); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3483 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3484 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3485 | ** contained one or more AND term from a notReady table. The |
| 3486 | ** terms from the notReady table could not be tested and will |
| 3487 | ** need to be tested later. |
| 3488 | */ |
| 3489 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3490 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3491 | /* If all of the OR-connected terms are optimized using the same |
| 3492 | ** index, and the index is opened using the same cursor number |
| 3493 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3494 | ** be possible to use that index as a covering index. |
| 3495 | ** |
| 3496 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3497 | ** uses an index, and this is either the first OR-connected term |
| 3498 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3499 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3500 | ** pCov to NULL to indicate that no candidate covering index will |
| 3501 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3502 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3503 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3504 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3505 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3506 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 3507 | && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3508 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3509 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3510 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3511 | }else{ |
| 3512 | pCov = 0; |
| 3513 | } |
| 3514 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3515 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3516 | sqlite3WhereEnd(pSubWInfo); |
| 3517 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3518 | } |
| 3519 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3520 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3521 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3522 | if( pAndExpr ){ |
| 3523 | pAndExpr->pLeft = 0; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3524 | sqlite3ExprDelete(db, pAndExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3525 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3526 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3527 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3528 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3529 | |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3530 | if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3531 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3532 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3533 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3534 | |
| 3535 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3536 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3537 | ** scan of the entire table. |
| 3538 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3539 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3540 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3541 | assert( bRev==0 || bRev==1 ); |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3542 | if( pTabItem->isRecursive ){ |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 3543 | /* Tables marked isRecursive have only a single row that is stored in |
dan | 4102815 | 2014-01-22 10:22:25 +0000 | [diff] [blame] | 3544 | ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3545 | pLevel->op = OP_Noop; |
| 3546 | }else{ |
| 3547 | pLevel->op = aStep[bRev]; |
| 3548 | pLevel->p1 = iCur; |
| 3549 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3550 | VdbeCoverageIf(v, bRev==0); |
| 3551 | VdbeCoverageIf(v, bRev!=0); |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3552 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3553 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3554 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3555 | |
| 3556 | /* Insert code to test every subexpression that can be completely |
| 3557 | ** computed using the current set of tables. |
| 3558 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3559 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3560 | Expr *pE; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3561 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3562 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3563 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3564 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3565 | testcase( pWInfo->untestedTerms==0 |
| 3566 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3567 | pWInfo->untestedTerms = 1; |
| 3568 | continue; |
| 3569 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3570 | pE = pTerm->pExpr; |
| 3571 | assert( pE!=0 ); |
| 3572 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3573 | continue; |
| 3574 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3575 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3576 | pTerm->wtFlags |= TERM_CODED; |
| 3577 | } |
| 3578 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3579 | /* Insert code to test for implied constraints based on transitivity |
| 3580 | ** of the "==" operator. |
| 3581 | ** |
| 3582 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3583 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3584 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3585 | ** the implied "t1.a=123" constraint. |
| 3586 | */ |
| 3587 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3588 | Expr *pE, *pEAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3589 | WhereTerm *pAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3590 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3591 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3592 | if( pTerm->leftCursor!=iCur ) continue; |
drh | cdc2e43 | 2013-07-01 17:27:19 +0000 | [diff] [blame] | 3593 | if( pLevel->iLeftJoin ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3594 | pE = pTerm->pExpr; |
| 3595 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3596 | assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3597 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3598 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3599 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3600 | testcase( pAlt->eOperator & WO_EQ ); |
| 3601 | testcase( pAlt->eOperator & WO_IN ); |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 3602 | VdbeModuleComment((v, "begin transitive constraint")); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3603 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 3604 | if( pEAlt ){ |
| 3605 | *pEAlt = *pAlt->pExpr; |
| 3606 | pEAlt->pLeft = pE->pLeft; |
| 3607 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 3608 | sqlite3StackFree(db, pEAlt); |
| 3609 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3612 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3613 | ** at least one row of the right table has matched the left table. |
| 3614 | */ |
| 3615 | if( pLevel->iLeftJoin ){ |
| 3616 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3617 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3618 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3619 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3620 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3621 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3622 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3623 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3624 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3625 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3626 | continue; |
| 3627 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3628 | assert( pTerm->pExpr ); |
| 3629 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3630 | pTerm->wtFlags |= TERM_CODED; |
| 3631 | } |
| 3632 | } |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3633 | |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3634 | return pLevel->notReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 3637 | #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 3638 | /* |
| 3639 | ** Generate "Explanation" text for a WhereTerm. |
| 3640 | */ |
| 3641 | static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){ |
| 3642 | char zType[4]; |
| 3643 | memcpy(zType, "...", 4); |
| 3644 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 3645 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 3646 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 3647 | sqlite3ExplainPrintf(v, "%s ", zType); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 3648 | sqlite3ExplainExpr(v, pTerm->pExpr); |
| 3649 | } |
| 3650 | #endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */ |
| 3651 | |
| 3652 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3653 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3654 | /* |
| 3655 | ** Print a WhereLoop object for debugging purposes |
| 3656 | */ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3657 | static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 3658 | WhereInfo *pWInfo = pWC->pWInfo; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3659 | int nb = 1+(pWInfo->pTabList->nSrc+7)/8; |
| 3660 | struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3661 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3662 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3663 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3664 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3665 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3666 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3667 | const char *zName; |
| 3668 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3669 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3670 | int i = sqlite3Strlen30(zName) - 1; |
| 3671 | while( zName[i]!='_' ) i--; |
| 3672 | zName += i; |
| 3673 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3674 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3675 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3676 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3677 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3678 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3679 | char *z; |
| 3680 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3681 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3682 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3683 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3684 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3685 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3686 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3687 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3688 | } |
drh | 0ace74a | 2014-06-16 21:30:29 +0000 | [diff] [blame] | 3689 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3690 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3691 | #ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 3692 | /* If the 0x100 bit of wheretracing is set, then show all of the constraint |
| 3693 | ** expressions in the WhereLoop.aLTerm[] array. |
| 3694 | */ |
| 3695 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ /* WHERETRACE 0x100 */ |
| 3696 | int i; |
| 3697 | Vdbe *v = pWInfo->pParse->pVdbe; |
| 3698 | sqlite3ExplainBegin(v); |
| 3699 | for(i=0; i<p->nLTerm; i++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3700 | WhereTerm *pTerm = p->aLTerm[i]; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3701 | if( pTerm==0 ) continue; |
drh | 7afc8b0 | 2013-10-28 22:33:36 +0000 | [diff] [blame] | 3702 | sqlite3ExplainPrintf(v, " (%d) #%-2d ", i+1, (int)(pTerm-pWC->a)); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3703 | sqlite3ExplainPush(v); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 3704 | whereExplainTerm(v, pTerm); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3705 | sqlite3ExplainPop(v); |
| 3706 | sqlite3ExplainNL(v); |
| 3707 | } |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3708 | sqlite3ExplainFinish(v); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3709 | sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3710 | } |
| 3711 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3712 | } |
| 3713 | #endif |
| 3714 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3715 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3716 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 3717 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3718 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3719 | static void whereLoopInit(WhereLoop *p){ |
| 3720 | p->aLTerm = p->aLTermSpace; |
| 3721 | p->nLTerm = 0; |
| 3722 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 3723 | p->wsFlags = 0; |
| 3724 | } |
| 3725 | |
| 3726 | /* |
| 3727 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 3728 | */ |
| 3729 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3730 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3731 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 3732 | sqlite3_free(p->u.vtab.idxStr); |
| 3733 | p->u.vtab.needFree = 0; |
| 3734 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3735 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3736 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3737 | sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo); |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3738 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 3739 | p->u.btree.pIndex = 0; |
| 3740 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3744 | /* |
| 3745 | ** Deallocate internal memory used by a WhereLoop object |
| 3746 | */ |
| 3747 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 3748 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3749 | whereLoopClearUnion(db, p); |
| 3750 | whereLoopInit(p); |
| 3751 | } |
| 3752 | |
| 3753 | /* |
| 3754 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 3755 | */ |
| 3756 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 3757 | WhereTerm **paNew; |
| 3758 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 3759 | n = (n+7)&~7; |
| 3760 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 3761 | if( paNew==0 ) return SQLITE_NOMEM; |
| 3762 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 3763 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3764 | p->aLTerm = paNew; |
| 3765 | p->nLSlot = n; |
| 3766 | return SQLITE_OK; |
| 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | ** Transfer content from the second pLoop into the first. |
| 3771 | */ |
| 3772 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3773 | whereLoopClearUnion(db, pTo); |
drh | 0d31dc3 | 2013-09-06 00:40:59 +0000 | [diff] [blame] | 3774 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 3775 | memset(&pTo->u, 0, sizeof(pTo->u)); |
| 3776 | return SQLITE_NOMEM; |
| 3777 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 3778 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 3779 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3780 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 3781 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3782 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3783 | pFrom->u.btree.pIndex = 0; |
| 3784 | } |
| 3785 | return SQLITE_OK; |
| 3786 | } |
| 3787 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3788 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3789 | ** Delete a WhereLoop object |
| 3790 | */ |
| 3791 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3792 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3793 | sqlite3DbFree(db, p); |
| 3794 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 3795 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3796 | /* |
| 3797 | ** Free a WhereInfo structure |
| 3798 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 3799 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 3800 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3801 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3802 | while( pWInfo->pLoops ){ |
| 3803 | WhereLoop *p = pWInfo->pLoops; |
| 3804 | pWInfo->pLoops = p->pNextLoop; |
| 3805 | whereLoopDelete(db, p); |
| 3806 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3807 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3811 | /* |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3812 | ** Return TRUE if both of the following are true: |
| 3813 | ** |
| 3814 | ** (1) X has the same or lower cost that Y |
| 3815 | ** (2) X is a proper subset of Y |
| 3816 | ** |
| 3817 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 3818 | ** than Y and that every WHERE clause term used by X is also used |
| 3819 | ** by Y. |
| 3820 | ** |
| 3821 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 3822 | ** to have a lower cost. This routine returns TRUE when that cost |
| 3823 | ** relationship is inverted and needs to be adjusted. |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3824 | */ |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3825 | static int whereLoopCheaperProperSubset( |
| 3826 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 3827 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 3828 | ){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3829 | int i, j; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3830 | if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */ |
| 3831 | if( pX->rRun >= pY->rRun ){ |
| 3832 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 3833 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3834 | } |
drh | 9ee8810 | 2014-05-07 20:33:17 +0000 | [diff] [blame] | 3835 | for(i=pX->nLTerm-1; i>=0; i--){ |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3836 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 3837 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 3838 | } |
| 3839 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 3840 | } |
| 3841 | return 1; /* All conditions meet */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | /* |
| 3845 | ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so |
| 3846 | ** that: |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3847 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3848 | ** (1) pTemplate costs less than any other WhereLoops that are a proper |
| 3849 | ** subset of pTemplate |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3850 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3851 | ** (2) pTemplate costs more than any other WhereLoops for which pTemplate |
| 3852 | ** is a proper subset. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3853 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3854 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 3855 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 3856 | ** also used by Y. |
drh | e724d3d | 2014-05-02 00:09:40 +0000 | [diff] [blame] | 3857 | ** |
| 3858 | ** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the |
| 3859 | ** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE |
| 3860 | ** clause terms covered, since some of the first nLTerm entries in aLTerm[] |
| 3861 | ** will be NULL (because they are skipped). That makes it more difficult |
| 3862 | ** to compare the loops. We could add extra code to do the comparison, and |
| 3863 | ** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this |
| 3864 | ** adjustment is sufficient minor, that it is very difficult to construct |
| 3865 | ** a test case where the extra code would improve the query plan. Better |
| 3866 | ** to avoid the added complexity and just omit cost adjustments to SKIPSCAN |
| 3867 | ** loops. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3868 | */ |
| 3869 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 3870 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
dan | e3bfbb7 | 2014-04-28 09:35:31 +0000 | [diff] [blame] | 3871 | if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3872 | for(; p; p=p->pNextLoop){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3873 | if( p->iTab!=pTemplate->iTab ) continue; |
| 3874 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
dan | e3bfbb7 | 2014-04-28 09:35:31 +0000 | [diff] [blame] | 3875 | if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3876 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 3877 | /* Adjust pTemplate cost downward so that it is cheaper than its |
| 3878 | ** subset p */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3879 | pTemplate->rRun = p->rRun; |
| 3880 | pTemplate->nOut = p->nOut - 1; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3881 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 3882 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 3883 | ** pTemplate is a proper subset of p */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3884 | pTemplate->rRun = p->rRun; |
| 3885 | pTemplate->nOut = p->nOut + 1; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3886 | } |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | /* |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3891 | ** Search the list of WhereLoops in *ppPrev looking for one that can be |
| 3892 | ** supplanted by pTemplate. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3893 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3894 | ** Return NULL if the WhereLoop list contains an entry that can supplant |
| 3895 | ** pTemplate, in other words if pTemplate does not belong on the list. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3896 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3897 | ** If pX is a WhereLoop that pTemplate can supplant, then return the |
| 3898 | ** link that points to pX. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3899 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3900 | ** If pTemplate cannot supplant any existing element of the list but needs |
| 3901 | ** to be added to the list, then return a pointer to the tail of the list. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3902 | */ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3903 | static WhereLoop **whereLoopFindLesser( |
| 3904 | WhereLoop **ppPrev, |
| 3905 | const WhereLoop *pTemplate |
| 3906 | ){ |
| 3907 | WhereLoop *p; |
| 3908 | for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 3909 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 3910 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 3911 | ** then those WhereLoops need to be considered separately. Neither is |
| 3912 | ** a candidate to replace the other. */ |
| 3913 | continue; |
| 3914 | } |
| 3915 | /* In the current implementation, the rSetup value is either zero |
| 3916 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 3917 | ** is the same for compatible WhereLoops. */ |
| 3918 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 3919 | || p->rSetup==pTemplate->rSetup ); |
| 3920 | |
| 3921 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 3922 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 3923 | ** rSetup. Call this SETUP-INVARIANT */ |
| 3924 | assert( p->rSetup>=pTemplate->rSetup ); |
| 3925 | |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 3926 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 3927 | ** UNIQUE constraint) with one or more == constraints is better |
| 3928 | ** than an automatic index. */ |
| 3929 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 3930 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 3931 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 3932 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 3933 | ){ |
| 3934 | break; |
| 3935 | } |
| 3936 | |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3937 | /* If existing WhereLoop p is better than pTemplate, pTemplate can be |
| 3938 | ** discarded. WhereLoop p is better if: |
| 3939 | ** (1) p has no more dependencies than pTemplate, and |
| 3940 | ** (2) p has an equal or lower cost than pTemplate |
| 3941 | */ |
| 3942 | if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */ |
| 3943 | && p->rSetup<=pTemplate->rSetup /* (2a) */ |
| 3944 | && p->rRun<=pTemplate->rRun /* (2b) */ |
| 3945 | && p->nOut<=pTemplate->nOut /* (2c) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3946 | ){ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3947 | return 0; /* Discard pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3948 | } |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3949 | |
| 3950 | /* If pTemplate is always better than p, then cause p to be overwritten |
| 3951 | ** with pTemplate. pTemplate is better than p if: |
| 3952 | ** (1) pTemplate has no more dependences than p, and |
| 3953 | ** (2) pTemplate has an equal or lower cost than p. |
| 3954 | */ |
| 3955 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ |
| 3956 | && p->rRun>=pTemplate->rRun /* (2a) */ |
| 3957 | && p->nOut>=pTemplate->nOut /* (2b) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3958 | ){ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 3959 | assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3960 | break; /* Cause p to be overwritten by pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3961 | } |
| 3962 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3963 | return ppPrev; |
| 3964 | } |
| 3965 | |
| 3966 | /* |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3967 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 3968 | ** |
| 3969 | ** An existing WhereLoop entry might be overwritten if the new template |
| 3970 | ** is better and has fewer dependencies. Or the template will be ignored |
| 3971 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 3972 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 3973 | ** added based on the template. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 3974 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3975 | ** If pBuilder->pOrSet is not NULL then we care about only the |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3976 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 3977 | ** information is gathered in the pBuilder->pOrSet object. This special |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 3978 | ** processing mode is used only for OR clause processing. |
| 3979 | ** |
| 3980 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
| 3981 | ** still might overwrite similar loops with the new template if the |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3982 | ** new template is better. Loops may be overwritten if the following |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3983 | ** conditions are met: |
| 3984 | ** |
| 3985 | ** (1) They have the same iTab. |
| 3986 | ** (2) They have the same iSortIdx. |
| 3987 | ** (3) The template has same or fewer dependencies than the current loop |
| 3988 | ** (4) The template has the same or lower cost than the current loop |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3989 | */ |
| 3990 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3991 | WhereLoop **ppPrev, *p; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3992 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 3993 | sqlite3 *db = pWInfo->pParse->db; |
| 3994 | |
| 3995 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 3996 | ** and prereqs. |
| 3997 | */ |
| 3998 | if( pBuilder->pOrSet!=0 ){ |
| 3999 | #if WHERETRACE_ENABLED |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4000 | u16 n = pBuilder->pOrSet->n; |
| 4001 | int x = |
| 4002 | #endif |
| 4003 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 4004 | pTemplate->nOut); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4005 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4006 | if( sqlite3WhereTrace & 0x8 ){ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 4007 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 4008 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4009 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4010 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4011 | return SQLITE_OK; |
| 4012 | } |
| 4013 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4014 | /* Look for an existing WhereLoop to replace with pTemplate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4015 | */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4016 | whereLoopAdjustCost(pWInfo->pLoops, pTemplate); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4017 | ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4018 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4019 | if( ppPrev==0 ){ |
| 4020 | /* There already exists a WhereLoop on the list that is better |
| 4021 | ** than pTemplate, so just ignore pTemplate */ |
| 4022 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4023 | if( sqlite3WhereTrace & 0x8 ){ |
| 4024 | sqlite3DebugPrintf("ins-noop: "); |
| 4025 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4026 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4027 | #endif |
| 4028 | return SQLITE_OK; |
| 4029 | }else{ |
| 4030 | p = *ppPrev; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | /* If we reach this point it means that either p[] should be overwritten |
| 4034 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4035 | ** WhereLoop and insert it. |
| 4036 | */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4037 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4038 | if( sqlite3WhereTrace & 0x8 ){ |
| 4039 | if( p!=0 ){ |
| 4040 | sqlite3DebugPrintf("ins-del: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4041 | whereLoopPrint(p, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4042 | } |
| 4043 | sqlite3DebugPrintf("ins-new: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4044 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4045 | } |
| 4046 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4047 | if( p==0 ){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4048 | /* Allocate a new WhereLoop to add to the end of the list */ |
| 4049 | *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4050 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4051 | whereLoopInit(p); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4052 | p->pNextLoop = 0; |
| 4053 | }else{ |
| 4054 | /* We will be overwriting WhereLoop p[]. But before we do, first |
| 4055 | ** go through the rest of the list and delete any other entries besides |
| 4056 | ** p[] that are also supplated by pTemplate */ |
| 4057 | WhereLoop **ppTail = &p->pNextLoop; |
| 4058 | WhereLoop *pToDel; |
| 4059 | while( *ppTail ){ |
| 4060 | ppTail = whereLoopFindLesser(ppTail, pTemplate); |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4061 | if( ppTail==0 ) break; |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4062 | pToDel = *ppTail; |
| 4063 | if( pToDel==0 ) break; |
| 4064 | *ppTail = pToDel->pNextLoop; |
| 4065 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4066 | if( sqlite3WhereTrace & 0x8 ){ |
| 4067 | sqlite3DebugPrintf("ins-del: "); |
| 4068 | whereLoopPrint(pToDel, pBuilder->pWC); |
| 4069 | } |
| 4070 | #endif |
| 4071 | whereLoopDelete(db, pToDel); |
| 4072 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4073 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4074 | whereLoopXfer(db, p, pTemplate); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4075 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4076 | Index *pIndex = p->u.btree.pIndex; |
| 4077 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4078 | p->u.btree.pIndex = 0; |
| 4079 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4080 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4081 | return SQLITE_OK; |
| 4082 | } |
| 4083 | |
| 4084 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4085 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 4086 | ** WHERE clause that reference the loop but which are not used by an |
| 4087 | ** index. |
| 4088 | ** |
| 4089 | ** In the current implementation, the first extra WHERE clause term reduces |
| 4090 | ** the number of output rows by a factor of 10 and each additional term |
| 4091 | ** reduces the number of output rows by sqrt(2). |
| 4092 | */ |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4093 | static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4094 | WhereTerm *pTerm, *pX; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4095 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4096 | int i, j; |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4097 | |
| 4098 | if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){ |
| 4099 | return; |
| 4100 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4101 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4102 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4103 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 4104 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4105 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 4106 | pX = pLoop->aLTerm[j]; |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 4107 | if( pX==0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4108 | if( pX==pTerm ) break; |
| 4109 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 4110 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4111 | if( j<0 ){ |
| 4112 | pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1); |
| 4113 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4114 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | /* |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 4118 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the |
| 4119 | ** index pIndex. Try to match one more. |
| 4120 | ** |
| 4121 | ** When this function is called, pBuilder->pNew->nOut contains the |
| 4122 | ** number of rows expected to be visited by filtering using the nEq |
| 4123 | ** terms only. If it is modified, this value is restored before this |
| 4124 | ** function returns. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4125 | ** |
| 4126 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4127 | ** INTEGER PRIMARY KEY. |
| 4128 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4129 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4130 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4131 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4132 | Index *pProbe, /* An index on pSrc */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4133 | LogEst nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4134 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4135 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4136 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4137 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4138 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4139 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4140 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4141 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4142 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4143 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4144 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4145 | u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4146 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4147 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4148 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4149 | int rc = SQLITE_OK; /* Return code */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4150 | LogEst rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4151 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4152 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4153 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4154 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4155 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4156 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4157 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4158 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4159 | opMask = WO_LT|WO_LE; |
| 4160 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4161 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4162 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4163 | opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4164 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4165 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4166 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4167 | assert( pNew->u.btree.nEq<=pProbe->nKeyCol ); |
| 4168 | if( pNew->u.btree.nEq < pProbe->nKeyCol ){ |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4169 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4170 | }else{ |
| 4171 | iCol = -1; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4172 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4173 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4174 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4175 | saved_nEq = pNew->u.btree.nEq; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4176 | saved_nSkip = pNew->u.btree.nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4177 | saved_nLTerm = pNew->nLTerm; |
| 4178 | saved_wsFlags = pNew->wsFlags; |
| 4179 | saved_prereq = pNew->prereq; |
| 4180 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4181 | pNew->rSetup = 0; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4182 | rLogSize = estLog(pProbe->aiRowLogEst[0]); |
drh | 64ff26f | 2013-11-18 19:32:15 +0000 | [diff] [blame] | 4183 | |
| 4184 | /* Consider using a skip-scan if there are no WHERE clause constraints |
| 4185 | ** available for the left-most terms of the index, and if the average |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 4186 | ** number of repeats in the left-most terms is at least 18. |
| 4187 | ** |
| 4188 | ** The magic number 18 is selected on the basis that scanning 17 rows |
| 4189 | ** is almost always quicker than an index seek (even though if the index |
| 4190 | ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 4191 | ** the code). And, even if it is not, it should not be too much slower. |
| 4192 | ** On the other hand, the extra seeks could end up being significantly |
| 4193 | ** more expensive. */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4194 | assert( 42==sqlite3LogEst(18) ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4195 | if( pTerm==0 |
| 4196 | && saved_nEq==saved_nSkip |
| 4197 | && saved_nEq+1<pProbe->nKeyCol |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4198 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
drh | 6c1de30 | 2013-12-22 20:44:10 +0000 | [diff] [blame] | 4199 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4200 | ){ |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 4201 | LogEst nIter; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4202 | pNew->u.btree.nEq++; |
| 4203 | pNew->u.btree.nSkip++; |
| 4204 | pNew->aLTerm[pNew->nLTerm++] = 0; |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 4205 | pNew->wsFlags |= WHERE_SKIPSCAN; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4206 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 4207 | pNew->nOut -= nIter; |
| 4208 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
drh | 89212fb | 2014-03-10 20:12:31 +0000 | [diff] [blame] | 4209 | pNew->nOut = saved_nOut; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4210 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4211 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4212 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4213 | LogEst rCostIdx; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4214 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4215 | int nIn = 0; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4216 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4217 | int nRecValid = pBuilder->nRecValid; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 4218 | #endif |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4219 | if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
dan | 8bff07a | 2013-08-29 14:56:14 +0000 | [diff] [blame] | 4220 | && (iCol<0 || pSrc->pTab->aCol[iCol].notNull) |
| 4221 | ){ |
| 4222 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 4223 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4224 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 4225 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4226 | pNew->wsFlags = saved_wsFlags; |
| 4227 | pNew->u.btree.nEq = saved_nEq; |
| 4228 | pNew->nLTerm = saved_nLTerm; |
| 4229 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4230 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4231 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4232 | |
| 4233 | assert( nInMul==0 |
| 4234 | || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| 4235 | || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 |
| 4236 | || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 |
| 4237 | ); |
| 4238 | |
| 4239 | if( eOp & WO_IN ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4240 | Expr *pExpr = pTerm->pExpr; |
| 4241 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4242 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4243 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4244 | nIn = 46; assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4245 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4246 | /* "x IN (value, value, ...)" */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4247 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4248 | } |
drh | 2b59b3a | 2014-03-20 13:26:47 +0000 | [diff] [blame] | 4249 | assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 4250 | ** changes "x IN (?)" into "x=?". */ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4251 | |
| 4252 | }else if( eOp & (WO_EQ) ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4253 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4254 | if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4255 | if( iCol>=0 && pProbe->onError==OE_None ){ |
| 4256 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 4257 | }else{ |
| 4258 | pNew->wsFlags |= WHERE_ONEROW; |
| 4259 | } |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4260 | } |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 4261 | }else if( eOp & WO_ISNULL ){ |
| 4262 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4263 | }else if( eOp & (WO_GT|WO_GE) ){ |
| 4264 | testcase( eOp & WO_GT ); |
| 4265 | testcase( eOp & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4266 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4267 | pBtm = pTerm; |
| 4268 | pTop = 0; |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 4269 | }else{ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4270 | assert( eOp & (WO_LT|WO_LE) ); |
| 4271 | testcase( eOp & WO_LT ); |
| 4272 | testcase( eOp & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4273 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4274 | pTop = pTerm; |
| 4275 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4276 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4277 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4278 | |
| 4279 | /* At this point pNew->nOut is set to the number of rows expected to |
| 4280 | ** be visited by the index scan before considering term pTerm, or the |
| 4281 | ** values of nIn and nInMul. In other words, assuming that all |
| 4282 | ** "x IN(...)" terms are replaced with "x = ?". This block updates |
| 4283 | ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ |
| 4284 | assert( pNew->nOut==saved_nOut ); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4285 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4286 | /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 |
| 4287 | ** data, using some other estimate. */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4288 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4289 | }else{ |
| 4290 | int nEq = ++pNew->u.btree.nEq; |
| 4291 | assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) ); |
| 4292 | |
| 4293 | assert( pNew->nOut==saved_nOut ); |
dan | 09e1df6 | 2014-04-29 16:10:22 +0000 | [diff] [blame] | 4294 | if( pTerm->truthProb<=0 && iCol>=0 ){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4295 | assert( (eOp & WO_IN) || nIn==0 ); |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 4296 | testcase( eOp & WO_IN ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4297 | pNew->nOut += pTerm->truthProb; |
| 4298 | pNew->nOut -= nIn; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4299 | }else{ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4300 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4301 | tRowcnt nOut = 0; |
| 4302 | if( nInMul==0 |
| 4303 | && pProbe->nSample |
| 4304 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 4305 | && OptimizationEnabled(db, SQLITE_Stat3) |
| 4306 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4307 | ){ |
| 4308 | Expr *pExpr = pTerm->pExpr; |
| 4309 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 4310 | testcase( eOp & WO_EQ ); |
| 4311 | testcase( eOp & WO_ISNULL ); |
| 4312 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
| 4313 | }else{ |
| 4314 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
| 4315 | } |
| 4316 | assert( rc!=SQLITE_OK || nOut>0 ); |
| 4317 | if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 4318 | if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ |
| 4319 | if( nOut ){ |
| 4320 | pNew->nOut = sqlite3LogEst(nOut); |
| 4321 | if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; |
| 4322 | pNew->nOut -= nIn; |
| 4323 | } |
| 4324 | } |
| 4325 | if( nOut==0 ) |
| 4326 | #endif |
| 4327 | { |
| 4328 | pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); |
| 4329 | if( eOp & WO_ISNULL ){ |
| 4330 | /* TUNING: If there is no likelihood() value, assume that a |
| 4331 | ** "col IS NULL" expression matches twice as many rows |
| 4332 | ** as (col=?). */ |
| 4333 | pNew->nOut += 10; |
| 4334 | } |
| 4335 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4336 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4337 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4338 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4339 | /* Set rCostIdx to the cost of visiting selected rows in index. Add |
| 4340 | ** it to pNew->rRun, which is currently set to the cost of the index |
| 4341 | ** seek only. Then, if this is a non-covering index, add the cost of |
| 4342 | ** visiting the rows in the main table. */ |
| 4343 | rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4344 | pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4345 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4346 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4347 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4348 | |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4349 | nOutUnadjusted = pNew->nOut; |
| 4350 | pNew->rRun += nInMul + nIn; |
| 4351 | pNew->nOut += nInMul + nIn; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4352 | whereLoopOutputAdjust(pBuilder->pWC, pNew); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4353 | rc = whereLoopInsert(pBuilder, pNew); |
dan | 440e6ff | 2014-04-28 08:49:54 +0000 | [diff] [blame] | 4354 | |
| 4355 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4356 | pNew->nOut = saved_nOut; |
| 4357 | }else{ |
| 4358 | pNew->nOut = nOutUnadjusted; |
| 4359 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4360 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4361 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4362 | && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0)) |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4363 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4364 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4365 | } |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4366 | pNew->nOut = saved_nOut; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4367 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4368 | pBuilder->nRecValid = nRecValid; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4369 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4370 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4371 | pNew->prereq = saved_prereq; |
| 4372 | pNew->u.btree.nEq = saved_nEq; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4373 | pNew->u.btree.nSkip = saved_nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4374 | pNew->wsFlags = saved_wsFlags; |
| 4375 | pNew->nOut = saved_nOut; |
| 4376 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4377 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4381 | ** Return True if it is possible that pIndex might be useful in |
| 4382 | ** implementing the ORDER BY clause in pBuilder. |
| 4383 | ** |
| 4384 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4385 | ** if there is no way for pIndex to be useful in implementing that |
| 4386 | ** ORDER BY clause. |
| 4387 | */ |
| 4388 | static int indexMightHelpWithOrderBy( |
| 4389 | WhereLoopBuilder *pBuilder, |
| 4390 | Index *pIndex, |
| 4391 | int iCursor |
| 4392 | ){ |
| 4393 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4394 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4395 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4396 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4397 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4398 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4399 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4400 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4401 | if( pExpr->iTable==iCursor ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4402 | for(jj=0; jj<pIndex->nKeyCol; jj++){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4403 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 4404 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | return 0; |
| 4408 | } |
| 4409 | |
| 4410 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4411 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4412 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4413 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4414 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4415 | Bitmask m = 0; |
| 4416 | int j; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4417 | for(j=pIdx->nColumn-1; j>=0; j--){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4418 | int x = pIdx->aiColumn[j]; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4419 | if( x>=0 ){ |
| 4420 | testcase( x==BMS-1 ); |
| 4421 | testcase( x==BMS-2 ); |
| 4422 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4423 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4424 | } |
| 4425 | return m; |
| 4426 | } |
| 4427 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4428 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 4429 | ** in the current query. Return true if it can be and false if not. |
| 4430 | */ |
| 4431 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 4432 | int i; |
| 4433 | WhereTerm *pTerm; |
| 4434 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4435 | if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1; |
| 4436 | } |
| 4437 | return 0; |
| 4438 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4439 | |
| 4440 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 4441 | ** Add all WhereLoop objects for a single table of the join where the table |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4442 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4443 | ** a b-tree table, not a virtual table. |
dan | 8164722 | 2014-04-30 15:00:16 +0000 | [diff] [blame] | 4444 | ** |
| 4445 | ** The costs (WhereLoop.rRun) of the b-tree loops added by this function |
| 4446 | ** are calculated as follows: |
| 4447 | ** |
| 4448 | ** For a full scan, assuming the table (or index) contains nRow rows: |
| 4449 | ** |
| 4450 | ** cost = nRow * 3.0 // full-table scan |
| 4451 | ** cost = nRow * K // scan of covering index |
| 4452 | ** cost = nRow * (K+3.0) // scan of non-covering index |
| 4453 | ** |
| 4454 | ** where K is a value between 1.1 and 3.0 set based on the relative |
| 4455 | ** estimated average size of the index and table records. |
| 4456 | ** |
| 4457 | ** For an index scan, where nVisit is the number of index rows visited |
| 4458 | ** by the scan, and nSeek is the number of seek operations required on |
| 4459 | ** the index b-tree: |
| 4460 | ** |
| 4461 | ** cost = nSeek * (log(nRow) + K * nVisit) // covering index |
| 4462 | ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index |
| 4463 | ** |
| 4464 | ** Normally, nSeek is 1. nSeek values greater than 1 come about if the |
| 4465 | ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when |
| 4466 | ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4467 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4468 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4469 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4470 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4471 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4472 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4473 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4474 | Index sPk; /* A fake index object for the primary key */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4475 | LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4476 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4477 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4478 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4479 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4480 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4481 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4482 | int b; /* A boolean value */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4483 | LogEst rSize; /* number of rows in the table */ |
| 4484 | LogEst rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4485 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4486 | Table *pTab; /* Table being queried */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4487 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4488 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4489 | pWInfo = pBuilder->pWInfo; |
| 4490 | pTabList = pWInfo->pTabList; |
| 4491 | pSrc = pTabList->a + pNew->iTab; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4492 | pTab = pSrc->pTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4493 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4494 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4495 | |
| 4496 | if( pSrc->pIndex ){ |
| 4497 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4498 | pProbe = pSrc->pIndex; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4499 | }else if( !HasRowid(pTab) ){ |
| 4500 | pProbe = pTab->pIndex; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4501 | }else{ |
| 4502 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4503 | ** variable sPk to represent the rowid primary key index. Make this |
| 4504 | ** fake index the first in a chain of Index objects with all of the real |
| 4505 | ** indices to follow */ |
| 4506 | Index *pFirst; /* First of real indices on the table */ |
| 4507 | memset(&sPk, 0, sizeof(Index)); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4508 | sPk.nKeyCol = 1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4509 | sPk.aiColumn = &aiColumnPk; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4510 | sPk.aiRowLogEst = aiRowEstPk; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4511 | sPk.onError = OE_Replace; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4512 | sPk.pTable = pTab; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4513 | sPk.szIdxRow = pTab->szTabRow; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4514 | aiRowEstPk[0] = pTab->nRowLogEst; |
| 4515 | aiRowEstPk[1] = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4516 | pFirst = pSrc->pTab->pIndex; |
| 4517 | if( pSrc->notIndexed==0 ){ |
| 4518 | /* The real indices of the table are only considered if the |
| 4519 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4520 | sPk.pNext = pFirst; |
| 4521 | } |
| 4522 | pProbe = &sPk; |
| 4523 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4524 | rSize = pTab->nRowLogEst; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4525 | rLogSize = estLog(rSize); |
| 4526 | |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4527 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4528 | /* Automatic indexes */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4529 | if( !pBuilder->pOrSet |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 4530 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 4531 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4532 | && !pSrc->viaCoroutine |
| 4533 | && !pSrc->notIndexed |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4534 | && HasRowid(pTab) |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4535 | && !pSrc->isCorrelated |
dan | 62ba4e4 | 2014-01-15 18:21:41 +0000 | [diff] [blame] | 4536 | && !pSrc->isRecursive |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4537 | ){ |
| 4538 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4539 | WhereTerm *pTerm; |
| 4540 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4541 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4542 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4543 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4544 | pNew->u.btree.nEq = 1; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4545 | pNew->u.btree.nSkip = 0; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4546 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4547 | pNew->nLTerm = 1; |
| 4548 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4549 | /* TUNING: One-time cost for computing the automatic index is |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4550 | ** approximately 7*N*log2(N) where N is the number of rows in |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4551 | ** the table being indexed. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4552 | pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) ); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4553 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 4554 | ** is more than the usual guess of 10 rows, since we have no way |
| 4555 | ** of knowning how selective the index will ultimately be. It would |
| 4556 | ** not be unreasonable to make this value much larger. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4557 | pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4558 | pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4559 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4560 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4561 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4562 | } |
| 4563 | } |
| 4564 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4565 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4566 | |
| 4567 | /* Loop over all indices |
| 4568 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4569 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4570 | if( pProbe->pPartIdxWhere!=0 |
| 4571 | && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){ |
| 4572 | continue; /* Partial index inappropriate for this query */ |
| 4573 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 4574 | rSize = pProbe->aiRowLogEst[0]; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4575 | pNew->u.btree.nEq = 0; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4576 | pNew->u.btree.nSkip = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4577 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4578 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4579 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4580 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4581 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4582 | pNew->u.btree.pIndex = pProbe; |
| 4583 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4584 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 4585 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4586 | if( pProbe->tnum<=0 ){ |
| 4587 | /* Integer primary key index */ |
| 4588 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4589 | |
| 4590 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4591 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4592 | /* TUNING: Cost of full table scan is (N*3.0). */ |
| 4593 | pNew->rRun = rSize + 16; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4594 | whereLoopOutputAdjust(pWC, pNew); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4595 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4596 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4597 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4598 | }else{ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4599 | Bitmask m; |
| 4600 | if( pProbe->isCovering ){ |
| 4601 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 4602 | m = 0; |
| 4603 | }else{ |
| 4604 | m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 4605 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 4606 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4607 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4608 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4609 | if( b |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 4610 | || !HasRowid(pTab) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4611 | || ( m==0 |
| 4612 | && pProbe->bUnordered==0 |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 4613 | && (pProbe->szIdxRow<pTab->szTabRow) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4614 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4615 | && sqlite3GlobalConfig.bUseCis |
| 4616 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 4617 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4618 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4619 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4620 | |
| 4621 | /* The cost of visiting the index rows is N*K, where K is |
| 4622 | ** between 1.1 and 3.0, depending on the relative sizes of the |
| 4623 | ** index and table rows. If this is a non-covering index scan, |
| 4624 | ** also add the cost of visiting table rows (N*3.0). */ |
| 4625 | pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; |
| 4626 | if( m!=0 ){ |
| 4627 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4628 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4629 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4630 | whereLoopOutputAdjust(pWC, pNew); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4631 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4632 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4633 | if( rc ) break; |
| 4634 | } |
| 4635 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4636 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4637 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4638 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 4639 | sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 4640 | pBuilder->nRecValid = 0; |
| 4641 | pBuilder->pRec = 0; |
dan | ddc2d6e | 2013-08-06 20:15:06 +0000 | [diff] [blame] | 4642 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4643 | |
| 4644 | /* If there was an INDEXED BY clause, then only that one index is |
| 4645 | ** considered. */ |
| 4646 | if( pSrc->pIndex ) break; |
| 4647 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4648 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4651 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4652 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4653 | ** Add all WhereLoop objects for a table of the join identified by |
| 4654 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4655 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4656 | static int whereLoopAddVirtual( |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4657 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 4658 | Bitmask mExtra |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4659 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4660 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4661 | Parse *pParse; /* The parsing context */ |
| 4662 | WhereClause *pWC; /* The WHERE clause */ |
| 4663 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4664 | Table *pTab; |
| 4665 | sqlite3 *db; |
| 4666 | sqlite3_index_info *pIdxInfo; |
| 4667 | struct sqlite3_index_constraint *pIdxCons; |
| 4668 | struct sqlite3_index_constraint_usage *pUsage; |
| 4669 | WhereTerm *pTerm; |
| 4670 | int i, j; |
| 4671 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4672 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4673 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4674 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4675 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4676 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4677 | int rc = SQLITE_OK; |
| 4678 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4679 | pWInfo = pBuilder->pWInfo; |
| 4680 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4681 | db = pParse->db; |
| 4682 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4683 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4684 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4685 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4686 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4687 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4688 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4689 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4690 | pNew->rSetup = 0; |
| 4691 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4692 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4693 | pNew->u.vtab.needFree = 0; |
| 4694 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4695 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4696 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 4697 | sqlite3DbFree(db, pIdxInfo); |
| 4698 | return SQLITE_NOMEM; |
| 4699 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4700 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4701 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4702 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4703 | iPhase++; |
| 4704 | if( iPhase>3 ) break; |
| 4705 | } |
| 4706 | if( !seenVar && iPhase>1 ) break; |
| 4707 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4708 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4709 | j = pIdxCons->iTermOffset; |
| 4710 | pTerm = &pWC->a[j]; |
| 4711 | switch( iPhase ){ |
| 4712 | case 0: /* Constants without IN operator */ |
| 4713 | pIdxCons->usable = 0; |
| 4714 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4715 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4716 | } |
| 4717 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4718 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4719 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4720 | pIdxCons->usable = 1; |
| 4721 | } |
| 4722 | break; |
| 4723 | case 1: /* Constants with IN operators */ |
| 4724 | assert( seenIn ); |
| 4725 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4726 | break; |
| 4727 | case 2: /* Variables without IN */ |
| 4728 | assert( seenVar ); |
| 4729 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4730 | break; |
| 4731 | default: /* Variables with IN */ |
| 4732 | assert( seenVar && seenIn ); |
| 4733 | pIdxCons->usable = 1; |
| 4734 | break; |
| 4735 | } |
| 4736 | } |
| 4737 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4738 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4739 | pIdxInfo->idxStr = 0; |
| 4740 | pIdxInfo->idxNum = 0; |
| 4741 | pIdxInfo->needToFreeIdxStr = 0; |
| 4742 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4743 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 4744 | pIdxInfo->estimatedRows = 25; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4745 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4746 | if( rc ) goto whereLoopAddVtab_exit; |
| 4747 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4748 | pNew->prereq = mExtra; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4749 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4750 | assert( pNew->nLSlot>=nConstraint ); |
| 4751 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4752 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4753 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4754 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4755 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4756 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4757 | || j<0 |
| 4758 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4759 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4760 | ){ |
| 4761 | rc = SQLITE_ERROR; |
| 4762 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4763 | goto whereLoopAddVtab_exit; |
| 4764 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4765 | testcase( iTerm==nConstraint-1 ); |
| 4766 | testcase( j==0 ); |
| 4767 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4768 | pTerm = &pWC->a[j]; |
| 4769 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4770 | assert( iTerm<pNew->nLSlot ); |
| 4771 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4772 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4773 | testcase( iTerm==15 ); |
| 4774 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4775 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4776 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4777 | if( pUsage[i].omit==0 ){ |
| 4778 | /* Do not attempt to use an IN constraint if the virtual table |
| 4779 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4780 | ** If we do attempt to use such a constraint, some rows might be |
| 4781 | ** repeated in the output. */ |
| 4782 | break; |
| 4783 | } |
| 4784 | /* A virtual table that is constrained by an IN clause may not |
| 4785 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4786 | ** is not necessarily related to the order of output terms and |
| 4787 | ** (2) Multiple outputs from a single IN value will not merge |
| 4788 | ** together. */ |
| 4789 | pIdxInfo->orderByConsumed = 0; |
| 4790 | } |
| 4791 | } |
| 4792 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4793 | if( i>=nConstraint ){ |
| 4794 | pNew->nLTerm = mxTerm+1; |
| 4795 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4796 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4797 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4798 | pIdxInfo->needToFreeIdxStr = 0; |
| 4799 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4800 | pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? |
| 4801 | pIdxInfo->nOrderBy : 0); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4802 | pNew->rSetup = 0; |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4803 | pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 4804 | pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4805 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4806 | if( pNew->u.vtab.needFree ){ |
| 4807 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4808 | pNew->u.vtab.needFree = 0; |
| 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | whereLoopAddVtab_exit: |
| 4814 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4815 | sqlite3DbFree(db, pIdxInfo); |
| 4816 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4817 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4818 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4819 | |
| 4820 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4821 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4822 | ** btrees or virtual tables. |
| 4823 | */ |
| 4824 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4825 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4826 | WhereClause *pWC; |
| 4827 | WhereLoop *pNew; |
| 4828 | WhereTerm *pTerm, *pWCEnd; |
| 4829 | int rc = SQLITE_OK; |
| 4830 | int iCur; |
| 4831 | WhereClause tempWC; |
| 4832 | WhereLoopBuilder sSubBuild; |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 4833 | WhereOrSet sSum, sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4834 | struct SrcList_item *pItem; |
| 4835 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4836 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4837 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4838 | pWCEnd = pWC->a + pWC->nTerm; |
| 4839 | pNew = pBuilder->pNew; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 4840 | memset(&sSum, 0, sizeof(sSum)); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4841 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 4842 | iCur = pItem->iCursor; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4843 | |
| 4844 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4845 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4846 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4847 | ){ |
| 4848 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4849 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4850 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4851 | int once = 1; |
| 4852 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4853 | |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4854 | sSubBuild = *pBuilder; |
| 4855 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4856 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4857 | |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4858 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4859 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4860 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4861 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4862 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4863 | tempWC.pOuter = pWC; |
| 4864 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4865 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4866 | tempWC.a = pOrTerm; |
| 4867 | sSubBuild.pWC = &tempWC; |
| 4868 | }else{ |
| 4869 | continue; |
| 4870 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4871 | sCur.n = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4872 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4873 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4874 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4875 | }else |
| 4876 | #endif |
| 4877 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4878 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4879 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4880 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 4881 | if( sCur.n==0 ){ |
| 4882 | sSum.n = 0; |
| 4883 | break; |
| 4884 | }else if( once ){ |
| 4885 | whereOrMove(&sSum, &sCur); |
| 4886 | once = 0; |
| 4887 | }else{ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 4888 | WhereOrSet sPrev; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4889 | whereOrMove(&sPrev, &sSum); |
| 4890 | sSum.n = 0; |
| 4891 | for(i=0; i<sPrev.n; i++){ |
| 4892 | for(j=0; j<sCur.n; j++){ |
| 4893 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4894 | sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 4895 | sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4899 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4900 | pNew->nLTerm = 1; |
| 4901 | pNew->aLTerm[0] = pTerm; |
| 4902 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4903 | pNew->rSetup = 0; |
| 4904 | pNew->iSortIdx = 0; |
| 4905 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4906 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 4907 | /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs |
| 4908 | ** of all sub-scans required by the OR-scan. However, due to rounding |
| 4909 | ** errors, it may be that the cost of the OR-scan is equal to its |
| 4910 | ** most expensive sub-scan. Add the smallest possible penalty |
| 4911 | ** (equivalent to multiplying the cost by 1.07) to ensure that |
| 4912 | ** this does not happen. Otherwise, for WHERE clauses such as the |
| 4913 | ** following where there is an index on "y": |
| 4914 | ** |
| 4915 | ** WHERE likelihood(x=?, 0.99) OR y=? |
| 4916 | ** |
| 4917 | ** the planner may elect to "OR" together a full-table scan and an |
| 4918 | ** index lookup. And other similarly odd results. */ |
| 4919 | pNew->rRun = sSum.a[i].rRun + 1; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4920 | pNew->nOut = sSum.a[i].nOut; |
| 4921 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4922 | rc = whereLoopInsert(pBuilder, pNew); |
| 4923 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4924 | } |
| 4925 | } |
| 4926 | return rc; |
| 4927 | } |
| 4928 | |
| 4929 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4930 | ** Add all WhereLoop objects for all tables |
| 4931 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4932 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4933 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4934 | Bitmask mExtra = 0; |
| 4935 | Bitmask mPrior = 0; |
| 4936 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4937 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4938 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4939 | sqlite3 *db = pWInfo->pParse->db; |
| 4940 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4941 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4942 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4943 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4944 | |
| 4945 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4946 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4947 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4948 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4949 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4950 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4951 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4952 | mExtra = mPrior; |
| 4953 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4954 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4955 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4956 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4957 | }else{ |
| 4958 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4959 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4960 | if( rc==SQLITE_OK ){ |
| 4961 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4962 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4963 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4964 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4965 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4966 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4967 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4970 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4971 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4972 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4973 | ** (or GROUP BY) without requiring a separate sort operation. Return N: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4974 | ** |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4975 | ** N>0: N terms of the ORDER BY clause are satisfied |
| 4976 | ** N==0: No terms of the ORDER BY clause are satisfied |
| 4977 | ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4978 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 4979 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 4980 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 4981 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4982 | ** and DISTINCT do not require rows to appear in any particular order as long |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 4983 | ** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT |
| 4984 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 4985 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4986 | */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4987 | static i8 wherePathSatisfiesOrderBy( |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4988 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4989 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4990 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4991 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 4992 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4993 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4994 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4995 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4996 | u8 revSet; /* True if rev is known */ |
| 4997 | u8 rev; /* Composite sort order */ |
| 4998 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4999 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 5000 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 5001 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5002 | u16 nKeyCol; /* Number of key columns in pIndex */ |
| 5003 | u16 nColumn; /* Total number of ordered columns in the index */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5004 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 5005 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 5006 | int i, j; /* Loop counters */ |
| 5007 | int iCur; /* Cursor number for current WhereLoop */ |
| 5008 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 5009 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5010 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 5011 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 5012 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 5013 | Index *pIndex; /* The index associated with pLoop */ |
| 5014 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 5015 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 5016 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5017 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5018 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5019 | |
| 5020 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5021 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 5022 | ** row of output. A WhereLoop is one-row if all of the following are true: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5023 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 5024 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5025 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 5026 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5027 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5028 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 5029 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 5030 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 5031 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 5032 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 5033 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 5034 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 5035 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 5036 | ** |
| 5037 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 5038 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 5039 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5040 | */ |
| 5041 | |
| 5042 | assert( pOrderBy!=0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5043 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5044 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5045 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5046 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5047 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 5048 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5049 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5050 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5051 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5052 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5053 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5054 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 9dfaf62 | 2014-04-25 14:42:17 +0000 | [diff] [blame] | 5055 | if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 5056 | if( pLoop->u.vtab.isOrdered ) obSat = obDone; |
| 5057 | break; |
| 5058 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5059 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5060 | |
| 5061 | /* Mark off any ORDER BY term X that is a column in the table of |
| 5062 | ** the current loop for which there is term in the WHERE |
| 5063 | ** clause of the form X IS NULL or X=? that reference only outer |
| 5064 | ** loops. |
| 5065 | */ |
| 5066 | for(i=0; i<nOrderBy; i++){ |
| 5067 | if( MASKBIT(i) & obSat ) continue; |
| 5068 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 5069 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 5070 | if( pOBExpr->iTable!=iCur ) continue; |
| 5071 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 5072 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 5073 | if( pTerm==0 ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5074 | if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5075 | const char *z1, *z2; |
| 5076 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5077 | if( !pColl ) pColl = db->pDfltColl; |
| 5078 | z1 = pColl->zName; |
| 5079 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 5080 | if( !pColl ) pColl = db->pDfltColl; |
| 5081 | z2 = pColl->zName; |
| 5082 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 5083 | } |
| 5084 | obSat |= MASKBIT(i); |
| 5085 | } |
| 5086 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5087 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 5088 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 5089 | pIndex = 0; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5090 | nKeyCol = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5091 | nColumn = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5092 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5093 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5094 | }else{ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5095 | nKeyCol = pIndex->nKeyCol; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5096 | nColumn = pIndex->nColumn; |
| 5097 | assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); |
| 5098 | assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable)); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5099 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5100 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5101 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5102 | /* Loop through all columns of the index and deal with the ones |
| 5103 | ** that are not constrained by == or IN. |
| 5104 | */ |
| 5105 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5106 | distinctColumns = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5107 | for(j=0; j<nColumn; j++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5108 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5109 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5110 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5111 | if( j<pLoop->u.btree.nEq |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 5112 | && pLoop->u.btree.nSkip==0 |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5113 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5114 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5115 | if( i & WO_ISNULL ){ |
| 5116 | testcase( isOrderDistinct ); |
| 5117 | isOrderDistinct = 0; |
| 5118 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5119 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5122 | /* Get the column number in the table (iColumn) and sort order |
| 5123 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5124 | */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5125 | if( pIndex ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5126 | iColumn = pIndex->aiColumn[j]; |
| 5127 | revIdx = pIndex->aSortOrder[j]; |
| 5128 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5129 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5130 | iColumn = -1; |
| 5131 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5132 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5133 | |
| 5134 | /* An unconstrained column that might be NULL means that this |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5135 | ** WhereLoop is not well-ordered |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5136 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5137 | if( isOrderDistinct |
| 5138 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5139 | && j>=pLoop->u.btree.nEq |
| 5140 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5141 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5142 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | /* Find the ORDER BY term that corresponds to the j-th column |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5146 | ** of the index and mark that ORDER BY term off |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5147 | */ |
| 5148 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5149 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5150 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5151 | if( MASKBIT(i) & obSat ) continue; |
| 5152 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5153 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5154 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5155 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5156 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5157 | if( pOBExpr->iTable!=iCur ) continue; |
| 5158 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5159 | if( iColumn>=0 ){ |
| 5160 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5161 | if( !pColl ) pColl = db->pDfltColl; |
| 5162 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5163 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5164 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5165 | break; |
| 5166 | } |
drh | 59b8f2e | 2014-03-22 00:27:14 +0000 | [diff] [blame] | 5167 | if( isMatch && (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 5168 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5169 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 5170 | if( revSet ){ |
| 5171 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 5172 | }else{ |
| 5173 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5174 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5175 | revSet = 1; |
| 5176 | } |
| 5177 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5178 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5179 | if( iColumn<0 ){ |
| 5180 | testcase( distinctColumns==0 ); |
| 5181 | distinctColumns = 1; |
| 5182 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5183 | obSat |= MASKBIT(i); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5184 | }else{ |
| 5185 | /* No match found */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5186 | if( j==0 || j<nKeyCol ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5187 | testcase( isOrderDistinct!=0 ); |
| 5188 | isOrderDistinct = 0; |
| 5189 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5190 | break; |
| 5191 | } |
| 5192 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5193 | if( distinctColumns ){ |
| 5194 | testcase( isOrderDistinct==0 ); |
| 5195 | isOrderDistinct = 1; |
| 5196 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5197 | } /* end-if not one-row */ |
| 5198 | |
| 5199 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5200 | if( isOrderDistinct ){ |
| 5201 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5202 | for(i=0; i<nOrderBy; i++){ |
| 5203 | Expr *p; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 5204 | Bitmask mTerm; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5205 | if( MASKBIT(i) & obSat ) continue; |
| 5206 | p = pOrderBy->a[i].pExpr; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 5207 | mTerm = exprTableUsage(&pWInfo->sMaskSet,p); |
| 5208 | if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; |
| 5209 | if( (mTerm&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5210 | obSat |= MASKBIT(i); |
| 5211 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5212 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5213 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5214 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 36ed034 | 2014-03-28 12:56:57 +0000 | [diff] [blame] | 5215 | if( obSat==obDone ) return (i8)nOrderBy; |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5216 | if( !isOrderDistinct ){ |
| 5217 | for(i=nOrderBy-1; i>0; i--){ |
| 5218 | Bitmask m = MASKBIT(i) - 1; |
| 5219 | if( (obSat&m)==m ) return i; |
| 5220 | } |
| 5221 | return 0; |
| 5222 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5223 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5226 | |
| 5227 | /* |
| 5228 | ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), |
| 5229 | ** the planner assumes that the specified pOrderBy list is actually a GROUP |
| 5230 | ** BY clause - and so any order that groups rows as required satisfies the |
| 5231 | ** request. |
| 5232 | ** |
| 5233 | ** Normally, in this case it is not possible for the caller to determine |
| 5234 | ** whether or not the rows are really being delivered in sorted order, or |
| 5235 | ** just in some other order that provides the required grouping. However, |
| 5236 | ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then |
| 5237 | ** this function may be called on the returned WhereInfo object. It returns |
| 5238 | ** true if the rows really will be sorted in the specified order, or false |
| 5239 | ** otherwise. |
| 5240 | ** |
| 5241 | ** For example, assuming: |
| 5242 | ** |
| 5243 | ** CREATE INDEX i1 ON t1(x, Y); |
| 5244 | ** |
| 5245 | ** then |
| 5246 | ** |
| 5247 | ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 |
| 5248 | ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 |
| 5249 | */ |
| 5250 | int sqlite3WhereIsSorted(WhereInfo *pWInfo){ |
| 5251 | assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); |
| 5252 | assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); |
| 5253 | return pWInfo->sorted; |
| 5254 | } |
| 5255 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5256 | #ifdef WHERETRACE_ENABLED |
| 5257 | /* For debugging use only: */ |
| 5258 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5259 | static char zName[65]; |
| 5260 | int i; |
| 5261 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5262 | if( pLast ) zName[i++] = pLast->cId; |
| 5263 | zName[i] = 0; |
| 5264 | return zName; |
| 5265 | } |
| 5266 | #endif |
| 5267 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5268 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5269 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5270 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5271 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5272 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5273 | ** Assume that the total number of output rows that will need to be sorted |
| 5274 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5275 | ** costs if nRowEst==0. |
| 5276 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5277 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5278 | ** error occurs. |
| 5279 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5280 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5281 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5282 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5283 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5284 | sqlite3 *db; /* The database connection */ |
| 5285 | int iLoop; /* Loop counter over the terms of the join */ |
| 5286 | int ii, jj; /* Loop counters */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5287 | int mxI = 0; /* Index of next entry to replace */ |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5288 | int nOrderBy; /* Number of ORDER BY clause terms */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5289 | LogEst rCost; /* Cost of a path */ |
| 5290 | LogEst nOut; /* Number of outputs */ |
| 5291 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5292 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5293 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5294 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5295 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5296 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5297 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5298 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 5299 | char *pSpace; /* Temporary memory used by this routine */ |
| 5300 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5301 | pParse = pWInfo->pParse; |
| 5302 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5303 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5304 | /* TUNING: For simple queries, only the best path is tracked. |
| 5305 | ** For 2-way joins, the 5 best paths are followed. |
| 5306 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | 2504c6c | 2014-06-02 11:26:33 +0000 | [diff] [blame] | 5307 | mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5308 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5309 | WHERETRACE(0x002, ("---- begin solver\n")); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5310 | |
| 5311 | /* Allocate and initialize space for aTo and aFrom */ |
| 5312 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5313 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 5314 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5315 | aTo = (WherePath*)pSpace; |
| 5316 | aFrom = aTo+mxChoice; |
| 5317 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5318 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5319 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5320 | pFrom->aLoop = pX; |
| 5321 | } |
| 5322 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5323 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 5324 | ** |
| 5325 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 5326 | ** of computing an automatic index is not paid back within the first 25 |
| 5327 | ** rows, then do not use the automatic index. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5328 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5329 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5330 | |
| 5331 | /* Precompute the cost of sorting the final result set, if the caller |
| 5332 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5333 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5334 | aFrom[0].isOrdered = 0; |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5335 | nOrderBy = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5336 | }else{ |
drh | 2504c6c | 2014-06-02 11:26:33 +0000 | [diff] [blame] | 5337 | aFrom[0].isOrdered = nLoop>0 ? -1 : 1; |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5338 | nOrderBy = pWInfo->pOrderBy->nExpr; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | /* Compute successively longer WherePaths using the previous generation |
| 5342 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5343 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5344 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5345 | nTo = 0; |
| 5346 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5347 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5348 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5349 | Bitmask revMask = 0; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5350 | i8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5351 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5352 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5353 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5354 | ** Compute its cost */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5355 | rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5356 | rCost = sqlite3LogEstAdd(rCost, pFrom->rCost); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5357 | nOut = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5358 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5359 | if( isOrdered<0 ){ |
| 5360 | isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5361 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5362 | iLoop, pWLoop, &revMask); |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5363 | if( isOrdered>=0 && isOrdered<nOrderBy ){ |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 5364 | /* TUNING: Estimated cost of a full external sort, where N is |
| 5365 | ** the number of rows to sort is: |
| 5366 | ** |
| 5367 | ** cost = (3.0 * N * log(N)). |
| 5368 | ** |
| 5369 | ** Or, if the order-by clause has X terms but only the last Y |
| 5370 | ** terms are out of order, then block-sorting will reduce the |
| 5371 | ** sorting cost to: |
| 5372 | ** |
| 5373 | ** cost = (3.0 * N * log(N)) * (Y/X) |
| 5374 | ** |
| 5375 | ** The (Y/X) term is implemented using stack variable rScale |
| 5376 | ** below. */ |
drh | 27de5c5 | 2014-03-27 18:36:34 +0000 | [diff] [blame] | 5377 | LogEst rScale, rSortCost; |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 5378 | assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); |
drh | 27de5c5 | 2014-03-27 18:36:34 +0000 | [diff] [blame] | 5379 | rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66; |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 5380 | rSortCost = nRowEst + estLog(nRowEst) + rScale + 16; |
| 5381 | |
drh | 6284db9 | 2014-03-19 23:24:49 +0000 | [diff] [blame] | 5382 | /* TUNING: The cost of implementing DISTINCT using a B-TREE is |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 5383 | ** similar but with a larger constant of proportionality. |
| 5384 | ** Multiply by an additional factor of 3.0. */ |
drh | 6284db9 | 2014-03-19 23:24:49 +0000 | [diff] [blame] | 5385 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5386 | rSortCost += 16; |
| 5387 | } |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5388 | WHERETRACE(0x002, |
| 5389 | ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", |
| 5390 | rSortCost, (nOrderBy-isOrdered), nOrderBy, rCost, |
| 5391 | sqlite3LogEstAdd(rCost,rSortCost))); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5392 | rCost = sqlite3LogEstAdd(rCost, rSortCost); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5393 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5394 | }else{ |
| 5395 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5396 | } |
| 5397 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5398 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5399 | if( pTo->maskLoop==maskNew |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5400 | && ((pTo->isOrdered^isOrdered)&80)==0 |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5401 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5402 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5403 | break; |
| 5404 | } |
| 5405 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5406 | if( jj>=nTo ){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 5407 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5408 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5409 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5410 | sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", |
| 5411 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5412 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5413 | } |
| 5414 | #endif |
| 5415 | continue; |
| 5416 | } |
| 5417 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5418 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5419 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5420 | jj = nTo++; |
| 5421 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5422 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5423 | jj = mxI; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5424 | } |
| 5425 | pTo = &aTo[jj]; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5426 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5427 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5428 | sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", |
| 5429 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5430 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5431 | } |
| 5432 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5433 | }else{ |
drh | 9740726 | 2014-06-17 12:33:55 +0000 | [diff] [blame] | 5434 | if( pTo->rCost<=rCost ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5435 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5436 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5437 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5438 | "Skip %s cost=%-3d,%3d order=%c", |
| 5439 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5440 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5441 | sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", |
| 5442 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5443 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5444 | } |
| 5445 | #endif |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5446 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5447 | continue; |
| 5448 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5449 | testcase( pTo->rCost==rCost+1 ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5450 | /* A new and better score for a previously created equivalent path */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5451 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5452 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5453 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5454 | "Update %s cost=%-3d,%3d order=%c", |
| 5455 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5456 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5457 | sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", |
| 5458 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5459 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5460 | } |
| 5461 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5462 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5463 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5464 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5465 | pTo->revLoop = revMask; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5466 | pTo->nRow = nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5467 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5468 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5469 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5470 | pTo->aLoop[iLoop] = pWLoop; |
| 5471 | if( nTo>=mxChoice ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5472 | mxI = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5473 | mxCost = aTo[0].rCost; |
| 5474 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
drh | 9740726 | 2014-06-17 12:33:55 +0000 | [diff] [blame] | 5475 | if( pTo->rCost>mxCost ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5476 | mxCost = pTo->rCost; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5477 | mxI = jj; |
| 5478 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5479 | } |
| 5480 | } |
| 5481 | } |
| 5482 | } |
| 5483 | |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5484 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5485 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5486 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5487 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5488 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5489 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5490 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| 5491 | if( pTo->isOrdered>0 ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5492 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5493 | }else{ |
| 5494 | sqlite3DebugPrintf("\n"); |
| 5495 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5496 | } |
| 5497 | } |
| 5498 | #endif |
| 5499 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5500 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5501 | pFrom = aTo; |
| 5502 | aTo = aFrom; |
| 5503 | aFrom = pFrom; |
| 5504 | nFrom = nTo; |
| 5505 | } |
| 5506 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5507 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5508 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5509 | sqlite3DbFree(db, pSpace); |
| 5510 | return SQLITE_ERROR; |
| 5511 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5512 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5513 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5514 | pFrom = aFrom; |
| 5515 | for(ii=1; ii<nFrom; ii++){ |
| 5516 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5517 | } |
| 5518 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5519 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5520 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5521 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5522 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5523 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5524 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5525 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5526 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 5527 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5528 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5529 | && nRowEst |
| 5530 | ){ |
| 5531 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5532 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5533 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5534 | if( rc==pWInfo->pResultSet->nExpr ){ |
| 5535 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5536 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5537 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5538 | if( pWInfo->pOrderBy ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5539 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5540 | if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 5541 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5542 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5543 | }else{ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5544 | pWInfo->nOBSat = pFrom->isOrdered; |
drh | ea6c36e | 2014-03-19 14:30:55 +0000 | [diff] [blame] | 5545 | if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5546 | pWInfo->revMask = pFrom->revLoop; |
| 5547 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5548 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 5549 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr |
| 5550 | ){ |
| 5551 | Bitmask notUsed = 0; |
| 5552 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
| 5553 | pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used |
| 5554 | ); |
| 5555 | assert( pWInfo->sorted==0 ); |
| 5556 | pWInfo->sorted = (nOrder==pWInfo->pOrderBy->nExpr); |
| 5557 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5558 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5559 | |
| 5560 | |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5561 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5562 | |
| 5563 | /* Free temporary memory and return success */ |
| 5564 | sqlite3DbFree(db, pSpace); |
| 5565 | return SQLITE_OK; |
| 5566 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5567 | |
| 5568 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5569 | ** Most queries use only a single table (they are not joins) and have |
| 5570 | ** simple == constraints against indexed fields. This routine attempts |
| 5571 | ** to plan those simple cases using much less ceremony than the |
| 5572 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5573 | ** times for the common case. |
| 5574 | ** |
| 5575 | ** Return non-zero on success, if this query can be handled by this |
| 5576 | ** no-frills query planner. Return zero if this query needs the |
| 5577 | ** general-purpose query planner. |
| 5578 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5579 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5580 | WhereInfo *pWInfo; |
| 5581 | struct SrcList_item *pItem; |
| 5582 | WhereClause *pWC; |
| 5583 | WhereTerm *pTerm; |
| 5584 | WhereLoop *pLoop; |
| 5585 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5586 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5587 | Table *pTab; |
| 5588 | Index *pIdx; |
| 5589 | |
| 5590 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5591 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5592 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5593 | pItem = pWInfo->pTabList->a; |
| 5594 | pTab = pItem->pTab; |
| 5595 | if( IsVirtual(pTab) ) return 0; |
| 5596 | if( pItem->zIndex ) return 0; |
| 5597 | iCur = pItem->iCursor; |
| 5598 | pWC = &pWInfo->sWC; |
| 5599 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5600 | pLoop->wsFlags = 0; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 5601 | pLoop->u.btree.nSkip = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5602 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5603 | if( pTerm ){ |
| 5604 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5605 | pLoop->aLTerm[0] = pTerm; |
| 5606 | pLoop->nLTerm = 1; |
| 5607 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5608 | /* TUNING: Cost of a rowid lookup is 10 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5609 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5610 | }else{ |
| 5611 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5612 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 5613 | assert( ArraySize(pLoop->aLTermSpace)==4 ); |
| 5614 | if( pIdx->onError==OE_None |
| 5615 | || pIdx->pPartIdxWhere!=0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5616 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5617 | ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5618 | for(j=0; j<pIdx->nKeyCol; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5619 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5620 | if( pTerm==0 ) break; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5621 | pLoop->aLTerm[j] = pTerm; |
| 5622 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5623 | if( j!=pIdx->nKeyCol ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5624 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5625 | if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5626 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5627 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5628 | pLoop->nLTerm = j; |
| 5629 | pLoop->u.btree.nEq = j; |
| 5630 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5631 | /* TUNING: Cost of a unique index lookup is 15 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5632 | pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5633 | break; |
| 5634 | } |
| 5635 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5636 | if( pLoop->wsFlags ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5637 | pLoop->nOut = (LogEst)1; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5638 | pWInfo->a[0].pWLoop = pLoop; |
| 5639 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5640 | pWInfo->a[0].iTabCur = iCur; |
| 5641 | pWInfo->nRowOut = 1; |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5642 | if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5643 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5644 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5645 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5646 | #ifdef SQLITE_DEBUG |
| 5647 | pLoop->cId = '0'; |
| 5648 | #endif |
| 5649 | return 1; |
| 5650 | } |
| 5651 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
| 5654 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5655 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 5656 | ** The return value is a pointer to an opaque structure that contains |
| 5657 | ** information needed to terminate the loop. Later, the calling routine |
| 5658 | ** should invoke sqlite3WhereEnd() with the return value of this function |
| 5659 | ** in order to complete the WHERE clause processing. |
| 5660 | ** |
| 5661 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5662 | ** |
| 5663 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5664 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5665 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5666 | ** example, if the SQL is this: |
| 5667 | ** |
| 5668 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5669 | ** |
| 5670 | ** Then the code generated is conceptually like the following: |
| 5671 | ** |
| 5672 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5673 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5674 | ** foreach row3 in t3 do / |
| 5675 | ** ... |
| 5676 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5677 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5678 | ** end / |
| 5679 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5680 | ** Note that the loops might not be nested in the order in which they |
| 5681 | ** appear in the FROM clause if a different order is better able to make |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 5682 | ** use of indices. Note also that when the IN operator appears in |
| 5683 | ** the WHERE clause, it might result in additional nested loops for |
| 5684 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5685 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5686 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5687 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5688 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5689 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5690 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5691 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5692 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5693 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5694 | ** data from the various tables of the loop. |
| 5695 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5696 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5697 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5698 | ** the tables have indices and there are terms in the WHERE clause that |
| 5699 | ** refer to those indices, a complete table scan can be avoided and the |
| 5700 | ** code will run much faster. Most of the work of this routine is checking |
| 5701 | ** to see if there are indices that can be used to speed up the loop. |
| 5702 | ** |
| 5703 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5704 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5705 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5706 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5707 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5708 | ** most loop) |
| 5709 | ** |
| 5710 | ** OUTER JOINS |
| 5711 | ** |
| 5712 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5713 | ** |
| 5714 | ** foreach row1 in t1 do |
| 5715 | ** flag = 0 |
| 5716 | ** foreach row2 in t2 do |
| 5717 | ** start: |
| 5718 | ** ... |
| 5719 | ** flag = 1 |
| 5720 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5721 | ** if flag==0 then |
| 5722 | ** move the row2 cursor to a null row |
| 5723 | ** goto start |
| 5724 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5725 | ** end |
| 5726 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5727 | ** ORDER BY CLAUSE PROCESSING |
| 5728 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5729 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 5730 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5731 | ** if there is one. If there is no ORDER BY clause or if this routine |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5732 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5733 | ** |
| 5734 | ** The iIdxCur parameter is the cursor number of an index. If |
| 5735 | ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index |
| 5736 | ** to use for OR clause processing. The WHERE clause should use this |
| 5737 | ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 5738 | ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 5739 | ** be used to compute the appropriate cursor depending on which index is |
| 5740 | ** used. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5741 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5742 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5743 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5744 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5745 | Expr *pWhere, /* The WHERE clause */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5746 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5747 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5748 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5749 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5750 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5751 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5752 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5753 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5754 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5755 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5756 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5757 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5758 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5759 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5760 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5761 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5762 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5763 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5764 | |
| 5765 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5766 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5767 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5768 | |
| 5769 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 5770 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 5771 | if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5772 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5773 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5774 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5775 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 5776 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 5777 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 5778 | } |
| 5779 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5780 | /* The number of tables in the FROM clause is limited by the number of |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5781 | ** bits in a Bitmask |
| 5782 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5783 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5784 | if( pTabList->nSrc>BMS ){ |
| 5785 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5786 | return 0; |
| 5787 | } |
| 5788 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5789 | /* This function normally generates a nested loop for all tables in |
| 5790 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5791 | ** only generate code for the first table in pTabList and assume that |
| 5792 | ** any cursors associated with subsequent tables are uninitialized. |
| 5793 | */ |
| 5794 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5795 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5796 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5797 | ** return value. A single allocation is used to store the WhereInfo |
| 5798 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5799 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5800 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5801 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5802 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5803 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5804 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5805 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5806 | sqlite3DbFree(db, pWInfo); |
| 5807 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5808 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5809 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5810 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5811 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5812 | pWInfo->pParse = pParse; |
| 5813 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5814 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5815 | pWInfo->pResultSet = pResultSet; |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 5816 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5817 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5818 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5819 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5820 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5821 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 5822 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 5823 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5824 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5825 | #ifdef SQLITE_DEBUG |
| 5826 | sWLB.pNew->cId = '*'; |
| 5827 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5828 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5829 | /* Split the WHERE clause into separate subexpressions where each |
| 5830 | ** subexpression is separated by an AND operator. |
| 5831 | */ |
| 5832 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5833 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 5834 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5835 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5836 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5837 | ** expression and either jump over all of the code or fall thru. |
| 5838 | */ |
drh | 759e858 | 2014-01-02 21:05:10 +0000 | [diff] [blame] | 5839 | for(ii=0; ii<sWLB.pWC->nTerm; ii++){ |
| 5840 | if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ |
| 5841 | sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, |
| 5842 | SQLITE_JUMPIFNULL); |
| 5843 | sWLB.pWC->a[ii].wtFlags |= TERM_CODED; |
| 5844 | } |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5845 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5846 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5847 | /* Special case: No FROM clause |
| 5848 | */ |
| 5849 | if( nTabList==0 ){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5850 | if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5851 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5852 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5853 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5856 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5857 | ** |
| 5858 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5859 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5860 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5861 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5862 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5863 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5864 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5865 | ** for all tables to the left of a left join is important. Ticket #3015. |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 5866 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5867 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5868 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5869 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5870 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5871 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5872 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5873 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5874 | } |
| 5875 | #ifndef NDEBUG |
| 5876 | { |
| 5877 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5878 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5879 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5880 | assert( (m-1)==toTheLeft ); |
| 5881 | toTheLeft |= m; |
| 5882 | } |
| 5883 | } |
| 5884 | #endif |
| 5885 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5886 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5887 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5888 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5889 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5890 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5891 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5892 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5893 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5894 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5895 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5896 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5897 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 5898 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5899 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5900 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5901 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5902 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5903 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5904 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5907 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5908 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 5909 | /* Display all terms of the WHERE clause */ |
| 5910 | #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 5911 | if( sqlite3WhereTrace & 0x100 ){ |
| 5912 | int i; |
| 5913 | Vdbe *v = pParse->pVdbe; |
| 5914 | sqlite3ExplainBegin(v); |
| 5915 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
drh | 7afc8b0 | 2013-10-28 22:33:36 +0000 | [diff] [blame] | 5916 | sqlite3ExplainPrintf(v, "#%-2d ", i); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 5917 | sqlite3ExplainPush(v); |
| 5918 | whereExplainTerm(v, &sWLB.pWC->a[i]); |
| 5919 | sqlite3ExplainPop(v); |
| 5920 | sqlite3ExplainNL(v); |
| 5921 | } |
| 5922 | sqlite3ExplainFinish(v); |
| 5923 | sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
| 5924 | } |
| 5925 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5926 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5927 | rc = whereLoopAddAll(&sWLB); |
| 5928 | if( rc ) goto whereBeginError; |
| 5929 | |
| 5930 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5931 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5932 | if( sqlite3WhereTrace ){ |
| 5933 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5934 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5935 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5936 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5937 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 5938 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 5939 | whereLoopPrint(p, sWLB.pWC); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5940 | } |
| 5941 | } |
| 5942 | #endif |
| 5943 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5944 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5945 | if( db->mallocFailed ) goto whereBeginError; |
| 5946 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5947 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5948 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5949 | } |
| 5950 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5951 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5952 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5953 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5954 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5955 | goto whereBeginError; |
| 5956 | } |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5957 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5958 | if( sqlite3WhereTrace ){ |
| 5959 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5960 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5961 | if( pWInfo->nOBSat>0 ){ |
| 5962 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5963 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5964 | switch( pWInfo->eDistinct ){ |
| 5965 | case WHERE_DISTINCT_UNIQUE: { |
| 5966 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5967 | break; |
| 5968 | } |
| 5969 | case WHERE_DISTINCT_ORDERED: { |
| 5970 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5971 | break; |
| 5972 | } |
| 5973 | case WHERE_DISTINCT_UNORDERED: { |
| 5974 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5975 | break; |
| 5976 | } |
| 5977 | } |
| 5978 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5979 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 5980 | whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5981 | } |
| 5982 | } |
| 5983 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5984 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 5985 | if( pWInfo->nLevel>=2 |
| 5986 | && pResultSet!=0 |
| 5987 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 5988 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5989 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
drh | 67a5ec7 | 2013-09-03 14:03:47 +0000 | [diff] [blame] | 5990 | if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5991 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5992 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5993 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5994 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 5995 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 5996 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5997 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5998 | break; |
| 5999 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6000 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6001 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 6002 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 6003 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 6004 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 6005 | ){ |
| 6006 | break; |
| 6007 | } |
| 6008 | } |
| 6009 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6010 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 6011 | pWInfo->nLevel--; |
| 6012 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6013 | } |
| 6014 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6015 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 6016 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6017 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6018 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 6019 | ** to use a one-pass algorithm, determine if this is appropriate. |
drh | 24b7fe9 | 2013-09-30 19:33:06 +0000 | [diff] [blame] | 6020 | ** The one-pass algorithm only works if the WHERE clause constrains |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6021 | ** the statement to update a single row. |
| 6022 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 6023 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6024 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 6025 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6026 | pWInfo->okOnePass = 1; |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 6027 | if( HasRowid(pTabList->a[0].pTab) ){ |
| 6028 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 6029 | } |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6030 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 6031 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6032 | /* Open all tables in the pTabList and any indices selected for |
| 6033 | ** searching those tables. |
| 6034 | */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6035 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6036 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6037 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6038 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6039 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6040 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6041 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6042 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6043 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6044 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 6045 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 6046 | /* Do nothing */ |
| 6047 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6048 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6049 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6050 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 6051 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6052 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 6053 | }else if( IsVirtual(pTab) ){ |
| 6054 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6055 | }else |
| 6056 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6057 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6058 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6059 | int op = OP_OpenRead; |
| 6060 | if( pWInfo->okOnePass ){ |
| 6061 | op = OP_OpenWrite; |
| 6062 | pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 6063 | }; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6064 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6065 | assert( pTabItem->iCursor==pLevel->iTabCur ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6066 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 6067 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 6068 | if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6069 | Bitmask b = pTabItem->colUsed; |
| 6070 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6071 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 6072 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 6073 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6074 | assert( n<=pTab->nCol ); |
| 6075 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6076 | }else{ |
| 6077 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6078 | } |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 6079 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6080 | Index *pIx = pLoop->u.btree.pIndex; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6081 | int iIndexCur; |
| 6082 | int op = OP_OpenRead; |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 6083 | /* iIdxCur is always set if to a positive value if ONEPASS is possible */ |
| 6084 | assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 6085 | if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6086 | && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 |
| 6087 | ){ |
| 6088 | /* This is one term of an OR-optimization using the PRIMARY KEY of a |
| 6089 | ** WITHOUT ROWID table. No need for a separate index */ |
| 6090 | iIndexCur = pLevel->iTabCur; |
| 6091 | op = 0; |
| 6092 | }else if( pWInfo->okOnePass ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6093 | Index *pJ = pTabItem->pTab->pIndex; |
| 6094 | iIndexCur = iIdxCur; |
| 6095 | assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 6096 | while( ALWAYS(pJ) && pJ!=pIx ){ |
| 6097 | iIndexCur++; |
| 6098 | pJ = pJ->pNext; |
| 6099 | } |
| 6100 | op = OP_OpenWrite; |
| 6101 | pWInfo->aiCurOnePass[1] = iIndexCur; |
| 6102 | }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ |
| 6103 | iIndexCur = iIdxCur; |
| 6104 | }else{ |
| 6105 | iIndexCur = pParse->nTab++; |
| 6106 | } |
| 6107 | pLevel->iIdxCur = iIndexCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6108 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 6109 | assert( iIndexCur>=0 ); |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6110 | if( op ){ |
| 6111 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 6112 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
| 6113 | VdbeComment((v, "%s", pIx->zName)); |
| 6114 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6115 | } |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6116 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6117 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6118 | } |
| 6119 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 6120 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6121 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6122 | /* Generate the code to do the search. Each iteration of the for |
| 6123 | ** loop below generates code for a single nested loop of the VM |
| 6124 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6125 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6126 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6127 | for(ii=0; ii<nTabList; ii++){ |
| 6128 | pLevel = &pWInfo->a[ii]; |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6129 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 6130 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 6131 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 6132 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 6133 | if( db->mallocFailed ) goto whereBeginError; |
| 6134 | } |
| 6135 | #endif |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6136 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6137 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6138 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 6139 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6140 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 6141 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6142 | /* Done. */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6143 | VdbeModuleComment((v, "Begin WHERE-core")); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6144 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6145 | |
| 6146 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6147 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6148 | if( pWInfo ){ |
| 6149 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6150 | whereInfoFree(db, pWInfo); |
| 6151 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6152 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6153 | } |
| 6154 | |
| 6155 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6156 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6157 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6158 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6159 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6160 | Parse *pParse = pWInfo->pParse; |
| 6161 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6162 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6163 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6164 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6165 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6166 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6167 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6168 | /* Generate loop termination code. |
| 6169 | */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6170 | VdbeModuleComment((v, "End WHERE-core")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 6171 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6172 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6173 | int addr; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6174 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6175 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6176 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6177 | if( pLevel->op!=OP_Noop ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6178 | sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6179 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6180 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 6181 | VdbeCoverageIf(v, pLevel->op==OP_Next); |
| 6182 | VdbeCoverageIf(v, pLevel->op==OP_Prev); |
| 6183 | VdbeCoverageIf(v, pLevel->op==OP_VNext); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6184 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6185 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 6186 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6187 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6188 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6189 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6190 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 6191 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6192 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 6193 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 6194 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6195 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6196 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6197 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 6198 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6199 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6200 | if( pLevel->addrSkip ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6201 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 6202 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 6203 | sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 6204 | sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6205 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6206 | if( pLevel->iLeftJoin ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6207 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6208 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 6209 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 6210 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 6211 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 6212 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 6213 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6214 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 6215 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 6216 | if( pLevel->op==OP_Return ){ |
| 6217 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 6218 | }else{ |
| 6219 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 6220 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 6221 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6222 | } |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6223 | VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6224 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6225 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6226 | |
| 6227 | /* The "break" point is here, just past the end of the outer loop. |
| 6228 | ** Set it. |
| 6229 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6230 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6231 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6232 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6233 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6234 | int k, last; |
| 6235 | VdbeOp *pOp; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6236 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6237 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6238 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 6239 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6240 | pLoop = pLevel->pWLoop; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6241 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6242 | /* For a co-routine, change all OP_Column references to the table of |
| 6243 | ** the co-routine into OP_SCopy of result contained in a register. |
| 6244 | ** OP_Rowid becomes OP_Null. |
| 6245 | */ |
dan | fbf0f0e | 2014-03-03 14:20:30 +0000 | [diff] [blame] | 6246 | if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6247 | last = sqlite3VdbeCurrentAddr(v); |
| 6248 | k = pLevel->addrBody; |
| 6249 | pOp = sqlite3VdbeGetOp(v, k); |
| 6250 | for(; k<last; k++, pOp++){ |
| 6251 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6252 | if( pOp->opcode==OP_Column ){ |
drh | c438df1 | 2014-04-03 16:29:31 +0000 | [diff] [blame] | 6253 | pOp->opcode = OP_Copy; |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6254 | pOp->p1 = pOp->p2 + pTabItem->regResult; |
| 6255 | pOp->p2 = pOp->p3; |
| 6256 | pOp->p3 = 0; |
| 6257 | }else if( pOp->opcode==OP_Rowid ){ |
| 6258 | pOp->opcode = OP_Null; |
| 6259 | pOp->p1 = 0; |
| 6260 | pOp->p3 = 0; |
| 6261 | } |
| 6262 | } |
| 6263 | continue; |
| 6264 | } |
| 6265 | |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6266 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 6267 | ** Except, do not close cursors that will be reused by the OR optimization |
| 6268 | ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors |
| 6269 | ** created for the ONEPASS optimization. |
| 6270 | */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6271 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 6272 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6273 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6274 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6275 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6276 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6277 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 6278 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6279 | if( (ws & WHERE_INDEXED)!=0 |
| 6280 | && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 |
| 6281 | && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] |
| 6282 | ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6283 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 6284 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 6287 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 6288 | ** from the index instead of from the table where possible. In some cases |
| 6289 | ** this optimization prevents the table from ever being read, which can |
| 6290 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6291 | ** |
| 6292 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 6293 | ** sqlite3WhereEnd will have created code that references the table |
| 6294 | ** directly. This loop scans all that code looking for opcodes |
| 6295 | ** that reference the table and converts them into opcodes that |
| 6296 | ** reference the index. |
| 6297 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6298 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 6299 | pIdx = pLoop->u.btree.pIndex; |
| 6300 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 6301 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6302 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6303 | if( pIdx && !db->mallocFailed ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6304 | last = sqlite3VdbeCurrentAddr(v); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6305 | k = pLevel->addrBody; |
| 6306 | pOp = sqlite3VdbeGetOp(v, k); |
| 6307 | for(; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6308 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6309 | if( pOp->opcode==OP_Column ){ |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 6310 | int x = pOp->p2; |
drh | 511717c | 2013-11-08 17:13:23 +0000 | [diff] [blame] | 6311 | assert( pIdx->pTable==pTab ); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 6312 | if( !HasRowid(pTab) ){ |
| 6313 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 6314 | x = pPk->aiColumn[x]; |
| 6315 | } |
| 6316 | x = sqlite3ColumnOfIndex(pIdx, x); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6317 | if( x>=0 ){ |
| 6318 | pOp->p2 = x; |
| 6319 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6320 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6321 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6322 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6323 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6324 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6325 | } |
| 6326 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6327 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6328 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6329 | |
| 6330 | /* Final cleanup |
| 6331 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 6332 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6333 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6334 | return; |
| 6335 | } |