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 | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 368 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 369 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 370 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 371 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 372 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 373 | ** that the collating sequence does not change. For example: |
| 374 | ** "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] | 375 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 376 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 377 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 378 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 379 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 380 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 381 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 382 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 383 | if( expRight==expLeft ){ |
| 384 | /* Either X and Y both have COLLATE operator or neither do */ |
| 385 | if( expRight ){ |
| 386 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 387 | ** used by clearing the EP_Collate flag from Y. */ |
| 388 | pExpr->pRight->flags &= ~EP_Collate; |
| 389 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 390 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 391 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 392 | ** it to be searched first. */ |
| 393 | pExpr->pLeft->flags |= EP_Collate; |
| 394 | } |
| 395 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 396 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 397 | if( pExpr->op>=TK_GT ){ |
| 398 | assert( TK_LT==TK_GT+2 ); |
| 399 | assert( TK_GE==TK_LE+2 ); |
| 400 | assert( TK_GT>TK_EQ ); |
| 401 | assert( TK_GT<TK_LE ); |
| 402 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 403 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 404 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 408 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 409 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 410 | static u16 operatorMask(int op){ |
| 411 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 412 | assert( allowedOp(op) ); |
| 413 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 414 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 415 | }else if( op==TK_ISNULL ){ |
| 416 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 417 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 418 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 419 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 420 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 421 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 422 | assert( op!=TK_IN || c==WO_IN ); |
| 423 | assert( op!=TK_EQ || c==WO_EQ ); |
| 424 | assert( op!=TK_LT || c==WO_LT ); |
| 425 | assert( op!=TK_LE || c==WO_LE ); |
| 426 | assert( op!=TK_GT || c==WO_GT ); |
| 427 | assert( op!=TK_GE || c==WO_GE ); |
| 428 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 432 | ** Advance to the next WhereTerm that matches according to the criteria |
| 433 | ** established when the pScan object was initialized by whereScanInit(). |
| 434 | ** Return NULL if there are no more matching WhereTerms. |
| 435 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 436 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 437 | int iCur; /* The cursor on the LHS of the term */ |
| 438 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 439 | Expr *pX; /* An expression being tested */ |
| 440 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 441 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 442 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 443 | |
| 444 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 445 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 446 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 447 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 448 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | e1a086e | 2013-10-28 20:15:56 +0000 | [diff] [blame] | 449 | if( pTerm->leftCursor==iCur |
| 450 | && pTerm->u.leftColumn==iColumn |
| 451 | && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 452 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 453 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 454 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 455 | ){ |
| 456 | int j; |
| 457 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 458 | assert( pX->op==TK_COLUMN ); |
| 459 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 460 | if( pScan->aEquiv[j]==pX->iTable |
| 461 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | if( j==pScan->nEquiv ){ |
| 466 | pScan->aEquiv[j] = pX->iTable; |
| 467 | pScan->aEquiv[j+1] = pX->iColumn; |
| 468 | pScan->nEquiv += 2; |
| 469 | } |
| 470 | } |
| 471 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 472 | /* Verify the affinity and collating sequence match */ |
| 473 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 474 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 475 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 476 | pX = pTerm->pExpr; |
| 477 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 478 | continue; |
| 479 | } |
| 480 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 481 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 482 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 483 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 484 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 485 | continue; |
| 486 | } |
| 487 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 488 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 489 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 490 | && pX->iTable==pScan->aEquiv[0] |
| 491 | && pX->iColumn==pScan->aEquiv[1] |
| 492 | ){ |
| 493 | continue; |
| 494 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 495 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 496 | return pTerm; |
| 497 | } |
| 498 | } |
| 499 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 500 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 501 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 502 | } |
| 503 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 504 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 505 | pScan->iEquiv += 2; |
| 506 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 512 | ** first match. Return NULL if there are no matches. |
| 513 | ** |
| 514 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 515 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 516 | ** iCur. The <op> must be one of the operators described by opMask. |
| 517 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 518 | ** If the search is for X and the WHERE clause contains terms of the |
| 519 | ** form X=Y then this routine might also return terms of the form |
| 520 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 521 | ** but is enough to handle most commonly occurring SQL statements. |
| 522 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 523 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 524 | ** index pIdx. |
| 525 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 526 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 527 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 528 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 529 | int iCur, /* Cursor to scan for */ |
| 530 | int iColumn, /* Column to scan for */ |
| 531 | u32 opMask, /* Operator(s) to scan for */ |
| 532 | Index *pIdx /* Must be compatible with this index */ |
| 533 | ){ |
| 534 | int j; |
| 535 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 536 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 537 | pScan->pOrigWC = pWC; |
| 538 | pScan->pWC = pWC; |
| 539 | if( pIdx && iColumn>=0 ){ |
| 540 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 541 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 542 | if( NEVER(j>pIdx->nColumn) ) return 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 543 | } |
| 544 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 545 | }else{ |
| 546 | pScan->idxaff = 0; |
| 547 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 548 | } |
| 549 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 550 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 551 | pScan->aEquiv[0] = iCur; |
| 552 | pScan->aEquiv[1] = iColumn; |
| 553 | pScan->nEquiv = 2; |
| 554 | pScan->iEquiv = 2; |
| 555 | return whereScanNext(pScan); |
| 556 | } |
| 557 | |
| 558 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 559 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 560 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 561 | ** the WO_xx operator codes specified by the op parameter. |
| 562 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 563 | ** |
| 564 | ** The term returned might by Y=<expr> if there is another constraint in |
| 565 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 566 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 567 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 568 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 569 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 570 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 571 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 572 | ** and ... and A9=A10 and A10=<expr>. |
| 573 | ** |
| 574 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 575 | ** then try for the one with no dependencies on <expr> - in other words where |
| 576 | ** <expr> is a constant expression of some kind. Only return entries of |
| 577 | ** 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] | 578 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 579 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 580 | */ |
| 581 | static WhereTerm *findTerm( |
| 582 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 583 | int iCur, /* Cursor number of LHS */ |
| 584 | int iColumn, /* Column number of LHS */ |
| 585 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 586 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 587 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 588 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 589 | WhereTerm *pResult = 0; |
| 590 | WhereTerm *p; |
| 591 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 592 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 593 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 594 | while( p ){ |
| 595 | if( (p->prereqRight & notReady)==0 ){ |
| 596 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 597 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 598 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 599 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 600 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 601 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 602 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 603 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 604 | } |
| 605 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 606 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 607 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 611 | */ |
| 612 | static void exprAnalyzeAll( |
| 613 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 614 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 615 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 616 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 617 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 618 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 622 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 623 | /* |
| 624 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 625 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 626 | ** so and false if not. |
| 627 | ** |
| 628 | ** In order for the operator to be optimizible, the RHS must be a string |
| 629 | ** literal that does not begin with a wildcard. |
| 630 | */ |
| 631 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 632 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 633 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 634 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 635 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 636 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 637 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 638 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 639 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 640 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 641 | int c; /* One character in z[] */ |
| 642 | int cnt; /* Number of non-wildcard prefix characters */ |
| 643 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 644 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 645 | sqlite3_value *pVal = 0; |
| 646 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 647 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 648 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 649 | return 0; |
| 650 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 651 | #ifdef SQLITE_EBCDIC |
| 652 | if( *pnoCase ) return 0; |
| 653 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 654 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 655 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 656 | if( pLeft->op!=TK_COLUMN |
| 657 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 658 | || IsVirtual(pLeft->pTab) |
| 659 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 660 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 661 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 662 | return 0; |
| 663 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 664 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 665 | |
drh | 6ade453 | 2014-01-16 15:31:41 +0000 | [diff] [blame] | 666 | pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 667 | op = pRight->op; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 668 | if( op==TK_VARIABLE ){ |
| 669 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 670 | int iCol = pRight->iColumn; |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 671 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 672 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 673 | z = (char *)sqlite3_value_text(pVal); |
| 674 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 675 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 676 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 677 | }else if( op==TK_STRING ){ |
| 678 | z = pRight->u.zToken; |
| 679 | } |
| 680 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 681 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 682 | 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] | 683 | cnt++; |
| 684 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 685 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 686 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 687 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 688 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 689 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 690 | *ppPrefix = pPrefix; |
| 691 | if( op==TK_VARIABLE ){ |
| 692 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 693 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 694 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 695 | /* If the rhs of the LIKE expression is a variable, and the current |
| 696 | ** value of the variable means there is no need to invoke the LIKE |
| 697 | ** function, then no OP_Variable will be added to the program. |
| 698 | ** This causes problems for the sqlite3_bind_parameter_name() |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 699 | ** API. To work around them, add a dummy OP_Variable here. |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 700 | */ |
| 701 | int r1 = sqlite3GetTempReg(pParse); |
| 702 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 703 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 704 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | }else{ |
| 708 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 709 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 710 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 711 | |
| 712 | sqlite3ValueFree(pVal); |
| 713 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 714 | } |
| 715 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 716 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 717 | |
| 718 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 719 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 720 | ** Check to see if the given expression is of the form |
| 721 | ** |
| 722 | ** column MATCH expr |
| 723 | ** |
| 724 | ** If it is then return TRUE. If not, return FALSE. |
| 725 | */ |
| 726 | static int isMatchOfColumn( |
| 727 | Expr *pExpr /* Test this expression */ |
| 728 | ){ |
| 729 | ExprList *pList; |
| 730 | |
| 731 | if( pExpr->op!=TK_FUNCTION ){ |
| 732 | return 0; |
| 733 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 734 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 735 | return 0; |
| 736 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 737 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 738 | if( pList->nExpr!=2 ){ |
| 739 | return 0; |
| 740 | } |
| 741 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 742 | return 0; |
| 743 | } |
| 744 | return 1; |
| 745 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 746 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 747 | |
| 748 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 749 | ** If the pBase expression originated in the ON or USING clause of |
| 750 | ** a join, then transfer the appropriate markings over to derived. |
| 751 | */ |
| 752 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 753 | if( pDerived ){ |
| 754 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 755 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 756 | } |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 757 | } |
| 758 | |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 759 | /* |
| 760 | ** Mark term iChild as being a child of term iParent |
| 761 | */ |
| 762 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 763 | pWC->a[iChild].iParent = iParent; |
| 764 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 765 | pWC->a[iParent].nChild++; |
| 766 | } |
| 767 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 768 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 769 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 770 | ** Analyze a term that consists of two or more OR-connected |
| 771 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 772 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 773 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 774 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 775 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 776 | ** This routine analyzes terms such as the middle term in the above example. |
| 777 | ** A WhereOrTerm object is computed and attached to the term under |
| 778 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 779 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 780 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 781 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 782 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 783 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 784 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 785 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 786 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 787 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 788 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 789 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 790 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 791 | ** (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] | 792 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 793 | ** CASE 1: |
| 794 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 795 | ** 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] | 796 | ** a single table T (as shown in example B above) then create a new virtual |
| 797 | ** term that is an equivalent IN expression. In other words, if the term |
| 798 | ** being analyzed is: |
| 799 | ** |
| 800 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 801 | ** |
| 802 | ** then create a new virtual term like this: |
| 803 | ** |
| 804 | ** x IN (expr1,expr2,expr3) |
| 805 | ** |
| 806 | ** CASE 2: |
| 807 | ** |
| 808 | ** If all subterms are indexable by a single table T, then set |
| 809 | ** |
| 810 | ** WhereTerm.eOperator = WO_OR |
| 811 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 812 | ** |
| 813 | ** A subterm is "indexable" if it is of the form |
| 814 | ** "T.C <op> <expr>" where C is any column of table T and |
| 815 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 816 | ** A subterm is also indexable if it is an AND of two or more |
| 817 | ** subsubterms at least one of which is indexable. Indexable AND |
| 818 | ** subterms have their eOperator set to WO_AND and they have |
| 819 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 820 | ** |
| 821 | ** From another point of view, "indexable" means that the subterm could |
| 822 | ** potentially be used with an index if an appropriate index exists. |
| 823 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 824 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 825 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 826 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 827 | ** All examples A through E above satisfy case 2. But if a term |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 828 | ** also satisfies case 1 (such as B) we know that the optimizer will |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 829 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 830 | ** satisfied. |
| 831 | ** |
| 832 | ** It might be the case that multiple tables are indexable. For example, |
| 833 | ** (E) above is indexable on tables P, Q, and R. |
| 834 | ** |
| 835 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 836 | ** separate indices to find rowids for each subterm and composing |
| 837 | ** the union of all rowids using a RowSet object. This is similar |
| 838 | ** to "bitmap indices" in other database engines. |
| 839 | ** |
| 840 | ** OTHERWISE: |
| 841 | ** |
| 842 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 843 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 844 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 845 | static void exprAnalyzeOrTerm( |
| 846 | SrcList *pSrc, /* the FROM clause */ |
| 847 | WhereClause *pWC, /* the complete WHERE clause */ |
| 848 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 849 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 850 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 851 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 852 | sqlite3 *db = pParse->db; /* Database connection */ |
| 853 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 854 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 855 | int i; /* Loop counters */ |
| 856 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 857 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 858 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 859 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 860 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 861 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 862 | /* |
| 863 | ** Break the OR clause into its separate subterms. The subterms are |
| 864 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 865 | ** object that is attached to the original OR clause term. |
| 866 | */ |
| 867 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 868 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 869 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 870 | if( pOrInfo==0 ) return; |
| 871 | pTerm->wtFlags |= TERM_ORINFO; |
| 872 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 873 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 874 | whereSplit(pOrWc, pExpr, TK_OR); |
| 875 | exprAnalyzeAll(pSrc, pOrWc); |
| 876 | if( db->mallocFailed ) return; |
| 877 | assert( pOrWc->nTerm>=2 ); |
| 878 | |
| 879 | /* |
| 880 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 881 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 882 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 883 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 884 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 885 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 886 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 887 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 888 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 889 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 890 | if( pAndInfo ){ |
| 891 | WhereClause *pAndWC; |
| 892 | WhereTerm *pAndTerm; |
| 893 | int j; |
| 894 | Bitmask b = 0; |
| 895 | pOrTerm->u.pAndInfo = pAndInfo; |
| 896 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 897 | pOrTerm->eOperator = WO_AND; |
| 898 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 899 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 900 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 901 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 902 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 903 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 904 | if( !db->mallocFailed ){ |
| 905 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 906 | assert( pAndTerm->pExpr ); |
| 907 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 908 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 909 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | indexable &= b; |
| 913 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 914 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 915 | /* Skip this term for now. We revisit it when we process the |
| 916 | ** corresponding TERM_VIRTUAL term */ |
| 917 | }else{ |
| 918 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 919 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 920 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 921 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 922 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 923 | } |
| 924 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 925 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 926 | chngToIN = 0; |
| 927 | }else{ |
| 928 | chngToIN &= b; |
| 929 | } |
| 930 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 931 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 932 | |
| 933 | /* |
| 934 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 935 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 936 | */ |
| 937 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 938 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 942 | ** we have to do some additional checking to see if case 1 really |
| 943 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 944 | ** |
| 945 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 946 | ** that there is no possibility of transforming the OR clause into an |
| 947 | ** IN operator because one or more terms in the OR clause contain |
| 948 | ** something other than == on a column in the single table. The 1-bit |
| 949 | ** case means that every term of the OR clause is of the form |
| 950 | ** "table.column=expr" for some single table. The one bit that is set |
| 951 | ** will correspond to the common table. We still need to check to make |
| 952 | ** sure the same column is used on all terms. The 2-bit case is when |
| 953 | ** the all terms are of the form "table1.column=table2.column". It |
| 954 | ** might be possible to form an IN operator with either table1.column |
| 955 | ** or table2.column as the LHS if either is common to every term of |
| 956 | ** the OR clause. |
| 957 | ** |
| 958 | ** Note that terms of the form "table.column1=table.column2" (the |
| 959 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 960 | */ |
| 961 | if( chngToIN ){ |
| 962 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 963 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 964 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 965 | int j = 0; /* Loop counter */ |
| 966 | |
| 967 | /* Search for a table and column that appears on one side or the |
| 968 | ** other of the == operator in every subterm. That table and column |
| 969 | ** will be recorded in iCursor and iColumn. There might not be any |
| 970 | ** such table and column. Set okToChngToIN if an appropriate table |
| 971 | ** and column is found but leave okToChngToIN false if not found. |
| 972 | */ |
| 973 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 974 | pOrTerm = pOrWc->a; |
| 975 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 976 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 977 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 978 | if( pOrTerm->leftCursor==iCursor ){ |
| 979 | /* This is the 2-bit case and we are on the second iteration and |
| 980 | ** current term is from the first iteration. So skip this term. */ |
| 981 | assert( j==1 ); |
| 982 | continue; |
| 983 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 984 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 985 | /* This term must be of the form t1.a==t2.b where t2 is in the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 986 | ** chngToIN set but t1 is not. This term will be either preceded |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 987 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 988 | ** and use its inversion. */ |
| 989 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 990 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 991 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 992 | continue; |
| 993 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 994 | iColumn = pOrTerm->u.leftColumn; |
| 995 | iCursor = pOrTerm->leftCursor; |
| 996 | break; |
| 997 | } |
| 998 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 999 | /* No candidate table+column was found. This can only occur |
| 1000 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1001 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1002 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1003 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1006 | testcase( j==1 ); |
| 1007 | |
| 1008 | /* We have found a candidate table and column. Check to see if that |
| 1009 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1010 | okToChngToIN = 1; |
| 1011 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1012 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1013 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1014 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1015 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1016 | okToChngToIN = 0; |
| 1017 | }else{ |
| 1018 | int affLeft, affRight; |
| 1019 | /* If the right-hand side is also a column, then the affinities |
| 1020 | ** of both right and left sides must be such that no type |
| 1021 | ** conversions are required on the right. (Ticket #2249) |
| 1022 | */ |
| 1023 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1024 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1025 | if( affRight!=0 && affRight!=affLeft ){ |
| 1026 | okToChngToIN = 0; |
| 1027 | }else{ |
| 1028 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1035 | ** case 1. In that case, construct a new virtual term that is |
| 1036 | ** pTerm converted into an IN operator. |
| 1037 | */ |
| 1038 | if( okToChngToIN ){ |
| 1039 | Expr *pDup; /* A transient duplicate expression */ |
| 1040 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1041 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1042 | Expr *pNew; /* The complete IN operator */ |
| 1043 | |
| 1044 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1045 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1046 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1047 | assert( pOrTerm->leftCursor==iCursor ); |
| 1048 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1049 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1050 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1051 | pLeft = pOrTerm->pExpr->pLeft; |
| 1052 | } |
| 1053 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1054 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1055 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1056 | if( pNew ){ |
| 1057 | int idxNew; |
| 1058 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1059 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1060 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1061 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1062 | testcase( idxNew==0 ); |
| 1063 | exprAnalyze(pSrc, pWC, idxNew); |
| 1064 | pTerm = &pWC->a[idxTerm]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1065 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1066 | }else{ |
| 1067 | sqlite3ExprListDelete(db, pList); |
| 1068 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1069 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1070 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1071 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1072 | } |
| 1073 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1074 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1075 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1076 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1077 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1078 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1079 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1080 | ** |
| 1081 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1082 | ** to the standard form of "X <op> <expr>". |
| 1083 | ** |
| 1084 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1085 | ** columns, then the original expression is unchanged and a new virtual |
| 1086 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1087 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1088 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1089 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1090 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1091 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1092 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1093 | static void exprAnalyze( |
| 1094 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1095 | WhereClause *pWC, /* the WHERE clause */ |
| 1096 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1097 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1098 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1099 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1100 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1101 | Expr *pExpr; /* The expression to be analyzed */ |
| 1102 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1103 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1104 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1105 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1106 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1107 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1108 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1109 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1110 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1111 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1112 | if( db->mallocFailed ){ |
| 1113 | return; |
| 1114 | } |
| 1115 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1116 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1117 | pExpr = pTerm->pExpr; |
| 1118 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1119 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1120 | op = pExpr->op; |
| 1121 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1122 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1123 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1124 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1125 | }else{ |
| 1126 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1127 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1128 | }else if( op==TK_ISNULL ){ |
| 1129 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1130 | }else{ |
| 1131 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1132 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1133 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1134 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1135 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1136 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1137 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1138 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1139 | } |
| 1140 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1141 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1142 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1143 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1144 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1145 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1146 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1147 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1148 | if( pLeft->op==TK_COLUMN ){ |
| 1149 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1150 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1151 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1152 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1153 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1154 | WhereTerm *pNew; |
| 1155 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1156 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1157 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1158 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1159 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1160 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1161 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1164 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1165 | if( idxNew==0 ) return; |
| 1166 | pNew = &pWC->a[idxNew]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1167 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1168 | pTerm = &pWC->a[idxTerm]; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1169 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1170 | if( pExpr->op==TK_EQ |
| 1171 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1172 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1173 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1174 | pTerm->eOperator |= WO_EQUIV; |
| 1175 | eExtraOp = WO_EQUIV; |
| 1176 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1177 | }else{ |
| 1178 | pDup = pExpr; |
| 1179 | pNew = pTerm; |
| 1180 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1181 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1182 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1183 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1184 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1185 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1186 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1187 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1188 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1191 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1192 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1193 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1194 | ** that define the range that the BETWEEN implements. For example: |
| 1195 | ** |
| 1196 | ** a BETWEEN b AND c |
| 1197 | ** |
| 1198 | ** is converted into: |
| 1199 | ** |
| 1200 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1201 | ** |
| 1202 | ** The two new terms are added onto the end of the WhereClause object. |
| 1203 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1204 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1205 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1206 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1207 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1208 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1209 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1210 | int i; |
| 1211 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1212 | assert( pList!=0 ); |
| 1213 | assert( pList->nExpr==2 ); |
| 1214 | for(i=0; i<2; i++){ |
| 1215 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1216 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1217 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1218 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1219 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1220 | transferJoinMarkings(pNewExpr, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1221 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1222 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1223 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1224 | pTerm = &pWC->a[idxTerm]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1225 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1226 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1227 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1228 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1229 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1230 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1231 | /* Analyze a term that is composed of two or more subterms connected by |
| 1232 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1233 | */ |
| 1234 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1235 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1236 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1237 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1238 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1239 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1240 | |
| 1241 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1242 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1243 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1244 | ** |
| 1245 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1246 | ** |
| 1247 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1248 | ** |
| 1249 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1250 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1251 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1252 | if( pWC->op==TK_AND |
| 1253 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1254 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1255 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1256 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1257 | Expr *pNewExpr1; |
| 1258 | Expr *pNewExpr2; |
| 1259 | int idxNew1; |
| 1260 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1261 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1262 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1263 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1264 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1265 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1266 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1267 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1268 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1269 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1270 | /* The point is to increment the last character before the first |
| 1271 | ** wildcard. But if we increment '@', that will push it into the |
| 1272 | ** alphabetic range where case conversions will mess up the |
| 1273 | ** inequality. To avoid this, make sure to also run the full |
| 1274 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1275 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1276 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1277 | c = sqlite3UpperToLower[c]; |
| 1278 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1279 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1280 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1281 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1282 | sCollSeqName.n = 6; |
| 1283 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1284 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1285 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1286 | pStr1, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1287 | transferJoinMarkings(pNewExpr1, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1288 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1289 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1290 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1291 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1292 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1293 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1294 | pStr2, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1295 | transferJoinMarkings(pNewExpr2, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1296 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1297 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1298 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1299 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1300 | if( isComplete ){ |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1301 | markTermAsChild(pWC, idxNew1, idxTerm); |
| 1302 | markTermAsChild(pWC, idxNew2, idxTerm); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 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; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1335 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1336 | pTerm = &pWC->a[idxTerm]; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1337 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1338 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1339 | } |
| 1340 | } |
| 1341 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1342 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1343 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1344 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1345 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1346 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1347 | ** virtual term of that form. |
| 1348 | ** |
| 1349 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1350 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1351 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1352 | ** the start of the loop will prevent any results from being returned. |
| 1353 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1354 | if( pExpr->op==TK_NOTNULL |
| 1355 | && pExpr->pLeft->op==TK_COLUMN |
| 1356 | && pExpr->pLeft->iColumn>=0 |
drh | d7d7147 | 2014-10-22 19:57:16 +0000 | [diff] [blame] | 1357 | && OptimizationEnabled(db, SQLITE_Stat34) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1358 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1359 | Expr *pNewExpr; |
| 1360 | Expr *pLeft = pExpr->pLeft; |
| 1361 | int idxNew; |
| 1362 | WhereTerm *pNewTerm; |
| 1363 | |
| 1364 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1365 | sqlite3ExprDup(db, pLeft, 0), |
| 1366 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1367 | |
| 1368 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1369 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1370 | if( idxNew ){ |
| 1371 | pNewTerm = &pWC->a[idxNew]; |
| 1372 | pNewTerm->prereqRight = 0; |
| 1373 | pNewTerm->leftCursor = pLeft->iTable; |
| 1374 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1375 | pNewTerm->eOperator = WO_GT; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1376 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1377 | pTerm = &pWC->a[idxTerm]; |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1378 | pTerm->wtFlags |= TERM_COPIED; |
| 1379 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1380 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1381 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1382 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1383 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1384 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1385 | ** an index for tables to the left of the join. |
| 1386 | */ |
| 1387 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1390 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1391 | ** This function searches pList for an entry that matches the iCol-th column |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1392 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1393 | ** |
| 1394 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1395 | ** no expression is found, -1 is returned. |
| 1396 | */ |
| 1397 | static int findIndexCol( |
| 1398 | Parse *pParse, /* Parse context */ |
| 1399 | ExprList *pList, /* Expression list to search */ |
| 1400 | int iBase, /* Cursor for table associated with pIdx */ |
| 1401 | Index *pIdx, /* Index to match column of */ |
| 1402 | int iCol /* Column of index to match */ |
| 1403 | ){ |
| 1404 | int i; |
| 1405 | const char *zColl = pIdx->azColl[iCol]; |
| 1406 | |
| 1407 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1408 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1409 | if( p->op==TK_COLUMN |
| 1410 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1411 | && p->iTable==iBase |
| 1412 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1413 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1414 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1415 | return i; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | return -1; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1424 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1425 | ** is redundant. |
| 1426 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1427 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1428 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1429 | */ |
| 1430 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1431 | Parse *pParse, /* Parsing context */ |
| 1432 | SrcList *pTabList, /* The FROM clause */ |
| 1433 | WhereClause *pWC, /* The WHERE clause */ |
| 1434 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1435 | ){ |
| 1436 | Table *pTab; |
| 1437 | Index *pIdx; |
| 1438 | int i; |
| 1439 | int iBase; |
| 1440 | |
| 1441 | /* If there is more than one table or sub-select in the FROM clause of |
| 1442 | ** this query, then it will not be possible to show that the DISTINCT |
| 1443 | ** clause is redundant. */ |
| 1444 | if( pTabList->nSrc!=1 ) return 0; |
| 1445 | iBase = pTabList->a[0].iCursor; |
| 1446 | pTab = pTabList->a[0].pTab; |
| 1447 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1448 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1449 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1450 | ** current SELECT is a correlated sub-query. |
| 1451 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1452 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1453 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1454 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /* Loop through all indices on the table, checking each to see if it makes |
| 1458 | ** the DISTINCT qualifier redundant. It does so if: |
| 1459 | ** |
| 1460 | ** 1. The index is itself UNIQUE, and |
| 1461 | ** |
| 1462 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1463 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1464 | ** where X is a constant value. The collation sequences of the |
| 1465 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1466 | ** |
| 1467 | ** 3. All of those index columns for which the WHERE clause does not |
| 1468 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1469 | */ |
| 1470 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 1471 | if( !IsUniqueIndex(pIdx) ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1472 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 1473 | i16 iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1474 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1475 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1476 | if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){ |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1481 | if( i==pIdx->nKeyCol ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1482 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1483 | return 1; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1489 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1490 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1491 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1492 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1493 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 1494 | static LogEst estLog(LogEst N){ |
drh | 696964d | 2014-06-12 15:46:46 +0000 | [diff] [blame] | 1495 | return N<=10 ? 0 : sqlite3LogEst(N) - 33; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1498 | /* |
| 1499 | ** Two routines for printing the content of an sqlite3_index_info |
| 1500 | ** structure. Used for testing and debugging only. If neither |
| 1501 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1502 | ** are no-ops. |
| 1503 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1504 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1505 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1506 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1507 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1508 | for(i=0; i<p->nConstraint; i++){ |
| 1509 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1510 | i, |
| 1511 | p->aConstraint[i].iColumn, |
| 1512 | p->aConstraint[i].iTermOffset, |
| 1513 | p->aConstraint[i].op, |
| 1514 | p->aConstraint[i].usable); |
| 1515 | } |
| 1516 | for(i=0; i<p->nOrderBy; i++){ |
| 1517 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1518 | i, |
| 1519 | p->aOrderBy[i].iColumn, |
| 1520 | p->aOrderBy[i].desc); |
| 1521 | } |
| 1522 | } |
| 1523 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1524 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1525 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1526 | for(i=0; i<p->nConstraint; i++){ |
| 1527 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1528 | i, |
| 1529 | p->aConstraintUsage[i].argvIndex, |
| 1530 | p->aConstraintUsage[i].omit); |
| 1531 | } |
| 1532 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1533 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1534 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1535 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 1536 | sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1537 | } |
| 1538 | #else |
| 1539 | #define TRACE_IDX_INPUTS(A) |
| 1540 | #define TRACE_IDX_OUTPUTS(A) |
| 1541 | #endif |
| 1542 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1543 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1544 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1545 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1546 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1547 | ** index existed. |
| 1548 | */ |
| 1549 | static int termCanDriveIndex( |
| 1550 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1551 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1552 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1553 | ){ |
| 1554 | char aff; |
| 1555 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1556 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1557 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1558 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1559 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1560 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 1561 | return 1; |
| 1562 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1563 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1564 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1565 | |
| 1566 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1567 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1568 | ** Generate code to construct the Index object for an automatic index |
| 1569 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1570 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1571 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1572 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1573 | Parse *pParse, /* The parsing context */ |
| 1574 | WhereClause *pWC, /* The WHERE clause */ |
| 1575 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1576 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1577 | WhereLevel *pLevel /* Write new index here */ |
| 1578 | ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1579 | int nKeyCol; /* Number of columns in the constructed index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1580 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1581 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1582 | Index *pIdx; /* Object describing the transient index */ |
| 1583 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1584 | int addrInit; /* Address of the initialization bypass jump */ |
| 1585 | Table *pTable; /* The table being indexed */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1586 | int addrTop; /* Top of the index fill loop */ |
| 1587 | int regRecord; /* Register holding an index record */ |
| 1588 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1589 | int i; /* Loop counter */ |
| 1590 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1591 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1592 | WhereLoop *pLoop; /* The Loop object */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 1593 | char *zNotUsed; /* Extra space on the end of pIdx */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1594 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 1595 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1596 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1597 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 1598 | int iContinue = 0; /* Jump here to skip excluded rows */ |
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 | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1614 | if( pLoop->prereq==0 |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame^] | 1615 | && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1616 | && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ |
| 1617 | pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
| 1618 | sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); |
| 1619 | } |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1620 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 1621 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1622 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1623 | testcase( iCol==BMS ); |
| 1624 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1625 | if( !sentWarning ){ |
| 1626 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 1627 | "automatic index on %s(%s)", pTable->zName, |
| 1628 | pTable->aCol[iCol].zName); |
| 1629 | sentWarning = 1; |
| 1630 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1631 | if( (idxCols & cMask)==0 ){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1632 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 1633 | goto end_auto_index_create; |
| 1634 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1635 | pLoop->aLTerm[nKeyCol++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1636 | idxCols |= cMask; |
| 1637 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1638 | } |
| 1639 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1640 | assert( nKeyCol>0 ); |
| 1641 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1642 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 1643 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1644 | |
| 1645 | /* Count the number of additional columns needed to create a |
| 1646 | ** covering index. A "covering index" is an index that contains all |
| 1647 | ** columns that are needed by the query. With a covering index, the |
| 1648 | ** original table never needs to be accessed. Automatic indices must |
| 1649 | ** be a covering index because the index will not be updated if the |
| 1650 | ** original table changes and the index and table cannot both be used |
| 1651 | ** if they go out of sync. |
| 1652 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1653 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1654 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1655 | testcase( pTable->nCol==BMS-1 ); |
| 1656 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1657 | for(i=0; i<mxBitCol; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1658 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1659 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1660 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1661 | nKeyCol += pTable->nCol - BMS + 1; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1662 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1663 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1664 | |
| 1665 | /* Construct the Index object to describe this index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1666 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1667 | if( pIdx==0 ) goto end_auto_index_create; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1668 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1669 | pIdx->zName = "auto-index"; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1670 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1671 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1672 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1673 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1674 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1675 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1676 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 1677 | testcase( iCol==BMS-1 ); |
| 1678 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1679 | if( (idxCols & cMask)==0 ){ |
| 1680 | Expr *pX = pTerm->pExpr; |
| 1681 | idxCols |= cMask; |
| 1682 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 1683 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 1684 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1685 | n++; |
| 1686 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1689 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1690 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1691 | /* Add additional columns needed to make the automatic index into |
| 1692 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1693 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1694 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1695 | pIdx->aiColumn[n] = i; |
| 1696 | pIdx->azColl[n] = "BINARY"; |
| 1697 | n++; |
| 1698 | } |
| 1699 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1700 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1701 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 1702 | pIdx->aiColumn[n] = i; |
| 1703 | pIdx->azColl[n] = "BINARY"; |
| 1704 | n++; |
| 1705 | } |
| 1706 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1707 | assert( n==nKeyCol ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1708 | pIdx->aiColumn[n] = -1; |
| 1709 | pIdx->azColl[n] = "BINARY"; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1710 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1711 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1712 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 1713 | pLevel->iIdxCur = pParse->nTab++; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1714 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 1715 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1716 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1717 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1718 | /* Fill the automatic index with content */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1719 | sqlite3ExprCachePush(pParse); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1720 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1721 | if( pPartial ){ |
| 1722 | iContinue = sqlite3VdbeMakeLabel(v); |
| 1723 | sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame^] | 1724 | pLoop->wsFlags |= WHERE_PARTIALIDX; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1725 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1726 | regRecord = sqlite3GetTempReg(pParse); |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 1727 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1728 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 1729 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1730 | if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1731 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1732 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1733 | sqlite3VdbeJumpHere(v, addrTop); |
| 1734 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1735 | sqlite3ExprCachePop(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1736 | |
| 1737 | /* Jump here when skipping the initialization */ |
| 1738 | sqlite3VdbeJumpHere(v, addrInit); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1739 | |
| 1740 | end_auto_index_create: |
| 1741 | sqlite3ExprDelete(pParse->db, pPartial); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1742 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1743 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1744 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1745 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1746 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1747 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 1748 | ** responsibility of the caller to eventually release the structure |
| 1749 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 1750 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 1751 | static sqlite3_index_info *allocateIndexInfo( |
| 1752 | Parse *pParse, |
| 1753 | WhereClause *pWC, |
| 1754 | struct SrcList_item *pSrc, |
| 1755 | ExprList *pOrderBy |
| 1756 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1757 | int i, j; |
| 1758 | int nTerm; |
| 1759 | struct sqlite3_index_constraint *pIdxCons; |
| 1760 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 1761 | struct sqlite3_index_constraint_usage *pUsage; |
| 1762 | WhereTerm *pTerm; |
| 1763 | int nOrderBy; |
| 1764 | sqlite3_index_info *pIdxInfo; |
| 1765 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1766 | /* Count the number of possible WHERE clause constraints referring |
| 1767 | ** to this virtual table */ |
| 1768 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 1769 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1770 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1771 | testcase( pTerm->eOperator & WO_IN ); |
| 1772 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 1773 | testcase( pTerm->eOperator & WO_ALL ); |
| 1774 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 1775 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1776 | nTerm++; |
| 1777 | } |
| 1778 | |
| 1779 | /* If the ORDER BY clause contains only columns in the current |
| 1780 | ** virtual table then allocate space for the aOrderBy part of |
| 1781 | ** the sqlite3_index_info structure. |
| 1782 | */ |
| 1783 | nOrderBy = 0; |
| 1784 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1785 | int n = pOrderBy->nExpr; |
| 1786 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1787 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 1788 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 1789 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1790 | if( i==n){ |
| 1791 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /* Allocate the sqlite3_index_info structure |
| 1796 | */ |
| 1797 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 1798 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 1799 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 1800 | if( pIdxInfo==0 ){ |
| 1801 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 1806 | ** many fields that are declared "const" to prevent xBestIndex from |
| 1807 | ** changing them. We have to do some funky casting in order to |
| 1808 | ** initialize those fields. |
| 1809 | */ |
| 1810 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 1811 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 1812 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 1813 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 1814 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 1815 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 1816 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 1817 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 1818 | pUsage; |
| 1819 | |
| 1820 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1821 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1822 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1823 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1824 | testcase( pTerm->eOperator & WO_IN ); |
| 1825 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 1826 | testcase( pTerm->eOperator & WO_ALL ); |
| 1827 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 1828 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1829 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 1830 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1831 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1832 | if( op==WO_IN ) op = WO_EQ; |
| 1833 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1834 | /* The direct assignment in the previous line is possible only because |
| 1835 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 1836 | ** following asserts verify this fact. */ |
| 1837 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 1838 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 1839 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 1840 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 1841 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 1842 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1843 | 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] | 1844 | j++; |
| 1845 | } |
| 1846 | for(i=0; i<nOrderBy; i++){ |
| 1847 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 1848 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 1849 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 1850 | } |
| 1851 | |
| 1852 | return pIdxInfo; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | ** The table object reference passed as the second argument to this function |
| 1857 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1858 | ** method of the virtual table with the sqlite3_index_info object that |
| 1859 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1860 | ** |
| 1861 | ** If an error occurs, pParse is populated with an error message and a |
| 1862 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 1863 | ** part of the sqlite3_index_info structure is left populated. |
| 1864 | ** |
| 1865 | ** Whether or not an error is returned, it is the responsibility of the |
| 1866 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 1867 | ** that this is required. |
| 1868 | */ |
| 1869 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 1870 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1871 | int i; |
| 1872 | int rc; |
| 1873 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1874 | TRACE_IDX_INPUTS(p); |
| 1875 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 1876 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1877 | |
| 1878 | if( rc!=SQLITE_OK ){ |
| 1879 | if( rc==SQLITE_NOMEM ){ |
| 1880 | pParse->db->mallocFailed = 1; |
| 1881 | }else if( !pVtab->zErrMsg ){ |
| 1882 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 1883 | }else{ |
| 1884 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 1885 | } |
| 1886 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 1887 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1888 | pVtab->zErrMsg = 0; |
| 1889 | |
| 1890 | for(i=0; i<p->nConstraint; i++){ |
| 1891 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 1892 | sqlite3ErrorMsg(pParse, |
| 1893 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | return pParse->nErr; |
| 1898 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1899 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1900 | |
| 1901 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1902 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1903 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1904 | ** Estimate the location of a particular key among all keys in an |
| 1905 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 1906 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1907 | ** aStat[0] Est. number of rows less than pVal |
| 1908 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1909 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1910 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1911 | */ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 1912 | static void whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1913 | Parse *pParse, /* Database connection */ |
| 1914 | Index *pIdx, /* Index to consider domain of */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 1915 | UnpackedRecord *pRec, /* Vector of values to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1916 | int roundUp, /* Round up if true. Round down if false */ |
| 1917 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1918 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1919 | IndexSample *aSample = pIdx->aSample; |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 1920 | int iCol; /* Index of required stats in anEq[] etc. */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1921 | int iMin = 0; /* Smallest sample not yet tested */ |
| 1922 | int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */ |
| 1923 | int iTest; /* Next sample to test */ |
| 1924 | int res; /* Result of comparison operation */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1925 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 1926 | #ifndef SQLITE_DEBUG |
| 1927 | UNUSED_PARAMETER( pParse ); |
| 1928 | #endif |
drh | 7f59475 | 2013-12-03 19:49:55 +0000 | [diff] [blame] | 1929 | assert( pRec!=0 ); |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 1930 | iCol = pRec->nField - 1; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 1931 | assert( pIdx->nSample>0 ); |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 1932 | assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1933 | do{ |
| 1934 | iTest = (iMin+i)/2; |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 1935 | res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1936 | if( res<0 ){ |
| 1937 | iMin = iTest+1; |
| 1938 | }else{ |
| 1939 | i = iTest; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1940 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1941 | }while( res && iMin<i ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1942 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1943 | #ifdef SQLITE_DEBUG |
| 1944 | /* The following assert statements check that the binary search code |
| 1945 | ** above found the right answer. This block serves no purpose other |
| 1946 | ** than to invoke the asserts. */ |
| 1947 | if( res==0 ){ |
| 1948 | /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 1949 | assert( i<pIdx->nSample ); |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 1950 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 1951 | || pParse->db->mallocFailed ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1952 | }else{ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1953 | /* Otherwise, pRec must be smaller than sample $i and larger than |
| 1954 | ** sample ($i-1). */ |
| 1955 | assert( i==pIdx->nSample |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 1956 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 1957 | || pParse->db->mallocFailed ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1958 | assert( i==0 |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 1959 | || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 1960 | || pParse->db->mallocFailed ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1961 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1962 | #endif /* ifdef SQLITE_DEBUG */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1963 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1964 | /* At this point, aSample[i] is the first sample that is greater than |
| 1965 | ** 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] | 1966 | ** than pVal. If aSample[i]==pVal, then res==0. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1967 | */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 1968 | if( res==0 ){ |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 1969 | aStat[0] = aSample[i].anLt[iCol]; |
| 1970 | aStat[1] = aSample[i].anEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1971 | }else{ |
| 1972 | tRowcnt iLower, iUpper, iGap; |
| 1973 | if( i==0 ){ |
| 1974 | iLower = 0; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 1975 | iUpper = aSample[0].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1976 | }else{ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1977 | i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 1978 | iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 1979 | iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1980 | } |
dan | 39caccf | 2014-07-01 11:54:02 +0000 | [diff] [blame] | 1981 | aStat[1] = pIdx->aAvgEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1982 | if( iLower>=iUpper ){ |
| 1983 | iGap = 0; |
| 1984 | }else{ |
| 1985 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1986 | } |
| 1987 | if( roundUp ){ |
| 1988 | iGap = (iGap*2)/3; |
| 1989 | }else{ |
| 1990 | iGap = iGap/3; |
| 1991 | } |
| 1992 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1993 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1994 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1995 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1996 | |
| 1997 | /* |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 1998 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| 1999 | ** bound on a range scan. Without considering pTerm, it is estimated |
| 2000 | ** that the scan will visit nNew rows. This function returns the number |
| 2001 | ** estimated to be visited after taking pTerm into account. |
| 2002 | ** |
| 2003 | ** If the user explicitly specified a likelihood() value for this term, |
| 2004 | ** then the return value is the likelihood multiplied by the number of |
| 2005 | ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term |
| 2006 | ** has a likelihood of 0.50, and any other term a likelihood of 0.25. |
| 2007 | */ |
| 2008 | static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ |
| 2009 | LogEst nRet = nNew; |
| 2010 | if( pTerm ){ |
| 2011 | if( pTerm->truthProb<=0 ){ |
| 2012 | nRet += pTerm->truthProb; |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2013 | }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2014 | nRet -= 20; assert( 20==sqlite3LogEst(4) ); |
| 2015 | } |
| 2016 | } |
| 2017 | return nRet; |
| 2018 | } |
| 2019 | |
mistachkin | 2d84ac4 | 2014-06-26 21:32:09 +0000 | [diff] [blame] | 2020 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2021 | /* |
| 2022 | ** This function is called to estimate the number of rows visited by a |
| 2023 | ** range-scan on a skip-scan index. For example: |
| 2024 | ** |
| 2025 | ** CREATE INDEX i1 ON t1(a, b, c); |
| 2026 | ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; |
| 2027 | ** |
| 2028 | ** Value pLoop->nOut is currently set to the estimated number of rows |
| 2029 | ** visited for scanning (a=? AND b=?). This function reduces that estimate |
| 2030 | ** by some factor to account for the (c BETWEEN ? AND ?) expression based |
| 2031 | ** on the stat4 data for the index. this scan will be peformed multiple |
| 2032 | ** times (once for each (a,b) combination that matches a=?) is dealt with |
| 2033 | ** by the caller. |
| 2034 | ** |
| 2035 | ** It does this by scanning through all stat4 samples, comparing values |
| 2036 | ** extracted from pLower and pUpper with the corresponding column in each |
| 2037 | ** sample. If L and U are the number of samples found to be less than or |
| 2038 | ** equal to the values extracted from pLower and pUpper respectively, and |
| 2039 | ** N is the total number of samples, the pLoop->nOut value is adjusted |
| 2040 | ** as follows: |
| 2041 | ** |
| 2042 | ** nOut = nOut * ( min(U - L, 1) / N ) |
| 2043 | ** |
| 2044 | ** If pLower is NULL, or a value cannot be extracted from the term, L is |
| 2045 | ** set to zero. If pUpper is NULL, or a value cannot be extracted from it, |
| 2046 | ** U is set to N. |
| 2047 | ** |
| 2048 | ** Normally, this function sets *pbDone to 1 before returning. However, |
| 2049 | ** if no value can be extracted from either pLower or pUpper (and so the |
| 2050 | ** estimate of the number of rows delivered remains unchanged), *pbDone |
| 2051 | ** is left as is. |
| 2052 | ** |
| 2053 | ** If an error occurs, an SQLite error code is returned. Otherwise, |
| 2054 | ** SQLITE_OK. |
| 2055 | */ |
| 2056 | static int whereRangeSkipScanEst( |
| 2057 | Parse *pParse, /* Parsing & code generating context */ |
| 2058 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2059 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 2060 | WhereLoop *pLoop, /* Update the .nOut value of this loop */ |
| 2061 | int *pbDone /* Set to true if at least one expr. value extracted */ |
| 2062 | ){ |
| 2063 | Index *p = pLoop->u.btree.pIndex; |
| 2064 | int nEq = pLoop->u.btree.nEq; |
| 2065 | sqlite3 *db = pParse->db; |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2066 | int nLower = -1; |
| 2067 | int nUpper = p->nSample+1; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2068 | int rc = SQLITE_OK; |
drh | d15f87e | 2014-07-24 22:41:20 +0000 | [diff] [blame] | 2069 | int iCol = p->aiColumn[nEq]; |
| 2070 | u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2071 | CollSeq *pColl; |
| 2072 | |
| 2073 | sqlite3_value *p1 = 0; /* Value extracted from pLower */ |
| 2074 | sqlite3_value *p2 = 0; /* Value extracted from pUpper */ |
| 2075 | sqlite3_value *pVal = 0; /* Value extracted from record */ |
| 2076 | |
| 2077 | pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]); |
| 2078 | if( pLower ){ |
| 2079 | rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2080 | nLower = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2081 | } |
| 2082 | if( pUpper && rc==SQLITE_OK ){ |
| 2083 | rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2084 | nUpper = p2 ? 0 : p->nSample; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | if( p1 || p2 ){ |
| 2088 | int i; |
| 2089 | int nDiff; |
| 2090 | for(i=0; rc==SQLITE_OK && i<p->nSample; i++){ |
| 2091 | rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal); |
| 2092 | if( rc==SQLITE_OK && p1 ){ |
| 2093 | int res = sqlite3MemCompare(p1, pVal, pColl); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2094 | if( res>=0 ) nLower++; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2095 | } |
| 2096 | if( rc==SQLITE_OK && p2 ){ |
| 2097 | int res = sqlite3MemCompare(p2, pVal, pColl); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2098 | if( res>=0 ) nUpper++; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2099 | } |
| 2100 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2101 | nDiff = (nUpper - nLower); |
| 2102 | if( nDiff<=0 ) nDiff = 1; |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2103 | |
| 2104 | /* If there is both an upper and lower bound specified, and the |
| 2105 | ** comparisons indicate that they are close together, use the fallback |
| 2106 | ** method (assume that the scan visits 1/64 of the rows) for estimating |
| 2107 | ** the number of rows visited. Otherwise, estimate the number of rows |
| 2108 | ** using the method described in the header comment for this function. */ |
| 2109 | if( nDiff!=1 || pUpper==0 || pLower==0 ){ |
| 2110 | int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); |
| 2111 | pLoop->nOut -= nAdjust; |
| 2112 | *pbDone = 1; |
| 2113 | WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", |
dan | fa88745 | 2014-06-28 15:26:10 +0000 | [diff] [blame] | 2114 | nLower, nUpper, nAdjust*-1, pLoop->nOut)); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2117 | }else{ |
| 2118 | assert( *pbDone==0 ); |
| 2119 | } |
| 2120 | |
| 2121 | sqlite3ValueFree(p1); |
| 2122 | sqlite3ValueFree(p2); |
| 2123 | sqlite3ValueFree(pVal); |
| 2124 | |
| 2125 | return rc; |
| 2126 | } |
mistachkin | 2d84ac4 | 2014-06-26 21:32:09 +0000 | [diff] [blame] | 2127 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2128 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2129 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2130 | ** This function is used to estimate the number of rows that will be visited |
| 2131 | ** by scanning an index for a range of values. The range may have an upper |
| 2132 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2133 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2134 | ** example, assuming that index p is on t1(a): |
| 2135 | ** |
| 2136 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2137 | ** |_____| |_____| |
| 2138 | ** | | |
| 2139 | ** pLower pUpper |
| 2140 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2141 | ** 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] | 2142 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2143 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2144 | ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 2145 | ** column subject to the range constraint. Or, equivalently, the number of |
| 2146 | ** equality constraints optimized by the proposed index scan. For example, |
| 2147 | ** assuming index p is on t1(a, b), and the SQL query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2148 | ** |
| 2149 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2150 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2151 | ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 2152 | ** left-most column of the index). Or, if the query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2153 | ** |
| 2154 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2155 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2156 | ** then nEq is set to 0. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2157 | ** |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2158 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2159 | ** number of rows that the index scan is expected to visit without |
| 2160 | ** considering the range constraints. If nEq is 0, this is the number of |
| 2161 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2162 | ** to account for the range constraints pLower and pUpper. |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2163 | ** |
| 2164 | ** 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] | 2165 | ** used, a single range inequality reduces the search space by a factor of 4. |
| 2166 | ** and a pair of constraints (x>? AND x<?) reduces the expected number of |
| 2167 | ** rows visited by a factor of 64. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2168 | */ |
| 2169 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2170 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2171 | WhereLoopBuilder *pBuilder, |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2172 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2173 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2174 | WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2175 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2176 | int rc = SQLITE_OK; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2177 | int nOut = pLoop->nOut; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2178 | LogEst nNew; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2179 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2180 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2181 | Index *p = pLoop->u.btree.pIndex; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2182 | int nEq = pLoop->u.btree.nEq; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2183 | |
drh | 74dade2 | 2013-09-04 18:14:53 +0000 | [diff] [blame] | 2184 | if( p->nSample>0 |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 2185 | && nEq<p->nSampleCol |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2186 | ){ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2187 | if( nEq==pBuilder->nRecValid ){ |
| 2188 | UnpackedRecord *pRec = pBuilder->pRec; |
| 2189 | tRowcnt a[2]; |
| 2190 | u8 aff; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2191 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2192 | /* Variable iLower will be set to the estimate of the number of rows in |
| 2193 | ** the index that are less than the lower bound of the range query. The |
| 2194 | ** lower bound being the concatenation of $P and $L, where $P is the |
| 2195 | ** key-prefix formed by the nEq values matched against the nEq left-most |
| 2196 | ** columns of the index, and $L is the value in pLower. |
| 2197 | ** |
| 2198 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 2199 | ** is not a simple variable or literal value), the lower bound of the |
| 2200 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 2201 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 2202 | ** ($P:$L) and the larger of the two returned values used. |
| 2203 | ** |
| 2204 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 2205 | ** less than the upper bound of the range query. Where the upper bound |
| 2206 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 2207 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 2208 | */ |
| 2209 | tRowcnt iLower; |
| 2210 | tRowcnt iUpper; |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2211 | |
drh | b34fc5b | 2014-08-28 17:20:37 +0000 | [diff] [blame] | 2212 | if( pRec ){ |
| 2213 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 2214 | pRec->nField = pBuilder->nRecValid; |
| 2215 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2216 | if( nEq==p->nKeyCol ){ |
| 2217 | aff = SQLITE_AFF_INTEGER; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2218 | }else{ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2219 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2220 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2221 | /* Determine iLower and iUpper using ($P) only. */ |
| 2222 | if( nEq==0 ){ |
| 2223 | iLower = 0; |
drh | 9f07cf7 | 2014-10-22 15:27:05 +0000 | [diff] [blame] | 2224 | iUpper = p->nRowEst0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2225 | }else{ |
| 2226 | /* Note: this call could be optimized away - since the same values must |
| 2227 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 2228 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2229 | iLower = a[0]; |
| 2230 | iUpper = a[0] + a[1]; |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2231 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2232 | |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2233 | assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 2234 | assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | 681fca0 | 2014-10-10 15:01:46 +0000 | [diff] [blame] | 2235 | assert( p->aSortOrder!=0 ); |
| 2236 | if( p->aSortOrder[nEq] ){ |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2237 | /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 2238 | SWAP(WhereTerm*, pLower, pUpper); |
| 2239 | } |
| 2240 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2241 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 2242 | if( pLower ){ |
| 2243 | int bOk; /* True if value is extracted from pExpr */ |
| 2244 | Expr *pExpr = pLower->pExpr->pRight; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2245 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 2246 | if( rc==SQLITE_OK && bOk ){ |
| 2247 | tRowcnt iNew; |
| 2248 | whereKeyStats(pParse, p, pRec, 0, a); |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2249 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2250 | if( iNew>iLower ) iLower = iNew; |
| 2251 | nOut--; |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2252 | pLower = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 2257 | if( pUpper ){ |
| 2258 | int bOk; /* True if value is extracted from pExpr */ |
| 2259 | Expr *pExpr = pUpper->pExpr->pRight; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2260 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 2261 | if( rc==SQLITE_OK && bOk ){ |
| 2262 | tRowcnt iNew; |
| 2263 | whereKeyStats(pParse, p, pRec, 1, a); |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2264 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2265 | if( iNew<iUpper ) iUpper = iNew; |
| 2266 | nOut--; |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2267 | pUpper = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | pBuilder->pRec = pRec; |
| 2272 | if( rc==SQLITE_OK ){ |
| 2273 | if( iUpper>iLower ){ |
| 2274 | nNew = sqlite3LogEst(iUpper - iLower); |
| 2275 | }else{ |
| 2276 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 2277 | } |
| 2278 | if( nNew<nOut ){ |
| 2279 | nOut = nNew; |
| 2280 | } |
drh | ae914d7 | 2014-08-28 19:38:22 +0000 | [diff] [blame] | 2281 | WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n", |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2282 | (u32)iLower, (u32)iUpper, nOut)); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2283 | } |
| 2284 | }else{ |
| 2285 | int bDone = 0; |
| 2286 | rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone); |
| 2287 | if( bDone ) return rc; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2288 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2289 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2290 | #else |
| 2291 | UNUSED_PARAMETER(pParse); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2292 | UNUSED_PARAMETER(pBuilder); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2293 | assert( pLower || pUpper ); |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2294 | #endif |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2295 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2296 | nNew = whereRangeAdjust(pLower, nOut); |
| 2297 | nNew = whereRangeAdjust(pUpper, nNew); |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2298 | |
drh | 4dd96a8 | 2014-10-24 15:26:29 +0000 | [diff] [blame] | 2299 | /* TUNING: If there is both an upper and lower limit and neither limit |
| 2300 | ** has an application-defined likelihood(), assume the range is |
dan | 42685f2 | 2014-04-28 19:34:06 +0000 | [diff] [blame] | 2301 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 2302 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 2303 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 2304 | ** match 1/64 of the index. */ |
drh | 4dd96a8 | 2014-10-24 15:26:29 +0000 | [diff] [blame] | 2305 | if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 2306 | nNew -= 20; |
| 2307 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2308 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2309 | nOut -= (pLower!=0) + (pUpper!=0); |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2310 | if( nNew<10 ) nNew = 10; |
| 2311 | if( nNew<nOut ) nOut = nNew; |
drh | ae914d7 | 2014-08-28 19:38:22 +0000 | [diff] [blame] | 2312 | #if defined(WHERETRACE_ENABLED) |
| 2313 | if( pLoop->nOut>nOut ){ |
| 2314 | WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", |
| 2315 | pLoop->nOut, nOut)); |
| 2316 | } |
| 2317 | #endif |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2318 | pLoop->nOut = (LogEst)nOut; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2319 | return rc; |
| 2320 | } |
| 2321 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2322 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2323 | /* |
| 2324 | ** Estimate the number of rows that will be returned based on |
| 2325 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2326 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2327 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2328 | ** for that index. When pExpr==NULL that means the constraint is |
| 2329 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2330 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2331 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2332 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2333 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2334 | ** |
| 2335 | ** This routine can fail if it is unable to load a collating sequence |
| 2336 | ** required for string comparison, or if unable to allocate memory |
| 2337 | ** for a UTF conversion required for comparison. The error is stored |
| 2338 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2339 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2340 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2341 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2342 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2343 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2344 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2345 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2346 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2347 | int nEq = pBuilder->pNew->u.btree.nEq; |
| 2348 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2349 | u8 aff; /* Column affinity */ |
| 2350 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2351 | tRowcnt a[2]; /* Statistics */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2352 | int bOk; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2353 | |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2354 | assert( nEq>=1 ); |
dan | fd984b8 | 2014-06-30 18:02:20 +0000 | [diff] [blame] | 2355 | assert( nEq<=p->nColumn ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2356 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2357 | assert( p->nSample>0 ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2358 | assert( pBuilder->nRecValid<nEq ); |
| 2359 | |
| 2360 | /* If values are not available for all fields of the index to the left |
| 2361 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 2362 | if( pBuilder->nRecValid<(nEq-1) ){ |
| 2363 | return SQLITE_NOTFOUND; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2364 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2365 | |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 2366 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 2367 | ** below would return the same value. */ |
dan | fd984b8 | 2014-06-30 18:02:20 +0000 | [diff] [blame] | 2368 | if( nEq>=p->nColumn ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2369 | *pnRow = 1; |
| 2370 | return SQLITE_OK; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2371 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2372 | |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2373 | aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity; |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2374 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); |
| 2375 | pBuilder->pRec = pRec; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2376 | if( rc!=SQLITE_OK ) return rc; |
| 2377 | if( bOk==0 ) return SQLITE_NOTFOUND; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2378 | pBuilder->nRecValid = nEq; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2379 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2380 | whereKeyStats(pParse, p, pRec, 0, a); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2381 | WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2382 | *pnRow = a[1]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2383 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2384 | return rc; |
| 2385 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2386 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2387 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2388 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2389 | /* |
| 2390 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2391 | ** an IN constraint where the right-hand side of the IN operator |
| 2392 | ** is a list of values. Example: |
| 2393 | ** |
| 2394 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2395 | ** |
| 2396 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2397 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2398 | ** non-zero. |
| 2399 | ** |
| 2400 | ** This routine can fail if it is unable to load a collating sequence |
| 2401 | ** required for string comparison, or if unable to allocate memory |
| 2402 | ** for a UTF conversion required for comparison. The error is stored |
| 2403 | ** in the pParse structure. |
| 2404 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2405 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2406 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2407 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2408 | 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] | 2409 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2410 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2411 | Index *p = pBuilder->pNew->u.btree.pIndex; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2412 | i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2413 | int nRecValid = pBuilder->nRecValid; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2414 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2415 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2416 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2417 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2418 | |
| 2419 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2420 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2421 | nEst = nRow0; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2422 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2423 | nRowEst += nEst; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2424 | pBuilder->nRecValid = nRecValid; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2425 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2426 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2427 | if( rc==SQLITE_OK ){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2428 | if( nRowEst > nRow0 ) nRowEst = nRow0; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2429 | *pnRow = nRowEst; |
drh | 5418b12 | 2014-08-28 13:42:13 +0000 | [diff] [blame] | 2430 | WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2431 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2432 | assert( pBuilder->nRecValid==nRecValid ); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2433 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2434 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2435 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2436 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2437 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2438 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2439 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2440 | ** or USING clause of that join. |
| 2441 | ** |
| 2442 | ** Consider the term t2.z='ok' in the following queries: |
| 2443 | ** |
| 2444 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2445 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2446 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2447 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2448 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2449 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2450 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2451 | ** |
| 2452 | ** 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] | 2453 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2454 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2455 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2456 | ** but joins might run a little slower. The trick is to disable as much |
| 2457 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2458 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2459 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2460 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2461 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2462 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2463 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 2464 | && (pLevel->notReady & pTerm->prereqAll)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2465 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2466 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2467 | if( pTerm->iParent>=0 ){ |
| 2468 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2469 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2470 | disableTerm(pLevel, pOther); |
| 2471 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2472 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2477 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2478 | ** to the n registers starting at base. |
| 2479 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2480 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2481 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2482 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2483 | ** |
| 2484 | ** This routine makes its own copy of zAff so that the caller is free |
| 2485 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2486 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2487 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2488 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2489 | if( zAff==0 ){ |
| 2490 | assert( pParse->db->mallocFailed ); |
| 2491 | return; |
| 2492 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2493 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2494 | |
| 2495 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2496 | ** and end of the affinity string. |
| 2497 | */ |
| 2498 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2499 | n--; |
| 2500 | base++; |
| 2501 | zAff++; |
| 2502 | } |
| 2503 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2504 | n--; |
| 2505 | } |
| 2506 | |
| 2507 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2508 | if( n>0 ){ |
| 2509 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2510 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2511 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2512 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2515 | |
| 2516 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2517 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2518 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2519 | ** coded. |
| 2520 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2521 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2522 | ** |
| 2523 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2524 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2525 | ** 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] | 2526 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2527 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2528 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2529 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2530 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2531 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2532 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2533 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2534 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2535 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2536 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2537 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2538 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2539 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2540 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2541 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2542 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2543 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2544 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2545 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2546 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2547 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2548 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2549 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2550 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2551 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2552 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2553 | && pLoop->u.btree.pIndex!=0 |
| 2554 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2555 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2556 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2557 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2558 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2559 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2560 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2561 | iReg = iTarget; |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2562 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2563 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2564 | testcase( bRev ); |
| 2565 | bRev = !bRev; |
| 2566 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2567 | iTab = pX->iTable; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2568 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 2569 | VdbeCoverageIf(v, bRev); |
| 2570 | VdbeCoverageIf(v, !bRev); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2571 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2572 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2573 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2574 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2575 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2576 | pLevel->u.in.nIn++; |
| 2577 | pLevel->u.in.aInLoop = |
| 2578 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2579 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2580 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2581 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2582 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2583 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2584 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2585 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2586 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2587 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2588 | } |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 2589 | pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2590 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2591 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2592 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2593 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2594 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2595 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2596 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2597 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2600 | /* |
| 2601 | ** Generate code that will evaluate all == and IN constraints for an |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2602 | ** index scan. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2603 | ** |
| 2604 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2605 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2606 | ** The index has as many as three equality constraints, but in this |
| 2607 | ** example, the third "c" value is an inequality. So only two |
| 2608 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2609 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2610 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2611 | ** |
| 2612 | ** In the example above nEq==2. But this subroutine works for any value |
| 2613 | ** 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] | 2614 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2615 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2616 | ** |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2617 | ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 2618 | ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 2619 | ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 2620 | ** occurs after the nEq quality constraints. |
| 2621 | ** |
| 2622 | ** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 2623 | ** the index of the first memory cell in that range. The code that |
| 2624 | ** calls this routine will use that memory range to store keys for |
| 2625 | ** start and termination conditions of the loop. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2626 | ** key value of the loop. If one or more IN operators appear, then |
| 2627 | ** this routine allocates an additional nEq memory cells for internal |
| 2628 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2629 | ** |
| 2630 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2631 | ** copy of the column affinity string of the index allocated using |
| 2632 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2633 | ** with equality constraints that use NONE affinity are set to |
| 2634 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2635 | ** |
| 2636 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2637 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2638 | ** |
| 2639 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2640 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2641 | ** no conversion should be attempted before using a t2.b value as part of |
| 2642 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2643 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2644 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2645 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2646 | Parse *pParse, /* Parsing context */ |
| 2647 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2648 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2649 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2650 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2651 | ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2652 | u16 nEq; /* The number of == or IN constraints to code */ |
| 2653 | u16 nSkip; /* Number of left-most columns to skip */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2654 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2655 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2656 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2657 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2658 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2659 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2660 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2661 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2662 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2663 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2664 | pLoop = pLevel->pWLoop; |
| 2665 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2666 | nEq = pLoop->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 2667 | nSkip = pLoop->nSkip; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2668 | pIdx = pLoop->u.btree.pIndex; |
| 2669 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2670 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2671 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2672 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2673 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2674 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2675 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2676 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2677 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2678 | if( !zAff ){ |
| 2679 | pParse->db->mallocFailed = 1; |
| 2680 | } |
| 2681 | |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2682 | if( nSkip ){ |
| 2683 | int iIdxCur = pLevel->iIdxCur; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2684 | sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 2685 | VdbeCoverageIf(v, bRev==0); |
| 2686 | VdbeCoverageIf(v, bRev!=0); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 2687 | VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 2688 | j = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 2689 | pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2690 | iIdxCur, 0, regBase, nSkip); |
| 2691 | VdbeCoverageIf(v, bRev==0); |
| 2692 | VdbeCoverageIf(v, bRev!=0); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 2693 | sqlite3VdbeJumpHere(v, j); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2694 | for(j=0; j<nSkip; j++){ |
| 2695 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 2696 | assert( pIdx->aiColumn[j]>=0 ); |
| 2697 | VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); |
| 2698 | } |
| 2699 | } |
| 2700 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2701 | /* Evaluate the equality constraints |
| 2702 | */ |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 2703 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2704 | for(j=nSkip; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2705 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2706 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2707 | assert( pTerm!=0 ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2708 | /* The following testcase is true for indices with redundant columns. |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2709 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2710 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 2711 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2712 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2713 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2714 | if( nReg==1 ){ |
| 2715 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2716 | regBase = r1; |
| 2717 | }else{ |
| 2718 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2719 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2720 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2721 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2722 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2723 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2724 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2725 | if( sqlite3ExprCanBeNull(pRight) ){ |
| 2726 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 2727 | VdbeCoverage(v); |
| 2728 | } |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2729 | if( zAff ){ |
| 2730 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2731 | zAff[j] = SQLITE_AFF_NONE; |
| 2732 | } |
| 2733 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2734 | zAff[j] = SQLITE_AFF_NONE; |
| 2735 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2736 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2739 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2740 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2743 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2744 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2745 | ** This routine is a helper for explainIndexRange() below |
| 2746 | ** |
| 2747 | ** pStr holds the text of an expression that we are building up one term |
| 2748 | ** at a time. This routine adds a new term to the end of the expression. |
| 2749 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 2750 | ** terms only. |
| 2751 | */ |
| 2752 | static void explainAppendTerm( |
| 2753 | StrAccum *pStr, /* The text expression being built */ |
| 2754 | int iTerm, /* Index of this term. First is zero */ |
| 2755 | const char *zColumn, /* Name of the column */ |
| 2756 | const char *zOp /* Name of the operator */ |
| 2757 | ){ |
| 2758 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 2759 | sqlite3StrAccumAppendAll(pStr, zColumn); |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2760 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 2761 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 2762 | } |
| 2763 | |
| 2764 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2765 | ** Argument pLevel describes a strategy for scanning table pTab. This |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2766 | ** function appends text to pStr that describes the subset of table |
| 2767 | ** rows scanned by the strategy in the form of an SQL expression. |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2768 | ** |
| 2769 | ** For example, if the query: |
| 2770 | ** |
| 2771 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 2772 | ** |
| 2773 | ** is run and there is an index on (a, b), then this function returns a |
| 2774 | ** string similar to: |
| 2775 | ** |
| 2776 | ** "a=? AND b>?" |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2777 | */ |
drh | 1f8817c | 2014-10-10 19:15:35 +0000 | [diff] [blame] | 2778 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2779 | Index *pIndex = pLoop->u.btree.pIndex; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2780 | u16 nEq = pLoop->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 2781 | u16 nSkip = pLoop->nSkip; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2782 | int i, j; |
| 2783 | Column *aCol = pTab->aCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2784 | i16 *aiColumn = pIndex->aiColumn; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2785 | |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2786 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 2787 | sqlite3StrAccumAppend(pStr, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2788 | for(i=0; i<nEq; i++){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 2789 | char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2790 | if( i>=nSkip ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2791 | explainAppendTerm(pStr, i, z, "="); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2792 | }else{ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2793 | if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 2794 | sqlite3XPrintf(pStr, 0, "ANY(%s)", z); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2795 | } |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2798 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2799 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 2800 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2801 | explainAppendTerm(pStr, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2802 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2803 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 2804 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2805 | explainAppendTerm(pStr, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2806 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2807 | sqlite3StrAccumAppend(pStr, ")", 1); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2810 | /* |
| 2811 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 2812 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 2813 | ** record is added to the output to describe the table scan strategy in |
| 2814 | ** pLevel. |
| 2815 | */ |
| 2816 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2817 | Parse *pParse, /* Parse context */ |
| 2818 | SrcList *pTabList, /* Table list this loop refers to */ |
| 2819 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 2820 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2821 | int iFrom, /* Value for "from" column of output */ |
| 2822 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2823 | ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 2824 | #ifndef SQLITE_DEBUG |
| 2825 | if( pParse->explain==2 ) |
| 2826 | #endif |
| 2827 | { |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2828 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2829 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 2830 | sqlite3 *db = pParse->db; /* Database handle */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2831 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2832 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2833 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 2834 | u32 flags; /* Flags that describe this loop */ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2835 | char *zMsg; /* Text to add to EQP output */ |
| 2836 | StrAccum str; /* EQP output string */ |
| 2837 | char zBuf[100]; /* Initial space for EQP output string */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2838 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2839 | pLoop = pLevel->pWLoop; |
| 2840 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2841 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2842 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2843 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 2844 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 2845 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2846 | |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2847 | sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 2848 | str.db = db; |
| 2849 | sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2850 | if( pItem->pSelect ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2851 | sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2852 | }else{ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2853 | sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2856 | if( pItem->zAlias ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2857 | sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2858 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2859 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 2860 | const char *zFmt = 0; |
| 2861 | Index *pIdx; |
| 2862 | |
| 2863 | assert( pLoop->u.btree.pIndex!=0 ); |
| 2864 | pIdx = pLoop->u.btree.pIndex; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2865 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2866 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
drh | c631faa | 2014-10-11 01:22:16 +0000 | [diff] [blame] | 2867 | if( isSearch ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2868 | zFmt = "PRIMARY KEY"; |
| 2869 | } |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame^] | 2870 | }else if( flags & WHERE_PARTIALIDX ){ |
| 2871 | zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2872 | }else if( flags & WHERE_AUTO_INDEX ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2873 | zFmt = "AUTOMATIC COVERING INDEX"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2874 | }else if( flags & WHERE_IDX_ONLY ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2875 | zFmt = "COVERING INDEX %s"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2876 | }else{ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2877 | zFmt = "INDEX %s"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 2878 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2879 | if( zFmt ){ |
| 2880 | sqlite3StrAccumAppend(&str, " USING ", 7); |
| 2881 | sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); |
| 2882 | explainIndexRange(&str, pLoop, pItem->pTab); |
| 2883 | } |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 2884 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2885 | const char *zRange; |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 2886 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2887 | zRange = "(rowid=?)"; |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 2888 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2889 | zRange = "(rowid>? AND rowid<?)"; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2890 | }else if( flags&WHERE_BTM_LIMIT ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2891 | zRange = "(rowid>?)"; |
| 2892 | }else{ |
| 2893 | assert( flags&WHERE_TOP_LIMIT); |
| 2894 | zRange = "(rowid<?)"; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2895 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2896 | sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY "); |
| 2897 | sqlite3StrAccumAppendAll(&str, zRange); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2898 | } |
| 2899 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2900 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2901 | sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s", |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2902 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2903 | } |
| 2904 | #endif |
drh | 98545bb | 2014-10-10 17:20:39 +0000 | [diff] [blame] | 2905 | #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 2906 | if( pLoop->nOut>=10 ){ |
| 2907 | sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 2908 | }else{ |
| 2909 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 2910 | } |
| 2911 | #endif |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 2912 | zMsg = sqlite3StrAccumFinish(&str); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2913 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2914 | } |
| 2915 | } |
| 2916 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2917 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2918 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 2919 | |
| 2920 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2921 | /* |
| 2922 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 2923 | ** implementation described by pWInfo. |
| 2924 | */ |
| 2925 | static Bitmask codeOneLoopStart( |
| 2926 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 2927 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 2928 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2929 | ){ |
| 2930 | int j, k; /* Loop counters */ |
| 2931 | int iCur; /* The VDBE cursor for the table */ |
| 2932 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 2933 | int omitTable; /* True if we use the index only */ |
| 2934 | int bRev; /* True if we need to scan in reverse order */ |
| 2935 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2936 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2937 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 2938 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 2939 | Parse *pParse; /* Parsing context */ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 2940 | sqlite3 *db; /* Database connection */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2941 | Vdbe *v; /* The prepared stmt under constructions */ |
| 2942 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 2943 | int addrBrk; /* Jump here to break out of the loop */ |
| 2944 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 2945 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 2946 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2947 | |
| 2948 | pParse = pWInfo->pParse; |
| 2949 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 2950 | pWC = &pWInfo->sWC; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 2951 | db = pParse->db; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2952 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2953 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2954 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 2955 | iCur = pTabItem->iCursor; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 2956 | pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2957 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 2958 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 2959 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 2960 | VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2961 | |
| 2962 | /* Create labels for the "break" and "continue" instructions |
| 2963 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 2964 | ** Jump to cont to go immediately to the next iteration of the |
| 2965 | ** loop. |
| 2966 | ** |
| 2967 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 2968 | ** means to continue with the next IN value combination. When |
| 2969 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 2970 | ** is the same as "addrBrk". |
| 2971 | */ |
| 2972 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 2973 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 2974 | |
| 2975 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 2976 | ** initialize a memory cell that records if this table matches any |
| 2977 | ** row of the left table of the join. |
| 2978 | */ |
| 2979 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 2980 | pLevel->iLeftJoin = ++pParse->nMem; |
| 2981 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 2982 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 2983 | } |
| 2984 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 2985 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 2986 | if( pTabItem->viaCoroutine ){ |
| 2987 | int regYield = pTabItem->regReturn; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 2988 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 2989 | pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2990 | VdbeCoverage(v); |
drh | 725de29 | 2014-02-08 13:12:19 +0000 | [diff] [blame] | 2991 | VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 2992 | pLevel->op = OP_Goto; |
| 2993 | }else |
| 2994 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2995 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2996 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 2997 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2998 | ** to access the data. |
| 2999 | */ |
| 3000 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3001 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3002 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3003 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3004 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3005 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3006 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3007 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3008 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3009 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 3010 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3011 | if( pTerm->eOperator & WO_IN ){ |
| 3012 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3013 | addrNotFound = pLevel->addrNxt; |
| 3014 | }else{ |
| 3015 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3016 | } |
| 3017 | } |
| 3018 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3019 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3020 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3021 | pLoop->u.vtab.idxStr, |
| 3022 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3023 | VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3024 | pLoop->u.vtab.needFree = 0; |
| 3025 | for(j=0; j<nConstraint && j<16; j++){ |
| 3026 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3027 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | pLevel->op = OP_VNext; |
| 3031 | pLevel->p1 = iCur; |
| 3032 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3033 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | d249090 | 2014-04-13 19:28:15 +0000 | [diff] [blame] | 3034 | sqlite3ExprCachePop(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3035 | }else |
| 3036 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3037 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3038 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3039 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3040 | ){ |
| 3041 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3042 | ** equality comparison against the ROWID field. Or |
| 3043 | ** we reference multiple rows using a "rowid IN (...)" |
| 3044 | ** construct. |
| 3045 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3046 | assert( pLoop->u.btree.nEq==1 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3047 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3048 | assert( pTerm!=0 ); |
| 3049 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3050 | assert( omitTable==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3051 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3052 | iReleaseReg = ++pParse->nMem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3053 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3054 | if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3055 | addrNxt = pLevel->addrNxt; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3056 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3057 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3058 | VdbeCoverage(v); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3059 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3060 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3061 | VdbeComment((v, "pk")); |
| 3062 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3063 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3064 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3065 | ){ |
| 3066 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3067 | */ |
| 3068 | int testOp = OP_Noop; |
| 3069 | int start; |
| 3070 | int memEndValue = 0; |
| 3071 | WhereTerm *pStart, *pEnd; |
| 3072 | |
| 3073 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3074 | j = 0; |
| 3075 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3076 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3077 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3078 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3079 | if( bRev ){ |
| 3080 | pTerm = pStart; |
| 3081 | pStart = pEnd; |
| 3082 | pEnd = pTerm; |
| 3083 | } |
| 3084 | if( pStart ){ |
| 3085 | Expr *pX; /* The expression that defines the start bound */ |
| 3086 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3087 | |
| 3088 | /* The following constant maps TK_xx codes into corresponding |
| 3089 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3090 | */ |
| 3091 | const u8 aMoveOp[] = { |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3092 | /* TK_GT */ OP_SeekGT, |
| 3093 | /* TK_LE */ OP_SeekLE, |
| 3094 | /* TK_LT */ OP_SeekLT, |
| 3095 | /* TK_GE */ OP_SeekGE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3096 | }; |
| 3097 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3098 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3099 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3100 | |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3101 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3102 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3103 | pX = pStart->pExpr; |
| 3104 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3105 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3106 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3107 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3108 | VdbeComment((v, "pk")); |
| 3109 | VdbeCoverageIf(v, pX->op==TK_GT); |
| 3110 | VdbeCoverageIf(v, pX->op==TK_LE); |
| 3111 | VdbeCoverageIf(v, pX->op==TK_LT); |
| 3112 | VdbeCoverageIf(v, pX->op==TK_GE); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3113 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3114 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3115 | disableTerm(pLevel, pStart); |
| 3116 | }else{ |
| 3117 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3118 | VdbeCoverageIf(v, bRev==0); |
| 3119 | VdbeCoverageIf(v, bRev!=0); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3120 | } |
| 3121 | if( pEnd ){ |
| 3122 | Expr *pX; |
| 3123 | pX = pEnd->pExpr; |
| 3124 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3125 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 3126 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3127 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3128 | memEndValue = ++pParse->nMem; |
| 3129 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3130 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3131 | testOp = bRev ? OP_Le : OP_Ge; |
| 3132 | }else{ |
| 3133 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3134 | } |
| 3135 | disableTerm(pLevel, pEnd); |
| 3136 | } |
| 3137 | start = sqlite3VdbeCurrentAddr(v); |
| 3138 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3139 | pLevel->p1 = iCur; |
| 3140 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3141 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3142 | if( testOp!=OP_Noop ){ |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3143 | iRowidReg = ++pParse->nMem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3144 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3145 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3146 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3147 | VdbeCoverageIf(v, testOp==OP_Le); |
| 3148 | VdbeCoverageIf(v, testOp==OP_Lt); |
| 3149 | VdbeCoverageIf(v, testOp==OP_Ge); |
| 3150 | VdbeCoverageIf(v, testOp==OP_Gt); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3151 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3152 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3153 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3154 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3155 | ** |
| 3156 | ** The WHERE clause may contain zero or more equality |
| 3157 | ** terms ("==" or "IN" operators) that refer to the N |
| 3158 | ** left-most columns of the index. It may also contain |
| 3159 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3160 | ** column that immediately follows the N equalities. Only |
| 3161 | ** the right-most column can be an inequality - the rest must |
| 3162 | ** use the "==" and "IN" operators. For example, if the |
| 3163 | ** index is on (x,y,z), then the following clauses are all |
| 3164 | ** optimized: |
| 3165 | ** |
| 3166 | ** x=5 |
| 3167 | ** x=5 AND y=10 |
| 3168 | ** x=5 AND y<10 |
| 3169 | ** x=5 AND y>5 AND y<10 |
| 3170 | ** x=5 AND y=5 AND z<=10 |
| 3171 | ** |
| 3172 | ** The z<10 term of the following cannot be used, only |
| 3173 | ** the x=5 term: |
| 3174 | ** |
| 3175 | ** x=5 AND z<10 |
| 3176 | ** |
| 3177 | ** N may be zero if there are inequality constraints. |
| 3178 | ** If there are no inequality constraints, then N is at |
| 3179 | ** least one. |
| 3180 | ** |
| 3181 | ** This case is also used when there are no WHERE clause |
| 3182 | ** constraints but an index is selected anyway, in order |
| 3183 | ** to force the output order to conform to an ORDER BY. |
| 3184 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3185 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3186 | 0, |
| 3187 | 0, |
| 3188 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3189 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3190 | OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3191 | OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 3192 | OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 3193 | OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3194 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3195 | static const u8 aEndOp[] = { |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3196 | OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 3197 | OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 3198 | OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 3199 | OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3200 | }; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3201 | u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3202 | int regBase; /* Base register holding constraint values */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3203 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3204 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3205 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3206 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3207 | int start_constraints; /* Start of range is constrained */ |
| 3208 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3209 | Index *pIdx; /* The index we will be using */ |
| 3210 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3211 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3212 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3213 | char *zStartAff; /* Affinity for start of range constraint */ |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3214 | char cEndAff = 0; /* Affinity for end of range constraint */ |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3215 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 3216 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3217 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3218 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3219 | iIdxCur = pLevel->iIdxCur; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3220 | assert( nEq>=pLoop->nSkip ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3221 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3222 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3223 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3224 | ** query, then the caller will only allow the loop to run for |
| 3225 | ** a single iteration. This means that the first row returned |
| 3226 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3227 | ** the first one after the nEq equality constraints in the index, |
| 3228 | ** this requires some special handling. |
| 3229 | */ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 3230 | assert( pWInfo->pOrderBy==0 |
| 3231 | || pWInfo->pOrderBy->nExpr==1 |
| 3232 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3233 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 3234 | && pWInfo->nOBSat>0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3235 | && (pIdx->nKeyCol>nEq) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3236 | ){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3237 | assert( pLoop->nSkip==0 ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3238 | bSeekPastNull = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3239 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | /* Find any inequality constraint terms for the start and end |
| 3243 | ** of the range. |
| 3244 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3245 | j = nEq; |
| 3246 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3247 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3248 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3249 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3250 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3251 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3252 | nExtraReg = 1; |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3253 | if( pRangeStart==0 |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3254 | && (j = pIdx->aiColumn[nEq])>=0 |
| 3255 | && pIdx->pTable->aCol[j].notNull==0 |
| 3256 | ){ |
| 3257 | bSeekPastNull = 1; |
| 3258 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3259 | } |
dan | 0df163a | 2014-03-06 12:36:26 +0000 | [diff] [blame] | 3260 | assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3261 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3262 | /* Generate code to evaluate all constraint terms using == or IN |
| 3263 | ** and store the values of those terms in an array of registers |
| 3264 | ** starting at regBase. |
| 3265 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3266 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3267 | assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 3268 | if( zStartAff ) cEndAff = zStartAff[nEq]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3269 | addrNxt = pLevel->addrNxt; |
| 3270 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3271 | /* If we are doing a reverse order scan on an ascending index, or |
| 3272 | ** a forward order scan on a descending index, interchange the |
| 3273 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3274 | */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3275 | if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3276 | || (bRev && pIdx->nKeyCol==nEq) |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3277 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3278 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3279 | SWAP(u8, bSeekPastNull, bStopAtNull); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3282 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3283 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3284 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3285 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3286 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3287 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3288 | start_constraints = pRangeStart || nEq>0; |
| 3289 | |
| 3290 | /* Seek the index cursor to the start of the range. */ |
| 3291 | nConstraint = nEq; |
| 3292 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3293 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3294 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3295 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 3296 | && sqlite3ExprCanBeNull(pRight) |
| 3297 | ){ |
| 3298 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 3299 | VdbeCoverage(v); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3300 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3301 | if( zStartAff ){ |
| 3302 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3303 | /* Since the comparison is to be performed with no conversions |
| 3304 | ** applied to the operands, set the affinity to apply to pRight to |
| 3305 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3306 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3307 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3308 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3309 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3310 | } |
| 3311 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3312 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3313 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3314 | }else if( bSeekPastNull ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3315 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3316 | nConstraint++; |
| 3317 | startEq = 0; |
| 3318 | start_constraints = 1; |
| 3319 | } |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3320 | codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3321 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3322 | assert( op!=0 ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3323 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3324 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3325 | VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 3326 | VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 3327 | VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 3328 | VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 3329 | VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 3330 | VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3331 | |
| 3332 | /* Load the value for the inequality constraint at the end of the |
| 3333 | ** range (if any). |
| 3334 | */ |
| 3335 | nConstraint = nEq; |
| 3336 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3337 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3338 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3339 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3340 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 3341 | && sqlite3ExprCanBeNull(pRight) |
| 3342 | ){ |
| 3343 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 3344 | VdbeCoverage(v); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3345 | } |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3346 | if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE |
| 3347 | && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) |
| 3348 | ){ |
| 3349 | codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); |
| 3350 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3351 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3352 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3353 | }else if( bStopAtNull ){ |
| 3354 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3355 | endEq = 0; |
| 3356 | nConstraint++; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3357 | } |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3358 | sqlite3DbFree(db, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3359 | |
| 3360 | /* Top of the loop body */ |
| 3361 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3362 | |
| 3363 | /* Check if the index cursor is past the end of the range. */ |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3364 | if( nConstraint ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3365 | op = aEndOp[bRev*2 + endEq]; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3366 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3367 | testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 3368 | testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 3369 | testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 3370 | testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3371 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3372 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3373 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3374 | disableTerm(pLevel, pRangeStart); |
| 3375 | disableTerm(pLevel, pRangeEnd); |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3376 | if( omitTable ){ |
| 3377 | /* pIdx is a covering index. No need to access the main table. */ |
| 3378 | }else if( HasRowid(pIdx->pTable) ){ |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3379 | iRowidReg = ++pParse->nMem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3380 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3381 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3382 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 3383 | }else if( iCur!=iIdxCur ){ |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3384 | Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 3385 | iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 3386 | for(j=0; j<pPk->nKeyCol; j++){ |
| 3387 | k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 3388 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 3389 | } |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 3390 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3391 | iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3392 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3393 | |
| 3394 | /* Record the instruction used to terminate the loop. Disable |
| 3395 | ** WHERE clause terms made redundant by the index range scan. |
| 3396 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3397 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3398 | pLevel->op = OP_Noop; |
| 3399 | }else if( bRev ){ |
| 3400 | pLevel->op = OP_Prev; |
| 3401 | }else{ |
| 3402 | pLevel->op = OP_Next; |
| 3403 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3404 | pLevel->p1 = iIdxCur; |
drh | 0c8a934 | 2014-03-20 12:17:35 +0000 | [diff] [blame] | 3405 | pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3406 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3407 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3408 | }else{ |
| 3409 | assert( pLevel->p5==0 ); |
| 3410 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3411 | }else |
| 3412 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3413 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3414 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3415 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3416 | ** |
| 3417 | ** Example: |
| 3418 | ** |
| 3419 | ** CREATE TABLE t1(a,b,c,d); |
| 3420 | ** CREATE INDEX i1 ON t1(a); |
| 3421 | ** CREATE INDEX i2 ON t1(b); |
| 3422 | ** CREATE INDEX i3 ON t1(c); |
| 3423 | ** |
| 3424 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3425 | ** |
| 3426 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3427 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3428 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3429 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3430 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3431 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3432 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3433 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3434 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3435 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3436 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3437 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3438 | ** Gosub 2 A |
| 3439 | ** sqlite3WhereEnd() |
| 3440 | ** |
| 3441 | ** Following the above, code to terminate the loop. Label A, the target |
| 3442 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3443 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3444 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3445 | ** Goto B # The loop is finished. |
| 3446 | ** |
| 3447 | ** A: <loop body> # Return data, whatever. |
| 3448 | ** |
| 3449 | ** Return 2 # Jump back to the Gosub |
| 3450 | ** |
| 3451 | ** B: <after the loop> |
| 3452 | ** |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3453 | ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3454 | ** use an ephemeral index instead of a RowSet to record the primary |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3455 | ** keys of the rows we have already seen. |
| 3456 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3457 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3458 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3459 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3460 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3461 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3462 | |
| 3463 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3464 | int regRowset = 0; /* Register for RowSet object */ |
| 3465 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3466 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3467 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3468 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3469 | int ii; /* Loop counter */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3470 | u16 wctrlFlags; /* Flags for sub-WHERE clause */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3471 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3472 | Table *pTab = pTabItem->pTab; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3473 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3474 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3475 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3476 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3477 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3478 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3479 | pLevel->op = OP_Return; |
| 3480 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3481 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3482 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3483 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3484 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3485 | */ |
| 3486 | if( pWInfo->nLevel>1 ){ |
| 3487 | int nNotReady; /* The number of notReady tables */ |
| 3488 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3489 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3490 | pOrTab = sqlite3StackAllocRaw(db, |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3491 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3492 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3493 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3494 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3495 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3496 | origSrc = pWInfo->pTabList->a; |
| 3497 | for(k=1; k<=nNotReady; k++){ |
| 3498 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3499 | } |
| 3500 | }else{ |
| 3501 | pOrTab = pWInfo->pTabList; |
| 3502 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3503 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3504 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3505 | ** equivalent to an empty rowset. Or, create an ephemeral index |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3506 | ** capable of holding primary keys in the case of a WITHOUT ROWID. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3507 | ** |
| 3508 | ** Also initialize regReturn to contain the address of the instruction |
| 3509 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3510 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3511 | ** over the top of the loop into the body of it. In this case the |
| 3512 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3513 | ** fall through to the next instruction, just as an OP_Next does if |
| 3514 | ** called on an uninitialized cursor. |
| 3515 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3516 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3517 | if( HasRowid(pTab) ){ |
| 3518 | regRowset = ++pParse->nMem; |
| 3519 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3520 | }else{ |
| 3521 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 3522 | regRowset = pParse->nTab++; |
| 3523 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 3524 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 3525 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3526 | regRowid = ++pParse->nMem; |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3527 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3528 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3529 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3530 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3531 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3532 | ** That way, terms in y that are factored into the disjunction will |
| 3533 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3534 | ** |
| 3535 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3536 | ** the "interesting" terms of z - terms that did not originate in the |
| 3537 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3538 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3539 | ** |
| 3540 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3541 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3542 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3543 | */ |
| 3544 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3545 | int iTerm; |
| 3546 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3547 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3548 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3549 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 7c32806 | 2014-02-11 01:50:29 +0000 | [diff] [blame] | 3550 | testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 3551 | testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 3552 | if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3553 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3554 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 3555 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3556 | } |
| 3557 | if( pAndExpr ){ |
| 3558 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3559 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3562 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 3563 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 3564 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 3565 | */ |
drh | 36be4c4 | 2014-09-30 17:31:23 +0000 | [diff] [blame] | 3566 | wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 3567 | | WHERE_FORCE_TABLE |
| 3568 | | WHERE_ONETABLE_ONLY; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3569 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3570 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3571 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3572 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 3573 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| 3574 | int j1 = 0; /* Address of jump operation */ |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3575 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3576 | pAndExpr->pLeft = pOrExpr; |
| 3577 | pOrExpr = pAndExpr; |
| 3578 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3579 | /* Loop through table entries that match term pOrTerm. */ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 3580 | WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3581 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3582 | wctrlFlags, iCovCur); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3583 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3584 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3585 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3586 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3587 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3588 | ); |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3589 | /* This is the sub-WHERE clause body. First skip over |
| 3590 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 3591 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 3592 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 3593 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3594 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3595 | int r; |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3596 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3597 | if( HasRowid(pTab) ){ |
| 3598 | r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3599 | j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet); |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3600 | VdbeCoverage(v); |
| 3601 | }else{ |
| 3602 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 3603 | int nPk = pPk->nKeyCol; |
| 3604 | int iPk; |
| 3605 | |
| 3606 | /* Read the PK into an array of temp registers. */ |
| 3607 | r = sqlite3GetTempRange(pParse, nPk); |
| 3608 | for(iPk=0; iPk<nPk; iPk++){ |
| 3609 | int iCol = pPk->aiColumn[iPk]; |
| 3610 | sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0); |
| 3611 | } |
| 3612 | |
| 3613 | /* Check if the temp table already contains this key. If so, |
| 3614 | ** the row has already been included in the result set and |
| 3615 | ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 3616 | ** insert the key into the temp table and proceed with processing |
| 3617 | ** the row. |
| 3618 | ** |
| 3619 | ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 3620 | ** is zero, assume that the key cannot already be present in |
| 3621 | ** the temp table. And if iSet is -1, assume that there is no |
| 3622 | ** need to insert the key into the temp table, as it will never |
| 3623 | ** be tested for. */ |
| 3624 | if( iSet ){ |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3625 | j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
drh | 68c1215 | 2014-05-26 20:25:34 +0000 | [diff] [blame] | 3626 | VdbeCoverage(v); |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3627 | } |
| 3628 | if( iSet>=0 ){ |
| 3629 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 3630 | sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); |
| 3631 | if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 3632 | } |
| 3633 | |
| 3634 | /* Release the array of temp registers */ |
| 3635 | sqlite3ReleaseTempRange(pParse, r, nPk); |
| 3636 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3637 | } |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3638 | |
| 3639 | /* Invoke the main loop body as a subroutine */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3640 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* Jump here (skipping the main loop body subroutine) if the |
| 3643 | ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3644 | if( j1 ) sqlite3VdbeJumpHere(v, j1); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3645 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3646 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3647 | ** contained one or more AND term from a notReady table. The |
| 3648 | ** terms from the notReady table could not be tested and will |
| 3649 | ** need to be tested later. |
| 3650 | */ |
| 3651 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3652 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3653 | /* If all of the OR-connected terms are optimized using the same |
| 3654 | ** index, and the index is opened using the same cursor number |
| 3655 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3656 | ** be possible to use that index as a covering index. |
| 3657 | ** |
| 3658 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3659 | ** uses an index, and this is either the first OR-connected term |
| 3660 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3661 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3662 | ** pCov to NULL to indicate that no candidate covering index will |
| 3663 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3664 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3665 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3666 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3667 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3668 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 3669 | && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3670 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3671 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3672 | pCov = pSubLoop->u.btree.pIndex; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3673 | wctrlFlags |= WHERE_REOPEN_IDX; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3674 | }else{ |
| 3675 | pCov = 0; |
| 3676 | } |
| 3677 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3678 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3679 | sqlite3WhereEnd(pSubWInfo); |
| 3680 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3683 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3684 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3685 | if( pAndExpr ){ |
| 3686 | pAndExpr->pLeft = 0; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3687 | sqlite3ExprDelete(db, pAndExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3688 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3689 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3690 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3691 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3692 | |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3693 | if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3694 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3695 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3696 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3697 | |
| 3698 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3699 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3700 | ** scan of the entire table. |
| 3701 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3702 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3703 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3704 | assert( bRev==0 || bRev==1 ); |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3705 | if( pTabItem->isRecursive ){ |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 3706 | /* Tables marked isRecursive have only a single row that is stored in |
dan | 4102815 | 2014-01-22 10:22:25 +0000 | [diff] [blame] | 3707 | ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3708 | pLevel->op = OP_Noop; |
| 3709 | }else{ |
| 3710 | pLevel->op = aStep[bRev]; |
| 3711 | pLevel->p1 = iCur; |
| 3712 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3713 | VdbeCoverageIf(v, bRev==0); |
| 3714 | VdbeCoverageIf(v, bRev!=0); |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3715 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3716 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3717 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3718 | |
| 3719 | /* Insert code to test every subexpression that can be completely |
| 3720 | ** computed using the current set of tables. |
| 3721 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3722 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3723 | Expr *pE; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3724 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3725 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3726 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3727 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3728 | testcase( pWInfo->untestedTerms==0 |
| 3729 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3730 | pWInfo->untestedTerms = 1; |
| 3731 | continue; |
| 3732 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3733 | pE = pTerm->pExpr; |
| 3734 | assert( pE!=0 ); |
| 3735 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3736 | continue; |
| 3737 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3738 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3739 | pTerm->wtFlags |= TERM_CODED; |
| 3740 | } |
| 3741 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3742 | /* Insert code to test for implied constraints based on transitivity |
| 3743 | ** of the "==" operator. |
| 3744 | ** |
| 3745 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3746 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3747 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3748 | ** the implied "t1.a=123" constraint. |
| 3749 | */ |
| 3750 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3751 | Expr *pE, *pEAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3752 | WhereTerm *pAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3753 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3754 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3755 | if( pTerm->leftCursor!=iCur ) continue; |
drh | cdc2e43 | 2013-07-01 17:27:19 +0000 | [diff] [blame] | 3756 | if( pLevel->iLeftJoin ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3757 | pE = pTerm->pExpr; |
| 3758 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3759 | assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3760 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3761 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3762 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3763 | testcase( pAlt->eOperator & WO_EQ ); |
| 3764 | testcase( pAlt->eOperator & WO_IN ); |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 3765 | VdbeModuleComment((v, "begin transitive constraint")); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3766 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 3767 | if( pEAlt ){ |
| 3768 | *pEAlt = *pAlt->pExpr; |
| 3769 | pEAlt->pLeft = pE->pLeft; |
| 3770 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 3771 | sqlite3StackFree(db, pEAlt); |
| 3772 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3775 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3776 | ** at least one row of the right table has matched the left table. |
| 3777 | */ |
| 3778 | if( pLevel->iLeftJoin ){ |
| 3779 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3780 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3781 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3782 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3783 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3784 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3785 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3786 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3787 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3788 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3789 | continue; |
| 3790 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3791 | assert( pTerm->pExpr ); |
| 3792 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3793 | pTerm->wtFlags |= TERM_CODED; |
| 3794 | } |
| 3795 | } |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3796 | |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3797 | return pLevel->notReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3800 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3801 | /* |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 3802 | ** Print the content of a WhereTerm object |
| 3803 | */ |
| 3804 | static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 3805 | if( pTerm==0 ){ |
| 3806 | sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 3807 | }else{ |
| 3808 | char zType[4]; |
| 3809 | memcpy(zType, "...", 4); |
| 3810 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 3811 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 3812 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 3813 | sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n", |
| 3814 | iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, |
| 3815 | pTerm->eOperator); |
| 3816 | sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 3817 | } |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 3818 | } |
| 3819 | #endif |
| 3820 | |
| 3821 | #ifdef WHERETRACE_ENABLED |
| 3822 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3823 | ** Print a WhereLoop object for debugging purposes |
| 3824 | */ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3825 | static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 3826 | WhereInfo *pWInfo = pWC->pWInfo; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3827 | int nb = 1+(pWInfo->pTabList->nSrc+7)/8; |
| 3828 | struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3829 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3830 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3831 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3832 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3833 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3834 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 3835 | const char *zName; |
| 3836 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3837 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3838 | int i = sqlite3Strlen30(zName) - 1; |
| 3839 | while( zName[i]!='_' ) i--; |
| 3840 | zName += i; |
| 3841 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3842 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3843 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3844 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3845 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3846 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3847 | char *z; |
| 3848 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3849 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3850 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3851 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3852 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3853 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3854 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3855 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3856 | } |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 3857 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3858 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 3859 | }else{ |
| 3860 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 3861 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3862 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 3863 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| 3864 | int i; |
| 3865 | for(i=0; i<p->nLTerm; i++){ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 3866 | whereTermPrint(p->aLTerm[i], i); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3869 | } |
| 3870 | #endif |
| 3871 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3872 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3873 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 3874 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3875 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3876 | static void whereLoopInit(WhereLoop *p){ |
| 3877 | p->aLTerm = p->aLTermSpace; |
| 3878 | p->nLTerm = 0; |
| 3879 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 3880 | p->wsFlags = 0; |
| 3881 | } |
| 3882 | |
| 3883 | /* |
| 3884 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 3885 | */ |
| 3886 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3887 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3888 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 3889 | sqlite3_free(p->u.vtab.idxStr); |
| 3890 | p->u.vtab.needFree = 0; |
| 3891 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3892 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3893 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3894 | sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo); |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3895 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 3896 | p->u.btree.pIndex = 0; |
| 3897 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3898 | } |
| 3899 | } |
| 3900 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3901 | /* |
| 3902 | ** Deallocate internal memory used by a WhereLoop object |
| 3903 | */ |
| 3904 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 3905 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3906 | whereLoopClearUnion(db, p); |
| 3907 | whereLoopInit(p); |
| 3908 | } |
| 3909 | |
| 3910 | /* |
| 3911 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 3912 | */ |
| 3913 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 3914 | WhereTerm **paNew; |
| 3915 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 3916 | n = (n+7)&~7; |
| 3917 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 3918 | if( paNew==0 ) return SQLITE_NOMEM; |
| 3919 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 3920 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3921 | p->aLTerm = paNew; |
| 3922 | p->nLSlot = n; |
| 3923 | return SQLITE_OK; |
| 3924 | } |
| 3925 | |
| 3926 | /* |
| 3927 | ** Transfer content from the second pLoop into the first. |
| 3928 | */ |
| 3929 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3930 | whereLoopClearUnion(db, pTo); |
drh | 0d31dc3 | 2013-09-06 00:40:59 +0000 | [diff] [blame] | 3931 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 3932 | memset(&pTo->u, 0, sizeof(pTo->u)); |
| 3933 | return SQLITE_NOMEM; |
| 3934 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 3935 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 3936 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3937 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 3938 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3939 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3940 | pFrom->u.btree.pIndex = 0; |
| 3941 | } |
| 3942 | return SQLITE_OK; |
| 3943 | } |
| 3944 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3945 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3946 | ** Delete a WhereLoop object |
| 3947 | */ |
| 3948 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3949 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3950 | sqlite3DbFree(db, p); |
| 3951 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 3952 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3953 | /* |
| 3954 | ** Free a WhereInfo structure |
| 3955 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 3956 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 3957 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3958 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3959 | while( pWInfo->pLoops ){ |
| 3960 | WhereLoop *p = pWInfo->pLoops; |
| 3961 | pWInfo->pLoops = p->pNextLoop; |
| 3962 | whereLoopDelete(db, p); |
| 3963 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3964 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3965 | } |
| 3966 | } |
| 3967 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3968 | /* |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3969 | ** Return TRUE if both of the following are true: |
| 3970 | ** |
| 3971 | ** (1) X has the same or lower cost that Y |
| 3972 | ** (2) X is a proper subset of Y |
| 3973 | ** |
| 3974 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 3975 | ** than Y and that every WHERE clause term used by X is also used |
| 3976 | ** by Y. |
| 3977 | ** |
| 3978 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 3979 | ** to have a lower cost. This routine returns TRUE when that cost |
| 3980 | ** relationship is inverted and needs to be adjusted. |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3981 | */ |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3982 | static int whereLoopCheaperProperSubset( |
| 3983 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 3984 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 3985 | ){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3986 | int i, j; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3987 | if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 3988 | return 0; /* X is not a subset of Y */ |
| 3989 | } |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3990 | if( pX->rRun >= pY->rRun ){ |
| 3991 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 3992 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3993 | } |
drh | 9ee8810 | 2014-05-07 20:33:17 +0000 | [diff] [blame] | 3994 | for(i=pX->nLTerm-1; i>=0; i--){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3995 | if( pX->aLTerm[i]==0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3996 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 3997 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 3998 | } |
| 3999 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 4000 | } |
| 4001 | return 1; /* All conditions meet */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /* |
| 4005 | ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so |
| 4006 | ** that: |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4007 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4008 | ** (1) pTemplate costs less than any other WhereLoops that are a proper |
| 4009 | ** subset of pTemplate |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4010 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4011 | ** (2) pTemplate costs more than any other WhereLoops for which pTemplate |
| 4012 | ** is a proper subset. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4013 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4014 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 4015 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 4016 | ** also used by Y. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4017 | */ |
| 4018 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 4019 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4020 | for(; p; p=p->pNextLoop){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4021 | if( p->iTab!=pTemplate->iTab ) continue; |
| 4022 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4023 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 4024 | /* Adjust pTemplate cost downward so that it is cheaper than its |
drh | f7f2e84 | 2014-10-21 18:16:21 +0000 | [diff] [blame] | 4025 | ** subset p. Except, do not adjust the cost estimate downward for |
| 4026 | ** a loop that skips more columns. */ |
| 4027 | if( pTemplate->nSkip>p->nSkip ) continue; |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 4028 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 4029 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4030 | pTemplate->rRun = p->rRun; |
| 4031 | pTemplate->nOut = p->nOut - 1; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4032 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 4033 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 4034 | ** pTemplate is a proper subset of p */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 4035 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 4036 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4037 | pTemplate->rRun = p->rRun; |
| 4038 | pTemplate->nOut = p->nOut + 1; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4039 | } |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /* |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4044 | ** Search the list of WhereLoops in *ppPrev looking for one that can be |
| 4045 | ** supplanted by pTemplate. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4046 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4047 | ** Return NULL if the WhereLoop list contains an entry that can supplant |
| 4048 | ** pTemplate, in other words if pTemplate does not belong on the list. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4049 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4050 | ** If pX is a WhereLoop that pTemplate can supplant, then return the |
| 4051 | ** link that points to pX. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4052 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4053 | ** If pTemplate cannot supplant any existing element of the list but needs |
| 4054 | ** 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] | 4055 | */ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4056 | static WhereLoop **whereLoopFindLesser( |
| 4057 | WhereLoop **ppPrev, |
| 4058 | const WhereLoop *pTemplate |
| 4059 | ){ |
| 4060 | WhereLoop *p; |
| 4061 | for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4062 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 4063 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 4064 | ** then those WhereLoops need to be considered separately. Neither is |
| 4065 | ** a candidate to replace the other. */ |
| 4066 | continue; |
| 4067 | } |
| 4068 | /* In the current implementation, the rSetup value is either zero |
| 4069 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 4070 | ** is the same for compatible WhereLoops. */ |
| 4071 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 4072 | || p->rSetup==pTemplate->rSetup ); |
| 4073 | |
| 4074 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 4075 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 4076 | ** rSetup. Call this SETUP-INVARIANT */ |
| 4077 | assert( p->rSetup>=pTemplate->rSetup ); |
| 4078 | |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4079 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 4080 | ** UNIQUE constraint) with one or more == constraints is better |
| 4081 | ** than an automatic index. */ |
| 4082 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 4083 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4084 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 4085 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 4086 | ){ |
| 4087 | break; |
| 4088 | } |
| 4089 | |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4090 | /* If existing WhereLoop p is better than pTemplate, pTemplate can be |
| 4091 | ** discarded. WhereLoop p is better if: |
| 4092 | ** (1) p has no more dependencies than pTemplate, and |
| 4093 | ** (2) p has an equal or lower cost than pTemplate |
| 4094 | */ |
| 4095 | if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */ |
| 4096 | && p->rSetup<=pTemplate->rSetup /* (2a) */ |
| 4097 | && p->rRun<=pTemplate->rRun /* (2b) */ |
| 4098 | && p->nOut<=pTemplate->nOut /* (2c) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4099 | ){ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4100 | return 0; /* Discard pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4101 | } |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4102 | |
| 4103 | /* If pTemplate is always better than p, then cause p to be overwritten |
| 4104 | ** with pTemplate. pTemplate is better than p if: |
| 4105 | ** (1) pTemplate has no more dependences than p, and |
| 4106 | ** (2) pTemplate has an equal or lower cost than p. |
| 4107 | */ |
| 4108 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ |
| 4109 | && p->rRun>=pTemplate->rRun /* (2a) */ |
| 4110 | && p->nOut>=pTemplate->nOut /* (2b) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4111 | ){ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4112 | assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4113 | break; /* Cause p to be overwritten by pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4116 | return ppPrev; |
| 4117 | } |
| 4118 | |
| 4119 | /* |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4120 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4121 | ** |
| 4122 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4123 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4124 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4125 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 4126 | ** added based on the template. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4127 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4128 | ** If pBuilder->pOrSet is not NULL then we care about only the |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4129 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 4130 | ** information is gathered in the pBuilder->pOrSet object. This special |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4131 | ** processing mode is used only for OR clause processing. |
| 4132 | ** |
| 4133 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
| 4134 | ** still might overwrite similar loops with the new template if the |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4135 | ** new template is better. Loops may be overwritten if the following |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4136 | ** conditions are met: |
| 4137 | ** |
| 4138 | ** (1) They have the same iTab. |
| 4139 | ** (2) They have the same iSortIdx. |
| 4140 | ** (3) The template has same or fewer dependencies than the current loop |
| 4141 | ** (4) The template has the same or lower cost than the current loop |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4142 | */ |
| 4143 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4144 | WhereLoop **ppPrev, *p; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4145 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 4146 | sqlite3 *db = pWInfo->pParse->db; |
| 4147 | |
| 4148 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 4149 | ** and prereqs. |
| 4150 | */ |
| 4151 | if( pBuilder->pOrSet!=0 ){ |
| 4152 | #if WHERETRACE_ENABLED |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4153 | u16 n = pBuilder->pOrSet->n; |
| 4154 | int x = |
| 4155 | #endif |
| 4156 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 4157 | pTemplate->nOut); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4158 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4159 | if( sqlite3WhereTrace & 0x8 ){ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 4160 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 4161 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4162 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4163 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4164 | return SQLITE_OK; |
| 4165 | } |
| 4166 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4167 | /* Look for an existing WhereLoop to replace with pTemplate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4168 | */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4169 | whereLoopAdjustCost(pWInfo->pLoops, pTemplate); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4170 | ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4171 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4172 | if( ppPrev==0 ){ |
| 4173 | /* There already exists a WhereLoop on the list that is better |
| 4174 | ** than pTemplate, so just ignore pTemplate */ |
| 4175 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4176 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4177 | sqlite3DebugPrintf(" skip: "); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4178 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4179 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4180 | #endif |
| 4181 | return SQLITE_OK; |
| 4182 | }else{ |
| 4183 | p = *ppPrev; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /* If we reach this point it means that either p[] should be overwritten |
| 4187 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4188 | ** WhereLoop and insert it. |
| 4189 | */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4190 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4191 | if( sqlite3WhereTrace & 0x8 ){ |
| 4192 | if( p!=0 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4193 | sqlite3DebugPrintf("replace: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4194 | whereLoopPrint(p, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4195 | } |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4196 | sqlite3DebugPrintf(" add: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4197 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4198 | } |
| 4199 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4200 | if( p==0 ){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4201 | /* Allocate a new WhereLoop to add to the end of the list */ |
| 4202 | *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4203 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4204 | whereLoopInit(p); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4205 | p->pNextLoop = 0; |
| 4206 | }else{ |
| 4207 | /* We will be overwriting WhereLoop p[]. But before we do, first |
| 4208 | ** go through the rest of the list and delete any other entries besides |
| 4209 | ** p[] that are also supplated by pTemplate */ |
| 4210 | WhereLoop **ppTail = &p->pNextLoop; |
| 4211 | WhereLoop *pToDel; |
| 4212 | while( *ppTail ){ |
| 4213 | ppTail = whereLoopFindLesser(ppTail, pTemplate); |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4214 | if( ppTail==0 ) break; |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4215 | pToDel = *ppTail; |
| 4216 | if( pToDel==0 ) break; |
| 4217 | *ppTail = pToDel->pNextLoop; |
| 4218 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4219 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4220 | sqlite3DebugPrintf(" delete: "); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4221 | whereLoopPrint(pToDel, pBuilder->pWC); |
| 4222 | } |
| 4223 | #endif |
| 4224 | whereLoopDelete(db, pToDel); |
| 4225 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4226 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4227 | whereLoopXfer(db, p, pTemplate); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4228 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4229 | Index *pIndex = p->u.btree.pIndex; |
| 4230 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4231 | p->u.btree.pIndex = 0; |
| 4232 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4233 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4234 | return SQLITE_OK; |
| 4235 | } |
| 4236 | |
| 4237 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4238 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 4239 | ** WHERE clause that reference the loop but which are not used by an |
| 4240 | ** index. |
| 4241 | ** |
| 4242 | ** In the current implementation, the first extra WHERE clause term reduces |
| 4243 | ** the number of output rows by a factor of 10 and each additional term |
| 4244 | ** reduces the number of output rows by sqrt(2). |
| 4245 | */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4246 | static void whereLoopOutputAdjust( |
| 4247 | WhereClause *pWC, /* The WHERE clause */ |
| 4248 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 4249 | LogEst nRow /* Number of rows in the entire table */ |
| 4250 | ){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4251 | WhereTerm *pTerm, *pX; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4252 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4253 | int i, j; |
mistachkin | 6b9da12 | 2014-09-06 02:00:41 +0000 | [diff] [blame] | 4254 | int nEq = 0; /* Number of = constraints not within likely()/unlikely() */ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4255 | |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4256 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4257 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4258 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 4259 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4260 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 4261 | pX = pLoop->aLTerm[j]; |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 4262 | if( pX==0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4263 | if( pX==pTerm ) break; |
| 4264 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 4265 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4266 | if( j<0 ){ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4267 | if( pTerm->truthProb<=0 ){ |
| 4268 | pLoop->nOut += pTerm->truthProb; |
| 4269 | }else{ |
| 4270 | pLoop->nOut--; |
| 4271 | if( pTerm->eOperator&WO_EQ ) nEq++; |
| 4272 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4273 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4274 | } |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4275 | /* TUNING: If there is at least one equality constraint in the WHERE |
| 4276 | ** clause that does not have a likelihood() explicitly assigned to it |
| 4277 | ** then do not let the estimated number of output rows exceed half |
| 4278 | ** the number of rows in the table. */ |
| 4279 | if( nEq && pLoop->nOut>nRow-10 ){ |
| 4280 | pLoop->nOut = nRow - 10; |
| 4281 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4285 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 4286 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| 4287 | */ |
| 4288 | #ifdef SQLITE_ENABLE_COSTMULT |
| 4289 | # define ApplyCostMultiplier(C,T) C += T |
| 4290 | #else |
| 4291 | # define ApplyCostMultiplier(C,T) |
| 4292 | #endif |
| 4293 | |
| 4294 | /* |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 4295 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the |
| 4296 | ** index pIndex. Try to match one more. |
| 4297 | ** |
| 4298 | ** When this function is called, pBuilder->pNew->nOut contains the |
| 4299 | ** number of rows expected to be visited by filtering using the nEq |
| 4300 | ** terms only. If it is modified, this value is restored before this |
| 4301 | ** function returns. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4302 | ** |
| 4303 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4304 | ** INTEGER PRIMARY KEY. |
| 4305 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4306 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4307 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4308 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4309 | Index *pProbe, /* An index on pSrc */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4310 | LogEst nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4311 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4312 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4313 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4314 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4315 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4316 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4317 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4318 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4319 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4320 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4321 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4322 | u16 saved_nSkip; /* Original value of pNew->nSkip */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4323 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4324 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4325 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4326 | int rc = SQLITE_OK; /* Return code */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4327 | LogEst rSize; /* Number of rows in the table */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4328 | LogEst rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4329 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4330 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4331 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4332 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4333 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4334 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4335 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4336 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4337 | opMask = WO_LT|WO_LE; |
| 4338 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4339 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4340 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4341 | 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] | 4342 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4343 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4344 | |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 4345 | assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 4346 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 4347 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4348 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4349 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4350 | saved_nEq = pNew->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4351 | saved_nSkip = pNew->nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4352 | saved_nLTerm = pNew->nLTerm; |
| 4353 | saved_wsFlags = pNew->wsFlags; |
| 4354 | saved_prereq = pNew->prereq; |
| 4355 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4356 | pNew->rSetup = 0; |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4357 | rSize = pProbe->aiRowLogEst[0]; |
| 4358 | rLogSize = estLog(rSize); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4359 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4360 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4361 | LogEst rCostIdx; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4362 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4363 | int nIn = 0; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4364 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4365 | int nRecValid = pBuilder->nRecValid; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 4366 | #endif |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4367 | if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
dan | 8bff07a | 2013-08-29 14:56:14 +0000 | [diff] [blame] | 4368 | && (iCol<0 || pSrc->pTab->aCol[iCol].notNull) |
| 4369 | ){ |
| 4370 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 4371 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4372 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 4373 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4374 | pNew->wsFlags = saved_wsFlags; |
| 4375 | pNew->u.btree.nEq = saved_nEq; |
| 4376 | pNew->nLTerm = saved_nLTerm; |
| 4377 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4378 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4379 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4380 | |
| 4381 | assert( nInMul==0 |
| 4382 | || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| 4383 | || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 |
| 4384 | || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 |
| 4385 | ); |
| 4386 | |
| 4387 | if( eOp & WO_IN ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4388 | Expr *pExpr = pTerm->pExpr; |
| 4389 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4390 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4391 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4392 | nIn = 46; assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4393 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4394 | /* "x IN (value, value, ...)" */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4395 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4396 | } |
drh | 2b59b3a | 2014-03-20 13:26:47 +0000 | [diff] [blame] | 4397 | assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 4398 | ** changes "x IN (?)" into "x=?". */ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4399 | |
| 4400 | }else if( eOp & (WO_EQ) ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4401 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4402 | if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4403 | if( iCol>=0 && !IsUniqueIndex(pProbe) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4404 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 4405 | }else{ |
| 4406 | pNew->wsFlags |= WHERE_ONEROW; |
| 4407 | } |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4408 | } |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 4409 | }else if( eOp & WO_ISNULL ){ |
| 4410 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4411 | }else if( eOp & (WO_GT|WO_GE) ){ |
| 4412 | testcase( eOp & WO_GT ); |
| 4413 | testcase( eOp & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4414 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4415 | pBtm = pTerm; |
| 4416 | pTop = 0; |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 4417 | }else{ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4418 | assert( eOp & (WO_LT|WO_LE) ); |
| 4419 | testcase( eOp & WO_LT ); |
| 4420 | testcase( eOp & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4421 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4422 | pTop = pTerm; |
| 4423 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4424 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4425 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4426 | |
| 4427 | /* At this point pNew->nOut is set to the number of rows expected to |
| 4428 | ** be visited by the index scan before considering term pTerm, or the |
| 4429 | ** values of nIn and nInMul. In other words, assuming that all |
| 4430 | ** "x IN(...)" terms are replaced with "x = ?". This block updates |
| 4431 | ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ |
| 4432 | assert( pNew->nOut==saved_nOut ); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4433 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4434 | /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 |
| 4435 | ** data, using some other estimate. */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4436 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4437 | }else{ |
| 4438 | int nEq = ++pNew->u.btree.nEq; |
| 4439 | assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) ); |
| 4440 | |
| 4441 | assert( pNew->nOut==saved_nOut ); |
dan | 09e1df6 | 2014-04-29 16:10:22 +0000 | [diff] [blame] | 4442 | if( pTerm->truthProb<=0 && iCol>=0 ){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4443 | assert( (eOp & WO_IN) || nIn==0 ); |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 4444 | testcase( eOp & WO_IN ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4445 | pNew->nOut += pTerm->truthProb; |
| 4446 | pNew->nOut -= nIn; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4447 | }else{ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4448 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4449 | tRowcnt nOut = 0; |
| 4450 | if( nInMul==0 |
| 4451 | && pProbe->nSample |
| 4452 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4453 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4454 | ){ |
| 4455 | Expr *pExpr = pTerm->pExpr; |
| 4456 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 4457 | testcase( eOp & WO_EQ ); |
| 4458 | testcase( eOp & WO_ISNULL ); |
| 4459 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
| 4460 | }else{ |
| 4461 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
| 4462 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4463 | if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 4464 | if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ |
| 4465 | if( nOut ){ |
| 4466 | pNew->nOut = sqlite3LogEst(nOut); |
| 4467 | if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; |
| 4468 | pNew->nOut -= nIn; |
| 4469 | } |
| 4470 | } |
| 4471 | if( nOut==0 ) |
| 4472 | #endif |
| 4473 | { |
| 4474 | pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); |
| 4475 | if( eOp & WO_ISNULL ){ |
| 4476 | /* TUNING: If there is no likelihood() value, assume that a |
| 4477 | ** "col IS NULL" expression matches twice as many rows |
| 4478 | ** as (col=?). */ |
| 4479 | pNew->nOut += 10; |
| 4480 | } |
| 4481 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4482 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4483 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4484 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4485 | /* Set rCostIdx to the cost of visiting selected rows in index. Add |
| 4486 | ** it to pNew->rRun, which is currently set to the cost of the index |
| 4487 | ** seek only. Then, if this is a non-covering index, add the cost of |
| 4488 | ** visiting the rows in the main table. */ |
| 4489 | rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4490 | pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4491 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4492 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4493 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4494 | ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4495 | |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4496 | nOutUnadjusted = pNew->nOut; |
| 4497 | pNew->rRun += nInMul + nIn; |
| 4498 | pNew->nOut += nInMul + nIn; |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4499 | whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4500 | rc = whereLoopInsert(pBuilder, pNew); |
dan | 440e6ff | 2014-04-28 08:49:54 +0000 | [diff] [blame] | 4501 | |
| 4502 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4503 | pNew->nOut = saved_nOut; |
| 4504 | }else{ |
| 4505 | pNew->nOut = nOutUnadjusted; |
| 4506 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4507 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4508 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 4509 | && pNew->u.btree.nEq<pProbe->nColumn |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4510 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4511 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4512 | } |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4513 | pNew->nOut = saved_nOut; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4514 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4515 | pBuilder->nRecValid = nRecValid; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4516 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4517 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4518 | pNew->prereq = saved_prereq; |
| 4519 | pNew->u.btree.nEq = saved_nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4520 | pNew->nSkip = saved_nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4521 | pNew->wsFlags = saved_wsFlags; |
| 4522 | pNew->nOut = saved_nOut; |
| 4523 | pNew->nLTerm = saved_nLTerm; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4524 | |
| 4525 | /* Consider using a skip-scan if there are no WHERE clause constraints |
| 4526 | ** available for the left-most terms of the index, and if the average |
| 4527 | ** number of repeats in the left-most terms is at least 18. |
| 4528 | ** |
| 4529 | ** The magic number 18 is selected on the basis that scanning 17 rows |
| 4530 | ** is almost always quicker than an index seek (even though if the index |
| 4531 | ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 4532 | ** the code). And, even if it is not, it should not be too much slower. |
| 4533 | ** On the other hand, the extra seeks could end up being significantly |
| 4534 | ** more expensive. */ |
| 4535 | assert( 42==sqlite3LogEst(18) ); |
| 4536 | if( saved_nEq==saved_nSkip |
| 4537 | && saved_nEq+1<pProbe->nKeyCol |
| 4538 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 4539 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 4540 | ){ |
| 4541 | LogEst nIter; |
| 4542 | pNew->u.btree.nEq++; |
| 4543 | pNew->nSkip++; |
| 4544 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 4545 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 4546 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4547 | pNew->nOut -= nIter; |
| 4548 | /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 4549 | ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 4550 | nIter += 5; |
| 4551 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 4552 | pNew->nOut = saved_nOut; |
| 4553 | pNew->u.btree.nEq = saved_nEq; |
| 4554 | pNew->nSkip = saved_nSkip; |
| 4555 | pNew->wsFlags = saved_wsFlags; |
| 4556 | } |
| 4557 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4558 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4562 | ** Return True if it is possible that pIndex might be useful in |
| 4563 | ** implementing the ORDER BY clause in pBuilder. |
| 4564 | ** |
| 4565 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4566 | ** if there is no way for pIndex to be useful in implementing that |
| 4567 | ** ORDER BY clause. |
| 4568 | */ |
| 4569 | static int indexMightHelpWithOrderBy( |
| 4570 | WhereLoopBuilder *pBuilder, |
| 4571 | Index *pIndex, |
| 4572 | int iCursor |
| 4573 | ){ |
| 4574 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4575 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4576 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4577 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4578 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4579 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4580 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4581 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4582 | if( pExpr->iTable==iCursor ){ |
drh | 137fd4f | 2014-09-19 02:01:37 +0000 | [diff] [blame] | 4583 | if( pExpr->iColumn<0 ) return 1; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4584 | for(jj=0; jj<pIndex->nKeyCol; jj++){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4585 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 4586 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4587 | } |
| 4588 | } |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
| 4592 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4593 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4594 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4595 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4596 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4597 | Bitmask m = 0; |
| 4598 | int j; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4599 | for(j=pIdx->nColumn-1; j>=0; j--){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4600 | int x = pIdx->aiColumn[j]; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4601 | if( x>=0 ){ |
| 4602 | testcase( x==BMS-1 ); |
| 4603 | testcase( x==BMS-2 ); |
| 4604 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4605 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4606 | } |
| 4607 | return m; |
| 4608 | } |
| 4609 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4610 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 4611 | ** in the current query. Return true if it can be and false if not. |
| 4612 | */ |
| 4613 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 4614 | int i; |
| 4615 | WhereTerm *pTerm; |
| 4616 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4617 | if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1; |
| 4618 | } |
| 4619 | return 0; |
| 4620 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4621 | |
| 4622 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 4623 | ** 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] | 4624 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4625 | ** a b-tree table, not a virtual table. |
dan | 8164722 | 2014-04-30 15:00:16 +0000 | [diff] [blame] | 4626 | ** |
| 4627 | ** The costs (WhereLoop.rRun) of the b-tree loops added by this function |
| 4628 | ** are calculated as follows: |
| 4629 | ** |
| 4630 | ** For a full scan, assuming the table (or index) contains nRow rows: |
| 4631 | ** |
| 4632 | ** cost = nRow * 3.0 // full-table scan |
| 4633 | ** cost = nRow * K // scan of covering index |
| 4634 | ** cost = nRow * (K+3.0) // scan of non-covering index |
| 4635 | ** |
| 4636 | ** where K is a value between 1.1 and 3.0 set based on the relative |
| 4637 | ** estimated average size of the index and table records. |
| 4638 | ** |
| 4639 | ** For an index scan, where nVisit is the number of index rows visited |
| 4640 | ** by the scan, and nSeek is the number of seek operations required on |
| 4641 | ** the index b-tree: |
| 4642 | ** |
| 4643 | ** cost = nSeek * (log(nRow) + K * nVisit) // covering index |
| 4644 | ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index |
| 4645 | ** |
| 4646 | ** Normally, nSeek is 1. nSeek values greater than 1 come about if the |
| 4647 | ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when |
| 4648 | ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. |
drh | 83a305f | 2014-07-22 12:05:32 +0000 | [diff] [blame] | 4649 | ** |
| 4650 | ** The estimated values (nRow, nVisit, nSeek) often contain a large amount |
| 4651 | ** of uncertainty. For this reason, scoring is designed to pick plans that |
| 4652 | ** "do the least harm" if the estimates are inaccurate. For example, a |
| 4653 | ** log(nRow) factor is omitted from a non-covering index scan in order to |
| 4654 | ** bias the scoring in favor of using an index, since the worst-case |
| 4655 | ** performance of using an index is far better than the worst-case performance |
| 4656 | ** of a full table scan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4657 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4658 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4659 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4660 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4661 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4662 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4663 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4664 | Index sPk; /* A fake index object for the primary key */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4665 | LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4666 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4667 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4668 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4669 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4670 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4671 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4672 | int b; /* A boolean value */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4673 | LogEst rSize; /* number of rows in the table */ |
| 4674 | LogEst rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4675 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4676 | Table *pTab; /* Table being queried */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4677 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4678 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4679 | pWInfo = pBuilder->pWInfo; |
| 4680 | pTabList = pWInfo->pTabList; |
| 4681 | pSrc = pTabList->a + pNew->iTab; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4682 | pTab = pSrc->pTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4683 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4684 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4685 | |
| 4686 | if( pSrc->pIndex ){ |
| 4687 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4688 | pProbe = pSrc->pIndex; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4689 | }else if( !HasRowid(pTab) ){ |
| 4690 | pProbe = pTab->pIndex; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4691 | }else{ |
| 4692 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4693 | ** variable sPk to represent the rowid primary key index. Make this |
| 4694 | ** fake index the first in a chain of Index objects with all of the real |
| 4695 | ** indices to follow */ |
| 4696 | Index *pFirst; /* First of real indices on the table */ |
| 4697 | memset(&sPk, 0, sizeof(Index)); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4698 | sPk.nKeyCol = 1; |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 4699 | sPk.nColumn = 1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4700 | sPk.aiColumn = &aiColumnPk; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4701 | sPk.aiRowLogEst = aiRowEstPk; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4702 | sPk.onError = OE_Replace; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4703 | sPk.pTable = pTab; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4704 | sPk.szIdxRow = pTab->szTabRow; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4705 | aiRowEstPk[0] = pTab->nRowLogEst; |
| 4706 | aiRowEstPk[1] = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4707 | pFirst = pSrc->pTab->pIndex; |
| 4708 | if( pSrc->notIndexed==0 ){ |
| 4709 | /* The real indices of the table are only considered if the |
| 4710 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4711 | sPk.pNext = pFirst; |
| 4712 | } |
| 4713 | pProbe = &sPk; |
| 4714 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4715 | rSize = pTab->nRowLogEst; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4716 | rLogSize = estLog(rSize); |
| 4717 | |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4718 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4719 | /* Automatic indexes */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4720 | if( !pBuilder->pOrSet |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 4721 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 4722 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4723 | && !pSrc->viaCoroutine |
| 4724 | && !pSrc->notIndexed |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4725 | && HasRowid(pTab) |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4726 | && !pSrc->isCorrelated |
dan | 62ba4e4 | 2014-01-15 18:21:41 +0000 | [diff] [blame] | 4727 | && !pSrc->isRecursive |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4728 | ){ |
| 4729 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4730 | WhereTerm *pTerm; |
| 4731 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4732 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4733 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4734 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4735 | pNew->u.btree.nEq = 1; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4736 | pNew->nSkip = 0; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4737 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4738 | pNew->nLTerm = 1; |
| 4739 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4740 | /* TUNING: One-time cost for computing the automatic index is |
drh | 7e07433 | 2014-09-22 14:30:51 +0000 | [diff] [blame] | 4741 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| 4742 | ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 4743 | ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 4744 | ** of X is smaller for views and subqueries so that the query planner |
| 4745 | ** will be more aggressive about generating automatic indexes for |
| 4746 | ** those objects, since there is no opportunity to add schema |
| 4747 | ** indexes on subqueries and views. */ |
| 4748 | pNew->rSetup = rLogSize + rSize + 4; |
| 4749 | if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 4750 | pNew->rSetup += 24; |
| 4751 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4752 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4753 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 4754 | ** is more than the usual guess of 10 rows, since we have no way |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4755 | ** of knowing how selective the index will ultimately be. It would |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4756 | ** not be unreasonable to make this value much larger. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4757 | pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4758 | pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4759 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4760 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4761 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4762 | } |
| 4763 | } |
| 4764 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4765 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4766 | |
| 4767 | /* Loop over all indices |
| 4768 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4769 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4770 | if( pProbe->pPartIdxWhere!=0 |
dan | 0829169 | 2014-08-27 17:37:20 +0000 | [diff] [blame] | 4771 | && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ |
| 4772 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4773 | continue; /* Partial index inappropriate for this query */ |
| 4774 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 4775 | rSize = pProbe->aiRowLogEst[0]; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4776 | pNew->u.btree.nEq = 0; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4777 | pNew->nSkip = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4778 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4779 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4780 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4781 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4782 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4783 | pNew->u.btree.pIndex = pProbe; |
| 4784 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4785 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 4786 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4787 | if( pProbe->tnum<=0 ){ |
| 4788 | /* Integer primary key index */ |
| 4789 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4790 | |
| 4791 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4792 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4793 | /* TUNING: Cost of full table scan is (N*3.0). */ |
| 4794 | pNew->rRun = rSize + 16; |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4795 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4796 | whereLoopOutputAdjust(pWC, pNew, rSize); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4797 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4798 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4799 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4800 | }else{ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4801 | Bitmask m; |
| 4802 | if( pProbe->isCovering ){ |
| 4803 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 4804 | m = 0; |
| 4805 | }else{ |
| 4806 | m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 4807 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 4808 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4809 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4810 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4811 | if( b |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 4812 | || !HasRowid(pTab) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4813 | || ( m==0 |
| 4814 | && pProbe->bUnordered==0 |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 4815 | && (pProbe->szIdxRow<pTab->szTabRow) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4816 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4817 | && sqlite3GlobalConfig.bUseCis |
| 4818 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 4819 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4820 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4821 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4822 | |
| 4823 | /* The cost of visiting the index rows is N*K, where K is |
| 4824 | ** between 1.1 and 3.0, depending on the relative sizes of the |
| 4825 | ** index and table rows. If this is a non-covering index scan, |
| 4826 | ** also add the cost of visiting table rows (N*3.0). */ |
| 4827 | pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; |
| 4828 | if( m!=0 ){ |
| 4829 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4830 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4831 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4832 | whereLoopOutputAdjust(pWC, pNew, rSize); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4833 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4834 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4835 | if( rc ) break; |
| 4836 | } |
| 4837 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4838 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4839 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4840 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 4841 | sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 4842 | pBuilder->nRecValid = 0; |
| 4843 | pBuilder->pRec = 0; |
dan | ddc2d6e | 2013-08-06 20:15:06 +0000 | [diff] [blame] | 4844 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4845 | |
| 4846 | /* If there was an INDEXED BY clause, then only that one index is |
| 4847 | ** considered. */ |
| 4848 | if( pSrc->pIndex ) break; |
| 4849 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4850 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4853 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4854 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4855 | ** Add all WhereLoop objects for a table of the join identified by |
| 4856 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4857 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4858 | static int whereLoopAddVirtual( |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4859 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 4860 | Bitmask mExtra |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4861 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4862 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4863 | Parse *pParse; /* The parsing context */ |
| 4864 | WhereClause *pWC; /* The WHERE clause */ |
| 4865 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4866 | Table *pTab; |
| 4867 | sqlite3 *db; |
| 4868 | sqlite3_index_info *pIdxInfo; |
| 4869 | struct sqlite3_index_constraint *pIdxCons; |
| 4870 | struct sqlite3_index_constraint_usage *pUsage; |
| 4871 | WhereTerm *pTerm; |
| 4872 | int i, j; |
| 4873 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4874 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4875 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4876 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4877 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4878 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4879 | int rc = SQLITE_OK; |
| 4880 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4881 | pWInfo = pBuilder->pWInfo; |
| 4882 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4883 | db = pParse->db; |
| 4884 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4885 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4886 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4887 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4888 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4889 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4890 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4891 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4892 | pNew->rSetup = 0; |
| 4893 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4894 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4895 | pNew->u.vtab.needFree = 0; |
| 4896 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4897 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4898 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 4899 | sqlite3DbFree(db, pIdxInfo); |
| 4900 | return SQLITE_NOMEM; |
| 4901 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4902 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4903 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4904 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4905 | iPhase++; |
| 4906 | if( iPhase>3 ) break; |
| 4907 | } |
| 4908 | if( !seenVar && iPhase>1 ) break; |
| 4909 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4910 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4911 | j = pIdxCons->iTermOffset; |
| 4912 | pTerm = &pWC->a[j]; |
| 4913 | switch( iPhase ){ |
| 4914 | case 0: /* Constants without IN operator */ |
| 4915 | pIdxCons->usable = 0; |
| 4916 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4917 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4918 | } |
| 4919 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4920 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4921 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4922 | pIdxCons->usable = 1; |
| 4923 | } |
| 4924 | break; |
| 4925 | case 1: /* Constants with IN operators */ |
| 4926 | assert( seenIn ); |
| 4927 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4928 | break; |
| 4929 | case 2: /* Variables without IN */ |
| 4930 | assert( seenVar ); |
| 4931 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4932 | break; |
| 4933 | default: /* Variables with IN */ |
| 4934 | assert( seenVar && seenIn ); |
| 4935 | pIdxCons->usable = 1; |
| 4936 | break; |
| 4937 | } |
| 4938 | } |
| 4939 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4940 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4941 | pIdxInfo->idxStr = 0; |
| 4942 | pIdxInfo->idxNum = 0; |
| 4943 | pIdxInfo->needToFreeIdxStr = 0; |
| 4944 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4945 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 4946 | pIdxInfo->estimatedRows = 25; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4947 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4948 | if( rc ) goto whereLoopAddVtab_exit; |
| 4949 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4950 | pNew->prereq = mExtra; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4951 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4952 | assert( pNew->nLSlot>=nConstraint ); |
| 4953 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4954 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4955 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4956 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4957 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4958 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4959 | || j<0 |
| 4960 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4961 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4962 | ){ |
| 4963 | rc = SQLITE_ERROR; |
| 4964 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4965 | goto whereLoopAddVtab_exit; |
| 4966 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4967 | testcase( iTerm==nConstraint-1 ); |
| 4968 | testcase( j==0 ); |
| 4969 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4970 | pTerm = &pWC->a[j]; |
| 4971 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4972 | assert( iTerm<pNew->nLSlot ); |
| 4973 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4974 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4975 | testcase( iTerm==15 ); |
| 4976 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4977 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4978 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4979 | if( pUsage[i].omit==0 ){ |
| 4980 | /* Do not attempt to use an IN constraint if the virtual table |
| 4981 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4982 | ** If we do attempt to use such a constraint, some rows might be |
| 4983 | ** repeated in the output. */ |
| 4984 | break; |
| 4985 | } |
| 4986 | /* A virtual table that is constrained by an IN clause may not |
| 4987 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4988 | ** is not necessarily related to the order of output terms and |
| 4989 | ** (2) Multiple outputs from a single IN value will not merge |
| 4990 | ** together. */ |
| 4991 | pIdxInfo->orderByConsumed = 0; |
| 4992 | } |
| 4993 | } |
| 4994 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4995 | if( i>=nConstraint ){ |
| 4996 | pNew->nLTerm = mxTerm+1; |
| 4997 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4998 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4999 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 5000 | pIdxInfo->needToFreeIdxStr = 0; |
| 5001 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5002 | pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? |
| 5003 | pIdxInfo->nOrderBy : 0); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5004 | pNew->rSetup = 0; |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 5005 | pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 5006 | pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5007 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5008 | if( pNew->u.vtab.needFree ){ |
| 5009 | sqlite3_free(pNew->u.vtab.idxStr); |
| 5010 | pNew->u.vtab.needFree = 0; |
| 5011 | } |
| 5012 | } |
| 5013 | } |
| 5014 | |
| 5015 | whereLoopAddVtab_exit: |
| 5016 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 5017 | sqlite3DbFree(db, pIdxInfo); |
| 5018 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5019 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5020 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5021 | |
| 5022 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5023 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 5024 | ** btrees or virtual tables. |
| 5025 | */ |
| 5026 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5027 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5028 | WhereClause *pWC; |
| 5029 | WhereLoop *pNew; |
| 5030 | WhereTerm *pTerm, *pWCEnd; |
| 5031 | int rc = SQLITE_OK; |
| 5032 | int iCur; |
| 5033 | WhereClause tempWC; |
| 5034 | WhereLoopBuilder sSubBuild; |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 5035 | WhereOrSet sSum, sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5036 | struct SrcList_item *pItem; |
| 5037 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5038 | pWC = pBuilder->pWC; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5039 | pWCEnd = pWC->a + pWC->nTerm; |
| 5040 | pNew = pBuilder->pNew; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 5041 | memset(&sSum, 0, sizeof(sSum)); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 5042 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 5043 | iCur = pItem->iCursor; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5044 | |
| 5045 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 5046 | if( (pTerm->eOperator & WO_OR)!=0 |
| 5047 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 5048 | ){ |
| 5049 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 5050 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 5051 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5052 | int once = 1; |
| 5053 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5054 | |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5055 | sSubBuild = *pBuilder; |
| 5056 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5057 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5058 | |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 5059 | WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5060 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5061 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5062 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 5063 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5064 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5065 | tempWC.pOuter = pWC; |
| 5066 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5067 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5068 | tempWC.a = pOrTerm; |
| 5069 | sSubBuild.pWC = &tempWC; |
| 5070 | }else{ |
| 5071 | continue; |
| 5072 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5073 | sCur.n = 0; |
drh | 5265149 | 2014-09-30 14:14:19 +0000 | [diff] [blame] | 5074 | #ifdef WHERETRACE_ENABLED |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 5075 | WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 5076 | (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 5077 | if( sqlite3WhereTrace & 0x400 ){ |
| 5078 | for(i=0; i<sSubBuild.pWC->nTerm; i++){ |
| 5079 | whereTermPrint(&sSubBuild.pWC->a[i], i); |
| 5080 | } |
drh | 5265149 | 2014-09-30 14:14:19 +0000 | [diff] [blame] | 5081 | } |
| 5082 | #endif |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5083 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5084 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 5085 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5086 | }else |
| 5087 | #endif |
| 5088 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5089 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 5090 | } |
drh | 36be4c4 | 2014-09-30 17:31:23 +0000 | [diff] [blame] | 5091 | if( rc==SQLITE_OK ){ |
| 5092 | rc = whereLoopAddOr(&sSubBuild, mExtra); |
| 5093 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5094 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 5095 | if( sCur.n==0 ){ |
| 5096 | sSum.n = 0; |
| 5097 | break; |
| 5098 | }else if( once ){ |
| 5099 | whereOrMove(&sSum, &sCur); |
| 5100 | once = 0; |
| 5101 | }else{ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 5102 | WhereOrSet sPrev; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5103 | whereOrMove(&sPrev, &sSum); |
| 5104 | sSum.n = 0; |
| 5105 | for(i=0; i<sPrev.n; i++){ |
| 5106 | for(j=0; j<sCur.n; j++){ |
| 5107 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5108 | sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 5109 | sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5110 | } |
| 5111 | } |
| 5112 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5113 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5114 | pNew->nLTerm = 1; |
| 5115 | pNew->aLTerm[0] = pTerm; |
| 5116 | pNew->wsFlags = WHERE_MULTI_OR; |
| 5117 | pNew->rSetup = 0; |
| 5118 | pNew->iSortIdx = 0; |
| 5119 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 5120 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 5121 | /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs |
| 5122 | ** of all sub-scans required by the OR-scan. However, due to rounding |
| 5123 | ** errors, it may be that the cost of the OR-scan is equal to its |
| 5124 | ** most expensive sub-scan. Add the smallest possible penalty |
| 5125 | ** (equivalent to multiplying the cost by 1.07) to ensure that |
| 5126 | ** this does not happen. Otherwise, for WHERE clauses such as the |
| 5127 | ** following where there is an index on "y": |
| 5128 | ** |
| 5129 | ** WHERE likelihood(x=?, 0.99) OR y=? |
| 5130 | ** |
| 5131 | ** the planner may elect to "OR" together a full-table scan and an |
| 5132 | ** index lookup. And other similarly odd results. */ |
| 5133 | pNew->rRun = sSum.a[i].rRun + 1; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5134 | pNew->nOut = sSum.a[i].nOut; |
| 5135 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 5136 | rc = whereLoopInsert(pBuilder, pNew); |
| 5137 | } |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 5138 | WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5139 | } |
| 5140 | } |
| 5141 | return rc; |
| 5142 | } |
| 5143 | |
| 5144 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5145 | ** Add all WhereLoop objects for all tables |
| 5146 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5147 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5148 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5149 | Bitmask mExtra = 0; |
| 5150 | Bitmask mPrior = 0; |
| 5151 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5152 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5153 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5154 | sqlite3 *db = pWInfo->pParse->db; |
| 5155 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5156 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5157 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5158 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5159 | |
| 5160 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5161 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5162 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5163 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5164 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5165 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5166 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5167 | mExtra = mPrior; |
| 5168 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5169 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5170 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 5171 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5172 | }else{ |
| 5173 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 5174 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5175 | if( rc==SQLITE_OK ){ |
| 5176 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 5177 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5178 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5179 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5180 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5181 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5182 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5185 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5186 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5187 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5188 | ** (or GROUP BY) without requiring a separate sort operation. Return N: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5189 | ** |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5190 | ** N>0: N terms of the ORDER BY clause are satisfied |
| 5191 | ** N==0: No terms of the ORDER BY clause are satisfied |
| 5192 | ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5193 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5194 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 5195 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 5196 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5197 | ** and DISTINCT do not require rows to appear in any particular order as long |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5198 | ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5199 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 5200 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5201 | */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5202 | static i8 wherePathSatisfiesOrderBy( |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5203 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5204 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5205 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5206 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 5207 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5208 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5209 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5210 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5211 | u8 revSet; /* True if rev is known */ |
| 5212 | u8 rev; /* Composite sort order */ |
| 5213 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5214 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 5215 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 5216 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5217 | u16 nKeyCol; /* Number of key columns in pIndex */ |
| 5218 | u16 nColumn; /* Total number of ordered columns in the index */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5219 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 5220 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 5221 | int i, j; /* Loop counters */ |
| 5222 | int iCur; /* Cursor number for current WhereLoop */ |
| 5223 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 5224 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5225 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 5226 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 5227 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 5228 | Index *pIndex; /* The index associated with pLoop */ |
| 5229 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 5230 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 5231 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5232 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5233 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5234 | |
| 5235 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5236 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 5237 | ** 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] | 5238 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 5239 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5240 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 5241 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5242 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5243 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 5244 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 5245 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 5246 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 5247 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 5248 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 5249 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 5250 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 5251 | ** |
| 5252 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 5253 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 5254 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5255 | */ |
| 5256 | |
| 5257 | assert( pOrderBy!=0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5258 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5259 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5260 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5261 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5262 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 5263 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5264 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5265 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5266 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5267 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5268 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5269 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 9dfaf62 | 2014-04-25 14:42:17 +0000 | [diff] [blame] | 5270 | if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 5271 | if( pLoop->u.vtab.isOrdered ) obSat = obDone; |
| 5272 | break; |
| 5273 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5274 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5275 | |
| 5276 | /* Mark off any ORDER BY term X that is a column in the table of |
| 5277 | ** the current loop for which there is term in the WHERE |
| 5278 | ** clause of the form X IS NULL or X=? that reference only outer |
| 5279 | ** loops. |
| 5280 | */ |
| 5281 | for(i=0; i<nOrderBy; i++){ |
| 5282 | if( MASKBIT(i) & obSat ) continue; |
| 5283 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 5284 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 5285 | if( pOBExpr->iTable!=iCur ) continue; |
| 5286 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 5287 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 5288 | if( pTerm==0 ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5289 | if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5290 | const char *z1, *z2; |
| 5291 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5292 | if( !pColl ) pColl = db->pDfltColl; |
| 5293 | z1 = pColl->zName; |
| 5294 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 5295 | if( !pColl ) pColl = db->pDfltColl; |
| 5296 | z2 = pColl->zName; |
| 5297 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 5298 | } |
| 5299 | obSat |= MASKBIT(i); |
| 5300 | } |
| 5301 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5302 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 5303 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 5304 | pIndex = 0; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5305 | nKeyCol = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5306 | nColumn = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5307 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5308 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5309 | }else{ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5310 | nKeyCol = pIndex->nKeyCol; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5311 | nColumn = pIndex->nColumn; |
| 5312 | assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); |
| 5313 | assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable)); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 5314 | isOrderDistinct = IsUniqueIndex(pIndex); |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5315 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5316 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5317 | /* Loop through all columns of the index and deal with the ones |
| 5318 | ** that are not constrained by == or IN. |
| 5319 | */ |
| 5320 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5321 | distinctColumns = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5322 | for(j=0; j<nColumn; j++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5323 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5324 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5325 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5326 | if( j<pLoop->u.btree.nEq |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 5327 | && pLoop->nSkip==0 |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5328 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5329 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5330 | if( i & WO_ISNULL ){ |
| 5331 | testcase( isOrderDistinct ); |
| 5332 | isOrderDistinct = 0; |
| 5333 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5334 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5337 | /* Get the column number in the table (iColumn) and sort order |
| 5338 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5339 | */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5340 | if( pIndex ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5341 | iColumn = pIndex->aiColumn[j]; |
| 5342 | revIdx = pIndex->aSortOrder[j]; |
| 5343 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5344 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5345 | iColumn = -1; |
| 5346 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5347 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5348 | |
| 5349 | /* An unconstrained column that might be NULL means that this |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5350 | ** WhereLoop is not well-ordered |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5351 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5352 | if( isOrderDistinct |
| 5353 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5354 | && j>=pLoop->u.btree.nEq |
| 5355 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5356 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5357 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
| 5360 | /* Find the ORDER BY term that corresponds to the j-th column |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5361 | ** of the index and mark that ORDER BY term off |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5362 | */ |
| 5363 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5364 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5365 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5366 | if( MASKBIT(i) & obSat ) continue; |
| 5367 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5368 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5369 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5370 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5371 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5372 | if( pOBExpr->iTable!=iCur ) continue; |
| 5373 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5374 | if( iColumn>=0 ){ |
| 5375 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5376 | if( !pColl ) pColl = db->pDfltColl; |
| 5377 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5378 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5379 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5380 | break; |
| 5381 | } |
drh | 4929047 | 2014-10-11 02:12:58 +0000 | [diff] [blame] | 5382 | if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | 59b8f2e | 2014-03-22 00:27:14 +0000 | [diff] [blame] | 5383 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5384 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 5385 | if( revSet ){ |
| 5386 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 5387 | }else{ |
| 5388 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5389 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5390 | revSet = 1; |
| 5391 | } |
| 5392 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5393 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5394 | if( iColumn<0 ){ |
| 5395 | testcase( distinctColumns==0 ); |
| 5396 | distinctColumns = 1; |
| 5397 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5398 | obSat |= MASKBIT(i); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5399 | }else{ |
| 5400 | /* No match found */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5401 | if( j==0 || j<nKeyCol ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5402 | testcase( isOrderDistinct!=0 ); |
| 5403 | isOrderDistinct = 0; |
| 5404 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5405 | break; |
| 5406 | } |
| 5407 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5408 | if( distinctColumns ){ |
| 5409 | testcase( isOrderDistinct==0 ); |
| 5410 | isOrderDistinct = 1; |
| 5411 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5412 | } /* end-if not one-row */ |
| 5413 | |
| 5414 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5415 | if( isOrderDistinct ){ |
| 5416 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5417 | for(i=0; i<nOrderBy; i++){ |
| 5418 | Expr *p; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 5419 | Bitmask mTerm; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5420 | if( MASKBIT(i) & obSat ) continue; |
| 5421 | p = pOrderBy->a[i].pExpr; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 5422 | mTerm = exprTableUsage(&pWInfo->sMaskSet,p); |
| 5423 | if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; |
| 5424 | if( (mTerm&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5425 | obSat |= MASKBIT(i); |
| 5426 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5427 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5428 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5429 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 36ed034 | 2014-03-28 12:56:57 +0000 | [diff] [blame] | 5430 | if( obSat==obDone ) return (i8)nOrderBy; |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5431 | if( !isOrderDistinct ){ |
| 5432 | for(i=nOrderBy-1; i>0; i--){ |
| 5433 | Bitmask m = MASKBIT(i) - 1; |
| 5434 | if( (obSat&m)==m ) return i; |
| 5435 | } |
| 5436 | return 0; |
| 5437 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5438 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5441 | |
| 5442 | /* |
| 5443 | ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), |
| 5444 | ** the planner assumes that the specified pOrderBy list is actually a GROUP |
| 5445 | ** BY clause - and so any order that groups rows as required satisfies the |
| 5446 | ** request. |
| 5447 | ** |
| 5448 | ** Normally, in this case it is not possible for the caller to determine |
| 5449 | ** whether or not the rows are really being delivered in sorted order, or |
| 5450 | ** just in some other order that provides the required grouping. However, |
| 5451 | ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then |
| 5452 | ** this function may be called on the returned WhereInfo object. It returns |
| 5453 | ** true if the rows really will be sorted in the specified order, or false |
| 5454 | ** otherwise. |
| 5455 | ** |
| 5456 | ** For example, assuming: |
| 5457 | ** |
| 5458 | ** CREATE INDEX i1 ON t1(x, Y); |
| 5459 | ** |
| 5460 | ** then |
| 5461 | ** |
| 5462 | ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 |
| 5463 | ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 |
| 5464 | */ |
| 5465 | int sqlite3WhereIsSorted(WhereInfo *pWInfo){ |
| 5466 | assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); |
| 5467 | assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); |
| 5468 | return pWInfo->sorted; |
| 5469 | } |
| 5470 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5471 | #ifdef WHERETRACE_ENABLED |
| 5472 | /* For debugging use only: */ |
| 5473 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5474 | static char zName[65]; |
| 5475 | int i; |
| 5476 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5477 | if( pLast ) zName[i++] = pLast->cId; |
| 5478 | zName[i] = 0; |
| 5479 | return zName; |
| 5480 | } |
| 5481 | #endif |
| 5482 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5483 | /* |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5484 | ** Return the cost of sorting nRow rows, assuming that the keys have |
| 5485 | ** nOrderby columns and that the first nSorted columns are already in |
| 5486 | ** order. |
| 5487 | */ |
| 5488 | static LogEst whereSortingCost( |
| 5489 | WhereInfo *pWInfo, |
| 5490 | LogEst nRow, |
| 5491 | int nOrderBy, |
| 5492 | int nSorted |
| 5493 | ){ |
| 5494 | /* TUNING: Estimated cost of a full external sort, where N is |
| 5495 | ** the number of rows to sort is: |
| 5496 | ** |
| 5497 | ** cost = (3.0 * N * log(N)). |
| 5498 | ** |
| 5499 | ** Or, if the order-by clause has X terms but only the last Y |
| 5500 | ** terms are out of order, then block-sorting will reduce the |
| 5501 | ** sorting cost to: |
| 5502 | ** |
| 5503 | ** cost = (3.0 * N * log(N)) * (Y/X) |
| 5504 | ** |
| 5505 | ** The (Y/X) term is implemented using stack variable rScale |
| 5506 | ** below. */ |
| 5507 | LogEst rScale, rSortCost; |
| 5508 | assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); |
| 5509 | rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; |
| 5510 | rSortCost = nRow + estLog(nRow) + rScale + 16; |
| 5511 | |
| 5512 | /* TUNING: The cost of implementing DISTINCT using a B-TREE is |
| 5513 | ** similar but with a larger constant of proportionality. |
| 5514 | ** Multiply by an additional factor of 3.0. */ |
| 5515 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5516 | rSortCost += 16; |
| 5517 | } |
| 5518 | |
| 5519 | return rSortCost; |
| 5520 | } |
| 5521 | |
| 5522 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5523 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5524 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5525 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5526 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5527 | ** Assume that the total number of output rows that will need to be sorted |
| 5528 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5529 | ** costs if nRowEst==0. |
| 5530 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5531 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5532 | ** error occurs. |
| 5533 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5534 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5535 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5536 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5537 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5538 | sqlite3 *db; /* The database connection */ |
| 5539 | int iLoop; /* Loop counter over the terms of the join */ |
| 5540 | int ii, jj; /* Loop counters */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5541 | int mxI = 0; /* Index of next entry to replace */ |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5542 | int nOrderBy; /* Number of ORDER BY clause terms */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5543 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5544 | LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5545 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5546 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5547 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5548 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5549 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5550 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5551 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5552 | LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5553 | char *pSpace; /* Temporary memory used by this routine */ |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 5554 | int nSpace; /* Bytes of space allocated at pSpace */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5555 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5556 | pParse = pWInfo->pParse; |
| 5557 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5558 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5559 | /* TUNING: For simple queries, only the best path is tracked. |
| 5560 | ** For 2-way joins, the 5 best paths are followed. |
| 5561 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | 2504c6c | 2014-06-02 11:26:33 +0000 | [diff] [blame] | 5562 | mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5563 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5564 | WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5565 | |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5566 | /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this |
| 5567 | ** case the purpose of this call is to estimate the number of rows returned |
| 5568 | ** by the overall query. Once this estimate has been obtained, the caller |
| 5569 | ** will invoke this function a second time, passing the estimate as the |
| 5570 | ** nRowEst parameter. */ |
| 5571 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 5572 | nOrderBy = 0; |
| 5573 | }else{ |
| 5574 | nOrderBy = pWInfo->pOrderBy->nExpr; |
| 5575 | } |
| 5576 | |
| 5577 | /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 5578 | nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5579 | nSpace += sizeof(LogEst) * nOrderBy; |
| 5580 | pSpace = sqlite3DbMallocRaw(db, nSpace); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5581 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5582 | aTo = (WherePath*)pSpace; |
| 5583 | aFrom = aTo+mxChoice; |
| 5584 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5585 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5586 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5587 | pFrom->aLoop = pX; |
| 5588 | } |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5589 | if( nOrderBy ){ |
| 5590 | /* If there is an ORDER BY clause and it is not being ignored, set up |
| 5591 | ** space for the aSortCost[] array. Each element of the aSortCost array |
| 5592 | ** is either zero - meaning it has not yet been initialized - or the |
| 5593 | ** cost of sorting nRowEst rows of data where the first X terms of |
| 5594 | ** the ORDER BY clause are already in order, where X is the array |
| 5595 | ** index. */ |
| 5596 | aSortCost = (LogEst*)pX; |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 5597 | memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5598 | } |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 5599 | assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); |
| 5600 | assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5601 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5602 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 5603 | ** |
| 5604 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 5605 | ** of computing an automatic index is not paid back within the first 25 |
| 5606 | ** rows, then do not use the automatic index. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5607 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5608 | nFrom = 1; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5609 | assert( aFrom[0].isOrdered==0 ); |
| 5610 | if( nOrderBy ){ |
| 5611 | /* If nLoop is zero, then there are no FROM terms in the query. Since |
| 5612 | ** in this case the query may return a maximum of one row, the results |
| 5613 | ** are already in the requested order. Set isOrdered to nOrderBy to |
| 5614 | ** indicate this. Or, if nLoop is greater than zero, set isOrdered to |
| 5615 | ** -1, indicating that the result set may or may not be ordered, |
| 5616 | ** depending on the loops added to the current plan. */ |
| 5617 | aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | /* Compute successively longer WherePaths using the previous generation |
| 5621 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5622 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5623 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5624 | nTo = 0; |
| 5625 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5626 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5627 | LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ |
| 5628 | LogEst rCost; /* Cost of path (pFrom+pWLoop) */ |
| 5629 | LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ |
| 5630 | i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 5631 | Bitmask maskNew; /* Mask of src visited by (..) */ |
| 5632 | Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 5633 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5634 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5635 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5636 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5637 | ** Compute its cost */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5638 | rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5639 | rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5640 | nOut = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5641 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5642 | if( isOrdered<0 ){ |
| 5643 | isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5644 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5645 | iLoop, pWLoop, &revMask); |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5646 | }else{ |
| 5647 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5648 | } |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5649 | if( isOrdered>=0 && isOrdered<nOrderBy ){ |
| 5650 | if( aSortCost[isOrdered]==0 ){ |
| 5651 | aSortCost[isOrdered] = whereSortingCost( |
| 5652 | pWInfo, nRowEst, nOrderBy, isOrdered |
| 5653 | ); |
| 5654 | } |
| 5655 | rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]); |
| 5656 | |
| 5657 | WHERETRACE(0x002, |
| 5658 | ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", |
| 5659 | aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, |
| 5660 | rUnsorted, rCost)); |
| 5661 | }else{ |
| 5662 | rCost = rUnsorted; |
| 5663 | } |
| 5664 | |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5665 | /* Check to see if pWLoop should be added to the set of |
| 5666 | ** mxChoice best-so-far paths. |
| 5667 | ** |
| 5668 | ** First look for an existing path among best-so-far paths |
| 5669 | ** that covers the same set of loops and has the same isOrdered |
| 5670 | ** setting as the current path candidate. |
drh | f2a9030 | 2014-08-07 20:37:01 +0000 | [diff] [blame] | 5671 | ** |
| 5672 | ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent |
| 5673 | ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range |
| 5674 | ** of legal values for isOrdered, -1..64. |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5675 | */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5676 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5677 | if( pTo->maskLoop==maskNew |
drh | f2a9030 | 2014-08-07 20:37:01 +0000 | [diff] [blame] | 5678 | && ((pTo->isOrdered^isOrdered)&0x80)==0 |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5679 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5680 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5681 | break; |
| 5682 | } |
| 5683 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5684 | if( jj>=nTo ){ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5685 | /* None of the existing best-so-far paths match the candidate. */ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5686 | if( nTo>=mxChoice |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5687 | && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted)) |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5688 | ){ |
| 5689 | /* The current candidate is no better than any of the mxChoice |
| 5690 | ** paths currently in the best-so-far buffer. So discard |
| 5691 | ** this candidate as not viable. */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5692 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5693 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5694 | sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", |
| 5695 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5696 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5697 | } |
| 5698 | #endif |
| 5699 | continue; |
| 5700 | } |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5701 | /* If we reach this points it means that the new candidate path |
| 5702 | ** needs to be added to the set of best-so-far paths. */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5703 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5704 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5705 | jj = nTo++; |
| 5706 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5707 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5708 | jj = mxI; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5709 | } |
| 5710 | pTo = &aTo[jj]; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5711 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5712 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5713 | sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", |
| 5714 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5715 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5716 | } |
| 5717 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5718 | }else{ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5719 | /* Control reaches here if best-so-far path pTo=aTo[jj] covers the |
| 5720 | ** same set of loops and has the sam isOrdered setting as the |
| 5721 | ** candidate path. Check to see if the candidate should replace |
| 5722 | ** pTo or if the candidate should be skipped */ |
| 5723 | if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5724 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5725 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5726 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5727 | "Skip %s cost=%-3d,%3d order=%c", |
| 5728 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5729 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5730 | sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", |
| 5731 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5732 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5733 | } |
| 5734 | #endif |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5735 | /* Discard the candidate path from further consideration */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5736 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5737 | continue; |
| 5738 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5739 | testcase( pTo->rCost==rCost+1 ); |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 5740 | /* Control reaches here if the candidate path is better than the |
| 5741 | ** pTo path. Replace pTo with the candidate. */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5742 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5743 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5744 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5745 | "Update %s cost=%-3d,%3d order=%c", |
| 5746 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5747 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5748 | sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", |
| 5749 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5750 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5751 | } |
| 5752 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5753 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5754 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5755 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5756 | pTo->revLoop = revMask; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5757 | pTo->nRow = nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5758 | pTo->rCost = rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5759 | pTo->rUnsorted = rUnsorted; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5760 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5761 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5762 | pTo->aLoop[iLoop] = pWLoop; |
| 5763 | if( nTo>=mxChoice ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5764 | mxI = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5765 | mxCost = aTo[0].rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5766 | mxUnsorted = aTo[0].nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5767 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5768 | if( pTo->rCost>mxCost |
| 5769 | || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted) |
| 5770 | ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5771 | mxCost = pTo->rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5772 | mxUnsorted = pTo->rUnsorted; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5773 | mxI = jj; |
| 5774 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5775 | } |
| 5776 | } |
| 5777 | } |
| 5778 | } |
| 5779 | |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5780 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 5781 | if( sqlite3WhereTrace & 0x02 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5782 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5783 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5784 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5785 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5786 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| 5787 | if( pTo->isOrdered>0 ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5788 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5789 | }else{ |
| 5790 | sqlite3DebugPrintf("\n"); |
| 5791 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5792 | } |
| 5793 | } |
| 5794 | #endif |
| 5795 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5796 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5797 | pFrom = aTo; |
| 5798 | aTo = aFrom; |
| 5799 | aFrom = pFrom; |
| 5800 | nFrom = nTo; |
| 5801 | } |
| 5802 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5803 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5804 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5805 | sqlite3DbFree(db, pSpace); |
| 5806 | return SQLITE_ERROR; |
| 5807 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5808 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5809 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5810 | pFrom = aFrom; |
| 5811 | for(ii=1; ii<nFrom; ii++){ |
| 5812 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5813 | } |
| 5814 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5815 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5816 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5817 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5818 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5819 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5820 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5821 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5822 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 5823 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5824 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5825 | && nRowEst |
| 5826 | ){ |
| 5827 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5828 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5829 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5830 | if( rc==pWInfo->pResultSet->nExpr ){ |
| 5831 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5832 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5833 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5834 | if( pWInfo->pOrderBy ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5835 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5836 | if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 5837 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5838 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5839 | }else{ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5840 | pWInfo->nOBSat = pFrom->isOrdered; |
drh | ea6c36e | 2014-03-19 14:30:55 +0000 | [diff] [blame] | 5841 | if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5842 | pWInfo->revMask = pFrom->revLoop; |
| 5843 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5844 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 5845 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr |
| 5846 | ){ |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 5847 | Bitmask revMask = 0; |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5848 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 5849 | pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5850 | ); |
| 5851 | assert( pWInfo->sorted==0 ); |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 5852 | if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| 5853 | pWInfo->sorted = 1; |
| 5854 | pWInfo->revMask = revMask; |
| 5855 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5856 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5857 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5858 | |
| 5859 | |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5860 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5861 | |
| 5862 | /* Free temporary memory and return success */ |
| 5863 | sqlite3DbFree(db, pSpace); |
| 5864 | return SQLITE_OK; |
| 5865 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5866 | |
| 5867 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5868 | ** Most queries use only a single table (they are not joins) and have |
| 5869 | ** simple == constraints against indexed fields. This routine attempts |
| 5870 | ** to plan those simple cases using much less ceremony than the |
| 5871 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5872 | ** times for the common case. |
| 5873 | ** |
| 5874 | ** Return non-zero on success, if this query can be handled by this |
| 5875 | ** no-frills query planner. Return zero if this query needs the |
| 5876 | ** general-purpose query planner. |
| 5877 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5878 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5879 | WhereInfo *pWInfo; |
| 5880 | struct SrcList_item *pItem; |
| 5881 | WhereClause *pWC; |
| 5882 | WhereTerm *pTerm; |
| 5883 | WhereLoop *pLoop; |
| 5884 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5885 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5886 | Table *pTab; |
| 5887 | Index *pIdx; |
| 5888 | |
| 5889 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5890 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5891 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5892 | pItem = pWInfo->pTabList->a; |
| 5893 | pTab = pItem->pTab; |
| 5894 | if( IsVirtual(pTab) ) return 0; |
| 5895 | if( pItem->zIndex ) return 0; |
| 5896 | iCur = pItem->iCursor; |
| 5897 | pWC = &pWInfo->sWC; |
| 5898 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5899 | pLoop->wsFlags = 0; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 5900 | pLoop->nSkip = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5901 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5902 | if( pTerm ){ |
| 5903 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5904 | pLoop->aLTerm[0] = pTerm; |
| 5905 | pLoop->nLTerm = 1; |
| 5906 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5907 | /* TUNING: Cost of a rowid lookup is 10 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5908 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5909 | }else{ |
| 5910 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5911 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 5912 | if( !IsUniqueIndex(pIdx) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5913 | || pIdx->pPartIdxWhere!=0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5914 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5915 | ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5916 | for(j=0; j<pIdx->nKeyCol; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5917 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5918 | if( pTerm==0 ) break; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5919 | pLoop->aLTerm[j] = pTerm; |
| 5920 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5921 | if( j!=pIdx->nKeyCol ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5922 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5923 | if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5924 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5925 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5926 | pLoop->nLTerm = j; |
| 5927 | pLoop->u.btree.nEq = j; |
| 5928 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5929 | /* TUNING: Cost of a unique index lookup is 15 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5930 | pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5931 | break; |
| 5932 | } |
| 5933 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5934 | if( pLoop->wsFlags ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5935 | pLoop->nOut = (LogEst)1; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5936 | pWInfo->a[0].pWLoop = pLoop; |
| 5937 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5938 | pWInfo->a[0].iTabCur = iCur; |
| 5939 | pWInfo->nRowOut = 1; |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5940 | if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5941 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5942 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5943 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5944 | #ifdef SQLITE_DEBUG |
| 5945 | pLoop->cId = '0'; |
| 5946 | #endif |
| 5947 | return 1; |
| 5948 | } |
| 5949 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5953 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 5954 | ** The return value is a pointer to an opaque structure that contains |
| 5955 | ** information needed to terminate the loop. Later, the calling routine |
| 5956 | ** should invoke sqlite3WhereEnd() with the return value of this function |
| 5957 | ** in order to complete the WHERE clause processing. |
| 5958 | ** |
| 5959 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5960 | ** |
| 5961 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5962 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5963 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5964 | ** example, if the SQL is this: |
| 5965 | ** |
| 5966 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5967 | ** |
| 5968 | ** Then the code generated is conceptually like the following: |
| 5969 | ** |
| 5970 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5971 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5972 | ** foreach row3 in t3 do / |
| 5973 | ** ... |
| 5974 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5975 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5976 | ** end / |
| 5977 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5978 | ** Note that the loops might not be nested in the order in which they |
| 5979 | ** 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] | 5980 | ** use of indices. Note also that when the IN operator appears in |
| 5981 | ** the WHERE clause, it might result in additional nested loops for |
| 5982 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5983 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5984 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5985 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5986 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5987 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5988 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5989 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5990 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5991 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5992 | ** data from the various tables of the loop. |
| 5993 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5994 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5995 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5996 | ** the tables have indices and there are terms in the WHERE clause that |
| 5997 | ** refer to those indices, a complete table scan can be avoided and the |
| 5998 | ** code will run much faster. Most of the work of this routine is checking |
| 5999 | ** to see if there are indices that can be used to speed up the loop. |
| 6000 | ** |
| 6001 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 6002 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 6003 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 6004 | ** loops are evaluated and if false a jump is made around all subsequent |
| 6005 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 6006 | ** most loop) |
| 6007 | ** |
| 6008 | ** OUTER JOINS |
| 6009 | ** |
| 6010 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 6011 | ** |
| 6012 | ** foreach row1 in t1 do |
| 6013 | ** flag = 0 |
| 6014 | ** foreach row2 in t2 do |
| 6015 | ** start: |
| 6016 | ** ... |
| 6017 | ** flag = 1 |
| 6018 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 6019 | ** if flag==0 then |
| 6020 | ** move the row2 cursor to a null row |
| 6021 | ** goto start |
| 6022 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6023 | ** end |
| 6024 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 6025 | ** ORDER BY CLAUSE PROCESSING |
| 6026 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 6027 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 6028 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 6029 | ** 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] | 6030 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6031 | ** |
| 6032 | ** The iIdxCur parameter is the cursor number of an index. If |
| 6033 | ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index |
| 6034 | ** to use for OR clause processing. The WHERE clause should use this |
| 6035 | ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 6036 | ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 6037 | ** be used to compute the appropriate cursor depending on which index is |
| 6038 | ** used. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6039 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6040 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 6041 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6042 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 6043 | Expr *pWhere, /* The WHERE clause */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6044 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6045 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 6046 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 6047 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6048 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 6049 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6050 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6051 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 6052 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6053 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6054 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6055 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6056 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6057 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6058 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6059 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 6060 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6061 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6062 | |
| 6063 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6064 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6065 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6066 | |
| 6067 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 6068 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 6069 | if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6070 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6071 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6072 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 6073 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 6074 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 6075 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 6076 | } |
| 6077 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6078 | /* 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] | 6079 | ** bits in a Bitmask |
| 6080 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 6081 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6082 | if( pTabList->nSrc>BMS ){ |
| 6083 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 6084 | return 0; |
| 6085 | } |
| 6086 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6087 | /* This function normally generates a nested loop for all tables in |
| 6088 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 6089 | ** only generate code for the first table in pTabList and assume that |
| 6090 | ** any cursors associated with subsequent tables are uninitialized. |
| 6091 | */ |
| 6092 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 6093 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6094 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 6095 | ** return value. A single allocation is used to store the WhereInfo |
| 6096 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 6097 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 6098 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 6099 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6100 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6101 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6102 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6103 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6104 | sqlite3DbFree(db, pWInfo); |
| 6105 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6106 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6107 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6108 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6109 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6110 | pWInfo->pParse = pParse; |
| 6111 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6112 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6113 | pWInfo->pResultSet = pResultSet; |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 6114 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6115 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6116 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6117 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6118 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6119 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 6120 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 6121 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6122 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 6123 | #ifdef SQLITE_DEBUG |
| 6124 | sWLB.pNew->cId = '*'; |
| 6125 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 6126 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6127 | /* Split the WHERE clause into separate subexpressions where each |
| 6128 | ** subexpression is separated by an AND operator. |
| 6129 | */ |
| 6130 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6131 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 6132 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6133 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 6134 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 6135 | ** expression and either jump over all of the code or fall thru. |
| 6136 | */ |
drh | 759e858 | 2014-01-02 21:05:10 +0000 | [diff] [blame] | 6137 | for(ii=0; ii<sWLB.pWC->nTerm; ii++){ |
| 6138 | if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ |
| 6139 | sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, |
| 6140 | SQLITE_JUMPIFNULL); |
| 6141 | sWLB.pWC->a[ii].wtFlags |= TERM_CODED; |
| 6142 | } |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 6143 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6144 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 6145 | /* Special case: No FROM clause |
| 6146 | */ |
| 6147 | if( nTabList==0 ){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 6148 | if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6149 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 6150 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 6151 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6154 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 6155 | ** |
| 6156 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 6157 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 6158 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 6159 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 6160 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 6161 | ** of the join. Subtracting one from the right table bitmask gives a |
| 6162 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 6163 | ** 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] | 6164 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6165 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 6166 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 6167 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 6168 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6169 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6170 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 6171 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6172 | } |
| 6173 | #ifndef NDEBUG |
| 6174 | { |
| 6175 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6176 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 6177 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6178 | assert( (m-1)==toTheLeft ); |
| 6179 | toTheLeft |= m; |
| 6180 | } |
| 6181 | } |
| 6182 | #endif |
| 6183 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6184 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 6185 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 6186 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 6187 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6188 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6189 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6190 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6191 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 6192 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6193 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6194 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 6195 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 6196 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6197 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 6198 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6199 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6200 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6201 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6202 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6205 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6206 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 6207 | #if defined(WHERETRACE_ENABLED) |
| 6208 | /* Display all terms of the WHERE clause */ |
| 6209 | if( sqlite3WhereTrace & 0x100 ){ |
| 6210 | int i; |
| 6211 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
| 6212 | whereTermPrint(&sWLB.pWC->a[i], i); |
| 6213 | } |
| 6214 | } |
| 6215 | #endif |
| 6216 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 6217 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6218 | rc = whereLoopAddAll(&sWLB); |
| 6219 | if( rc ) goto whereBeginError; |
| 6220 | |
| 6221 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6222 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6223 | if( sqlite3WhereTrace ){ |
| 6224 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6225 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6226 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 6227 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6228 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 6229 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 6230 | whereLoopPrint(p, sWLB.pWC); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6231 | } |
| 6232 | } |
| 6233 | #endif |
| 6234 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6235 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6236 | if( db->mallocFailed ) goto whereBeginError; |
| 6237 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 6238 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6239 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6240 | } |
| 6241 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6242 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 6243 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 6244 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 6245 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 6246 | goto whereBeginError; |
| 6247 | } |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6248 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6249 | if( sqlite3WhereTrace ){ |
| 6250 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6251 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 6252 | if( pWInfo->nOBSat>0 ){ |
| 6253 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 6254 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6255 | switch( pWInfo->eDistinct ){ |
| 6256 | case WHERE_DISTINCT_UNIQUE: { |
| 6257 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 6258 | break; |
| 6259 | } |
| 6260 | case WHERE_DISTINCT_ORDERED: { |
| 6261 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 6262 | break; |
| 6263 | } |
| 6264 | case WHERE_DISTINCT_UNORDERED: { |
| 6265 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 6266 | break; |
| 6267 | } |
| 6268 | } |
| 6269 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6270 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 6271 | whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6272 | } |
| 6273 | } |
| 6274 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6275 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 6276 | if( pWInfo->nLevel>=2 |
| 6277 | && pResultSet!=0 |
| 6278 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 6279 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6280 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
drh | 67a5ec7 | 2013-09-03 14:03:47 +0000 | [diff] [blame] | 6281 | if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6282 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6283 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6284 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6285 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 6286 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 6287 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6288 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6289 | break; |
| 6290 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6291 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6292 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 6293 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 6294 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 6295 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 6296 | ){ |
| 6297 | break; |
| 6298 | } |
| 6299 | } |
| 6300 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6301 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 6302 | pWInfo->nLevel--; |
| 6303 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6304 | } |
| 6305 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6306 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 6307 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6308 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6309 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 6310 | ** to use a one-pass algorithm, determine if this is appropriate. |
drh | 24b7fe9 | 2013-09-30 19:33:06 +0000 | [diff] [blame] | 6311 | ** The one-pass algorithm only works if the WHERE clause constrains |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6312 | ** the statement to update a single row. |
| 6313 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 6314 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6315 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 6316 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6317 | pWInfo->okOnePass = 1; |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 6318 | if( HasRowid(pTabList->a[0].pTab) ){ |
| 6319 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 6320 | } |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6321 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 6322 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6323 | /* Open all tables in the pTabList and any indices selected for |
| 6324 | ** searching those tables. |
| 6325 | */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6326 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6327 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6328 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6329 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6330 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6331 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6332 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6333 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6334 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6335 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 6336 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 6337 | /* Do nothing */ |
| 6338 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6339 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6340 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6341 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 6342 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6343 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 6344 | }else if( IsVirtual(pTab) ){ |
| 6345 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6346 | }else |
| 6347 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6348 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6349 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6350 | int op = OP_OpenRead; |
| 6351 | if( pWInfo->okOnePass ){ |
| 6352 | op = OP_OpenWrite; |
| 6353 | pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 6354 | }; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6355 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6356 | assert( pTabItem->iCursor==pLevel->iTabCur ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6357 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 6358 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 6359 | if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6360 | Bitmask b = pTabItem->colUsed; |
| 6361 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6362 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 6363 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 6364 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6365 | assert( n<=pTab->nCol ); |
| 6366 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6367 | }else{ |
| 6368 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6369 | } |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 6370 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6371 | Index *pIx = pLoop->u.btree.pIndex; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6372 | int iIndexCur; |
| 6373 | int op = OP_OpenRead; |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 6374 | /* iIdxCur is always set if to a positive value if ONEPASS is possible */ |
| 6375 | assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 6376 | if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6377 | && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 |
| 6378 | ){ |
| 6379 | /* This is one term of an OR-optimization using the PRIMARY KEY of a |
| 6380 | ** WITHOUT ROWID table. No need for a separate index */ |
| 6381 | iIndexCur = pLevel->iTabCur; |
| 6382 | op = 0; |
| 6383 | }else if( pWInfo->okOnePass ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6384 | Index *pJ = pTabItem->pTab->pIndex; |
| 6385 | iIndexCur = iIdxCur; |
| 6386 | assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 6387 | while( ALWAYS(pJ) && pJ!=pIx ){ |
| 6388 | iIndexCur++; |
| 6389 | pJ = pJ->pNext; |
| 6390 | } |
| 6391 | op = OP_OpenWrite; |
| 6392 | pWInfo->aiCurOnePass[1] = iIndexCur; |
| 6393 | }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ |
| 6394 | iIndexCur = iIdxCur; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 6395 | if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6396 | }else{ |
| 6397 | iIndexCur = pParse->nTab++; |
| 6398 | } |
| 6399 | pLevel->iIdxCur = iIndexCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6400 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 6401 | assert( iIndexCur>=0 ); |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6402 | if( op ){ |
| 6403 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 6404 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
| 6405 | VdbeComment((v, "%s", pIx->zName)); |
| 6406 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6407 | } |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6408 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6409 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6410 | } |
| 6411 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 6412 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6413 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6414 | /* Generate the code to do the search. Each iteration of the for |
| 6415 | ** loop below generates code for a single nested loop of the VM |
| 6416 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6417 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6418 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6419 | for(ii=0; ii<nTabList; ii++){ |
| 6420 | pLevel = &pWInfo->a[ii]; |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6421 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 6422 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 6423 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 6424 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 6425 | if( db->mallocFailed ) goto whereBeginError; |
| 6426 | } |
| 6427 | #endif |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6428 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6429 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6430 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 6431 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6432 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 6433 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6434 | /* Done. */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6435 | VdbeModuleComment((v, "Begin WHERE-core")); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6436 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6437 | |
| 6438 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6439 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6440 | if( pWInfo ){ |
| 6441 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6442 | whereInfoFree(db, pWInfo); |
| 6443 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6444 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6448 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6449 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6450 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6451 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6452 | Parse *pParse = pWInfo->pParse; |
| 6453 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6454 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6455 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6456 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6457 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6458 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6459 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6460 | /* Generate loop termination code. |
| 6461 | */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6462 | VdbeModuleComment((v, "End WHERE-core")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 6463 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6464 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6465 | int addr; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6466 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6467 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6468 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6469 | if( pLevel->op!=OP_Noop ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6470 | sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6471 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6472 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 6473 | VdbeCoverageIf(v, pLevel->op==OP_Next); |
| 6474 | VdbeCoverageIf(v, pLevel->op==OP_Prev); |
| 6475 | VdbeCoverageIf(v, pLevel->op==OP_VNext); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6476 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6477 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 6478 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6479 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6480 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6481 | 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] | 6482 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 6483 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6484 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 6485 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 6486 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6487 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6488 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6489 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 6490 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6491 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6492 | if( pLevel->addrSkip ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6493 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 6494 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 6495 | sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 6496 | sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6497 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6498 | if( pLevel->iLeftJoin ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6499 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6500 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 6501 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 6502 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 6503 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 6504 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 6505 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6506 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 6507 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 6508 | if( pLevel->op==OP_Return ){ |
| 6509 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 6510 | }else{ |
| 6511 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 6512 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 6513 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6514 | } |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6515 | VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6516 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6517 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6518 | |
| 6519 | /* The "break" point is here, just past the end of the outer loop. |
| 6520 | ** Set it. |
| 6521 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6522 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6523 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6524 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6525 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6526 | int k, last; |
| 6527 | VdbeOp *pOp; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6528 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6529 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6530 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 6531 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6532 | pLoop = pLevel->pWLoop; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6533 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6534 | /* For a co-routine, change all OP_Column references to the table of |
| 6535 | ** the co-routine into OP_SCopy of result contained in a register. |
| 6536 | ** OP_Rowid becomes OP_Null. |
| 6537 | */ |
dan | fbf0f0e | 2014-03-03 14:20:30 +0000 | [diff] [blame] | 6538 | if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6539 | last = sqlite3VdbeCurrentAddr(v); |
| 6540 | k = pLevel->addrBody; |
| 6541 | pOp = sqlite3VdbeGetOp(v, k); |
| 6542 | for(; k<last; k++, pOp++){ |
| 6543 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6544 | if( pOp->opcode==OP_Column ){ |
drh | c438df1 | 2014-04-03 16:29:31 +0000 | [diff] [blame] | 6545 | pOp->opcode = OP_Copy; |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 6546 | pOp->p1 = pOp->p2 + pTabItem->regResult; |
| 6547 | pOp->p2 = pOp->p3; |
| 6548 | pOp->p3 = 0; |
| 6549 | }else if( pOp->opcode==OP_Rowid ){ |
| 6550 | pOp->opcode = OP_Null; |
| 6551 | pOp->p1 = 0; |
| 6552 | pOp->p3 = 0; |
| 6553 | } |
| 6554 | } |
| 6555 | continue; |
| 6556 | } |
| 6557 | |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6558 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 6559 | ** Except, do not close cursors that will be reused by the OR optimization |
| 6560 | ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors |
| 6561 | ** created for the ONEPASS optimization. |
| 6562 | */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6563 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 6564 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6565 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6566 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6567 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6568 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6569 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 6570 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6571 | if( (ws & WHERE_INDEXED)!=0 |
| 6572 | && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 |
| 6573 | && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] |
| 6574 | ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6575 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 6576 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 6579 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 6580 | ** from the index instead of from the table where possible. In some cases |
| 6581 | ** this optimization prevents the table from ever being read, which can |
| 6582 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6583 | ** |
| 6584 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 6585 | ** sqlite3WhereEnd will have created code that references the table |
| 6586 | ** directly. This loop scans all that code looking for opcodes |
| 6587 | ** that reference the table and converts them into opcodes that |
| 6588 | ** reference the index. |
| 6589 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6590 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 6591 | pIdx = pLoop->u.btree.pIndex; |
| 6592 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 6593 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6594 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6595 | if( pIdx && !db->mallocFailed ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6596 | last = sqlite3VdbeCurrentAddr(v); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6597 | k = pLevel->addrBody; |
| 6598 | pOp = sqlite3VdbeGetOp(v, k); |
| 6599 | for(; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6600 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6601 | if( pOp->opcode==OP_Column ){ |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 6602 | int x = pOp->p2; |
drh | 511717c | 2013-11-08 17:13:23 +0000 | [diff] [blame] | 6603 | assert( pIdx->pTable==pTab ); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 6604 | if( !HasRowid(pTab) ){ |
| 6605 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 6606 | x = pPk->aiColumn[x]; |
| 6607 | } |
| 6608 | x = sqlite3ColumnOfIndex(pIdx, x); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6609 | if( x>=0 ){ |
| 6610 | pOp->p2 = x; |
| 6611 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6612 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6613 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6614 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6615 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6616 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6617 | } |
| 6618 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6619 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6620 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6621 | |
| 6622 | /* Final cleanup |
| 6623 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 6624 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6625 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6626 | return; |
| 6627 | } |