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 | |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 22 | /* Forward declaration of methods */ |
| 23 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 24 | |
| 25 | /* Test variable that can be set to enable WHERE tracing */ |
| 26 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 27 | /***/ int sqlite3WhereTrace = 0; |
| 28 | #endif |
| 29 | |
| 30 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 31 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 32 | ** Return the estimated number of output rows from a WHERE clause |
| 33 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 34 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 35 | return sqlite3LogEstToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
| 39 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 40 | ** WHERE clause returns outputs for DISTINCT processing. |
| 41 | */ |
| 42 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 43 | return pWInfo->eDistinct; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 48 | ** Return FALSE if the output needs to be sorted. |
| 49 | */ |
| 50 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 51 | return pWInfo->nOBSat; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | ** Return the VDBE address or label to jump to in order to continue |
| 56 | ** immediately with the next row of a WHERE clause. |
| 57 | */ |
| 58 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 59 | assert( pWInfo->iContinue!=0 ); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 60 | return pWInfo->iContinue; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | ** Return the VDBE address or label to jump to in order to break |
| 65 | ** out of a WHERE loop. |
| 66 | */ |
| 67 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 68 | return pWInfo->iBreak; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 73 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 74 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 75 | ** |
| 76 | ** If the ONEPASS optimization is used (if this routine returns true) |
| 77 | ** then also write the indices of open cursors used by ONEPASS |
| 78 | ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 79 | ** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 80 | ** Either value may be -1, indicating that cursor is not used. |
| 81 | ** Any cursors returned will have been opened for writing. |
| 82 | ** |
| 83 | ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 84 | ** unable to use the ONEPASS optimization. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 85 | */ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 86 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 87 | memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 88 | return pWInfo->okOnePass; |
| 89 | } |
| 90 | |
| 91 | /* |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 92 | ** Move the content of pSrc into pDest |
| 93 | */ |
| 94 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 95 | pDest->n = pSrc->n; |
| 96 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 101 | ** |
| 102 | ** The new entry might overwrite an existing entry, or it might be |
| 103 | ** appended, or it might be discarded. Do whatever is the right thing |
| 104 | ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 105 | */ |
| 106 | static int whereOrInsert( |
| 107 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 108 | Bitmask prereq, /* Prerequisites of the new entry */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 109 | LogEst rRun, /* Run-cost of the new entry */ |
| 110 | LogEst nOut /* Number of outputs for the new entry */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 111 | ){ |
| 112 | u16 i; |
| 113 | WhereOrCost *p; |
| 114 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 115 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 116 | goto whereOrInsert_done; |
| 117 | } |
| 118 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | if( pSet->n<N_OR_COST ){ |
| 123 | p = &pSet->a[pSet->n++]; |
| 124 | p->nOut = nOut; |
| 125 | }else{ |
| 126 | p = pSet->a; |
| 127 | for(i=1; i<pSet->n; i++){ |
| 128 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 129 | } |
| 130 | if( p->rRun<=rRun ) return 0; |
| 131 | } |
| 132 | whereOrInsert_done: |
| 133 | p->prereq = prereq; |
| 134 | p->rRun = rRun; |
| 135 | if( p->nOut>nOut ) p->nOut = nOut; |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 140 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 141 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 142 | static void whereClauseInit( |
| 143 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 144 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 145 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 146 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 147 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 148 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 149 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 150 | pWC->a = pWC->aStatic; |
| 151 | } |
| 152 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 153 | /* Forward reference */ |
| 154 | static void whereClauseClear(WhereClause*); |
| 155 | |
| 156 | /* |
| 157 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 158 | */ |
| 159 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 160 | whereClauseClear(&p->wc); |
| 161 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 166 | */ |
| 167 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 168 | whereClauseClear(&p->wc); |
| 169 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 170 | } |
| 171 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 172 | /* |
| 173 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 174 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 175 | */ |
| 176 | static void whereClauseClear(WhereClause *pWC){ |
| 177 | int i; |
| 178 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 179 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 180 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 181 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 182 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 183 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 184 | if( a->wtFlags & TERM_ORINFO ){ |
| 185 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 186 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 187 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 188 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 189 | } |
| 190 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 191 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 196 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 197 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 198 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 199 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 200 | ** allocation error. The memory allocation failure will be recorded in |
| 201 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 202 | ** |
| 203 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 204 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 205 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 206 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 207 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 208 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 209 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 210 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 211 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 212 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 213 | */ |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 214 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 215 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 216 | int idx; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 217 | testcase( wtFlags & TERM_VIRTUAL ); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 218 | if( pWC->nTerm>=pWC->nSlot ){ |
| 219 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 220 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 221 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 222 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 223 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 224 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 225 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 226 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 227 | return 0; |
| 228 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 229 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 230 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 231 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 232 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 233 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | fe32daa | 2014-12-05 19:50:58 +0000 | [diff] [blame] | 234 | memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm)); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 235 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 236 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 237 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
drh | d05ab6a | 2014-10-25 13:42:16 +0000 | [diff] [blame] | 238 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 239 | }else{ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 240 | pTerm->truthProb = 1; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 241 | } |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 242 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 243 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 244 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 245 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 246 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 247 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 248 | |
| 249 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 250 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 251 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 252 | ** operator specified in the op parameter. The WhereClause structure |
| 253 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 254 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 255 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 256 | ** \________/ \_______________/ \________________/ |
| 257 | ** slot[0] slot[1] slot[2] |
| 258 | ** |
| 259 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 260 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 261 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 262 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 263 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 264 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 265 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 266 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
drh | 0f517ea | 2015-04-21 02:12:13 +0000 | [diff] [blame] | 267 | Expr *pE2 = sqlite3ExprSkipCollate(pExpr); |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 268 | pWC->op = op; |
drh | 0f517ea | 2015-04-21 02:12:13 +0000 | [diff] [blame] | 269 | if( pE2==0 ) return; |
| 270 | if( pE2->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 271 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 272 | }else{ |
drh | 0f517ea | 2015-04-21 02:12:13 +0000 | [diff] [blame] | 273 | whereSplit(pWC, pE2->pLeft, op); |
| 274 | whereSplit(pWC, pE2->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 275 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 279 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 280 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 281 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 282 | |
| 283 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 284 | ** Return the bitmask for the given cursor number. Return 0 if |
| 285 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 286 | */ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 287 | Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 288 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 289 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 290 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 291 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 292 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 293 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 294 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 299 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 300 | ** |
| 301 | ** There is one cursor per table in the FROM clause. The number of |
| 302 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 303 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 304 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 305 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 306 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 307 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 308 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 312 | ** These routines walk (recursively) an expression tree and generate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 313 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 314 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 316 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 317 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 318 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 319 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 320 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 321 | if( p->op==TK_COLUMN ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 322 | mask = sqlite3WhereGetMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 323 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 324 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 325 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 326 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 327 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 328 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 329 | }else{ |
| 330 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 331 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 332 | return mask; |
| 333 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 334 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 335 | int i; |
| 336 | Bitmask mask = 0; |
| 337 | if( pList ){ |
| 338 | for(i=0; i<pList->nExpr; i++){ |
| 339 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 342 | return mask; |
| 343 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 344 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 345 | Bitmask mask = 0; |
| 346 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 347 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 348 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 349 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 350 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 351 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 352 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 353 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 354 | int i; |
| 355 | for(i=0; i<pSrc->nSrc; i++){ |
| 356 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 357 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 358 | } |
| 359 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 360 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 361 | } |
| 362 | return mask; |
| 363 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 364 | |
| 365 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 366 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 367 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 368 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 369 | */ |
| 370 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 371 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 372 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 373 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 374 | assert( TK_GE==TK_EQ+4 ); |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 375 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 379 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 380 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 381 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 382 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 383 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 384 | ** that the collating sequence does not change. For example: |
| 385 | ** "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] | 386 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 387 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 388 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 389 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 390 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 391 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 392 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 393 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 394 | if( expRight==expLeft ){ |
| 395 | /* Either X and Y both have COLLATE operator or neither do */ |
| 396 | if( expRight ){ |
| 397 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 398 | ** used by clearing the EP_Collate flag from Y. */ |
| 399 | pExpr->pRight->flags &= ~EP_Collate; |
| 400 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 401 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 402 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 403 | ** it to be searched first. */ |
| 404 | pExpr->pLeft->flags |= EP_Collate; |
| 405 | } |
| 406 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 407 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 408 | if( pExpr->op>=TK_GT ){ |
| 409 | assert( TK_LT==TK_GT+2 ); |
| 410 | assert( TK_GE==TK_LE+2 ); |
| 411 | assert( TK_GT>TK_EQ ); |
| 412 | assert( TK_GT<TK_LE ); |
| 413 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 414 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 415 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 419 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 420 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 421 | static u16 operatorMask(int op){ |
| 422 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 423 | assert( allowedOp(op) ); |
| 424 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 425 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 426 | }else if( op==TK_ISNULL ){ |
| 427 | c = WO_ISNULL; |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 428 | }else if( op==TK_IS ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 429 | c = WO_IS; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 430 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 431 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 432 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 433 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 434 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 435 | assert( op!=TK_IN || c==WO_IN ); |
| 436 | assert( op!=TK_EQ || c==WO_EQ ); |
| 437 | assert( op!=TK_LT || c==WO_LT ); |
| 438 | assert( op!=TK_LE || c==WO_LE ); |
| 439 | assert( op!=TK_GT || c==WO_GT ); |
| 440 | assert( op!=TK_GE || c==WO_GE ); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 441 | assert( op!=TK_IS || c==WO_IS ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 442 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 446 | ** Advance to the next WhereTerm that matches according to the criteria |
| 447 | ** established when the pScan object was initialized by whereScanInit(). |
| 448 | ** Return NULL if there are no more matching WhereTerms. |
| 449 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 450 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 451 | int iCur; /* The cursor on the LHS of the term */ |
| 452 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 453 | Expr *pX; /* An expression being tested */ |
| 454 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 455 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 456 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 457 | |
| 458 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 459 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 460 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 461 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 462 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | e1a086e | 2013-10-28 20:15:56 +0000 | [diff] [blame] | 463 | if( pTerm->leftCursor==iCur |
| 464 | && pTerm->u.leftColumn==iColumn |
| 465 | && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 466 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 467 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 468 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 469 | ){ |
| 470 | int j; |
| 471 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 472 | assert( pX->op==TK_COLUMN ); |
| 473 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 474 | if( pScan->aEquiv[j]==pX->iTable |
| 475 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | if( j==pScan->nEquiv ){ |
| 480 | pScan->aEquiv[j] = pX->iTable; |
| 481 | pScan->aEquiv[j+1] = pX->iColumn; |
| 482 | pScan->nEquiv += 2; |
| 483 | } |
| 484 | } |
| 485 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 486 | /* Verify the affinity and collating sequence match */ |
| 487 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 488 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 489 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 490 | pX = pTerm->pExpr; |
| 491 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 492 | continue; |
| 493 | } |
| 494 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 495 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 496 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 497 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 498 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 499 | continue; |
| 500 | } |
| 501 | } |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 502 | if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 503 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 504 | && pX->iTable==pScan->aEquiv[0] |
| 505 | && pX->iColumn==pScan->aEquiv[1] |
| 506 | ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 507 | testcase( pTerm->eOperator & WO_IS ); |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 508 | continue; |
| 509 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 510 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 511 | return pTerm; |
| 512 | } |
| 513 | } |
| 514 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 515 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 516 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 517 | } |
| 518 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 519 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 520 | pScan->iEquiv += 2; |
| 521 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 527 | ** first match. Return NULL if there are no matches. |
| 528 | ** |
| 529 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 530 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 531 | ** iCur. The <op> must be one of the operators described by opMask. |
| 532 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 533 | ** If the search is for X and the WHERE clause contains terms of the |
| 534 | ** form X=Y then this routine might also return terms of the form |
| 535 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 536 | ** but is enough to handle most commonly occurring SQL statements. |
| 537 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 538 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 539 | ** index pIdx. |
| 540 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 541 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 542 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 543 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 544 | int iCur, /* Cursor to scan for */ |
| 545 | int iColumn, /* Column to scan for */ |
| 546 | u32 opMask, /* Operator(s) to scan for */ |
| 547 | Index *pIdx /* Must be compatible with this index */ |
| 548 | ){ |
| 549 | int j; |
| 550 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 551 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 552 | pScan->pOrigWC = pWC; |
| 553 | pScan->pWC = pWC; |
| 554 | if( pIdx && iColumn>=0 ){ |
| 555 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 556 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 557 | if( NEVER(j>pIdx->nColumn) ) return 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 558 | } |
| 559 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 560 | }else{ |
| 561 | pScan->idxaff = 0; |
| 562 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 563 | } |
| 564 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 565 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 566 | pScan->aEquiv[0] = iCur; |
| 567 | pScan->aEquiv[1] = iColumn; |
| 568 | pScan->nEquiv = 2; |
| 569 | pScan->iEquiv = 2; |
| 570 | return whereScanNext(pScan); |
| 571 | } |
| 572 | |
| 573 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 574 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 575 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 576 | ** the WO_xx operator codes specified by the op parameter. |
| 577 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 578 | ** |
| 579 | ** The term returned might by Y=<expr> if there is another constraint in |
| 580 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 581 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 582 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 583 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 584 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 585 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 586 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 587 | ** and ... and A9=A10 and A10=<expr>. |
| 588 | ** |
| 589 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 590 | ** then try for the one with no dependencies on <expr> - in other words where |
| 591 | ** <expr> is a constant expression of some kind. Only return entries of |
| 592 | ** 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] | 593 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 594 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 595 | */ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 596 | WhereTerm *sqlite3WhereFindTerm( |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 597 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 598 | int iCur, /* Cursor number of LHS */ |
| 599 | int iColumn, /* Column number of LHS */ |
| 600 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 601 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 602 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 603 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 604 | WhereTerm *pResult = 0; |
| 605 | WhereTerm *p; |
| 606 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 607 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 608 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 609 | op &= WO_EQ|WO_IS; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 610 | while( p ){ |
| 611 | if( (p->prereqRight & notReady)==0 ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 612 | if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ |
| 613 | testcase( p->eOperator & WO_IS ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 614 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 615 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 616 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 617 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 618 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 619 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 620 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 621 | } |
| 622 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 623 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 624 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 625 | |
| 626 | /* |
| 627 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 628 | ** |
| 629 | ** Note that exprAnalyze() might add new virtual terms onto the |
| 630 | ** end of the WHERE clause. We do not want to analyze these new |
| 631 | ** virtual terms, so start analyzing at the end and work forward |
| 632 | ** so that the added virtual terms are never processed. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 633 | */ |
| 634 | static void exprAnalyzeAll( |
| 635 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 636 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 637 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 638 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 639 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 640 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 644 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 645 | /* |
| 646 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 647 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 648 | ** so and false if not. |
| 649 | ** |
| 650 | ** In order for the operator to be optimizible, the RHS must be a string |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 651 | ** literal that does not begin with a wildcard. The LHS must be a column |
| 652 | ** that may only be NULL, a string, or a BLOB, never a number. (This means |
drh | e655a0e | 2015-05-16 18:31:44 +0000 | [diff] [blame] | 653 | ** that virtual tables cannot participate in the LIKE optimization.) The |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 654 | ** collating sequence for the column on the LHS must be appropriate for |
| 655 | ** the operator. |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 656 | */ |
| 657 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 658 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 659 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 660 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 661 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 662 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 663 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 664 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 665 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 666 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 667 | int c; /* One character in z[] */ |
| 668 | int cnt; /* Number of non-wildcard prefix characters */ |
| 669 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 670 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 671 | sqlite3_value *pVal = 0; |
| 672 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 673 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 674 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 675 | return 0; |
| 676 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 677 | #ifdef SQLITE_EBCDIC |
| 678 | if( *pnoCase ) return 0; |
| 679 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 680 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 681 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 682 | if( pLeft->op!=TK_COLUMN |
| 683 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 684 | || IsVirtual(pLeft->pTab) /* Value might be numeric */ |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 685 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 686 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 687 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 688 | return 0; |
| 689 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 690 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 691 | |
drh | 6ade453 | 2014-01-16 15:31:41 +0000 | [diff] [blame] | 692 | pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 693 | op = pRight->op; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 694 | if( op==TK_VARIABLE ){ |
| 695 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 696 | int iCol = pRight->iColumn; |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 697 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 698 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 699 | z = (char *)sqlite3_value_text(pVal); |
| 700 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 701 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 702 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 703 | }else if( op==TK_STRING ){ |
| 704 | z = pRight->u.zToken; |
| 705 | } |
| 706 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 707 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 708 | 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] | 709 | cnt++; |
| 710 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 711 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 712 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 713 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 714 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 715 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 716 | *ppPrefix = pPrefix; |
| 717 | if( op==TK_VARIABLE ){ |
| 718 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 719 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 720 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 721 | /* If the rhs of the LIKE expression is a variable, and the current |
| 722 | ** value of the variable means there is no need to invoke the LIKE |
| 723 | ** function, then no OP_Variable will be added to the program. |
| 724 | ** This causes problems for the sqlite3_bind_parameter_name() |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 725 | ** API. To work around them, add a dummy OP_Variable here. |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 726 | */ |
| 727 | int r1 = sqlite3GetTempReg(pParse); |
| 728 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 729 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 730 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | }else{ |
| 734 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 735 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 736 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 737 | |
| 738 | sqlite3ValueFree(pVal); |
| 739 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 740 | } |
| 741 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 742 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 743 | |
| 744 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 745 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 746 | ** Check to see if the given expression is of the form |
| 747 | ** |
| 748 | ** column MATCH expr |
| 749 | ** |
| 750 | ** If it is then return TRUE. If not, return FALSE. |
| 751 | */ |
| 752 | static int isMatchOfColumn( |
| 753 | Expr *pExpr /* Test this expression */ |
| 754 | ){ |
| 755 | ExprList *pList; |
| 756 | |
| 757 | if( pExpr->op!=TK_FUNCTION ){ |
| 758 | return 0; |
| 759 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 760 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 761 | return 0; |
| 762 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 763 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 764 | if( pList->nExpr!=2 ){ |
| 765 | return 0; |
| 766 | } |
| 767 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 768 | return 0; |
| 769 | } |
| 770 | return 1; |
| 771 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 772 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 773 | |
| 774 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 775 | ** If the pBase expression originated in the ON or USING clause of |
| 776 | ** a join, then transfer the appropriate markings over to derived. |
| 777 | */ |
| 778 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 779 | if( pDerived ){ |
| 780 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 781 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 782 | } |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 783 | } |
| 784 | |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 785 | /* |
| 786 | ** Mark term iChild as being a child of term iParent |
| 787 | */ |
| 788 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 789 | pWC->a[iChild].iParent = iParent; |
| 790 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 791 | pWC->a[iParent].nChild++; |
| 792 | } |
| 793 | |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 794 | /* |
| 795 | ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not |
| 796 | ** a conjunction, then return just pTerm when N==0. If N is exceeds |
| 797 | ** the number of available subterms, return NULL. |
| 798 | */ |
| 799 | static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ |
| 800 | if( pTerm->eOperator!=WO_AND ){ |
| 801 | return N==0 ? pTerm : 0; |
| 802 | } |
| 803 | if( N<pTerm->u.pAndInfo->wc.nTerm ){ |
| 804 | return &pTerm->u.pAndInfo->wc.a[N]; |
| 805 | } |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | ** Subterms pOne and pTwo are contained within WHERE clause pWC. The |
| 811 | ** two subterms are in disjunction - they are OR-ed together. |
| 812 | ** |
| 813 | ** If these two terms are both of the form: "A op B" with the same |
| 814 | ** A and B values but different operators and if the operators are |
| 815 | ** compatible (if one is = and the other is <, for example) then |
drh | c03acf2 | 2015-03-16 13:12:34 +0000 | [diff] [blame] | 816 | ** add a new virtual AND term to pWC that is the combination of the |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 817 | ** two. |
| 818 | ** |
| 819 | ** Some examples: |
| 820 | ** |
| 821 | ** x<y OR x=y --> x<=y |
| 822 | ** x=y OR x=y --> x=y |
| 823 | ** x<=y OR x<y --> x<=y |
| 824 | ** |
| 825 | ** The following is NOT generated: |
| 826 | ** |
| 827 | ** x<y OR x>y --> x!=y |
| 828 | */ |
| 829 | static void whereCombineDisjuncts( |
| 830 | SrcList *pSrc, /* the FROM clause */ |
| 831 | WhereClause *pWC, /* The complete WHERE clause */ |
| 832 | WhereTerm *pOne, /* First disjunct */ |
| 833 | WhereTerm *pTwo /* Second disjunct */ |
| 834 | ){ |
| 835 | u16 eOp = pOne->eOperator | pTwo->eOperator; |
| 836 | sqlite3 *db; /* Database connection (for malloc) */ |
| 837 | Expr *pNew; /* New virtual expression */ |
| 838 | int op; /* Operator for the combined expression */ |
| 839 | int idxNew; /* Index in pWC of the next virtual term */ |
| 840 | |
| 841 | if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 842 | if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 843 | if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp |
| 844 | && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; |
| 845 | assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); |
| 846 | assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); |
| 847 | if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; |
| 848 | if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return; |
| 849 | /* If we reach this point, it means the two subterms can be combined */ |
| 850 | if( (eOp & (eOp-1))!=0 ){ |
| 851 | if( eOp & (WO_LT|WO_LE) ){ |
| 852 | eOp = WO_LE; |
| 853 | }else{ |
| 854 | assert( eOp & (WO_GT|WO_GE) ); |
| 855 | eOp = WO_GE; |
| 856 | } |
| 857 | } |
| 858 | db = pWC->pWInfo->pParse->db; |
| 859 | pNew = sqlite3ExprDup(db, pOne->pExpr, 0); |
| 860 | if( pNew==0 ) return; |
| 861 | for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); } |
| 862 | pNew->op = op; |
| 863 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 864 | exprAnalyze(pSrc, pWC, idxNew); |
| 865 | } |
| 866 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 867 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 868 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 869 | ** Analyze a term that consists of two or more OR-connected |
| 870 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 871 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 872 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 873 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 874 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 875 | ** This routine analyzes terms such as the middle term in the above example. |
| 876 | ** A WhereOrTerm object is computed and attached to the term under |
| 877 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 878 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 879 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 880 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 881 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 882 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 883 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 884 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 885 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 886 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 887 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 888 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 889 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 890 | ** (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 | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 891 | ** (F) x>A OR (x=A AND y>=B) |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 892 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 893 | ** CASE 1: |
| 894 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 895 | ** 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] | 896 | ** a single table T (as shown in example B above) then create a new virtual |
| 897 | ** term that is an equivalent IN expression. In other words, if the term |
| 898 | ** being analyzed is: |
| 899 | ** |
| 900 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 901 | ** |
| 902 | ** then create a new virtual term like this: |
| 903 | ** |
| 904 | ** x IN (expr1,expr2,expr3) |
| 905 | ** |
| 906 | ** CASE 2: |
| 907 | ** |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 908 | ** If there are exactly two disjuncts and one side has x>A and the other side |
drh | c03acf2 | 2015-03-16 13:12:34 +0000 | [diff] [blame] | 909 | ** has x=A (for the same x and A) then add a new virtual conjunct term to the |
| 910 | ** WHERE clause of the form "x>=A". Example: |
| 911 | ** |
| 912 | ** x>A OR (x=A AND y>B) adds: x>=A |
| 913 | ** |
| 914 | ** The added conjunct can sometimes be helpful in query planning. |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 915 | ** |
| 916 | ** CASE 3: |
| 917 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 918 | ** If all subterms are indexable by a single table T, then set |
| 919 | ** |
| 920 | ** WhereTerm.eOperator = WO_OR |
| 921 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 922 | ** |
| 923 | ** A subterm is "indexable" if it is of the form |
| 924 | ** "T.C <op> <expr>" where C is any column of table T and |
| 925 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 926 | ** A subterm is also indexable if it is an AND of two or more |
| 927 | ** subsubterms at least one of which is indexable. Indexable AND |
| 928 | ** subterms have their eOperator set to WO_AND and they have |
| 929 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 930 | ** |
| 931 | ** From another point of view, "indexable" means that the subterm could |
| 932 | ** potentially be used with an index if an appropriate index exists. |
| 933 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 934 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 935 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 936 | ** |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 937 | ** All examples A through E above satisfy case 3. But if a term |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 938 | ** also satisfies case 1 (such as B) we know that the optimizer will |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 939 | ** always prefer case 1, so in that case we pretend that case 3 is not |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 940 | ** satisfied. |
| 941 | ** |
| 942 | ** It might be the case that multiple tables are indexable. For example, |
| 943 | ** (E) above is indexable on tables P, Q, and R. |
| 944 | ** |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 945 | ** Terms that satisfy case 3 are candidates for lookup by using |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 946 | ** separate indices to find rowids for each subterm and composing |
| 947 | ** the union of all rowids using a RowSet object. This is similar |
| 948 | ** to "bitmap indices" in other database engines. |
| 949 | ** |
| 950 | ** OTHERWISE: |
| 951 | ** |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 952 | ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 953 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 954 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 955 | static void exprAnalyzeOrTerm( |
| 956 | SrcList *pSrc, /* the FROM clause */ |
| 957 | WhereClause *pWC, /* the complete WHERE clause */ |
| 958 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 959 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 960 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 961 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 962 | sqlite3 *db = pParse->db; /* Database connection */ |
| 963 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 964 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 965 | int i; /* Loop counters */ |
| 966 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 967 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 968 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 969 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 970 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 971 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 972 | /* |
| 973 | ** Break the OR clause into its separate subterms. The subterms are |
| 974 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 975 | ** object that is attached to the original OR clause term. |
| 976 | */ |
| 977 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 978 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 979 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 980 | if( pOrInfo==0 ) return; |
| 981 | pTerm->wtFlags |= TERM_ORINFO; |
| 982 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 983 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 984 | whereSplit(pOrWc, pExpr, TK_OR); |
| 985 | exprAnalyzeAll(pSrc, pOrWc); |
| 986 | if( db->mallocFailed ) return; |
| 987 | assert( pOrWc->nTerm>=2 ); |
| 988 | |
| 989 | /* |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 990 | ** Compute the set of tables that might satisfy cases 1 or 3. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 991 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 992 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 993 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 994 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 995 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 996 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 997 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 998 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 999 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1000 | if( pAndInfo ){ |
| 1001 | WhereClause *pAndWC; |
| 1002 | WhereTerm *pAndTerm; |
| 1003 | int j; |
| 1004 | Bitmask b = 0; |
| 1005 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1006 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1007 | pOrTerm->eOperator = WO_AND; |
| 1008 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1009 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1010 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1011 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1012 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1013 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1014 | if( !db->mallocFailed ){ |
| 1015 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1016 | assert( pAndTerm->pExpr ); |
| 1017 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1018 | b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1019 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | indexable &= b; |
| 1023 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1024 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1025 | /* Skip this term for now. We revisit it when we process the |
| 1026 | ** corresponding TERM_VIRTUAL term */ |
| 1027 | }else{ |
| 1028 | Bitmask b; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1029 | b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1030 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1031 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1032 | b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1033 | } |
| 1034 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1035 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1036 | chngToIN = 0; |
| 1037 | }else{ |
| 1038 | chngToIN &= b; |
| 1039 | } |
| 1040 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1041 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 1044 | ** Record the set of tables that satisfy case 3. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1045 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1046 | */ |
| 1047 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1048 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1049 | |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 1050 | /* For a two-way OR, attempt to implementation case 2. |
| 1051 | */ |
| 1052 | if( indexable && pOrWc->nTerm==2 ){ |
| 1053 | int iOne = 0; |
| 1054 | WhereTerm *pOne; |
| 1055 | while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ |
| 1056 | int iTwo = 0; |
| 1057 | WhereTerm *pTwo; |
| 1058 | while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ |
| 1059 | whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1064 | /* |
| 1065 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1066 | ** we have to do some additional checking to see if case 1 really |
| 1067 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1068 | ** |
| 1069 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1070 | ** that there is no possibility of transforming the OR clause into an |
| 1071 | ** IN operator because one or more terms in the OR clause contain |
| 1072 | ** something other than == on a column in the single table. The 1-bit |
| 1073 | ** case means that every term of the OR clause is of the form |
| 1074 | ** "table.column=expr" for some single table. The one bit that is set |
| 1075 | ** will correspond to the common table. We still need to check to make |
| 1076 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1077 | ** the all terms are of the form "table1.column=table2.column". It |
| 1078 | ** might be possible to form an IN operator with either table1.column |
| 1079 | ** or table2.column as the LHS if either is common to every term of |
| 1080 | ** the OR clause. |
| 1081 | ** |
| 1082 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1083 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1084 | */ |
| 1085 | if( chngToIN ){ |
| 1086 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1087 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1088 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1089 | int j = 0; /* Loop counter */ |
| 1090 | |
| 1091 | /* Search for a table and column that appears on one side or the |
| 1092 | ** other of the == operator in every subterm. That table and column |
| 1093 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1094 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1095 | ** and column is found but leave okToChngToIN false if not found. |
| 1096 | */ |
| 1097 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1098 | pOrTerm = pOrWc->a; |
| 1099 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1100 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1101 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1102 | if( pOrTerm->leftCursor==iCursor ){ |
| 1103 | /* This is the 2-bit case and we are on the second iteration and |
| 1104 | ** current term is from the first iteration. So skip this term. */ |
| 1105 | assert( j==1 ); |
| 1106 | continue; |
| 1107 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1108 | if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1109 | /* 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] | 1110 | ** chngToIN set but t1 is not. This term will be either preceded |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1111 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1112 | ** and use its inversion. */ |
| 1113 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1114 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1115 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1116 | continue; |
| 1117 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1118 | iColumn = pOrTerm->u.leftColumn; |
| 1119 | iCursor = pOrTerm->leftCursor; |
| 1120 | break; |
| 1121 | } |
| 1122 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1123 | /* No candidate table+column was found. This can only occur |
| 1124 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1125 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1126 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1127 | assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1128 | break; |
| 1129 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1130 | testcase( j==1 ); |
| 1131 | |
| 1132 | /* We have found a candidate table and column. Check to see if that |
| 1133 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1134 | okToChngToIN = 1; |
| 1135 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1136 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1137 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1138 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1139 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1140 | okToChngToIN = 0; |
| 1141 | }else{ |
| 1142 | int affLeft, affRight; |
| 1143 | /* If the right-hand side is also a column, then the affinities |
| 1144 | ** of both right and left sides must be such that no type |
| 1145 | ** conversions are required on the right. (Ticket #2249) |
| 1146 | */ |
| 1147 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1148 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1149 | if( affRight!=0 && affRight!=affLeft ){ |
| 1150 | okToChngToIN = 0; |
| 1151 | }else{ |
| 1152 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1159 | ** case 1. In that case, construct a new virtual term that is |
| 1160 | ** pTerm converted into an IN operator. |
| 1161 | */ |
| 1162 | if( okToChngToIN ){ |
| 1163 | Expr *pDup; /* A transient duplicate expression */ |
| 1164 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1165 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1166 | Expr *pNew; /* The complete IN operator */ |
| 1167 | |
| 1168 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1169 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1170 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1171 | assert( pOrTerm->leftCursor==iCursor ); |
| 1172 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1173 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1174 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1175 | pLeft = pOrTerm->pExpr->pLeft; |
| 1176 | } |
| 1177 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1178 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1179 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1180 | if( pNew ){ |
| 1181 | int idxNew; |
| 1182 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1183 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1184 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1185 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1186 | testcase( idxNew==0 ); |
| 1187 | exprAnalyze(pSrc, pWC, idxNew); |
| 1188 | pTerm = &pWC->a[idxTerm]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1189 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1190 | }else{ |
| 1191 | sqlite3ExprListDelete(db, pList); |
| 1192 | } |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 1193 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1194 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1195 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1196 | } |
| 1197 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1198 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1199 | /* |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1200 | ** We already know that pExpr is a binary operator where both operands are |
| 1201 | ** column references. This routine checks to see if pExpr is an equivalence |
| 1202 | ** relation: |
| 1203 | ** 1. The SQLITE_Transitive optimization must be enabled |
| 1204 | ** 2. Must be either an == or an IS operator |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 1205 | ** 3. Not originating in the ON clause of an OUTER JOIN |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1206 | ** 4. The affinities of A and B must be compatible |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1207 | ** 5a. Both operands use the same collating sequence OR |
| 1208 | ** 5b. The overall collating sequence is BINARY |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1209 | ** If this routine returns TRUE, that means that the RHS can be substituted |
| 1210 | ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. |
| 1211 | ** This is an optimization. No harm comes from returning 0. But if 1 is |
| 1212 | ** returned when it should not be, then incorrect answers might result. |
| 1213 | */ |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1214 | static int termIsEquivalence(Parse *pParse, Expr *pExpr){ |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1215 | char aff1, aff2; |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1216 | CollSeq *pColl; |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1217 | const char *zColl1, *zColl2; |
| 1218 | if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; |
| 1219 | if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; |
| 1220 | if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; |
| 1221 | aff1 = sqlite3ExprAffinity(pExpr->pLeft); |
| 1222 | aff2 = sqlite3ExprAffinity(pExpr->pRight); |
| 1223 | if( aff1!=aff2 |
| 1224 | && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) |
| 1225 | ){ |
| 1226 | return 0; |
| 1227 | } |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1228 | pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); |
| 1229 | if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1; |
| 1230 | pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
drh | c4d56dd | 2015-05-18 12:18:37 +0000 | [diff] [blame] | 1231 | /* Since pLeft and pRight are both a column references, their collating |
| 1232 | ** sequence should always be defined. */ |
| 1233 | zColl1 = ALWAYS(pColl) ? pColl->zName : 0; |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1234 | pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); |
drh | c4d56dd | 2015-05-18 12:18:37 +0000 | [diff] [blame] | 1235 | zColl2 = ALWAYS(pColl) ? pColl->zName : 0; |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1236 | return sqlite3StrICmp(zColl1, zColl2)==0; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1240 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1241 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1242 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1243 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1244 | ** |
| 1245 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1246 | ** to the standard form of "X <op> <expr>". |
| 1247 | ** |
| 1248 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1249 | ** columns, then the original expression is unchanged and a new virtual |
| 1250 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1251 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1252 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1253 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1254 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1255 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1256 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1257 | static void exprAnalyze( |
| 1258 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1259 | WhereClause *pWC, /* the WHERE clause */ |
| 1260 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1261 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1262 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1263 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1264 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1265 | Expr *pExpr; /* The expression to be analyzed */ |
| 1266 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1267 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1268 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1269 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1270 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1271 | int noCase = 0; /* uppercase equivalent to lowercase */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1272 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1273 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1274 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1275 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1276 | if( db->mallocFailed ){ |
| 1277 | return; |
| 1278 | } |
| 1279 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1280 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1281 | pExpr = pTerm->pExpr; |
| 1282 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1283 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1284 | op = pExpr->op; |
| 1285 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1286 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1287 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1288 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1289 | }else{ |
| 1290 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1291 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1292 | }else if( op==TK_ISNULL ){ |
| 1293 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1294 | }else{ |
| 1295 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1296 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1297 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1298 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1299 | Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1300 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1301 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1302 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1303 | } |
| 1304 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1305 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1306 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1307 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1308 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1309 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1310 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1311 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1312 | if( pLeft->op==TK_COLUMN ){ |
| 1313 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1314 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1315 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1316 | } |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 1317 | if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1318 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1319 | WhereTerm *pNew; |
| 1320 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1321 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1322 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1323 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1324 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1325 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1326 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1327 | return; |
| 1328 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1329 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1330 | if( idxNew==0 ) return; |
| 1331 | pNew = &pWC->a[idxNew]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1332 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | ea19cc1 | 2015-05-16 19:17:17 +0000 | [diff] [blame] | 1333 | if( op==TK_IS ) pNew->wtFlags |= TERM_IS; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1334 | pTerm = &pWC->a[idxTerm]; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1335 | pTerm->wtFlags |= TERM_COPIED; |
drh | ea19cc1 | 2015-05-16 19:17:17 +0000 | [diff] [blame] | 1336 | |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1337 | if( termIsEquivalence(pParse, pDup) ){ |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1338 | pTerm->eOperator |= WO_EQUIV; |
| 1339 | eExtraOp = WO_EQUIV; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1340 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1341 | }else{ |
| 1342 | pDup = pExpr; |
| 1343 | pNew = pTerm; |
| 1344 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1345 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1346 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1347 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1348 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1349 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1350 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1351 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1352 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1355 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1356 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1357 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1358 | ** that define the range that the BETWEEN implements. For example: |
| 1359 | ** |
| 1360 | ** a BETWEEN b AND c |
| 1361 | ** |
| 1362 | ** is converted into: |
| 1363 | ** |
| 1364 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1365 | ** |
| 1366 | ** The two new terms are added onto the end of the WhereClause object. |
| 1367 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1368 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1369 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1370 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1371 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1372 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1373 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1374 | int i; |
| 1375 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1376 | assert( pList!=0 ); |
| 1377 | assert( pList->nExpr==2 ); |
| 1378 | for(i=0; i<2; i++){ |
| 1379 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1380 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1381 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1382 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1383 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1384 | transferJoinMarkings(pNewExpr, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1385 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1386 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1387 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1388 | pTerm = &pWC->a[idxTerm]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1389 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1390 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1391 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1392 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1393 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1394 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1395 | /* Analyze a term that is composed of two or more subterms connected by |
| 1396 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1397 | */ |
| 1398 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1399 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1400 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1401 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1402 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1403 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1404 | |
| 1405 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1406 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1407 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1408 | ** |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1409 | ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1410 | ** |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1411 | ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1412 | ** |
| 1413 | ** The last character of the prefix "abc" is incremented to form the |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1414 | ** termination condition "abd". If case is not significant (the default |
| 1415 | ** for LIKE) then the lower-bound is made all uppercase and the upper- |
| 1416 | ** bound is made all lowercase so that the bounds also work when comparing |
| 1417 | ** BLOBs. |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1418 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1419 | if( pWC->op==TK_AND |
| 1420 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1421 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1422 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1423 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1424 | Expr *pNewExpr1; |
| 1425 | Expr *pNewExpr2; |
| 1426 | int idxNew1; |
| 1427 | int idxNew2; |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1428 | const char *zCollSeqName; /* Name of collating sequence */ |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1429 | const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1430 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1431 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1432 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1433 | |
| 1434 | /* Convert the lower bound to upper-case and the upper bound to |
| 1435 | ** lower-case (upper-case is less than lower-case in ASCII) so that |
| 1436 | ** the range constraints also work for BLOBs |
| 1437 | */ |
| 1438 | if( noCase && !pParse->db->mallocFailed ){ |
| 1439 | int i; |
| 1440 | char c; |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1441 | pTerm->wtFlags |= TERM_LIKE; |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1442 | for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ |
| 1443 | pStr1->u.zToken[i] = sqlite3Toupper(c); |
| 1444 | pStr2->u.zToken[i] = sqlite3Tolower(c); |
| 1445 | } |
| 1446 | } |
| 1447 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1448 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1449 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1450 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1451 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1452 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1453 | /* The point is to increment the last character before the first |
| 1454 | ** wildcard. But if we increment '@', that will push it into the |
| 1455 | ** alphabetic range where case conversions will mess up the |
| 1456 | ** inequality. To avoid this, make sure to also run the full |
| 1457 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1458 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1459 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1460 | c = sqlite3UpperToLower[c]; |
| 1461 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1462 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1463 | } |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1464 | zCollSeqName = noCase ? "NOCASE" : "BINARY"; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1465 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1466 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1467 | sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1468 | pStr1, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1469 | transferJoinMarkings(pNewExpr1, pExpr); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1470 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1471 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1472 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1473 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1474 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1475 | sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1476 | pStr2, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1477 | transferJoinMarkings(pNewExpr2, pExpr); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1478 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1479 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1480 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1481 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1482 | if( isComplete ){ |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1483 | markTermAsChild(pWC, idxNew1, idxTerm); |
| 1484 | markTermAsChild(pWC, idxNew2, idxTerm); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1488 | |
| 1489 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1490 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1491 | ** current expression is of the form: column MATCH expr. |
| 1492 | ** This information is used by the xBestIndex methods of |
| 1493 | ** virtual tables. The native query optimizer does not attempt |
| 1494 | ** to do anything with MATCH functions. |
| 1495 | */ |
| 1496 | if( isMatchOfColumn(pExpr) ){ |
| 1497 | int idxNew; |
| 1498 | Expr *pRight, *pLeft; |
| 1499 | WhereTerm *pNewTerm; |
| 1500 | Bitmask prereqColumn, prereqExpr; |
| 1501 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1502 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1503 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1504 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1505 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1506 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1507 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1508 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1509 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1510 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1511 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1512 | pNewTerm = &pWC->a[idxNew]; |
| 1513 | pNewTerm->prereqRight = prereqExpr; |
| 1514 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1515 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1516 | pNewTerm->eOperator = WO_MATCH; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1517 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1518 | pTerm = &pWC->a[idxTerm]; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1519 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1520 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1521 | } |
| 1522 | } |
| 1523 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1524 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1525 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1526 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1527 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1528 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1529 | ** virtual term of that form. |
| 1530 | ** |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 1531 | ** Note that the virtual term must be tagged with TERM_VNULL. |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1532 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1533 | if( pExpr->op==TK_NOTNULL |
| 1534 | && pExpr->pLeft->op==TK_COLUMN |
| 1535 | && pExpr->pLeft->iColumn>=0 |
drh | d7d7147 | 2014-10-22 19:57:16 +0000 | [diff] [blame] | 1536 | && OptimizationEnabled(db, SQLITE_Stat34) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1537 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1538 | Expr *pNewExpr; |
| 1539 | Expr *pLeft = pExpr->pLeft; |
| 1540 | int idxNew; |
| 1541 | WhereTerm *pNewTerm; |
| 1542 | |
| 1543 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1544 | sqlite3ExprDup(db, pLeft, 0), |
| 1545 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1546 | |
| 1547 | idxNew = whereClauseInsert(pWC, pNewExpr, |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 1548 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1549 | if( idxNew ){ |
| 1550 | pNewTerm = &pWC->a[idxNew]; |
| 1551 | pNewTerm->prereqRight = 0; |
| 1552 | pNewTerm->leftCursor = pLeft->iTable; |
| 1553 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1554 | pNewTerm->eOperator = WO_GT; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1555 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1556 | pTerm = &pWC->a[idxTerm]; |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1557 | pTerm->wtFlags |= TERM_COPIED; |
| 1558 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1559 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1560 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1561 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1562 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1563 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1564 | ** an index for tables to the left of the join. |
| 1565 | */ |
| 1566 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1569 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1570 | ** This function searches pList for an entry that matches the iCol-th column |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1571 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1572 | ** |
| 1573 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1574 | ** no expression is found, -1 is returned. |
| 1575 | */ |
| 1576 | static int findIndexCol( |
| 1577 | Parse *pParse, /* Parse context */ |
| 1578 | ExprList *pList, /* Expression list to search */ |
| 1579 | int iBase, /* Cursor for table associated with pIdx */ |
| 1580 | Index *pIdx, /* Index to match column of */ |
| 1581 | int iCol /* Column of index to match */ |
| 1582 | ){ |
| 1583 | int i; |
| 1584 | const char *zColl = pIdx->azColl[iCol]; |
| 1585 | |
| 1586 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1587 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1588 | if( p->op==TK_COLUMN |
| 1589 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1590 | && p->iTable==iBase |
| 1591 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1592 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | 65df68e | 2015-04-15 05:31:02 +0000 | [diff] [blame] | 1593 | if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1594 | return i; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | return -1; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1603 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1604 | ** is redundant. |
| 1605 | ** |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 1606 | ** A DISTINCT list is redundant if any subset of the columns in the |
| 1607 | ** DISTINCT list are collectively unique and individually non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1608 | */ |
| 1609 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1610 | Parse *pParse, /* Parsing context */ |
| 1611 | SrcList *pTabList, /* The FROM clause */ |
| 1612 | WhereClause *pWC, /* The WHERE clause */ |
| 1613 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1614 | ){ |
| 1615 | Table *pTab; |
| 1616 | Index *pIdx; |
| 1617 | int i; |
| 1618 | int iBase; |
| 1619 | |
| 1620 | /* If there is more than one table or sub-select in the FROM clause of |
| 1621 | ** this query, then it will not be possible to show that the DISTINCT |
| 1622 | ** clause is redundant. */ |
| 1623 | if( pTabList->nSrc!=1 ) return 0; |
| 1624 | iBase = pTabList->a[0].iCursor; |
| 1625 | pTab = pTabList->a[0].pTab; |
| 1626 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1627 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1628 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1629 | ** current SELECT is a correlated sub-query. |
| 1630 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1631 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1632 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1633 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /* Loop through all indices on the table, checking each to see if it makes |
| 1637 | ** the DISTINCT qualifier redundant. It does so if: |
| 1638 | ** |
| 1639 | ** 1. The index is itself UNIQUE, and |
| 1640 | ** |
| 1641 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1642 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1643 | ** where X is a constant value. The collation sequences of the |
| 1644 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1645 | ** |
| 1646 | ** 3. All of those index columns for which the WHERE clause does not |
| 1647 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1648 | */ |
| 1649 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 1650 | if( !IsUniqueIndex(pIdx) ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1651 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 1652 | i16 iCol = pIdx->aiColumn[i]; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 1653 | if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1654 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1655 | if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){ |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1656 | break; |
| 1657 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1658 | } |
| 1659 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1660 | if( i==pIdx->nKeyCol ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1661 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1662 | return 1; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1668 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1669 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1670 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1671 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1672 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 1673 | static LogEst estLog(LogEst N){ |
drh | 696964d | 2014-06-12 15:46:46 +0000 | [diff] [blame] | 1674 | return N<=10 ? 0 : sqlite3LogEst(N) - 33; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1677 | /* |
drh | 7b3aa08 | 2015-05-29 13:55:33 +0000 | [diff] [blame] | 1678 | ** Convert OP_Column opcodes to OP_Copy in previously generated code. |
| 1679 | ** |
| 1680 | ** This routine runs over generated VDBE code and translates OP_Column |
| 1681 | ** opcodes into OP_Copy, and OP_Rowid into OP_Null, when the table is being |
| 1682 | ** accessed via co-routine instead of via table lookup. |
| 1683 | */ |
| 1684 | static void translateColumnToCopy( |
| 1685 | Vdbe *v, /* The VDBE containing code to translate */ |
| 1686 | int iStart, /* Translate from this opcode to the end */ |
| 1687 | int iTabCur, /* OP_Column/OP_Rowid references to this table */ |
| 1688 | int iRegister /* The first column is in this register */ |
| 1689 | ){ |
| 1690 | VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart); |
| 1691 | int iEnd = sqlite3VdbeCurrentAddr(v); |
| 1692 | for(; iStart<iEnd; iStart++, pOp++){ |
| 1693 | if( pOp->p1!=iTabCur ) continue; |
| 1694 | if( pOp->opcode==OP_Column ){ |
| 1695 | pOp->opcode = OP_Copy; |
| 1696 | pOp->p1 = pOp->p2 + iRegister; |
| 1697 | pOp->p2 = pOp->p3; |
| 1698 | pOp->p3 = 0; |
| 1699 | }else if( pOp->opcode==OP_Rowid ){ |
| 1700 | pOp->opcode = OP_Null; |
| 1701 | pOp->p1 = 0; |
| 1702 | pOp->p3 = 0; |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | /* |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1708 | ** Two routines for printing the content of an sqlite3_index_info |
| 1709 | ** structure. Used for testing and debugging only. If neither |
| 1710 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1711 | ** are no-ops. |
| 1712 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1713 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1714 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1715 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1716 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1717 | for(i=0; i<p->nConstraint; i++){ |
| 1718 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1719 | i, |
| 1720 | p->aConstraint[i].iColumn, |
| 1721 | p->aConstraint[i].iTermOffset, |
| 1722 | p->aConstraint[i].op, |
| 1723 | p->aConstraint[i].usable); |
| 1724 | } |
| 1725 | for(i=0; i<p->nOrderBy; i++){ |
| 1726 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1727 | i, |
| 1728 | p->aOrderBy[i].iColumn, |
| 1729 | p->aOrderBy[i].desc); |
| 1730 | } |
| 1731 | } |
| 1732 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1733 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1734 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1735 | for(i=0; i<p->nConstraint; i++){ |
| 1736 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1737 | i, |
| 1738 | p->aConstraintUsage[i].argvIndex, |
| 1739 | p->aConstraintUsage[i].omit); |
| 1740 | } |
| 1741 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1742 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1743 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1744 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 1745 | sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1746 | } |
| 1747 | #else |
| 1748 | #define TRACE_IDX_INPUTS(A) |
| 1749 | #define TRACE_IDX_OUTPUTS(A) |
| 1750 | #endif |
| 1751 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1752 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1753 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1754 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1755 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1756 | ** index existed. |
| 1757 | */ |
| 1758 | static int termCanDriveIndex( |
| 1759 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1760 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1761 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1762 | ){ |
| 1763 | char aff; |
| 1764 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 1765 | if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1766 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1767 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1768 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1769 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
drh | e0cc3c2 | 2015-05-13 17:54:08 +0000 | [diff] [blame] | 1770 | testcase( pTerm->pExpr->op==TK_IS ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1771 | return 1; |
| 1772 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1773 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1774 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1775 | |
| 1776 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1777 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1778 | ** Generate code to construct the Index object for an automatic index |
| 1779 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1780 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1781 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1782 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1783 | Parse *pParse, /* The parsing context */ |
| 1784 | WhereClause *pWC, /* The WHERE clause */ |
| 1785 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1786 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1787 | WhereLevel *pLevel /* Write new index here */ |
| 1788 | ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1789 | int nKeyCol; /* Number of columns in the constructed index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1790 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1791 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1792 | Index *pIdx; /* Object describing the transient index */ |
| 1793 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1794 | int addrInit; /* Address of the initialization bypass jump */ |
| 1795 | Table *pTable; /* The table being indexed */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1796 | int addrTop; /* Top of the index fill loop */ |
| 1797 | int regRecord; /* Register holding an index record */ |
| 1798 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1799 | int i; /* Loop counter */ |
| 1800 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1801 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1802 | WhereLoop *pLoop; /* The Loop object */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 1803 | char *zNotUsed; /* Extra space on the end of pIdx */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1804 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 1805 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1806 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1807 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 1808 | int iContinue = 0; /* Jump here to skip excluded rows */ |
drh | 7b3aa08 | 2015-05-29 13:55:33 +0000 | [diff] [blame] | 1809 | struct SrcList_item *pTabItem; /* FROM clause term being indexed */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1810 | |
| 1811 | /* Generate code to skip over the creation and initialization of the |
| 1812 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 1813 | v = pParse->pVdbe; |
| 1814 | assert( v!=0 ); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 1815 | addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1816 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1817 | /* Count the number of columns that will be added to the index |
| 1818 | ** and used to match WHERE clause constraints */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1819 | nKeyCol = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1820 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1821 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1822 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1823 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 1824 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 13cc90c | 2015-02-25 00:24:41 +0000 | [diff] [blame] | 1825 | Expr *pExpr = pTerm->pExpr; |
| 1826 | assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */ |
| 1827 | || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */ |
| 1828 | || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1829 | if( pLoop->prereq==0 |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame] | 1830 | && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
drh | 13cc90c | 2015-02-25 00:24:41 +0000 | [diff] [blame] | 1831 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1832 | && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1833 | pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
drh | 13cc90c | 2015-02-25 00:24:41 +0000 | [diff] [blame] | 1834 | sqlite3ExprDup(pParse->db, pExpr, 0)); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1835 | } |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1836 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 1837 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1838 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1839 | testcase( iCol==BMS ); |
| 1840 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1841 | if( !sentWarning ){ |
| 1842 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 1843 | "automatic index on %s(%s)", pTable->zName, |
| 1844 | pTable->aCol[iCol].zName); |
| 1845 | sentWarning = 1; |
| 1846 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1847 | if( (idxCols & cMask)==0 ){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1848 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 1849 | goto end_auto_index_create; |
| 1850 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1851 | pLoop->aLTerm[nKeyCol++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1852 | idxCols |= cMask; |
| 1853 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1856 | assert( nKeyCol>0 ); |
| 1857 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1858 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 1859 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1860 | |
| 1861 | /* Count the number of additional columns needed to create a |
| 1862 | ** covering index. A "covering index" is an index that contains all |
| 1863 | ** columns that are needed by the query. With a covering index, the |
| 1864 | ** original table never needs to be accessed. Automatic indices must |
| 1865 | ** be a covering index because the index will not be updated if the |
| 1866 | ** original table changes and the index and table cannot both be used |
| 1867 | ** if they go out of sync. |
| 1868 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1869 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | c3ef4fa | 2014-10-28 15:58:50 +0000 | [diff] [blame] | 1870 | mxBitCol = MIN(BMS-1,pTable->nCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1871 | testcase( pTable->nCol==BMS-1 ); |
| 1872 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1873 | for(i=0; i<mxBitCol; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1874 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1875 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1876 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1877 | nKeyCol += pTable->nCol - BMS + 1; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1878 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1879 | |
| 1880 | /* Construct the Index object to describe this index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1881 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1882 | if( pIdx==0 ) goto end_auto_index_create; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1883 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1884 | pIdx->zName = "auto-index"; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1885 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1886 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1887 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1888 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1889 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1890 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1891 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 1892 | testcase( iCol==BMS-1 ); |
| 1893 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1894 | if( (idxCols & cMask)==0 ){ |
| 1895 | Expr *pX = pTerm->pExpr; |
| 1896 | idxCols |= cMask; |
| 1897 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 1898 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 2903183 | 2015-04-15 07:34:25 +0000 | [diff] [blame] | 1899 | pIdx->azColl[n] = pColl ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1900 | n++; |
| 1901 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1904 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1905 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1906 | /* Add additional columns needed to make the automatic index into |
| 1907 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1908 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1909 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1910 | pIdx->aiColumn[n] = i; |
| 1911 | pIdx->azColl[n] = "BINARY"; |
| 1912 | n++; |
| 1913 | } |
| 1914 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1915 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1916 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 1917 | pIdx->aiColumn[n] = i; |
| 1918 | pIdx->azColl[n] = "BINARY"; |
| 1919 | n++; |
| 1920 | } |
| 1921 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1922 | assert( n==nKeyCol ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1923 | pIdx->aiColumn[n] = -1; |
| 1924 | pIdx->azColl[n] = "BINARY"; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1925 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1926 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1927 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 1928 | pLevel->iIdxCur = pParse->nTab++; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1929 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 1930 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1931 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1932 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1933 | /* Fill the automatic index with content */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1934 | sqlite3ExprCachePush(pParse); |
drh | 7b3aa08 | 2015-05-29 13:55:33 +0000 | [diff] [blame] | 1935 | pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom]; |
| 1936 | if( pTabItem->viaCoroutine ){ |
| 1937 | int regYield = pTabItem->regReturn; |
| 1938 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
| 1939 | addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 1940 | VdbeCoverage(v); |
| 1941 | VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
| 1942 | }else{ |
| 1943 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 1944 | } |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1945 | if( pPartial ){ |
| 1946 | iContinue = sqlite3VdbeMakeLabel(v); |
| 1947 | sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame] | 1948 | pLoop->wsFlags |= WHERE_PARTIALIDX; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1949 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1950 | regRecord = sqlite3GetTempReg(pParse); |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 1951 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1952 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 1953 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1954 | if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
drh | 7b3aa08 | 2015-05-29 13:55:33 +0000 | [diff] [blame] | 1955 | if( pTabItem->viaCoroutine ){ |
| 1956 | translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult); |
| 1957 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); |
| 1958 | pTabItem->viaCoroutine = 0; |
| 1959 | }else{ |
| 1960 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 1961 | } |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1962 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1963 | sqlite3VdbeJumpHere(v, addrTop); |
| 1964 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1965 | sqlite3ExprCachePop(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1966 | |
| 1967 | /* Jump here when skipping the initialization */ |
| 1968 | sqlite3VdbeJumpHere(v, addrInit); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1969 | |
| 1970 | end_auto_index_create: |
| 1971 | sqlite3ExprDelete(pParse->db, pPartial); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1972 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1973 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1974 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1975 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1976 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1977 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 1978 | ** responsibility of the caller to eventually release the structure |
| 1979 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 1980 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 1981 | static sqlite3_index_info *allocateIndexInfo( |
| 1982 | Parse *pParse, |
| 1983 | WhereClause *pWC, |
| 1984 | struct SrcList_item *pSrc, |
| 1985 | ExprList *pOrderBy |
| 1986 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1987 | int i, j; |
| 1988 | int nTerm; |
| 1989 | struct sqlite3_index_constraint *pIdxCons; |
| 1990 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 1991 | struct sqlite3_index_constraint_usage *pUsage; |
| 1992 | WhereTerm *pTerm; |
| 1993 | int nOrderBy; |
| 1994 | sqlite3_index_info *pIdxInfo; |
| 1995 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1996 | /* Count the number of possible WHERE clause constraints referring |
| 1997 | ** to this virtual table */ |
| 1998 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 1999 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2000 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2001 | testcase( pTerm->eOperator & WO_IN ); |
| 2002 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 2003 | testcase( pTerm->eOperator & WO_IS ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 2004 | testcase( pTerm->eOperator & WO_ALL ); |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 2005 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2006 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2007 | nTerm++; |
| 2008 | } |
| 2009 | |
| 2010 | /* If the ORDER BY clause contains only columns in the current |
| 2011 | ** virtual table then allocate space for the aOrderBy part of |
| 2012 | ** the sqlite3_index_info structure. |
| 2013 | */ |
| 2014 | nOrderBy = 0; |
| 2015 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2016 | int n = pOrderBy->nExpr; |
| 2017 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2018 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2019 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2020 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2021 | if( i==n){ |
| 2022 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | /* Allocate the sqlite3_index_info structure |
| 2027 | */ |
| 2028 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2029 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2030 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2031 | if( pIdxInfo==0 ){ |
| 2032 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2033 | return 0; |
| 2034 | } |
| 2035 | |
| 2036 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2037 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2038 | ** changing them. We have to do some funky casting in order to |
| 2039 | ** initialize those fields. |
| 2040 | */ |
| 2041 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2042 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2043 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2044 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2045 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2046 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2047 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2048 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2049 | pUsage; |
| 2050 | |
| 2051 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2052 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2053 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2054 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2055 | testcase( pTerm->eOperator & WO_IN ); |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 2056 | testcase( pTerm->eOperator & WO_IS ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2057 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 2058 | testcase( pTerm->eOperator & WO_ALL ); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 2059 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2060 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2061 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2062 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2063 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2064 | if( op==WO_IN ) op = WO_EQ; |
| 2065 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2066 | /* The direct assignment in the previous line is possible only because |
| 2067 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2068 | ** following asserts verify this fact. */ |
| 2069 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2070 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2071 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2072 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2073 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2074 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2075 | 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] | 2076 | j++; |
| 2077 | } |
| 2078 | for(i=0; i<nOrderBy; i++){ |
| 2079 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2080 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2081 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2082 | } |
| 2083 | |
| 2084 | return pIdxInfo; |
| 2085 | } |
| 2086 | |
| 2087 | /* |
| 2088 | ** The table object reference passed as the second argument to this function |
| 2089 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2090 | ** method of the virtual table with the sqlite3_index_info object that |
| 2091 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2092 | ** |
| 2093 | ** If an error occurs, pParse is populated with an error message and a |
| 2094 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2095 | ** part of the sqlite3_index_info structure is left populated. |
| 2096 | ** |
| 2097 | ** Whether or not an error is returned, it is the responsibility of the |
| 2098 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2099 | ** that this is required. |
| 2100 | */ |
| 2101 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2102 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2103 | int i; |
| 2104 | int rc; |
| 2105 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2106 | TRACE_IDX_INPUTS(p); |
| 2107 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2108 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2109 | |
| 2110 | if( rc!=SQLITE_OK ){ |
| 2111 | if( rc==SQLITE_NOMEM ){ |
| 2112 | pParse->db->mallocFailed = 1; |
| 2113 | }else if( !pVtab->zErrMsg ){ |
| 2114 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2115 | }else{ |
| 2116 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2117 | } |
| 2118 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2119 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2120 | pVtab->zErrMsg = 0; |
| 2121 | |
| 2122 | for(i=0; i<p->nConstraint; i++){ |
| 2123 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2124 | sqlite3ErrorMsg(pParse, |
| 2125 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | return pParse->nErr; |
| 2130 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2131 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2132 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2133 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2134 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2135 | ** Estimate the location of a particular key among all keys in an |
| 2136 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2137 | ** |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2138 | ** aStat[0] Est. number of rows less than pRec |
| 2139 | ** aStat[1] Est. number of rows equal to pRec |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2140 | ** |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2141 | ** Return the index of the sample that is the smallest sample that |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2142 | ** is greater than or equal to pRec. Note that this index is not an index |
| 2143 | ** into the aSample[] array - it is an index into a virtual set of samples |
| 2144 | ** based on the contents of aSample[] and the number of fields in record |
| 2145 | ** pRec. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2146 | */ |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2147 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2148 | Parse *pParse, /* Database connection */ |
| 2149 | Index *pIdx, /* Index to consider domain of */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2150 | UnpackedRecord *pRec, /* Vector of values to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2151 | int roundUp, /* Round up if true. Round down if false */ |
| 2152 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2153 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 2154 | IndexSample *aSample = pIdx->aSample; |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 2155 | int iCol; /* Index of required stats in anEq[] etc. */ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2156 | int i; /* Index of first sample >= pRec */ |
| 2157 | int iSample; /* Smallest sample larger than or equal to pRec */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2158 | int iMin = 0; /* Smallest sample not yet tested */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2159 | int iTest; /* Next sample to test */ |
| 2160 | int res; /* Result of comparison operation */ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2161 | int nField; /* Number of fields in pRec */ |
| 2162 | tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2163 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2164 | #ifndef SQLITE_DEBUG |
| 2165 | UNUSED_PARAMETER( pParse ); |
| 2166 | #endif |
drh | 7f59475 | 2013-12-03 19:49:55 +0000 | [diff] [blame] | 2167 | assert( pRec!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2168 | assert( pIdx->nSample>0 ); |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2169 | assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); |
| 2170 | |
| 2171 | /* Do a binary search to find the first sample greater than or equal |
| 2172 | ** to pRec. If pRec contains a single field, the set of samples to search |
| 2173 | ** is simply the aSample[] array. If the samples in aSample[] contain more |
| 2174 | ** than one fields, all fields following the first are ignored. |
| 2175 | ** |
| 2176 | ** If pRec contains N fields, where N is more than one, then as well as the |
| 2177 | ** samples in aSample[] (truncated to N fields), the search also has to |
| 2178 | ** consider prefixes of those samples. For example, if the set of samples |
| 2179 | ** in aSample is: |
| 2180 | ** |
| 2181 | ** aSample[0] = (a, 5) |
| 2182 | ** aSample[1] = (a, 10) |
| 2183 | ** aSample[2] = (b, 5) |
| 2184 | ** aSample[3] = (c, 100) |
| 2185 | ** aSample[4] = (c, 105) |
| 2186 | ** |
| 2187 | ** Then the search space should ideally be the samples above and the |
| 2188 | ** unique prefixes [a], [b] and [c]. But since that is hard to organize, |
| 2189 | ** the code actually searches this set: |
| 2190 | ** |
| 2191 | ** 0: (a) |
| 2192 | ** 1: (a, 5) |
| 2193 | ** 2: (a, 10) |
| 2194 | ** 3: (a, 10) |
| 2195 | ** 4: (b) |
| 2196 | ** 5: (b, 5) |
| 2197 | ** 6: (c) |
| 2198 | ** 7: (c, 100) |
| 2199 | ** 8: (c, 105) |
| 2200 | ** 9: (c, 105) |
| 2201 | ** |
| 2202 | ** For each sample in the aSample[] array, N samples are present in the |
| 2203 | ** effective sample array. In the above, samples 0 and 1 are based on |
| 2204 | ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. |
| 2205 | ** |
| 2206 | ** Often, sample i of each block of N effective samples has (i+1) fields. |
| 2207 | ** Except, each sample may be extended to ensure that it is greater than or |
| 2208 | ** equal to the previous sample in the array. For example, in the above, |
| 2209 | ** sample 2 is the first sample of a block of N samples, so at first it |
| 2210 | ** appears that it should be 1 field in size. However, that would make it |
| 2211 | ** smaller than sample 1, so the binary search would not work. As a result, |
| 2212 | ** it is extended to two fields. The duplicates that this creates do not |
| 2213 | ** cause any problems. |
| 2214 | */ |
| 2215 | nField = pRec->nField; |
| 2216 | iCol = 0; |
| 2217 | iSample = pIdx->nSample * nField; |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2218 | do{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2219 | int iSamp; /* Index in aSample[] of test sample */ |
| 2220 | int n; /* Number of fields in test sample */ |
| 2221 | |
| 2222 | iTest = (iMin+iSample)/2; |
| 2223 | iSamp = iTest / nField; |
| 2224 | if( iSamp>0 ){ |
| 2225 | /* The proposed effective sample is a prefix of sample aSample[iSamp]. |
| 2226 | ** Specifically, the shortest prefix of at least (1 + iTest%nField) |
| 2227 | ** fields that is greater than the previous effective sample. */ |
| 2228 | for(n=(iTest % nField) + 1; n<nField; n++){ |
| 2229 | if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break; |
| 2230 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2231 | }else{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2232 | n = iTest + 1; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2233 | } |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2234 | |
| 2235 | pRec->nField = n; |
| 2236 | res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec); |
| 2237 | if( res<0 ){ |
| 2238 | iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1]; |
| 2239 | iMin = iTest+1; |
| 2240 | }else if( res==0 && n<nField ){ |
| 2241 | iLower = aSample[iSamp].anLt[n-1]; |
| 2242 | iMin = iTest+1; |
| 2243 | res = -1; |
| 2244 | }else{ |
| 2245 | iSample = iTest; |
| 2246 | iCol = n-1; |
| 2247 | } |
| 2248 | }while( res && iMin<iSample ); |
| 2249 | i = iSample / nField; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2250 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2251 | #ifdef SQLITE_DEBUG |
| 2252 | /* The following assert statements check that the binary search code |
| 2253 | ** above found the right answer. This block serves no purpose other |
| 2254 | ** than to invoke the asserts. */ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2255 | if( pParse->db->mallocFailed==0 ){ |
| 2256 | if( res==0 ){ |
| 2257 | /* If (res==0) is true, then pRec must be equal to sample i. */ |
| 2258 | assert( i<pIdx->nSample ); |
| 2259 | assert( iCol==nField-1 ); |
| 2260 | pRec->nField = nField; |
| 2261 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 2262 | || pParse->db->mallocFailed |
| 2263 | ); |
| 2264 | }else{ |
| 2265 | /* Unless i==pIdx->nSample, indicating that pRec is larger than |
| 2266 | ** all samples in the aSample[] array, pRec must be smaller than the |
| 2267 | ** (iCol+1) field prefix of sample i. */ |
| 2268 | assert( i<=pIdx->nSample && i>=0 ); |
| 2269 | pRec->nField = iCol+1; |
| 2270 | assert( i==pIdx->nSample |
| 2271 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 2272 | || pParse->db->mallocFailed ); |
| 2273 | |
| 2274 | /* if i==0 and iCol==0, then record pRec is smaller than all samples |
| 2275 | ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must |
| 2276 | ** be greater than or equal to the (iCol) field prefix of sample i. |
| 2277 | ** If (i>0), then pRec must also be greater than sample (i-1). */ |
| 2278 | if( iCol>0 ){ |
| 2279 | pRec->nField = iCol; |
| 2280 | assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 |
| 2281 | || pParse->db->mallocFailed ); |
| 2282 | } |
| 2283 | if( i>0 ){ |
| 2284 | pRec->nField = nField; |
| 2285 | assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 2286 | || pParse->db->mallocFailed ); |
| 2287 | } |
| 2288 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2289 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2290 | #endif /* ifdef SQLITE_DEBUG */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2291 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2292 | if( res==0 ){ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2293 | /* Record pRec is equal to sample i */ |
| 2294 | assert( iCol==nField-1 ); |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2295 | aStat[0] = aSample[i].anLt[iCol]; |
| 2296 | aStat[1] = aSample[i].anEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2297 | }else{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2298 | /* At this point, the (iCol+1) field prefix of aSample[i] is the first |
| 2299 | ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec |
| 2300 | ** is larger than all samples in the array. */ |
| 2301 | tRowcnt iUpper, iGap; |
| 2302 | if( i>=pIdx->nSample ){ |
| 2303 | iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2304 | }else{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2305 | iUpper = aSample[i].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2306 | } |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2307 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2308 | if( iLower>=iUpper ){ |
| 2309 | iGap = 0; |
| 2310 | }else{ |
| 2311 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2312 | } |
| 2313 | if( roundUp ){ |
| 2314 | iGap = (iGap*2)/3; |
| 2315 | }else{ |
| 2316 | iGap = iGap/3; |
| 2317 | } |
| 2318 | aStat[0] = iLower + iGap; |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2319 | aStat[1] = pIdx->aAvgEq[iCol]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2320 | } |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2321 | |
| 2322 | /* Restore the pRec->nField value before returning. */ |
| 2323 | pRec->nField = nField; |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2324 | return i; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2325 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2326 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2327 | |
| 2328 | /* |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2329 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| 2330 | ** bound on a range scan. Without considering pTerm, it is estimated |
| 2331 | ** that the scan will visit nNew rows. This function returns the number |
| 2332 | ** estimated to be visited after taking pTerm into account. |
| 2333 | ** |
| 2334 | ** If the user explicitly specified a likelihood() value for this term, |
| 2335 | ** then the return value is the likelihood multiplied by the number of |
| 2336 | ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term |
| 2337 | ** has a likelihood of 0.50, and any other term a likelihood of 0.25. |
| 2338 | */ |
| 2339 | static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ |
| 2340 | LogEst nRet = nNew; |
| 2341 | if( pTerm ){ |
| 2342 | if( pTerm->truthProb<=0 ){ |
| 2343 | nRet += pTerm->truthProb; |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2344 | }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2345 | nRet -= 20; assert( 20==sqlite3LogEst(4) ); |
| 2346 | } |
| 2347 | } |
| 2348 | return nRet; |
| 2349 | } |
| 2350 | |
mistachkin | 2d84ac4 | 2014-06-26 21:32:09 +0000 | [diff] [blame] | 2351 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2352 | /* |
| 2353 | ** This function is called to estimate the number of rows visited by a |
| 2354 | ** range-scan on a skip-scan index. For example: |
| 2355 | ** |
| 2356 | ** CREATE INDEX i1 ON t1(a, b, c); |
| 2357 | ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; |
| 2358 | ** |
| 2359 | ** Value pLoop->nOut is currently set to the estimated number of rows |
| 2360 | ** visited for scanning (a=? AND b=?). This function reduces that estimate |
| 2361 | ** by some factor to account for the (c BETWEEN ? AND ?) expression based |
| 2362 | ** on the stat4 data for the index. this scan will be peformed multiple |
| 2363 | ** times (once for each (a,b) combination that matches a=?) is dealt with |
| 2364 | ** by the caller. |
| 2365 | ** |
| 2366 | ** It does this by scanning through all stat4 samples, comparing values |
| 2367 | ** extracted from pLower and pUpper with the corresponding column in each |
| 2368 | ** sample. If L and U are the number of samples found to be less than or |
| 2369 | ** equal to the values extracted from pLower and pUpper respectively, and |
| 2370 | ** N is the total number of samples, the pLoop->nOut value is adjusted |
| 2371 | ** as follows: |
| 2372 | ** |
| 2373 | ** nOut = nOut * ( min(U - L, 1) / N ) |
| 2374 | ** |
| 2375 | ** If pLower is NULL, or a value cannot be extracted from the term, L is |
| 2376 | ** set to zero. If pUpper is NULL, or a value cannot be extracted from it, |
| 2377 | ** U is set to N. |
| 2378 | ** |
| 2379 | ** Normally, this function sets *pbDone to 1 before returning. However, |
| 2380 | ** if no value can be extracted from either pLower or pUpper (and so the |
| 2381 | ** estimate of the number of rows delivered remains unchanged), *pbDone |
| 2382 | ** is left as is. |
| 2383 | ** |
| 2384 | ** If an error occurs, an SQLite error code is returned. Otherwise, |
| 2385 | ** SQLITE_OK. |
| 2386 | */ |
| 2387 | static int whereRangeSkipScanEst( |
| 2388 | Parse *pParse, /* Parsing & code generating context */ |
| 2389 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2390 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 2391 | WhereLoop *pLoop, /* Update the .nOut value of this loop */ |
| 2392 | int *pbDone /* Set to true if at least one expr. value extracted */ |
| 2393 | ){ |
| 2394 | Index *p = pLoop->u.btree.pIndex; |
| 2395 | int nEq = pLoop->u.btree.nEq; |
| 2396 | sqlite3 *db = pParse->db; |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2397 | int nLower = -1; |
| 2398 | int nUpper = p->nSample+1; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2399 | int rc = SQLITE_OK; |
drh | d15f87e | 2014-07-24 22:41:20 +0000 | [diff] [blame] | 2400 | int iCol = p->aiColumn[nEq]; |
| 2401 | u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2402 | CollSeq *pColl; |
| 2403 | |
| 2404 | sqlite3_value *p1 = 0; /* Value extracted from pLower */ |
| 2405 | sqlite3_value *p2 = 0; /* Value extracted from pUpper */ |
| 2406 | sqlite3_value *pVal = 0; /* Value extracted from record */ |
| 2407 | |
| 2408 | pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]); |
| 2409 | if( pLower ){ |
| 2410 | rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2411 | nLower = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2412 | } |
| 2413 | if( pUpper && rc==SQLITE_OK ){ |
| 2414 | rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2415 | nUpper = p2 ? 0 : p->nSample; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | if( p1 || p2 ){ |
| 2419 | int i; |
| 2420 | int nDiff; |
| 2421 | for(i=0; rc==SQLITE_OK && i<p->nSample; i++){ |
| 2422 | rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal); |
| 2423 | if( rc==SQLITE_OK && p1 ){ |
| 2424 | int res = sqlite3MemCompare(p1, pVal, pColl); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2425 | if( res>=0 ) nLower++; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2426 | } |
| 2427 | if( rc==SQLITE_OK && p2 ){ |
| 2428 | int res = sqlite3MemCompare(p2, pVal, pColl); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2429 | if( res>=0 ) nUpper++; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2430 | } |
| 2431 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2432 | nDiff = (nUpper - nLower); |
| 2433 | if( nDiff<=0 ) nDiff = 1; |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2434 | |
| 2435 | /* If there is both an upper and lower bound specified, and the |
| 2436 | ** comparisons indicate that they are close together, use the fallback |
| 2437 | ** method (assume that the scan visits 1/64 of the rows) for estimating |
| 2438 | ** the number of rows visited. Otherwise, estimate the number of rows |
| 2439 | ** using the method described in the header comment for this function. */ |
| 2440 | if( nDiff!=1 || pUpper==0 || pLower==0 ){ |
| 2441 | int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); |
| 2442 | pLoop->nOut -= nAdjust; |
| 2443 | *pbDone = 1; |
| 2444 | WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", |
dan | fa88745 | 2014-06-28 15:26:10 +0000 | [diff] [blame] | 2445 | nLower, nUpper, nAdjust*-1, pLoop->nOut)); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2448 | }else{ |
| 2449 | assert( *pbDone==0 ); |
| 2450 | } |
| 2451 | |
| 2452 | sqlite3ValueFree(p1); |
| 2453 | sqlite3ValueFree(p2); |
| 2454 | sqlite3ValueFree(pVal); |
| 2455 | |
| 2456 | return rc; |
| 2457 | } |
mistachkin | 2d84ac4 | 2014-06-26 21:32:09 +0000 | [diff] [blame] | 2458 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2459 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2460 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2461 | ** This function is used to estimate the number of rows that will be visited |
| 2462 | ** by scanning an index for a range of values. The range may have an upper |
| 2463 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2464 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2465 | ** example, assuming that index p is on t1(a): |
| 2466 | ** |
| 2467 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2468 | ** |_____| |_____| |
| 2469 | ** | | |
| 2470 | ** pLower pUpper |
| 2471 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2472 | ** 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] | 2473 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2474 | ** |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2475 | ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2476 | ** column subject to the range constraint. Or, equivalently, the number of |
| 2477 | ** equality constraints optimized by the proposed index scan. For example, |
| 2478 | ** assuming index p is on t1(a, b), and the SQL query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2479 | ** |
| 2480 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2481 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2482 | ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 2483 | ** left-most column of the index). Or, if the query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2484 | ** |
| 2485 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2486 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2487 | ** then nEq is set to 0. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2488 | ** |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2489 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2490 | ** number of rows that the index scan is expected to visit without |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2491 | ** considering the range constraints. If nEq is 0, then *pnOut is the number of |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2492 | ** 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] | 2493 | ** to account for the range constraints pLower and pUpper. |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2494 | ** |
| 2495 | ** 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] | 2496 | ** used, a single range inequality reduces the search space by a factor of 4. |
| 2497 | ** and a pair of constraints (x>? AND x<?) reduces the expected number of |
| 2498 | ** rows visited by a factor of 64. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2499 | */ |
| 2500 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2501 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2502 | WhereLoopBuilder *pBuilder, |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2503 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2504 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2505 | WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2506 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2507 | int rc = SQLITE_OK; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2508 | int nOut = pLoop->nOut; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2509 | LogEst nNew; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2510 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2511 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2512 | Index *p = pLoop->u.btree.pIndex; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2513 | int nEq = pLoop->u.btree.nEq; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2514 | |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2515 | if( p->nSample>0 && nEq<p->nSampleCol ){ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2516 | if( nEq==pBuilder->nRecValid ){ |
| 2517 | UnpackedRecord *pRec = pBuilder->pRec; |
| 2518 | tRowcnt a[2]; |
| 2519 | u8 aff; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2520 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2521 | /* Variable iLower will be set to the estimate of the number of rows in |
| 2522 | ** the index that are less than the lower bound of the range query. The |
| 2523 | ** lower bound being the concatenation of $P and $L, where $P is the |
| 2524 | ** key-prefix formed by the nEq values matched against the nEq left-most |
| 2525 | ** columns of the index, and $L is the value in pLower. |
| 2526 | ** |
| 2527 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 2528 | ** is not a simple variable or literal value), the lower bound of the |
| 2529 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 2530 | ** if $L is available, whereKeyStats() is called for both ($P) and |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2531 | ** ($P:$L) and the larger of the two returned values is used. |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2532 | ** |
| 2533 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 2534 | ** less than the upper bound of the range query. Where the upper bound |
| 2535 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 2536 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2537 | ** |
| 2538 | ** The number of rows between the two bounds is then just iUpper-iLower. |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2539 | */ |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2540 | tRowcnt iLower; /* Rows less than the lower bound */ |
| 2541 | tRowcnt iUpper; /* Rows less than the upper bound */ |
| 2542 | int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 2543 | int iUprIdx = -1; /* aSample[] for the upper bound */ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2544 | |
drh | b34fc5b | 2014-08-28 17:20:37 +0000 | [diff] [blame] | 2545 | if( pRec ){ |
| 2546 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 2547 | pRec->nField = pBuilder->nRecValid; |
| 2548 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2549 | if( nEq==p->nKeyCol ){ |
| 2550 | aff = SQLITE_AFF_INTEGER; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2551 | }else{ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2552 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2553 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2554 | /* Determine iLower and iUpper using ($P) only. */ |
| 2555 | if( nEq==0 ){ |
| 2556 | iLower = 0; |
drh | 9f07cf7 | 2014-10-22 15:27:05 +0000 | [diff] [blame] | 2557 | iUpper = p->nRowEst0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2558 | }else{ |
| 2559 | /* Note: this call could be optimized away - since the same values must |
| 2560 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 2561 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2562 | iLower = a[0]; |
| 2563 | iUpper = a[0] + a[1]; |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2564 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2565 | |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2566 | assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 2567 | assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | 681fca0 | 2014-10-10 15:01:46 +0000 | [diff] [blame] | 2568 | assert( p->aSortOrder!=0 ); |
| 2569 | if( p->aSortOrder[nEq] ){ |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2570 | /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 2571 | SWAP(WhereTerm*, pLower, pUpper); |
| 2572 | } |
| 2573 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2574 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 2575 | if( pLower ){ |
| 2576 | int bOk; /* True if value is extracted from pExpr */ |
| 2577 | Expr *pExpr = pLower->pExpr->pRight; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2578 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 2579 | if( rc==SQLITE_OK && bOk ){ |
| 2580 | tRowcnt iNew; |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2581 | iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2582 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2583 | if( iNew>iLower ) iLower = iNew; |
| 2584 | nOut--; |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2585 | pLower = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 2590 | if( pUpper ){ |
| 2591 | int bOk; /* True if value is extracted from pExpr */ |
| 2592 | Expr *pExpr = pUpper->pExpr->pRight; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2593 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 2594 | if( rc==SQLITE_OK && bOk ){ |
| 2595 | tRowcnt iNew; |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2596 | iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2597 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2598 | if( iNew<iUpper ) iUpper = iNew; |
| 2599 | nOut--; |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2600 | pUpper = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | pBuilder->pRec = pRec; |
| 2605 | if( rc==SQLITE_OK ){ |
| 2606 | if( iUpper>iLower ){ |
| 2607 | nNew = sqlite3LogEst(iUpper - iLower); |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2608 | /* TUNING: If both iUpper and iLower are derived from the same |
| 2609 | ** sample, then assume they are 4x more selective. This brings |
| 2610 | ** the estimated selectivity more in line with what it would be |
| 2611 | ** if estimated without the use of STAT3/4 tables. */ |
| 2612 | if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2613 | }else{ |
| 2614 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 2615 | } |
| 2616 | if( nNew<nOut ){ |
| 2617 | nOut = nNew; |
| 2618 | } |
drh | ae914d7 | 2014-08-28 19:38:22 +0000 | [diff] [blame] | 2619 | WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n", |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2620 | (u32)iLower, (u32)iUpper, nOut)); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2621 | } |
| 2622 | }else{ |
| 2623 | int bDone = 0; |
| 2624 | rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone); |
| 2625 | if( bDone ) return rc; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2626 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2627 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2628 | #else |
| 2629 | UNUSED_PARAMETER(pParse); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2630 | UNUSED_PARAMETER(pBuilder); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2631 | assert( pLower || pUpper ); |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2632 | #endif |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2633 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2634 | nNew = whereRangeAdjust(pLower, nOut); |
| 2635 | nNew = whereRangeAdjust(pUpper, nNew); |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2636 | |
drh | 4dd96a8 | 2014-10-24 15:26:29 +0000 | [diff] [blame] | 2637 | /* TUNING: If there is both an upper and lower limit and neither limit |
| 2638 | ** has an application-defined likelihood(), assume the range is |
dan | 42685f2 | 2014-04-28 19:34:06 +0000 | [diff] [blame] | 2639 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 2640 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 2641 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 2642 | ** match 1/64 of the index. */ |
drh | 4dd96a8 | 2014-10-24 15:26:29 +0000 | [diff] [blame] | 2643 | if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 2644 | nNew -= 20; |
| 2645 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2646 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2647 | nOut -= (pLower!=0) + (pUpper!=0); |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2648 | if( nNew<10 ) nNew = 10; |
| 2649 | if( nNew<nOut ) nOut = nNew; |
drh | ae914d7 | 2014-08-28 19:38:22 +0000 | [diff] [blame] | 2650 | #if defined(WHERETRACE_ENABLED) |
| 2651 | if( pLoop->nOut>nOut ){ |
| 2652 | WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", |
| 2653 | pLoop->nOut, nOut)); |
| 2654 | } |
| 2655 | #endif |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2656 | pLoop->nOut = (LogEst)nOut; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2657 | return rc; |
| 2658 | } |
| 2659 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2660 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2661 | /* |
| 2662 | ** Estimate the number of rows that will be returned based on |
| 2663 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2664 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2665 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2666 | ** for that index. When pExpr==NULL that means the constraint is |
| 2667 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2668 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2669 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2670 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2671 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2672 | ** |
| 2673 | ** This routine can fail if it is unable to load a collating sequence |
| 2674 | ** required for string comparison, or if unable to allocate memory |
| 2675 | ** for a UTF conversion required for comparison. The error is stored |
| 2676 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2677 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2678 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2679 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2680 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2681 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2682 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2683 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2684 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2685 | int nEq = pBuilder->pNew->u.btree.nEq; |
| 2686 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2687 | u8 aff; /* Column affinity */ |
| 2688 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2689 | tRowcnt a[2]; /* Statistics */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2690 | int bOk; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2691 | |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2692 | assert( nEq>=1 ); |
dan | fd984b8 | 2014-06-30 18:02:20 +0000 | [diff] [blame] | 2693 | assert( nEq<=p->nColumn ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2694 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2695 | assert( p->nSample>0 ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2696 | assert( pBuilder->nRecValid<nEq ); |
| 2697 | |
| 2698 | /* If values are not available for all fields of the index to the left |
| 2699 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 2700 | if( pBuilder->nRecValid<(nEq-1) ){ |
| 2701 | return SQLITE_NOTFOUND; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2702 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2703 | |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 2704 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 2705 | ** below would return the same value. */ |
dan | fd984b8 | 2014-06-30 18:02:20 +0000 | [diff] [blame] | 2706 | if( nEq>=p->nColumn ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2707 | *pnRow = 1; |
| 2708 | return SQLITE_OK; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2709 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2710 | |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2711 | aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity; |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2712 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); |
| 2713 | pBuilder->pRec = pRec; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2714 | if( rc!=SQLITE_OK ) return rc; |
| 2715 | if( bOk==0 ) return SQLITE_NOTFOUND; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2716 | pBuilder->nRecValid = nEq; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2717 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2718 | whereKeyStats(pParse, p, pRec, 0, a); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2719 | WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2720 | *pnRow = a[1]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2721 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2722 | return rc; |
| 2723 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2724 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2725 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2726 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2727 | /* |
| 2728 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2729 | ** an IN constraint where the right-hand side of the IN operator |
| 2730 | ** is a list of values. Example: |
| 2731 | ** |
| 2732 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2733 | ** |
| 2734 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2735 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2736 | ** non-zero. |
| 2737 | ** |
| 2738 | ** This routine can fail if it is unable to load a collating sequence |
| 2739 | ** required for string comparison, or if unable to allocate memory |
| 2740 | ** for a UTF conversion required for comparison. The error is stored |
| 2741 | ** in the pParse structure. |
| 2742 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2743 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2744 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2745 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2746 | 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] | 2747 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2748 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2749 | Index *p = pBuilder->pNew->u.btree.pIndex; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2750 | i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2751 | int nRecValid = pBuilder->nRecValid; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2752 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2753 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2754 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2755 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2756 | |
| 2757 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2758 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2759 | nEst = nRow0; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2760 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2761 | nRowEst += nEst; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2762 | pBuilder->nRecValid = nRecValid; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2763 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2764 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2765 | if( rc==SQLITE_OK ){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2766 | if( nRowEst > nRow0 ) nRowEst = nRow0; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2767 | *pnRow = nRowEst; |
drh | 5418b12 | 2014-08-28 13:42:13 +0000 | [diff] [blame] | 2768 | WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2769 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2770 | assert( pBuilder->nRecValid==nRecValid ); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2771 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2772 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2773 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2774 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2775 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 2776 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2777 | /* |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 2778 | ** Print the content of a WhereTerm object |
| 2779 | */ |
| 2780 | static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 2781 | if( pTerm==0 ){ |
| 2782 | sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 2783 | }else{ |
| 2784 | char zType[4]; |
| 2785 | memcpy(zType, "...", 4); |
| 2786 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 2787 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 2788 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 2789 | sqlite3DebugPrintf( |
| 2790 | "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n", |
| 2791 | iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, |
| 2792 | pTerm->eOperator, pTerm->wtFlags); |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 2793 | sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 2794 | } |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 2795 | } |
| 2796 | #endif |
| 2797 | |
| 2798 | #ifdef WHERETRACE_ENABLED |
| 2799 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2800 | ** Print a WhereLoop object for debugging purposes |
| 2801 | */ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 2802 | static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 2803 | WhereInfo *pWInfo = pWC->pWInfo; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2804 | int nb = 1+(pWInfo->pTabList->nSrc+7)/8; |
| 2805 | struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2806 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 2807 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 2808 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 2809 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2810 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2811 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 2812 | const char *zName; |
| 2813 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 2814 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 2815 | int i = sqlite3Strlen30(zName) - 1; |
| 2816 | while( zName[i]!='_' ) i--; |
| 2817 | zName += i; |
| 2818 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 2819 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2820 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 2821 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2822 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2823 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2824 | char *z; |
| 2825 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 2826 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 2827 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2828 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 2829 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2830 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 2831 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2832 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2833 | } |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 2834 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 2835 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 2836 | }else{ |
| 2837 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 2838 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2839 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 2840 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| 2841 | int i; |
| 2842 | for(i=0; i<p->nLTerm; i++){ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 2843 | whereTermPrint(p->aLTerm[i], i); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 2844 | } |
| 2845 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 2846 | } |
| 2847 | #endif |
| 2848 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2849 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2850 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 2851 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2852 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2853 | static void whereLoopInit(WhereLoop *p){ |
| 2854 | p->aLTerm = p->aLTermSpace; |
| 2855 | p->nLTerm = 0; |
| 2856 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 2857 | p->wsFlags = 0; |
| 2858 | } |
| 2859 | |
| 2860 | /* |
| 2861 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 2862 | */ |
| 2863 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 2864 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 2865 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 2866 | sqlite3_free(p->u.vtab.idxStr); |
| 2867 | p->u.vtab.needFree = 0; |
| 2868 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 2869 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 2870 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 2871 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 2872 | p->u.btree.pIndex = 0; |
| 2873 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2877 | /* |
| 2878 | ** Deallocate internal memory used by a WhereLoop object |
| 2879 | */ |
| 2880 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 2881 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 2882 | whereLoopClearUnion(db, p); |
| 2883 | whereLoopInit(p); |
| 2884 | } |
| 2885 | |
| 2886 | /* |
| 2887 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 2888 | */ |
| 2889 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 2890 | WhereTerm **paNew; |
| 2891 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 2892 | n = (n+7)&~7; |
| 2893 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 2894 | if( paNew==0 ) return SQLITE_NOMEM; |
| 2895 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 2896 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 2897 | p->aLTerm = paNew; |
| 2898 | p->nLSlot = n; |
| 2899 | return SQLITE_OK; |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | ** Transfer content from the second pLoop into the first. |
| 2904 | */ |
| 2905 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2906 | whereLoopClearUnion(db, pTo); |
drh | 0d31dc3 | 2013-09-06 00:40:59 +0000 | [diff] [blame] | 2907 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 2908 | memset(&pTo->u, 0, sizeof(pTo->u)); |
| 2909 | return SQLITE_NOMEM; |
| 2910 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 2911 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 2912 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2913 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 2914 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 2915 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2916 | pFrom->u.btree.pIndex = 0; |
| 2917 | } |
| 2918 | return SQLITE_OK; |
| 2919 | } |
| 2920 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2921 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2922 | ** Delete a WhereLoop object |
| 2923 | */ |
| 2924 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2925 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2926 | sqlite3DbFree(db, p); |
| 2927 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 2928 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2929 | /* |
| 2930 | ** Free a WhereInfo structure |
| 2931 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 2932 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2933 | if( ALWAYS(pWInfo) ){ |
dan | f89aa47 | 2015-04-25 12:20:24 +0000 | [diff] [blame] | 2934 | int i; |
| 2935 | for(i=0; i<pWInfo->nLevel; i++){ |
| 2936 | WhereLevel *pLevel = &pWInfo->a[i]; |
| 2937 | if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ |
| 2938 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 2939 | } |
| 2940 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 2941 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2942 | while( pWInfo->pLoops ){ |
| 2943 | WhereLoop *p = pWInfo->pLoops; |
| 2944 | pWInfo->pLoops = p->pNextLoop; |
| 2945 | whereLoopDelete(db, p); |
| 2946 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2947 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2948 | } |
| 2949 | } |
| 2950 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2951 | /* |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 2952 | ** Return TRUE if all of the following are true: |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 2953 | ** |
| 2954 | ** (1) X has the same or lower cost that Y |
| 2955 | ** (2) X is a proper subset of Y |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 2956 | ** (3) X skips at least as many columns as Y |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 2957 | ** |
| 2958 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 2959 | ** than Y and that every WHERE clause term used by X is also used |
| 2960 | ** by Y. |
| 2961 | ** |
| 2962 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 2963 | ** to have a lower cost. This routine returns TRUE when that cost |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 2964 | ** relationship is inverted and needs to be adjusted. The third rule |
| 2965 | ** was added because if X uses skip-scan less than Y it still might |
| 2966 | ** deserve a lower cost even if it is a proper subset of Y. |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 2967 | */ |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 2968 | static int whereLoopCheaperProperSubset( |
| 2969 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 2970 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 2971 | ){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 2972 | int i, j; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 2973 | if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 2974 | return 0; /* X is not a subset of Y */ |
| 2975 | } |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 2976 | if( pY->nSkip > pX->nSkip ) return 0; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 2977 | if( pX->rRun >= pY->rRun ){ |
| 2978 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 2979 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 2980 | } |
drh | 9ee8810 | 2014-05-07 20:33:17 +0000 | [diff] [blame] | 2981 | for(i=pX->nLTerm-1; i>=0; i--){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 2982 | if( pX->aLTerm[i]==0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 2983 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 2984 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 2985 | } |
| 2986 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 2987 | } |
| 2988 | return 1; /* All conditions meet */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so |
| 2993 | ** that: |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 2994 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 2995 | ** (1) pTemplate costs less than any other WhereLoops that are a proper |
| 2996 | ** subset of pTemplate |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 2997 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 2998 | ** (2) pTemplate costs more than any other WhereLoops for which pTemplate |
| 2999 | ** is a proper subset. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3000 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3001 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 3002 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 3003 | ** also used by Y. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3004 | */ |
| 3005 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 3006 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3007 | for(; p; p=p->pNextLoop){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3008 | if( p->iTab!=pTemplate->iTab ) continue; |
| 3009 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3010 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 3011 | /* Adjust pTemplate cost downward so that it is cheaper than its |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 3012 | ** subset p. */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 3013 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 3014 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3015 | pTemplate->rRun = p->rRun; |
| 3016 | pTemplate->nOut = p->nOut - 1; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 3017 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 3018 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 3019 | ** pTemplate is a proper subset of p */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 3020 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 3021 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 3022 | pTemplate->rRun = p->rRun; |
| 3023 | pTemplate->nOut = p->nOut + 1; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | /* |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3029 | ** Search the list of WhereLoops in *ppPrev looking for one that can be |
| 3030 | ** supplanted by pTemplate. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3031 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3032 | ** Return NULL if the WhereLoop list contains an entry that can supplant |
| 3033 | ** pTemplate, in other words if pTemplate does not belong on the list. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3034 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3035 | ** If pX is a WhereLoop that pTemplate can supplant, then return the |
| 3036 | ** link that points to pX. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3037 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3038 | ** If pTemplate cannot supplant any existing element of the list but needs |
| 3039 | ** 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] | 3040 | */ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3041 | static WhereLoop **whereLoopFindLesser( |
| 3042 | WhereLoop **ppPrev, |
| 3043 | const WhereLoop *pTemplate |
| 3044 | ){ |
| 3045 | WhereLoop *p; |
| 3046 | for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 3047 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 3048 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 3049 | ** then those WhereLoops need to be considered separately. Neither is |
| 3050 | ** a candidate to replace the other. */ |
| 3051 | continue; |
| 3052 | } |
| 3053 | /* In the current implementation, the rSetup value is either zero |
| 3054 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 3055 | ** is the same for compatible WhereLoops. */ |
| 3056 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 3057 | || p->rSetup==pTemplate->rSetup ); |
| 3058 | |
| 3059 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 3060 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 3061 | ** rSetup. Call this SETUP-INVARIANT */ |
| 3062 | assert( p->rSetup>=pTemplate->rSetup ); |
| 3063 | |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 3064 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 3065 | ** UNIQUE constraint) with one or more == constraints is better |
dan | 70273d0 | 2014-11-14 19:34:20 +0000 | [diff] [blame] | 3066 | ** than an automatic index. Unless it is a skip-scan. */ |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 3067 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
dan | 70273d0 | 2014-11-14 19:34:20 +0000 | [diff] [blame] | 3068 | && (pTemplate->nSkip)==0 |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 3069 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 3070 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 3071 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 3072 | ){ |
| 3073 | break; |
| 3074 | } |
| 3075 | |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3076 | /* If existing WhereLoop p is better than pTemplate, pTemplate can be |
| 3077 | ** discarded. WhereLoop p is better if: |
| 3078 | ** (1) p has no more dependencies than pTemplate, and |
| 3079 | ** (2) p has an equal or lower cost than pTemplate |
| 3080 | */ |
| 3081 | if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */ |
| 3082 | && p->rSetup<=pTemplate->rSetup /* (2a) */ |
| 3083 | && p->rRun<=pTemplate->rRun /* (2b) */ |
| 3084 | && p->nOut<=pTemplate->nOut /* (2c) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3085 | ){ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3086 | return 0; /* Discard pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3087 | } |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3088 | |
| 3089 | /* If pTemplate is always better than p, then cause p to be overwritten |
| 3090 | ** with pTemplate. pTemplate is better than p if: |
| 3091 | ** (1) pTemplate has no more dependences than p, and |
| 3092 | ** (2) pTemplate has an equal or lower cost than p. |
| 3093 | */ |
| 3094 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ |
| 3095 | && p->rRun>=pTemplate->rRun /* (2a) */ |
| 3096 | && p->nOut>=pTemplate->nOut /* (2b) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3097 | ){ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 3098 | assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3099 | break; /* Cause p to be overwritten by pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3100 | } |
| 3101 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3102 | return ppPrev; |
| 3103 | } |
| 3104 | |
| 3105 | /* |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3106 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 3107 | ** |
| 3108 | ** An existing WhereLoop entry might be overwritten if the new template |
| 3109 | ** is better and has fewer dependencies. Or the template will be ignored |
| 3110 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 3111 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3112 | ** added based on the template. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3113 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3114 | ** If pBuilder->pOrSet is not NULL then we care about only the |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3115 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 3116 | ** information is gathered in the pBuilder->pOrSet object. This special |
| 3117 | ** processing mode is used only for OR clause processing. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3118 | ** |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3119 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3120 | ** still might overwrite similar loops with the new template if the |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3121 | ** new template is better. Loops may be overwritten if the following |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3122 | ** conditions are met: |
| 3123 | ** |
| 3124 | ** (1) They have the same iTab. |
| 3125 | ** (2) They have the same iSortIdx. |
| 3126 | ** (3) The template has same or fewer dependencies than the current loop |
| 3127 | ** (4) The template has the same or lower cost than the current loop |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3128 | */ |
| 3129 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3130 | WhereLoop **ppPrev, *p; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3131 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 3132 | sqlite3 *db = pWInfo->pParse->db; |
| 3133 | |
| 3134 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 3135 | ** and prereqs. |
| 3136 | */ |
| 3137 | if( pBuilder->pOrSet!=0 ){ |
| 3138 | #if WHERETRACE_ENABLED |
| 3139 | u16 n = pBuilder->pOrSet->n; |
| 3140 | int x = |
| 3141 | #endif |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3142 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 3143 | pTemplate->nOut); |
| 3144 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3145 | if( sqlite3WhereTrace & 0x8 ){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3146 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 3147 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3148 | } |
| 3149 | #endif |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3150 | return SQLITE_OK; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3153 | /* Look for an existing WhereLoop to replace with pTemplate |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 3154 | */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 3155 | whereLoopAdjustCost(pWInfo->pLoops, pTemplate); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3156 | ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3157 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3158 | if( ppPrev==0 ){ |
| 3159 | /* There already exists a WhereLoop on the list that is better |
| 3160 | ** than pTemplate, so just ignore pTemplate */ |
| 3161 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 3162 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 3163 | sqlite3DebugPrintf(" skip: "); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3164 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3165 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3166 | #endif |
| 3167 | return SQLITE_OK; |
| 3168 | }else{ |
| 3169 | p = *ppPrev; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | /* If we reach this point it means that either p[] should be overwritten |
| 3173 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 3174 | ** WhereLoop and insert it. |
| 3175 | */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3176 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3177 | if( sqlite3WhereTrace & 0x8 ){ |
| 3178 | if( p!=0 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 3179 | sqlite3DebugPrintf("replace: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3180 | whereLoopPrint(p, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3181 | } |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 3182 | sqlite3DebugPrintf(" add: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3183 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3184 | } |
| 3185 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3186 | if( p==0 ){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3187 | /* Allocate a new WhereLoop to add to the end of the list */ |
| 3188 | *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3189 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3190 | whereLoopInit(p); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3191 | p->pNextLoop = 0; |
| 3192 | }else{ |
| 3193 | /* We will be overwriting WhereLoop p[]. But before we do, first |
| 3194 | ** go through the rest of the list and delete any other entries besides |
| 3195 | ** p[] that are also supplated by pTemplate */ |
| 3196 | WhereLoop **ppTail = &p->pNextLoop; |
| 3197 | WhereLoop *pToDel; |
| 3198 | while( *ppTail ){ |
| 3199 | ppTail = whereLoopFindLesser(ppTail, pTemplate); |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 3200 | if( ppTail==0 ) break; |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3201 | pToDel = *ppTail; |
| 3202 | if( pToDel==0 ) break; |
| 3203 | *ppTail = pToDel->pNextLoop; |
| 3204 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 3205 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 3206 | sqlite3DebugPrintf(" delete: "); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 3207 | whereLoopPrint(pToDel, pBuilder->pWC); |
| 3208 | } |
| 3209 | #endif |
| 3210 | whereLoopDelete(db, pToDel); |
| 3211 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3212 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3213 | whereLoopXfer(db, p, pTemplate); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3214 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3215 | Index *pIndex = p->u.btree.pIndex; |
| 3216 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3217 | p->u.btree.pIndex = 0; |
| 3218 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3219 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3220 | return SQLITE_OK; |
| 3221 | } |
| 3222 | |
| 3223 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3224 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 3225 | ** WHERE clause that reference the loop but which are not used by an |
| 3226 | ** index. |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3227 | * |
| 3228 | ** For every WHERE clause term that is not used by the index |
| 3229 | ** and which has a truth probability assigned by one of the likelihood(), |
| 3230 | ** likely(), or unlikely() SQL functions, reduce the estimated number |
| 3231 | ** of output rows by the probability specified. |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3232 | ** |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3233 | ** TUNING: For every WHERE clause term that is not used by the index |
| 3234 | ** and which does not have an assigned truth probability, heuristics |
| 3235 | ** described below are used to try to estimate the truth probability. |
| 3236 | ** TODO --> Perhaps this is something that could be improved by better |
| 3237 | ** table statistics. |
| 3238 | ** |
drh | ab4624d | 2014-11-22 19:52:10 +0000 | [diff] [blame] | 3239 | ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% |
| 3240 | ** value corresponds to -1 in LogEst notation, so this means decrement |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3241 | ** the WhereLoop.nOut field for every such WHERE clause term. |
| 3242 | ** |
| 3243 | ** Heuristic 2: If there exists one or more WHERE clause terms of the |
| 3244 | ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the |
| 3245 | ** final output row estimate is no greater than 1/4 of the total number |
| 3246 | ** of rows in the table. In other words, assume that x==EXPR will filter |
| 3247 | ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the |
| 3248 | ** "x" column is boolean or else -1 or 0 or 1 is a common default value |
| 3249 | ** on the "x" column and so in that case only cap the output row estimate |
| 3250 | ** at 1/2 instead of 1/4. |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3251 | */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3252 | static void whereLoopOutputAdjust( |
| 3253 | WhereClause *pWC, /* The WHERE clause */ |
| 3254 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 3255 | LogEst nRow /* Number of rows in the entire table */ |
| 3256 | ){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 3257 | WhereTerm *pTerm, *pX; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3258 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3259 | int i, j, k; |
| 3260 | LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 3261 | |
drh | a389825 | 2014-11-22 12:22:13 +0000 | [diff] [blame] | 3262 | assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3263 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 3264 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3265 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 3266 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 3267 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 3268 | pX = pLoop->aLTerm[j]; |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 3269 | if( pX==0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 3270 | if( pX==pTerm ) break; |
| 3271 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 3272 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3273 | if( j<0 ){ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3274 | if( pTerm->truthProb<=0 ){ |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3275 | /* If a truth probability is specified using the likelihood() hints, |
| 3276 | ** then use the probability provided by the application. */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3277 | pLoop->nOut += pTerm->truthProb; |
| 3278 | }else{ |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3279 | /* In the absence of explicit truth probabilities, use heuristics to |
| 3280 | ** guess a reasonable truth probability. */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3281 | pLoop->nOut--; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 3282 | if( pTerm->eOperator&(WO_EQ|WO_IS) ){ |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3283 | Expr *pRight = pTerm->pExpr->pRight; |
drh | e0cc3c2 | 2015-05-13 17:54:08 +0000 | [diff] [blame] | 3284 | testcase( pTerm->pExpr->op==TK_IS ); |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3285 | if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ |
| 3286 | k = 10; |
| 3287 | }else{ |
| 3288 | k = 20; |
| 3289 | } |
| 3290 | if( iReduce<k ) iReduce = k; |
| 3291 | } |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3292 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3293 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3294 | } |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 3295 | if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | /* |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 3299 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 3300 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| 3301 | */ |
| 3302 | #ifdef SQLITE_ENABLE_COSTMULT |
| 3303 | # define ApplyCostMultiplier(C,T) C += T |
| 3304 | #else |
| 3305 | # define ApplyCostMultiplier(C,T) |
| 3306 | #endif |
| 3307 | |
| 3308 | /* |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 3309 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the |
| 3310 | ** index pIndex. Try to match one more. |
| 3311 | ** |
| 3312 | ** When this function is called, pBuilder->pNew->nOut contains the |
| 3313 | ** number of rows expected to be visited by filtering using the nEq |
| 3314 | ** terms only. If it is modified, this value is restored before this |
| 3315 | ** function returns. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3316 | ** |
| 3317 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 3318 | ** INTEGER PRIMARY KEY. |
| 3319 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3320 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3321 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 3322 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 3323 | Index *pProbe, /* An index on pSrc */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3324 | LogEst nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3325 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3326 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 3327 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 3328 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3329 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 3330 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3331 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3332 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3333 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 3334 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3335 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3336 | u16 saved_nSkip; /* Original value of pNew->nSkip */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3337 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3338 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3339 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3340 | int rc = SQLITE_OK; /* Return code */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3341 | LogEst rSize; /* Number of rows in the table */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3342 | LogEst rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 3343 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3344 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3345 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3346 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3347 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3348 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3349 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 3350 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 3351 | opMask = WO_LT|WO_LE; |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 3352 | }else if( /*pProbe->tnum<=0 ||*/ (pSrc->jointype & JT_LEFT)!=0 ){ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3353 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3354 | }else{ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 3355 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3356 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3357 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3358 | |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 3359 | assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 3360 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 3361 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3362 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 3363 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3364 | saved_nEq = pNew->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3365 | saved_nSkip = pNew->nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3366 | saved_nLTerm = pNew->nLTerm; |
| 3367 | saved_wsFlags = pNew->wsFlags; |
| 3368 | saved_prereq = pNew->prereq; |
| 3369 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3370 | pNew->rSetup = 0; |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3371 | rSize = pProbe->aiRowLogEst[0]; |
| 3372 | rLogSize = estLog(rSize); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3373 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3374 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3375 | LogEst rCostIdx; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3376 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3377 | int nIn = 0; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 3378 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 3379 | int nRecValid = pBuilder->nRecValid; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3380 | #endif |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3381 | if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
dan | 8bff07a | 2013-08-29 14:56:14 +0000 | [diff] [blame] | 3382 | && (iCol<0 || pSrc->pTab->aCol[iCol].notNull) |
| 3383 | ){ |
| 3384 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 3385 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 3386 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 3387 | |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 3388 | /* Do not allow the upper bound of a LIKE optimization range constraint |
| 3389 | ** to mix with a lower range bound from some other source */ |
| 3390 | if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; |
| 3391 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3392 | pNew->wsFlags = saved_wsFlags; |
| 3393 | pNew->u.btree.nEq = saved_nEq; |
| 3394 | pNew->nLTerm = saved_nLTerm; |
| 3395 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 3396 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 3397 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3398 | |
| 3399 | assert( nInMul==0 |
| 3400 | || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| 3401 | || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 |
| 3402 | || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 |
| 3403 | ); |
| 3404 | |
| 3405 | if( eOp & WO_IN ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3406 | Expr *pExpr = pTerm->pExpr; |
| 3407 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 3408 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 3409 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3410 | nIn = 46; assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3411 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 3412 | /* "x IN (value, value, ...)" */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3413 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 3414 | } |
drh | 2b59b3a | 2014-03-20 13:26:47 +0000 | [diff] [blame] | 3415 | assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 3416 | ** changes "x IN (?)" into "x=?". */ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3417 | |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 3418 | }else if( eOp & (WO_EQ|WO_IS) ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3419 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3420 | if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){ |
dan | 2813bde | 2015-04-11 11:44:27 +0000 | [diff] [blame] | 3421 | if( iCol>=0 && pProbe->uniqNotNull==0 ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 3422 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 3423 | }else{ |
| 3424 | pNew->wsFlags |= WHERE_ONEROW; |
| 3425 | } |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 3426 | } |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 3427 | }else if( eOp & WO_ISNULL ){ |
| 3428 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3429 | }else if( eOp & (WO_GT|WO_GE) ){ |
| 3430 | testcase( eOp & WO_GT ); |
| 3431 | testcase( eOp & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3432 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 3433 | pBtm = pTerm; |
| 3434 | pTop = 0; |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 3435 | if( pTerm->wtFlags & TERM_LIKEOPT ){ |
drh | 8031462 | 2015-03-09 13:01:02 +0000 | [diff] [blame] | 3436 | /* Range contraints that come from the LIKE optimization are |
| 3437 | ** always used in pairs. */ |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 3438 | pTop = &pTerm[1]; |
| 3439 | assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 3440 | assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 3441 | assert( pTop->eOperator==WO_LT ); |
| 3442 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 3443 | pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 3444 | pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 3445 | } |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 3446 | }else{ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3447 | assert( eOp & (WO_LT|WO_LE) ); |
| 3448 | testcase( eOp & WO_LT ); |
| 3449 | testcase( eOp & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3450 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 3451 | pTop = pTerm; |
| 3452 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3453 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3454 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3455 | |
| 3456 | /* At this point pNew->nOut is set to the number of rows expected to |
| 3457 | ** be visited by the index scan before considering term pTerm, or the |
| 3458 | ** values of nIn and nInMul. In other words, assuming that all |
| 3459 | ** "x IN(...)" terms are replaced with "x = ?". This block updates |
| 3460 | ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ |
| 3461 | assert( pNew->nOut==saved_nOut ); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 3462 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3463 | /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 |
| 3464 | ** data, using some other estimate. */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 3465 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3466 | }else{ |
| 3467 | int nEq = ++pNew->u.btree.nEq; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 3468 | assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3469 | |
| 3470 | assert( pNew->nOut==saved_nOut ); |
dan | 09e1df6 | 2014-04-29 16:10:22 +0000 | [diff] [blame] | 3471 | if( pTerm->truthProb<=0 && iCol>=0 ){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3472 | assert( (eOp & WO_IN) || nIn==0 ); |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 3473 | testcase( eOp & WO_IN ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3474 | pNew->nOut += pTerm->truthProb; |
| 3475 | pNew->nOut -= nIn; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3476 | }else{ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 3477 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3478 | tRowcnt nOut = 0; |
| 3479 | if( nInMul==0 |
| 3480 | && pProbe->nSample |
| 3481 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3482 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3483 | ){ |
| 3484 | Expr *pExpr = pTerm->pExpr; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 3485 | if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ |
| 3486 | testcase( eOp & WO_EQ ); |
| 3487 | testcase( eOp & WO_IS ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3488 | testcase( eOp & WO_ISNULL ); |
| 3489 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
| 3490 | }else{ |
| 3491 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
| 3492 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3493 | if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 3494 | if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ |
| 3495 | if( nOut ){ |
| 3496 | pNew->nOut = sqlite3LogEst(nOut); |
| 3497 | if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; |
| 3498 | pNew->nOut -= nIn; |
| 3499 | } |
| 3500 | } |
| 3501 | if( nOut==0 ) |
| 3502 | #endif |
| 3503 | { |
| 3504 | pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); |
| 3505 | if( eOp & WO_ISNULL ){ |
| 3506 | /* TUNING: If there is no likelihood() value, assume that a |
| 3507 | ** "col IS NULL" expression matches twice as many rows |
| 3508 | ** as (col=?). */ |
| 3509 | pNew->nOut += 10; |
| 3510 | } |
| 3511 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 3512 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 3513 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3514 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3515 | /* Set rCostIdx to the cost of visiting selected rows in index. Add |
| 3516 | ** it to pNew->rRun, which is currently set to the cost of the index |
| 3517 | ** seek only. Then, if this is a non-covering index, add the cost of |
| 3518 | ** visiting the rows in the main table. */ |
| 3519 | rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3520 | pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 3521 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3522 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3523 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 3524 | ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3525 | |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3526 | nOutUnadjusted = pNew->nOut; |
| 3527 | pNew->rRun += nInMul + nIn; |
| 3528 | pNew->nOut += nInMul + nIn; |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3529 | whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3530 | rc = whereLoopInsert(pBuilder, pNew); |
dan | 440e6ff | 2014-04-28 08:49:54 +0000 | [diff] [blame] | 3531 | |
| 3532 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 3533 | pNew->nOut = saved_nOut; |
| 3534 | }else{ |
| 3535 | pNew->nOut = nOutUnadjusted; |
| 3536 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 3537 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3538 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 3539 | && pNew->u.btree.nEq<pProbe->nColumn |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3540 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3541 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3542 | } |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 3543 | pNew->nOut = saved_nOut; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 3544 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 3545 | pBuilder->nRecValid = nRecValid; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 3546 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3547 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3548 | pNew->prereq = saved_prereq; |
| 3549 | pNew->u.btree.nEq = saved_nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3550 | pNew->nSkip = saved_nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3551 | pNew->wsFlags = saved_wsFlags; |
| 3552 | pNew->nOut = saved_nOut; |
| 3553 | pNew->nLTerm = saved_nLTerm; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3554 | |
| 3555 | /* Consider using a skip-scan if there are no WHERE clause constraints |
| 3556 | ** available for the left-most terms of the index, and if the average |
| 3557 | ** number of repeats in the left-most terms is at least 18. |
| 3558 | ** |
| 3559 | ** The magic number 18 is selected on the basis that scanning 17 rows |
| 3560 | ** is almost always quicker than an index seek (even though if the index |
| 3561 | ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 3562 | ** the code). And, even if it is not, it should not be too much slower. |
| 3563 | ** On the other hand, the extra seeks could end up being significantly |
| 3564 | ** more expensive. */ |
| 3565 | assert( 42==sqlite3LogEst(18) ); |
| 3566 | if( saved_nEq==saved_nSkip |
| 3567 | && saved_nEq+1<pProbe->nKeyCol |
drh | f9df2fb | 2014-11-15 19:08:13 +0000 | [diff] [blame] | 3568 | && pProbe->noSkipScan==0 |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3569 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 3570 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 3571 | ){ |
| 3572 | LogEst nIter; |
| 3573 | pNew->u.btree.nEq++; |
| 3574 | pNew->nSkip++; |
| 3575 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 3576 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 3577 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3578 | pNew->nOut -= nIter; |
| 3579 | /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 3580 | ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 3581 | nIter += 5; |
| 3582 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 3583 | pNew->nOut = saved_nOut; |
| 3584 | pNew->u.btree.nEq = saved_nEq; |
| 3585 | pNew->nSkip = saved_nSkip; |
| 3586 | pNew->wsFlags = saved_wsFlags; |
| 3587 | } |
| 3588 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3589 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3593 | ** Return True if it is possible that pIndex might be useful in |
| 3594 | ** implementing the ORDER BY clause in pBuilder. |
| 3595 | ** |
| 3596 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 3597 | ** if there is no way for pIndex to be useful in implementing that |
| 3598 | ** ORDER BY clause. |
| 3599 | */ |
| 3600 | static int indexMightHelpWithOrderBy( |
| 3601 | WhereLoopBuilder *pBuilder, |
| 3602 | Index *pIndex, |
| 3603 | int iCursor |
| 3604 | ){ |
| 3605 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 3606 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3607 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3608 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3609 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3610 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 3611 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3612 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 3613 | if( pExpr->iTable==iCursor ){ |
drh | 137fd4f | 2014-09-19 02:01:37 +0000 | [diff] [blame] | 3614 | if( pExpr->iColumn<0 ) return 1; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3615 | for(jj=0; jj<pIndex->nKeyCol; jj++){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 3616 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 3617 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | return 0; |
| 3621 | } |
| 3622 | |
| 3623 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 3624 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 3625 | ** the table is used by an index. Only the first 63 columns are considered. |
| 3626 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 3627 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 3628 | Bitmask m = 0; |
| 3629 | int j; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 3630 | for(j=pIdx->nColumn-1; j>=0; j--){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 3631 | int x = pIdx->aiColumn[j]; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 3632 | if( x>=0 ){ |
| 3633 | testcase( x==BMS-1 ); |
| 3634 | testcase( x==BMS-2 ); |
| 3635 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 3636 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 3637 | } |
| 3638 | return m; |
| 3639 | } |
| 3640 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 3641 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 3642 | ** in the current query. Return true if it can be and false if not. |
| 3643 | */ |
| 3644 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 3645 | int i; |
| 3646 | WhereTerm *pTerm; |
| 3647 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
dan | 2a45cb5 | 2015-02-24 20:10:49 +0000 | [diff] [blame] | 3648 | Expr *pExpr = pTerm->pExpr; |
| 3649 | if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab) |
| 3650 | && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) |
drh | 077f06e | 2015-02-24 16:48:59 +0000 | [diff] [blame] | 3651 | ){ |
| 3652 | return 1; |
| 3653 | } |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 3654 | } |
| 3655 | return 0; |
| 3656 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 3657 | |
| 3658 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 3659 | ** 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] | 3660 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 3661 | ** a b-tree table, not a virtual table. |
dan | 8164722 | 2014-04-30 15:00:16 +0000 | [diff] [blame] | 3662 | ** |
| 3663 | ** The costs (WhereLoop.rRun) of the b-tree loops added by this function |
| 3664 | ** are calculated as follows: |
| 3665 | ** |
| 3666 | ** For a full scan, assuming the table (or index) contains nRow rows: |
| 3667 | ** |
| 3668 | ** cost = nRow * 3.0 // full-table scan |
| 3669 | ** cost = nRow * K // scan of covering index |
| 3670 | ** cost = nRow * (K+3.0) // scan of non-covering index |
| 3671 | ** |
| 3672 | ** where K is a value between 1.1 and 3.0 set based on the relative |
| 3673 | ** estimated average size of the index and table records. |
| 3674 | ** |
| 3675 | ** For an index scan, where nVisit is the number of index rows visited |
| 3676 | ** by the scan, and nSeek is the number of seek operations required on |
| 3677 | ** the index b-tree: |
| 3678 | ** |
| 3679 | ** cost = nSeek * (log(nRow) + K * nVisit) // covering index |
| 3680 | ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index |
| 3681 | ** |
| 3682 | ** Normally, nSeek is 1. nSeek values greater than 1 come about if the |
| 3683 | ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when |
| 3684 | ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. |
drh | 83a305f | 2014-07-22 12:05:32 +0000 | [diff] [blame] | 3685 | ** |
| 3686 | ** The estimated values (nRow, nVisit, nSeek) often contain a large amount |
| 3687 | ** of uncertainty. For this reason, scoring is designed to pick plans that |
| 3688 | ** "do the least harm" if the estimates are inaccurate. For example, a |
| 3689 | ** log(nRow) factor is omitted from a non-covering index scan in order to |
| 3690 | ** bias the scoring in favor of using an index, since the worst-case |
| 3691 | ** performance of using an index is far better than the worst-case performance |
| 3692 | ** of a full table scan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3693 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3694 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3695 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3696 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3697 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3698 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3699 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3700 | Index sPk; /* A fake index object for the primary key */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3701 | LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3702 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3703 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3704 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3705 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3706 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 3707 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3708 | int b; /* A boolean value */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3709 | LogEst rSize; /* number of rows in the table */ |
| 3710 | LogEst rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 3711 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 3712 | Table *pTab; /* Table being queried */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3713 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3714 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3715 | pWInfo = pBuilder->pWInfo; |
| 3716 | pTabList = pWInfo->pTabList; |
| 3717 | pSrc = pTabList->a + pNew->iTab; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 3718 | pTab = pSrc->pTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 3719 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 3720 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3721 | |
| 3722 | if( pSrc->pIndex ){ |
| 3723 | /* An INDEXED BY clause specifies a particular index to use */ |
| 3724 | pProbe = pSrc->pIndex; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 3725 | }else if( !HasRowid(pTab) ){ |
| 3726 | pProbe = pTab->pIndex; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3727 | }else{ |
| 3728 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 3729 | ** variable sPk to represent the rowid primary key index. Make this |
| 3730 | ** fake index the first in a chain of Index objects with all of the real |
| 3731 | ** indices to follow */ |
| 3732 | Index *pFirst; /* First of real indices on the table */ |
| 3733 | memset(&sPk, 0, sizeof(Index)); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3734 | sPk.nKeyCol = 1; |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 3735 | sPk.nColumn = 1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3736 | sPk.aiColumn = &aiColumnPk; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3737 | sPk.aiRowLogEst = aiRowEstPk; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3738 | sPk.onError = OE_Replace; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 3739 | sPk.pTable = pTab; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3740 | sPk.szIdxRow = pTab->szTabRow; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3741 | aiRowEstPk[0] = pTab->nRowLogEst; |
| 3742 | aiRowEstPk[1] = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3743 | pFirst = pSrc->pTab->pIndex; |
| 3744 | if( pSrc->notIndexed==0 ){ |
| 3745 | /* The real indices of the table are only considered if the |
| 3746 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 3747 | sPk.pNext = pFirst; |
| 3748 | } |
| 3749 | pProbe = &sPk; |
| 3750 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3751 | rSize = pTab->nRowLogEst; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3752 | rLogSize = estLog(rSize); |
| 3753 | |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 3754 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3755 | /* Automatic indexes */ |
drh | d092ed4 | 2015-05-29 14:36:30 +0000 | [diff] [blame] | 3756 | if( !pBuilder->pOrSet /* Not part of an OR optimization */ |
drh | 8e8e7ef | 2015-03-02 17:25:00 +0000 | [diff] [blame] | 3757 | && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 3758 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
drh | d092ed4 | 2015-05-29 14:36:30 +0000 | [diff] [blame] | 3759 | && pSrc->pIndex==0 /* Has no INDEXED BY clause */ |
| 3760 | && !pSrc->notIndexed /* Has no NOT INDEXED clause */ |
| 3761 | && HasRowid(pTab) /* Is not a WITHOUT ROWID table. (FIXME: Why not?) */ |
| 3762 | && !pSrc->isCorrelated /* Not a correlated subquery */ |
| 3763 | && !pSrc->isRecursive /* Not a recursive common table expression. */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3764 | ){ |
| 3765 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3766 | WhereTerm *pTerm; |
| 3767 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 3768 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 3769 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3770 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 3771 | pNew->u.btree.nEq = 1; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3772 | pNew->nSkip = 0; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3773 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3774 | pNew->nLTerm = 1; |
| 3775 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 3776 | /* TUNING: One-time cost for computing the automatic index is |
drh | 7e07433 | 2014-09-22 14:30:51 +0000 | [diff] [blame] | 3777 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| 3778 | ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 3779 | ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 3780 | ** of X is smaller for views and subqueries so that the query planner |
| 3781 | ** will be more aggressive about generating automatic indexes for |
| 3782 | ** those objects, since there is no opportunity to add schema |
| 3783 | ** indexes on subqueries and views. */ |
| 3784 | pNew->rSetup = rLogSize + rSize + 4; |
| 3785 | if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 3786 | pNew->rSetup += 24; |
| 3787 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 3788 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3789 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 3790 | ** 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] | 3791 | ** of knowing how selective the index will ultimately be. It would |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3792 | ** not be unreasonable to make this value much larger. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 3793 | pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 3794 | pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3795 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3796 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3797 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3798 | } |
| 3799 | } |
| 3800 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 3801 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3802 | |
| 3803 | /* Loop over all indices |
| 3804 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3805 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 3806 | if( pProbe->pPartIdxWhere!=0 |
dan | 0829169 | 2014-08-27 17:37:20 +0000 | [diff] [blame] | 3807 | && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ |
| 3808 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 3809 | continue; /* Partial index inappropriate for this query */ |
| 3810 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 3811 | rSize = pProbe->aiRowLogEst[0]; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3812 | pNew->u.btree.nEq = 0; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3813 | pNew->nSkip = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3814 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3815 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3816 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3817 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 3818 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3819 | pNew->u.btree.pIndex = pProbe; |
| 3820 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3821 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 3822 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3823 | if( pProbe->tnum<=0 ){ |
| 3824 | /* Integer primary key index */ |
| 3825 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3826 | |
| 3827 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 3828 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3829 | /* TUNING: Cost of full table scan is (N*3.0). */ |
| 3830 | pNew->rRun = rSize + 16; |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 3831 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3832 | whereLoopOutputAdjust(pWC, pNew, rSize); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3833 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3834 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3835 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3836 | }else{ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 3837 | Bitmask m; |
| 3838 | if( pProbe->isCovering ){ |
| 3839 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 3840 | m = 0; |
| 3841 | }else{ |
| 3842 | m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 3843 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 3844 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3845 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3846 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3847 | if( b |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 3848 | || !HasRowid(pTab) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3849 | || ( m==0 |
| 3850 | && pProbe->bUnordered==0 |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 3851 | && (pProbe->szIdxRow<pTab->szTabRow) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3852 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 3853 | && sqlite3GlobalConfig.bUseCis |
| 3854 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 3855 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 3856 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3857 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 3858 | |
| 3859 | /* The cost of visiting the index rows is N*K, where K is |
| 3860 | ** between 1.1 and 3.0, depending on the relative sizes of the |
| 3861 | ** index and table rows. If this is a non-covering index scan, |
| 3862 | ** also add the cost of visiting table rows (N*3.0). */ |
| 3863 | pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; |
| 3864 | if( m!=0 ){ |
| 3865 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 3866 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 3867 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 3868 | whereLoopOutputAdjust(pWC, pNew, rSize); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3869 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3870 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3871 | if( rc ) break; |
| 3872 | } |
| 3873 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 3874 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3875 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 3876 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 3877 | sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 3878 | pBuilder->nRecValid = 0; |
| 3879 | pBuilder->pRec = 0; |
dan | ddc2d6e | 2013-08-06 20:15:06 +0000 | [diff] [blame] | 3880 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3881 | |
| 3882 | /* If there was an INDEXED BY clause, then only that one index is |
| 3883 | ** considered. */ |
| 3884 | if( pSrc->pIndex ) break; |
| 3885 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3886 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 3889 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3890 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 3891 | ** Add all WhereLoop objects for a table of the join identified by |
| 3892 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3893 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3894 | static int whereLoopAddVirtual( |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 3895 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 3896 | Bitmask mExtra |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3897 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3898 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3899 | Parse *pParse; /* The parsing context */ |
| 3900 | WhereClause *pWC; /* The WHERE clause */ |
| 3901 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 3902 | Table *pTab; |
| 3903 | sqlite3 *db; |
| 3904 | sqlite3_index_info *pIdxInfo; |
| 3905 | struct sqlite3_index_constraint *pIdxCons; |
| 3906 | struct sqlite3_index_constraint_usage *pUsage; |
| 3907 | WhereTerm *pTerm; |
| 3908 | int i, j; |
| 3909 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3910 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3911 | int seenIn = 0; /* True if an IN operator is seen */ |
| 3912 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 3913 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 3914 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3915 | int rc = SQLITE_OK; |
| 3916 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3917 | pWInfo = pBuilder->pWInfo; |
| 3918 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3919 | db = pParse->db; |
| 3920 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3921 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3922 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 3923 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 3924 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 3925 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3926 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3927 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3928 | pNew->rSetup = 0; |
| 3929 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3930 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3931 | pNew->u.vtab.needFree = 0; |
| 3932 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3933 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3934 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 3935 | sqlite3DbFree(db, pIdxInfo); |
| 3936 | return SQLITE_NOMEM; |
| 3937 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3938 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 3939 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3940 | if( !seenIn && (iPhase&1)!=0 ){ |
| 3941 | iPhase++; |
| 3942 | if( iPhase>3 ) break; |
| 3943 | } |
| 3944 | if( !seenVar && iPhase>1 ) break; |
| 3945 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 3946 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 3947 | j = pIdxCons->iTermOffset; |
| 3948 | pTerm = &pWC->a[j]; |
| 3949 | switch( iPhase ){ |
| 3950 | case 0: /* Constants without IN operator */ |
| 3951 | pIdxCons->usable = 0; |
| 3952 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 3953 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3954 | } |
| 3955 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3956 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3957 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3958 | pIdxCons->usable = 1; |
| 3959 | } |
| 3960 | break; |
| 3961 | case 1: /* Constants with IN operators */ |
| 3962 | assert( seenIn ); |
| 3963 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 3964 | break; |
| 3965 | case 2: /* Variables without IN */ |
| 3966 | assert( seenVar ); |
| 3967 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 3968 | break; |
| 3969 | default: /* Variables with IN */ |
| 3970 | assert( seenVar && seenIn ); |
| 3971 | pIdxCons->usable = 1; |
| 3972 | break; |
| 3973 | } |
| 3974 | } |
| 3975 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 3976 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 3977 | pIdxInfo->idxStr = 0; |
| 3978 | pIdxInfo->idxNum = 0; |
| 3979 | pIdxInfo->needToFreeIdxStr = 0; |
| 3980 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 3981 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 3982 | pIdxInfo->estimatedRows = 25; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3983 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 3984 | if( rc ) goto whereLoopAddVtab_exit; |
| 3985 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 3986 | pNew->prereq = mExtra; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 3987 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3988 | assert( pNew->nLSlot>=nConstraint ); |
| 3989 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3990 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3991 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3992 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 3993 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3994 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3995 | || j<0 |
| 3996 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3997 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3998 | ){ |
| 3999 | rc = SQLITE_ERROR; |
| 4000 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4001 | goto whereLoopAddVtab_exit; |
| 4002 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4003 | testcase( iTerm==nConstraint-1 ); |
| 4004 | testcase( j==0 ); |
| 4005 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4006 | pTerm = &pWC->a[j]; |
| 4007 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4008 | assert( iTerm<pNew->nLSlot ); |
| 4009 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4010 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4011 | testcase( iTerm==15 ); |
| 4012 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4013 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4014 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4015 | if( pUsage[i].omit==0 ){ |
| 4016 | /* Do not attempt to use an IN constraint if the virtual table |
| 4017 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4018 | ** If we do attempt to use such a constraint, some rows might be |
| 4019 | ** repeated in the output. */ |
| 4020 | break; |
| 4021 | } |
| 4022 | /* A virtual table that is constrained by an IN clause may not |
| 4023 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4024 | ** is not necessarily related to the order of output terms and |
| 4025 | ** (2) Multiple outputs from a single IN value will not merge |
| 4026 | ** together. */ |
| 4027 | pIdxInfo->orderByConsumed = 0; |
| 4028 | } |
| 4029 | } |
| 4030 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4031 | if( i>=nConstraint ){ |
| 4032 | pNew->nLTerm = mxTerm+1; |
| 4033 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4034 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4035 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4036 | pIdxInfo->needToFreeIdxStr = 0; |
| 4037 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4038 | pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? |
| 4039 | pIdxInfo->nOrderBy : 0); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4040 | pNew->rSetup = 0; |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4041 | pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 4042 | pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4043 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4044 | if( pNew->u.vtab.needFree ){ |
| 4045 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4046 | pNew->u.vtab.needFree = 0; |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | whereLoopAddVtab_exit: |
| 4052 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4053 | sqlite3DbFree(db, pIdxInfo); |
| 4054 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4055 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4056 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4057 | |
| 4058 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4059 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4060 | ** btrees or virtual tables. |
| 4061 | */ |
| 4062 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4063 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4064 | WhereClause *pWC; |
| 4065 | WhereLoop *pNew; |
| 4066 | WhereTerm *pTerm, *pWCEnd; |
| 4067 | int rc = SQLITE_OK; |
| 4068 | int iCur; |
| 4069 | WhereClause tempWC; |
| 4070 | WhereLoopBuilder sSubBuild; |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 4071 | WhereOrSet sSum, sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4072 | struct SrcList_item *pItem; |
| 4073 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4074 | pWC = pBuilder->pWC; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4075 | pWCEnd = pWC->a + pWC->nTerm; |
| 4076 | pNew = pBuilder->pNew; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 4077 | memset(&sSum, 0, sizeof(sSum)); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4078 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 4079 | iCur = pItem->iCursor; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4080 | |
| 4081 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4082 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4083 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4084 | ){ |
| 4085 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4086 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4087 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4088 | int once = 1; |
| 4089 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4090 | |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4091 | sSubBuild = *pBuilder; |
| 4092 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4093 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4094 | |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 4095 | WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4096 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4097 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4098 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4099 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4100 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4101 | tempWC.pOuter = pWC; |
| 4102 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4103 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4104 | tempWC.a = pOrTerm; |
| 4105 | sSubBuild.pWC = &tempWC; |
| 4106 | }else{ |
| 4107 | continue; |
| 4108 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4109 | sCur.n = 0; |
drh | 5265149 | 2014-09-30 14:14:19 +0000 | [diff] [blame] | 4110 | #ifdef WHERETRACE_ENABLED |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 4111 | WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 4112 | (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 4113 | if( sqlite3WhereTrace & 0x400 ){ |
| 4114 | for(i=0; i<sSubBuild.pWC->nTerm; i++){ |
| 4115 | whereTermPrint(&sSubBuild.pWC->a[i], i); |
| 4116 | } |
drh | 5265149 | 2014-09-30 14:14:19 +0000 | [diff] [blame] | 4117 | } |
| 4118 | #endif |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4119 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4120 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4121 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4122 | }else |
| 4123 | #endif |
| 4124 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4125 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4126 | } |
drh | 36be4c4 | 2014-09-30 17:31:23 +0000 | [diff] [blame] | 4127 | if( rc==SQLITE_OK ){ |
| 4128 | rc = whereLoopAddOr(&sSubBuild, mExtra); |
| 4129 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4130 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 4131 | if( sCur.n==0 ){ |
| 4132 | sSum.n = 0; |
| 4133 | break; |
| 4134 | }else if( once ){ |
| 4135 | whereOrMove(&sSum, &sCur); |
| 4136 | once = 0; |
| 4137 | }else{ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 4138 | WhereOrSet sPrev; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4139 | whereOrMove(&sPrev, &sSum); |
| 4140 | sSum.n = 0; |
| 4141 | for(i=0; i<sPrev.n; i++){ |
| 4142 | for(j=0; j<sCur.n; j++){ |
| 4143 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4144 | sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 4145 | sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4146 | } |
| 4147 | } |
| 4148 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4149 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4150 | pNew->nLTerm = 1; |
| 4151 | pNew->aLTerm[0] = pTerm; |
| 4152 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4153 | pNew->rSetup = 0; |
| 4154 | pNew->iSortIdx = 0; |
| 4155 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4156 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 4157 | /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs |
| 4158 | ** of all sub-scans required by the OR-scan. However, due to rounding |
| 4159 | ** errors, it may be that the cost of the OR-scan is equal to its |
| 4160 | ** most expensive sub-scan. Add the smallest possible penalty |
| 4161 | ** (equivalent to multiplying the cost by 1.07) to ensure that |
| 4162 | ** this does not happen. Otherwise, for WHERE clauses such as the |
| 4163 | ** following where there is an index on "y": |
| 4164 | ** |
| 4165 | ** WHERE likelihood(x=?, 0.99) OR y=? |
| 4166 | ** |
| 4167 | ** the planner may elect to "OR" together a full-table scan and an |
| 4168 | ** index lookup. And other similarly odd results. */ |
| 4169 | pNew->rRun = sSum.a[i].rRun + 1; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4170 | pNew->nOut = sSum.a[i].nOut; |
| 4171 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4172 | rc = whereLoopInsert(pBuilder, pNew); |
| 4173 | } |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 4174 | WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | return rc; |
| 4178 | } |
| 4179 | |
| 4180 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4181 | ** Add all WhereLoop objects for all tables |
| 4182 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4183 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4184 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4185 | Bitmask mExtra = 0; |
| 4186 | Bitmask mPrior = 0; |
| 4187 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4188 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4189 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4190 | sqlite3 *db = pWInfo->pParse->db; |
| 4191 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4192 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4193 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4194 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4195 | |
| 4196 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4197 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4198 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4199 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4200 | pNew->iTab = iTab; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 4201 | pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4202 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4203 | mExtra = mPrior; |
| 4204 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4205 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4206 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 4207 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4208 | }else{ |
| 4209 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4210 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4211 | if( rc==SQLITE_OK ){ |
| 4212 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4213 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4214 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4215 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4216 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4217 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4218 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4221 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4222 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4223 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4224 | ** (or GROUP BY) without requiring a separate sort operation. Return N: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4225 | ** |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4226 | ** N>0: N terms of the ORDER BY clause are satisfied |
| 4227 | ** N==0: No terms of the ORDER BY clause are satisfied |
| 4228 | ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4229 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 4230 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 4231 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 4232 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4233 | ** 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] | 4234 | ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 4235 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 4236 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4237 | */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4238 | static i8 wherePathSatisfiesOrderBy( |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4239 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4240 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4241 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4242 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 4243 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4244 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4245 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4246 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4247 | u8 revSet; /* True if rev is known */ |
| 4248 | u8 rev; /* Composite sort order */ |
| 4249 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4250 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 4251 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 4252 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 4253 | u16 nKeyCol; /* Number of key columns in pIndex */ |
| 4254 | u16 nColumn; /* Total number of ordered columns in the index */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4255 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 4256 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 4257 | int i, j; /* Loop counters */ |
| 4258 | int iCur; /* Cursor number for current WhereLoop */ |
| 4259 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 4260 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4261 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 4262 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 4263 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 4264 | Index *pIndex; /* The index associated with pLoop */ |
| 4265 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 4266 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 4267 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4268 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4269 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4270 | |
| 4271 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4272 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 4273 | ** 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] | 4274 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 4275 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4276 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 4277 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4278 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4279 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 4280 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 4281 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 4282 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 4283 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 4284 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 4285 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 4286 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 4287 | ** |
| 4288 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 4289 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 4290 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4291 | */ |
| 4292 | |
| 4293 | assert( pOrderBy!=0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4294 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4295 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4296 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4297 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4298 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 4299 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4300 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4301 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4302 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4303 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4304 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4305 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 9dfaf62 | 2014-04-25 14:42:17 +0000 | [diff] [blame] | 4306 | if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4307 | if( pLoop->u.vtab.isOrdered ) obSat = obDone; |
| 4308 | break; |
| 4309 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4310 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4311 | |
| 4312 | /* Mark off any ORDER BY term X that is a column in the table of |
| 4313 | ** the current loop for which there is term in the WHERE |
| 4314 | ** clause of the form X IS NULL or X=? that reference only outer |
| 4315 | ** loops. |
| 4316 | */ |
| 4317 | for(i=0; i<nOrderBy; i++){ |
| 4318 | if( MASKBIT(i) & obSat ) continue; |
| 4319 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 4320 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 4321 | if( pOBExpr->iTable!=iCur ) continue; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 4322 | pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4323 | ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4324 | if( pTerm==0 ) continue; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4325 | if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4326 | const char *z1, *z2; |
| 4327 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 4328 | if( !pColl ) pColl = db->pDfltColl; |
| 4329 | z1 = pColl->zName; |
| 4330 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 4331 | if( !pColl ) pColl = db->pDfltColl; |
| 4332 | z2 = pColl->zName; |
| 4333 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
drh | e0cc3c2 | 2015-05-13 17:54:08 +0000 | [diff] [blame] | 4334 | testcase( pTerm->pExpr->op==TK_IS ); |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4335 | } |
| 4336 | obSat |= MASKBIT(i); |
| 4337 | } |
| 4338 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4339 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 4340 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 4341 | pIndex = 0; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4342 | nKeyCol = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 4343 | nColumn = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4344 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4345 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4346 | }else{ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4347 | nKeyCol = pIndex->nKeyCol; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 4348 | nColumn = pIndex->nColumn; |
| 4349 | assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); |
| 4350 | assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable)); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4351 | isOrderDistinct = IsUniqueIndex(pIndex); |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4352 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4353 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4354 | /* Loop through all columns of the index and deal with the ones |
| 4355 | ** that are not constrained by == or IN. |
| 4356 | */ |
| 4357 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4358 | distinctColumns = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 4359 | for(j=0; j<nColumn; j++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4360 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 4361 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4362 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4363 | if( j<pLoop->u.btree.nEq |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4364 | && pLoop->nSkip==0 |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4365 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4366 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4367 | if( i & WO_ISNULL ){ |
| 4368 | testcase( isOrderDistinct ); |
| 4369 | isOrderDistinct = 0; |
| 4370 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4371 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4374 | /* Get the column number in the table (iColumn) and sort order |
| 4375 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4376 | */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 4377 | if( pIndex ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4378 | iColumn = pIndex->aiColumn[j]; |
| 4379 | revIdx = pIndex->aSortOrder[j]; |
| 4380 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4381 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4382 | iColumn = -1; |
| 4383 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4384 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4385 | |
| 4386 | /* An unconstrained column that might be NULL means that this |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 4387 | ** WhereLoop is not well-ordered |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4388 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4389 | if( isOrderDistinct |
| 4390 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4391 | && j>=pLoop->u.btree.nEq |
| 4392 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 4393 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4394 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | /* Find the ORDER BY term that corresponds to the j-th column |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4398 | ** of the index and mark that ORDER BY term off |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4399 | */ |
| 4400 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4401 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4402 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 4403 | if( MASKBIT(i) & obSat ) continue; |
| 4404 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 4405 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 4406 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4407 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4408 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4409 | if( pOBExpr->iTable!=iCur ) continue; |
| 4410 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4411 | if( iColumn>=0 ){ |
| 4412 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 4413 | if( !pColl ) pColl = db->pDfltColl; |
| 4414 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 4415 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4416 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4417 | break; |
| 4418 | } |
drh | 4929047 | 2014-10-11 02:12:58 +0000 | [diff] [blame] | 4419 | if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | 59b8f2e | 2014-03-22 00:27:14 +0000 | [diff] [blame] | 4420 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 4421 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 4422 | if( revSet ){ |
| 4423 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 4424 | }else{ |
| 4425 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 4426 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 4427 | revSet = 1; |
| 4428 | } |
| 4429 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4430 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4431 | if( iColumn<0 ){ |
| 4432 | testcase( distinctColumns==0 ); |
| 4433 | distinctColumns = 1; |
| 4434 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4435 | obSat |= MASKBIT(i); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4436 | }else{ |
| 4437 | /* No match found */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4438 | if( j==0 || j<nKeyCol ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4439 | testcase( isOrderDistinct!=0 ); |
| 4440 | isOrderDistinct = 0; |
| 4441 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4442 | break; |
| 4443 | } |
| 4444 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 4445 | if( distinctColumns ){ |
| 4446 | testcase( isOrderDistinct==0 ); |
| 4447 | isOrderDistinct = 1; |
| 4448 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4449 | } /* end-if not one-row */ |
| 4450 | |
| 4451 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4452 | if( isOrderDistinct ){ |
| 4453 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4454 | for(i=0; i<nOrderBy; i++){ |
| 4455 | Expr *p; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 4456 | Bitmask mTerm; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4457 | if( MASKBIT(i) & obSat ) continue; |
| 4458 | p = pOrderBy->a[i].pExpr; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 4459 | mTerm = exprTableUsage(&pWInfo->sMaskSet,p); |
| 4460 | if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; |
| 4461 | if( (mTerm&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4462 | obSat |= MASKBIT(i); |
| 4463 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 4464 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4465 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4466 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 36ed034 | 2014-03-28 12:56:57 +0000 | [diff] [blame] | 4467 | if( obSat==obDone ) return (i8)nOrderBy; |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 4468 | if( !isOrderDistinct ){ |
| 4469 | for(i=nOrderBy-1; i>0; i--){ |
| 4470 | Bitmask m = MASKBIT(i) - 1; |
| 4471 | if( (obSat&m)==m ) return i; |
| 4472 | } |
| 4473 | return 0; |
| 4474 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4475 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4478 | |
| 4479 | /* |
| 4480 | ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), |
| 4481 | ** the planner assumes that the specified pOrderBy list is actually a GROUP |
| 4482 | ** BY clause - and so any order that groups rows as required satisfies the |
| 4483 | ** request. |
| 4484 | ** |
| 4485 | ** Normally, in this case it is not possible for the caller to determine |
| 4486 | ** whether or not the rows are really being delivered in sorted order, or |
| 4487 | ** just in some other order that provides the required grouping. However, |
| 4488 | ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then |
| 4489 | ** this function may be called on the returned WhereInfo object. It returns |
| 4490 | ** true if the rows really will be sorted in the specified order, or false |
| 4491 | ** otherwise. |
| 4492 | ** |
| 4493 | ** For example, assuming: |
| 4494 | ** |
| 4495 | ** CREATE INDEX i1 ON t1(x, Y); |
| 4496 | ** |
| 4497 | ** then |
| 4498 | ** |
| 4499 | ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 |
| 4500 | ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 |
| 4501 | */ |
| 4502 | int sqlite3WhereIsSorted(WhereInfo *pWInfo){ |
| 4503 | assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); |
| 4504 | assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); |
| 4505 | return pWInfo->sorted; |
| 4506 | } |
| 4507 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4508 | #ifdef WHERETRACE_ENABLED |
| 4509 | /* For debugging use only: */ |
| 4510 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 4511 | static char zName[65]; |
| 4512 | int i; |
| 4513 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 4514 | if( pLast ) zName[i++] = pLast->cId; |
| 4515 | zName[i] = 0; |
| 4516 | return zName; |
| 4517 | } |
| 4518 | #endif |
| 4519 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4520 | /* |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4521 | ** Return the cost of sorting nRow rows, assuming that the keys have |
| 4522 | ** nOrderby columns and that the first nSorted columns are already in |
| 4523 | ** order. |
| 4524 | */ |
| 4525 | static LogEst whereSortingCost( |
| 4526 | WhereInfo *pWInfo, |
| 4527 | LogEst nRow, |
| 4528 | int nOrderBy, |
| 4529 | int nSorted |
| 4530 | ){ |
| 4531 | /* TUNING: Estimated cost of a full external sort, where N is |
| 4532 | ** the number of rows to sort is: |
| 4533 | ** |
| 4534 | ** cost = (3.0 * N * log(N)). |
| 4535 | ** |
| 4536 | ** Or, if the order-by clause has X terms but only the last Y |
| 4537 | ** terms are out of order, then block-sorting will reduce the |
| 4538 | ** sorting cost to: |
| 4539 | ** |
| 4540 | ** cost = (3.0 * N * log(N)) * (Y/X) |
| 4541 | ** |
| 4542 | ** The (Y/X) term is implemented using stack variable rScale |
| 4543 | ** below. */ |
| 4544 | LogEst rScale, rSortCost; |
| 4545 | assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); |
| 4546 | rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; |
| 4547 | rSortCost = nRow + estLog(nRow) + rScale + 16; |
| 4548 | |
| 4549 | /* TUNING: The cost of implementing DISTINCT using a B-TREE is |
| 4550 | ** similar but with a larger constant of proportionality. |
| 4551 | ** Multiply by an additional factor of 3.0. */ |
| 4552 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 4553 | rSortCost += 16; |
| 4554 | } |
| 4555 | |
| 4556 | return rSortCost; |
| 4557 | } |
| 4558 | |
| 4559 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 4560 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4561 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 4562 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 4563 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4564 | ** Assume that the total number of output rows that will need to be sorted |
| 4565 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 4566 | ** costs if nRowEst==0. |
| 4567 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4568 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 4569 | ** error occurs. |
| 4570 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4571 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4572 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4573 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4574 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4575 | sqlite3 *db; /* The database connection */ |
| 4576 | int iLoop; /* Loop counter over the terms of the join */ |
| 4577 | int ii, jj; /* Loop counters */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4578 | int mxI = 0; /* Index of next entry to replace */ |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 4579 | int nOrderBy; /* Number of ORDER BY clause terms */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4580 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4581 | LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4582 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 4583 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 4584 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 4585 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 4586 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 4587 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 4588 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4589 | LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4590 | char *pSpace; /* Temporary memory used by this routine */ |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 4591 | int nSpace; /* Bytes of space allocated at pSpace */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4592 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4593 | pParse = pWInfo->pParse; |
| 4594 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4595 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4596 | /* TUNING: For simple queries, only the best path is tracked. |
| 4597 | ** For 2-way joins, the 5 best paths are followed. |
| 4598 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | 2504c6c | 2014-06-02 11:26:33 +0000 | [diff] [blame] | 4599 | mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4600 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4601 | WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4602 | |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4603 | /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this |
| 4604 | ** case the purpose of this call is to estimate the number of rows returned |
| 4605 | ** by the overall query. Once this estimate has been obtained, the caller |
| 4606 | ** will invoke this function a second time, passing the estimate as the |
| 4607 | ** nRowEst parameter. */ |
| 4608 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 4609 | nOrderBy = 0; |
| 4610 | }else{ |
| 4611 | nOrderBy = pWInfo->pOrderBy->nExpr; |
| 4612 | } |
| 4613 | |
| 4614 | /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 4615 | nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 4616 | nSpace += sizeof(LogEst) * nOrderBy; |
| 4617 | pSpace = sqlite3DbMallocRaw(db, nSpace); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4618 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 4619 | aTo = (WherePath*)pSpace; |
| 4620 | aFrom = aTo+mxChoice; |
| 4621 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 4622 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 4623 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4624 | pFrom->aLoop = pX; |
| 4625 | } |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4626 | if( nOrderBy ){ |
| 4627 | /* If there is an ORDER BY clause and it is not being ignored, set up |
| 4628 | ** space for the aSortCost[] array. Each element of the aSortCost array |
| 4629 | ** is either zero - meaning it has not yet been initialized - or the |
| 4630 | ** cost of sorting nRowEst rows of data where the first X terms of |
| 4631 | ** the ORDER BY clause are already in order, where X is the array |
| 4632 | ** index. */ |
| 4633 | aSortCost = (LogEst*)pX; |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 4634 | memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4635 | } |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 4636 | assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); |
| 4637 | assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4638 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4639 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 4640 | ** |
dan | f104abb | 2015-03-16 20:40:00 +0000 | [diff] [blame] | 4641 | ** TUNING: Do not let the number of iterations go above 28. If the cost |
| 4642 | ** of computing an automatic index is not paid back within the first 28 |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4643 | ** rows, then do not use the automatic index. */ |
dan | f104abb | 2015-03-16 20:40:00 +0000 | [diff] [blame] | 4644 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4645 | nFrom = 1; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4646 | assert( aFrom[0].isOrdered==0 ); |
| 4647 | if( nOrderBy ){ |
| 4648 | /* If nLoop is zero, then there are no FROM terms in the query. Since |
| 4649 | ** in this case the query may return a maximum of one row, the results |
| 4650 | ** are already in the requested order. Set isOrdered to nOrderBy to |
| 4651 | ** indicate this. Or, if nLoop is greater than zero, set isOrdered to |
| 4652 | ** -1, indicating that the result set may or may not be ordered, |
| 4653 | ** depending on the loops added to the current plan. */ |
| 4654 | aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | /* Compute successively longer WherePaths using the previous generation |
| 4658 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 4659 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4660 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 4661 | nTo = 0; |
| 4662 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 4663 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4664 | LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ |
| 4665 | LogEst rCost; /* Cost of path (pFrom+pWLoop) */ |
| 4666 | LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ |
| 4667 | i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 4668 | Bitmask maskNew; /* Mask of src visited by (..) */ |
| 4669 | Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 4670 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4671 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 4672 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4673 | /* At this point, pWLoop is a candidate to be the next loop. |
| 4674 | ** Compute its cost */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4675 | rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 4676 | rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4677 | nOut = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4678 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4679 | if( isOrdered<0 ){ |
| 4680 | isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4681 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4682 | iLoop, pWLoop, &revMask); |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 4683 | }else{ |
| 4684 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4685 | } |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4686 | if( isOrdered>=0 && isOrdered<nOrderBy ){ |
| 4687 | if( aSortCost[isOrdered]==0 ){ |
| 4688 | aSortCost[isOrdered] = whereSortingCost( |
| 4689 | pWInfo, nRowEst, nOrderBy, isOrdered |
| 4690 | ); |
| 4691 | } |
| 4692 | rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]); |
| 4693 | |
| 4694 | WHERETRACE(0x002, |
| 4695 | ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", |
| 4696 | aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, |
| 4697 | rUnsorted, rCost)); |
| 4698 | }else{ |
| 4699 | rCost = rUnsorted; |
| 4700 | } |
| 4701 | |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4702 | /* Check to see if pWLoop should be added to the set of |
| 4703 | ** mxChoice best-so-far paths. |
| 4704 | ** |
| 4705 | ** First look for an existing path among best-so-far paths |
| 4706 | ** that covers the same set of loops and has the same isOrdered |
| 4707 | ** setting as the current path candidate. |
drh | f2a9030 | 2014-08-07 20:37:01 +0000 | [diff] [blame] | 4708 | ** |
| 4709 | ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent |
| 4710 | ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range |
| 4711 | ** of legal values for isOrdered, -1..64. |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4712 | */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4713 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4714 | if( pTo->maskLoop==maskNew |
drh | f2a9030 | 2014-08-07 20:37:01 +0000 | [diff] [blame] | 4715 | && ((pTo->isOrdered^isOrdered)&0x80)==0 |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4716 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4717 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4718 | break; |
| 4719 | } |
| 4720 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4721 | if( jj>=nTo ){ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4722 | /* None of the existing best-so-far paths match the candidate. */ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4723 | if( nTo>=mxChoice |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4724 | && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted)) |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4725 | ){ |
| 4726 | /* The current candidate is no better than any of the mxChoice |
| 4727 | ** paths currently in the best-so-far buffer. So discard |
| 4728 | ** this candidate as not viable. */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4729 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4730 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4731 | sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", |
| 4732 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4733 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4734 | } |
| 4735 | #endif |
| 4736 | continue; |
| 4737 | } |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4738 | /* If we reach this points it means that the new candidate path |
| 4739 | ** needs to be added to the set of best-so-far paths. */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4740 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4741 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4742 | jj = nTo++; |
| 4743 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4744 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4745 | jj = mxI; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4746 | } |
| 4747 | pTo = &aTo[jj]; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4748 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4749 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4750 | sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", |
| 4751 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4752 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4753 | } |
| 4754 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 4755 | }else{ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4756 | /* Control reaches here if best-so-far path pTo=aTo[jj] covers the |
| 4757 | ** same set of loops and has the sam isOrdered setting as the |
| 4758 | ** candidate path. Check to see if the candidate should replace |
| 4759 | ** pTo or if the candidate should be skipped */ |
| 4760 | if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4761 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4762 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4763 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4764 | "Skip %s cost=%-3d,%3d order=%c", |
| 4765 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4766 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4767 | sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", |
| 4768 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4769 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4770 | } |
| 4771 | #endif |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4772 | /* Discard the candidate path from further consideration */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4773 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4774 | continue; |
| 4775 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4776 | testcase( pTo->rCost==rCost+1 ); |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 4777 | /* Control reaches here if the candidate path is better than the |
| 4778 | ** pTo path. Replace pTo with the candidate. */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4779 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4780 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4781 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4782 | "Update %s cost=%-3d,%3d order=%c", |
| 4783 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4784 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4785 | sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", |
| 4786 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4787 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4788 | } |
| 4789 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4790 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4791 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4792 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4793 | pTo->revLoop = revMask; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4794 | pTo->nRow = nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4795 | pTo->rCost = rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4796 | pTo->rUnsorted = rUnsorted; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4797 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4798 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 4799 | pTo->aLoop[iLoop] = pWLoop; |
| 4800 | if( nTo>=mxChoice ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4801 | mxI = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4802 | mxCost = aTo[0].rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4803 | mxUnsorted = aTo[0].nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4804 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4805 | if( pTo->rCost>mxCost |
| 4806 | || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted) |
| 4807 | ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4808 | mxCost = pTo->rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 4809 | mxUnsorted = pTo->rUnsorted; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 4810 | mxI = jj; |
| 4811 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4817 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 4818 | if( sqlite3WhereTrace & 0x02 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4819 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4820 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4821 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4822 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4823 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| 4824 | if( pTo->isOrdered>0 ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4825 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 4826 | }else{ |
| 4827 | sqlite3DebugPrintf("\n"); |
| 4828 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 4829 | } |
| 4830 | } |
| 4831 | #endif |
| 4832 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4833 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4834 | pFrom = aTo; |
| 4835 | aTo = aFrom; |
| 4836 | aFrom = pFrom; |
| 4837 | nFrom = nTo; |
| 4838 | } |
| 4839 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 4840 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4841 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 4842 | sqlite3DbFree(db, pSpace); |
| 4843 | return SQLITE_ERROR; |
| 4844 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4845 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4846 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4847 | pFrom = aFrom; |
| 4848 | for(ii=1; ii<nFrom; ii++){ |
| 4849 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 4850 | } |
| 4851 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4852 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4853 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4854 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 4855 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4856 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4857 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4858 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 4859 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 4860 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 4861 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4862 | && nRowEst |
| 4863 | ){ |
| 4864 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4865 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 4866 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 4867 | if( rc==pWInfo->pResultSet->nExpr ){ |
| 4868 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 4869 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4870 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 4871 | if( pWInfo->pOrderBy ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4872 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 4873 | if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 4874 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 4875 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4876 | }else{ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 4877 | pWInfo->nOBSat = pFrom->isOrdered; |
drh | ea6c36e | 2014-03-19 14:30:55 +0000 | [diff] [blame] | 4878 | if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4879 | pWInfo->revMask = pFrom->revLoop; |
| 4880 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4881 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
drh | 11b0481 | 2015-04-12 01:22:04 +0000 | [diff] [blame] | 4882 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4883 | ){ |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 4884 | Bitmask revMask = 0; |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4885 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 4886 | pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4887 | ); |
| 4888 | assert( pWInfo->sorted==0 ); |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 4889 | if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| 4890 | pWInfo->sorted = 1; |
| 4891 | pWInfo->revMask = revMask; |
| 4892 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4893 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4894 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 4895 | |
| 4896 | |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4897 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4898 | |
| 4899 | /* Free temporary memory and return success */ |
| 4900 | sqlite3DbFree(db, pSpace); |
| 4901 | return SQLITE_OK; |
| 4902 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4903 | |
| 4904 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4905 | ** Most queries use only a single table (they are not joins) and have |
| 4906 | ** simple == constraints against indexed fields. This routine attempts |
| 4907 | ** to plan those simple cases using much less ceremony than the |
| 4908 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 4909 | ** times for the common case. |
| 4910 | ** |
| 4911 | ** Return non-zero on success, if this query can be handled by this |
| 4912 | ** no-frills query planner. Return zero if this query needs the |
| 4913 | ** general-purpose query planner. |
| 4914 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4915 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4916 | WhereInfo *pWInfo; |
| 4917 | struct SrcList_item *pItem; |
| 4918 | WhereClause *pWC; |
| 4919 | WhereTerm *pTerm; |
| 4920 | WhereLoop *pLoop; |
| 4921 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4922 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4923 | Table *pTab; |
| 4924 | Index *pIdx; |
| 4925 | |
| 4926 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4927 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4928 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 4929 | pItem = pWInfo->pTabList->a; |
| 4930 | pTab = pItem->pTab; |
| 4931 | if( IsVirtual(pTab) ) return 0; |
drh | d62fbb5 | 2015-06-04 12:08:53 +0000 | [diff] [blame] | 4932 | if( pItem->zIndexedBy ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4933 | iCur = pItem->iCursor; |
| 4934 | pWC = &pWInfo->sWC; |
| 4935 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4936 | pLoop->wsFlags = 0; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4937 | pLoop->nSkip = 0; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 4938 | pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4939 | if( pTerm ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4940 | testcase( pTerm->eOperator & WO_IS ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4941 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 4942 | pLoop->aLTerm[0] = pTerm; |
| 4943 | pLoop->nLTerm = 1; |
| 4944 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4945 | /* TUNING: Cost of a rowid lookup is 10 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4946 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4947 | }else{ |
| 4948 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
mistachkin | 4e5bef8 | 2015-05-15 20:14:00 +0000 | [diff] [blame] | 4949 | int opMask; |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 4950 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4951 | if( !IsUniqueIndex(pIdx) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 4952 | || pIdx->pPartIdxWhere!=0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4953 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 4954 | ) continue; |
mistachkin | 4e5bef8 | 2015-05-15 20:14:00 +0000 | [diff] [blame] | 4955 | opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4956 | for(j=0; j<pIdx->nKeyCol; j++){ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 4957 | pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4958 | if( pTerm==0 ) break; |
dan | 3072b53 | 2015-05-15 19:59:23 +0000 | [diff] [blame] | 4959 | testcase( pTerm->eOperator & WO_IS ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4960 | pLoop->aLTerm[j] = pTerm; |
| 4961 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4962 | if( j!=pIdx->nKeyCol ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4963 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4964 | if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4965 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 4966 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4967 | pLoop->nLTerm = j; |
| 4968 | pLoop->u.btree.nEq = j; |
| 4969 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4970 | /* TUNING: Cost of a unique index lookup is 15 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4971 | pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4972 | break; |
| 4973 | } |
| 4974 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 4975 | if( pLoop->wsFlags ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4976 | pLoop->nOut = (LogEst)1; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 4977 | pWInfo->a[0].pWLoop = pLoop; |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 4978 | pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 4979 | pWInfo->a[0].iTabCur = iCur; |
| 4980 | pWInfo->nRowOut = 1; |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 4981 | if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4982 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 4983 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 4984 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 4985 | #ifdef SQLITE_DEBUG |
| 4986 | pLoop->cId = '0'; |
| 4987 | #endif |
| 4988 | return 1; |
| 4989 | } |
| 4990 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 4994 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 4995 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4996 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4997 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4998 | ** in order to complete the WHERE clause processing. |
| 4999 | ** |
| 5000 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5001 | ** |
| 5002 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5003 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5004 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5005 | ** example, if the SQL is this: |
| 5006 | ** |
| 5007 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5008 | ** |
| 5009 | ** Then the code generated is conceptually like the following: |
| 5010 | ** |
| 5011 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5012 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5013 | ** foreach row3 in t3 do / |
| 5014 | ** ... |
| 5015 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5016 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5017 | ** end / |
| 5018 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5019 | ** Note that the loops might not be nested in the order in which they |
| 5020 | ** 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] | 5021 | ** use of indices. Note also that when the IN operator appears in |
| 5022 | ** the WHERE clause, it might result in additional nested loops for |
| 5023 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5024 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5025 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5026 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5027 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5028 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5029 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5030 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5031 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5032 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5033 | ** data from the various tables of the loop. |
| 5034 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5035 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5036 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5037 | ** the tables have indices and there are terms in the WHERE clause that |
| 5038 | ** refer to those indices, a complete table scan can be avoided and the |
| 5039 | ** code will run much faster. Most of the work of this routine is checking |
| 5040 | ** to see if there are indices that can be used to speed up the loop. |
| 5041 | ** |
| 5042 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5043 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5044 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5045 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5046 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5047 | ** most loop) |
| 5048 | ** |
| 5049 | ** OUTER JOINS |
| 5050 | ** |
| 5051 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5052 | ** |
| 5053 | ** foreach row1 in t1 do |
| 5054 | ** flag = 0 |
| 5055 | ** foreach row2 in t2 do |
| 5056 | ** start: |
| 5057 | ** ... |
| 5058 | ** flag = 1 |
| 5059 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5060 | ** if flag==0 then |
| 5061 | ** move the row2 cursor to a null row |
| 5062 | ** goto start |
| 5063 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5064 | ** end |
| 5065 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5066 | ** ORDER BY CLAUSE PROCESSING |
| 5067 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5068 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 5069 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5070 | ** 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] | 5071 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5072 | ** |
| 5073 | ** The iIdxCur parameter is the cursor number of an index. If |
| 5074 | ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index |
| 5075 | ** to use for OR clause processing. The WHERE clause should use this |
| 5076 | ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 5077 | ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 5078 | ** be used to compute the appropriate cursor depending on which index is |
| 5079 | ** used. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5080 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5081 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5082 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5083 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5084 | Expr *pWhere, /* The WHERE clause */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5085 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5086 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5087 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5088 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5089 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5090 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5091 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5092 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5093 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5094 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5095 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5096 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5097 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5098 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5099 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5100 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5101 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5102 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5103 | |
| 5104 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5105 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5106 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5107 | |
| 5108 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 5109 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 5110 | if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5111 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5112 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5113 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5114 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 5115 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 5116 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 5117 | } |
| 5118 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5119 | /* 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] | 5120 | ** bits in a Bitmask |
| 5121 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5122 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5123 | if( pTabList->nSrc>BMS ){ |
| 5124 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
| 5127 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5128 | /* This function normally generates a nested loop for all tables in |
| 5129 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5130 | ** only generate code for the first table in pTabList and assume that |
| 5131 | ** any cursors associated with subsequent tables are uninitialized. |
| 5132 | */ |
| 5133 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5134 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5135 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5136 | ** return value. A single allocation is used to store the WhereInfo |
| 5137 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5138 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5139 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5140 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5141 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5142 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5143 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5144 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5145 | sqlite3DbFree(db, pWInfo); |
| 5146 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5147 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5148 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5149 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5150 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5151 | pWInfo->pParse = pParse; |
| 5152 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5153 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5154 | pWInfo->pResultSet = pResultSet; |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 5155 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5156 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5157 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5158 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5159 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5160 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 5161 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 5162 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5163 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5164 | #ifdef SQLITE_DEBUG |
| 5165 | sWLB.pNew->cId = '*'; |
| 5166 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5167 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5168 | /* Split the WHERE clause into separate subexpressions where each |
| 5169 | ** subexpression is separated by an AND operator. |
| 5170 | */ |
| 5171 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5172 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 5173 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5174 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5175 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5176 | ** expression and either jump over all of the code or fall thru. |
| 5177 | */ |
drh | 759e858 | 2014-01-02 21:05:10 +0000 | [diff] [blame] | 5178 | for(ii=0; ii<sWLB.pWC->nTerm; ii++){ |
| 5179 | if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ |
| 5180 | sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, |
| 5181 | SQLITE_JUMPIFNULL); |
| 5182 | sWLB.pWC->a[ii].wtFlags |= TERM_CODED; |
| 5183 | } |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5184 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5185 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5186 | /* Special case: No FROM clause |
| 5187 | */ |
| 5188 | if( nTabList==0 ){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5189 | if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5190 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5191 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5192 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5195 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5196 | ** |
| 5197 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5198 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5199 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5200 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5201 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5202 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5203 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5204 | ** 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] | 5205 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5206 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5207 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5208 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5209 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5210 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5211 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5212 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5213 | } |
| 5214 | #ifndef NDEBUG |
| 5215 | { |
| 5216 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5217 | for(ii=0; ii<pTabList->nSrc; ii++){ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 5218 | Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5219 | assert( (m-1)==toTheLeft ); |
| 5220 | toTheLeft |= m; |
| 5221 | } |
| 5222 | } |
| 5223 | #endif |
| 5224 | |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5225 | /* Analyze all of the subexpressions. */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5226 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5227 | if( db->mallocFailed ) goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5228 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5229 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5230 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 5231 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5232 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5233 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5234 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5235 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5236 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5237 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5240 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5241 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 5242 | #if defined(WHERETRACE_ENABLED) |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5243 | if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 5244 | int i; |
| 5245 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
| 5246 | whereTermPrint(&sWLB.pWC->a[i], i); |
| 5247 | } |
| 5248 | } |
| 5249 | #endif |
| 5250 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5251 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5252 | rc = whereLoopAddAll(&sWLB); |
| 5253 | if( rc ) goto whereBeginError; |
| 5254 | |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5255 | #ifdef WHERETRACE_ENABLED |
| 5256 | if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5257 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5258 | int i; |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5259 | static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5260 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5261 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 5262 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 5263 | whereLoopPrint(p, sWLB.pWC); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5264 | } |
| 5265 | } |
| 5266 | #endif |
| 5267 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5268 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5269 | if( db->mallocFailed ) goto whereBeginError; |
| 5270 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5271 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5272 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5273 | } |
| 5274 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5275 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5276 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5277 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5278 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5279 | goto whereBeginError; |
| 5280 | } |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5281 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5282 | if( sqlite3WhereTrace ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5283 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 5284 | if( pWInfo->nOBSat>0 ){ |
| 5285 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5286 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5287 | switch( pWInfo->eDistinct ){ |
| 5288 | case WHERE_DISTINCT_UNIQUE: { |
| 5289 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5290 | break; |
| 5291 | } |
| 5292 | case WHERE_DISTINCT_ORDERED: { |
| 5293 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5294 | break; |
| 5295 | } |
| 5296 | case WHERE_DISTINCT_UNORDERED: { |
| 5297 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5298 | break; |
| 5299 | } |
| 5300 | } |
| 5301 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5302 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 5303 | whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5304 | } |
| 5305 | } |
| 5306 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5307 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 5308 | if( pWInfo->nLevel>=2 |
| 5309 | && pResultSet!=0 |
| 5310 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 5311 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5312 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
drh | 67a5ec7 | 2013-09-03 14:03:47 +0000 | [diff] [blame] | 5313 | if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5314 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5315 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5316 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5317 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 5318 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 5319 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5320 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5321 | break; |
| 5322 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5323 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5324 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 5325 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 5326 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 5327 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 5328 | ){ |
| 5329 | break; |
| 5330 | } |
| 5331 | } |
| 5332 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5333 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 5334 | pWInfo->nLevel--; |
| 5335 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5336 | } |
| 5337 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5338 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 5339 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5340 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5341 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5342 | ** to use a one-pass algorithm, determine if this is appropriate. |
drh | 24b7fe9 | 2013-09-30 19:33:06 +0000 | [diff] [blame] | 5343 | ** The one-pass algorithm only works if the WHERE clause constrains |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 5344 | ** the statement to update or delete a single row. |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5345 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5346 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5347 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 5348 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5349 | pWInfo->okOnePass = 1; |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 5350 | if( HasRowid(pTabList->a[0].pTab) ){ |
| 5351 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 5352 | } |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5353 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5354 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5355 | /* Open all tables in the pTabList and any indices selected for |
| 5356 | ** searching those tables. |
| 5357 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5358 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5359 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5360 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5361 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5362 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5363 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5364 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5365 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5366 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 5367 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 5368 | /* Do nothing */ |
| 5369 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5370 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5371 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5372 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 5373 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5374 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 5375 | }else if( IsVirtual(pTab) ){ |
| 5376 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5377 | }else |
| 5378 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5379 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5380 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5381 | int op = OP_OpenRead; |
| 5382 | if( pWInfo->okOnePass ){ |
| 5383 | op = OP_OpenWrite; |
| 5384 | pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 5385 | }; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5386 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5387 | assert( pTabItem->iCursor==pLevel->iTabCur ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5388 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 5389 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 5390 | if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5391 | Bitmask b = pTabItem->colUsed; |
| 5392 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5393 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5394 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 5395 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5396 | assert( n<=pTab->nCol ); |
| 5397 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5398 | }else{ |
| 5399 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5400 | } |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 5401 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5402 | Index *pIx = pLoop->u.btree.pIndex; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5403 | int iIndexCur; |
| 5404 | int op = OP_OpenRead; |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 5405 | /* iIdxCur is always set if to a positive value if ONEPASS is possible */ |
| 5406 | assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 5407 | if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 5408 | && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 |
| 5409 | ){ |
| 5410 | /* This is one term of an OR-optimization using the PRIMARY KEY of a |
| 5411 | ** WITHOUT ROWID table. No need for a separate index */ |
| 5412 | iIndexCur = pLevel->iTabCur; |
| 5413 | op = 0; |
| 5414 | }else if( pWInfo->okOnePass ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5415 | Index *pJ = pTabItem->pTab->pIndex; |
| 5416 | iIndexCur = iIdxCur; |
| 5417 | assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 5418 | while( ALWAYS(pJ) && pJ!=pIx ){ |
| 5419 | iIndexCur++; |
| 5420 | pJ = pJ->pNext; |
| 5421 | } |
| 5422 | op = OP_OpenWrite; |
| 5423 | pWInfo->aiCurOnePass[1] = iIndexCur; |
| 5424 | }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ |
| 5425 | iIndexCur = iIdxCur; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 5426 | if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5427 | }else{ |
| 5428 | iIndexCur = pParse->nTab++; |
| 5429 | } |
| 5430 | pLevel->iIdxCur = iIndexCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5431 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 5432 | assert( iIndexCur>=0 ); |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 5433 | if( op ){ |
| 5434 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 5435 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 5436 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 |
| 5437 | && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0 |
| 5438 | && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 |
| 5439 | ){ |
| 5440 | sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ |
| 5441 | } |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 5442 | VdbeComment((v, "%s", pIx->zName)); |
| 5443 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5444 | } |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 5445 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5446 | } |
| 5447 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 5448 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5449 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5450 | /* Generate the code to do the search. Each iteration of the for |
| 5451 | ** loop below generates code for a single nested loop of the VM |
| 5452 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5453 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5454 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5455 | for(ii=0; ii<nTabList; ii++){ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 5456 | int addrExplain; |
| 5457 | int wsFlags; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5458 | pLevel = &pWInfo->a[ii]; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 5459 | wsFlags = pLevel->pWLoop->wsFlags; |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 5460 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 5461 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 5462 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 5463 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 5464 | if( db->mallocFailed ) goto whereBeginError; |
| 5465 | } |
| 5466 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 5467 | addrExplain = sqlite3WhereExplainOneScan( |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 5468 | pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 5469 | ); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 5470 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 5471 | notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 5472 | pWInfo->iContinue = pLevel->addrCont; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 5473 | if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame^] | 5474 | sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 5475 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5476 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 5477 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 5478 | /* Done. */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 5479 | VdbeModuleComment((v, "Begin WHERE-core")); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5480 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5481 | |
| 5482 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5483 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5484 | if( pWInfo ){ |
| 5485 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5486 | whereInfoFree(db, pWInfo); |
| 5487 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5488 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5492 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5493 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5494 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5495 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5496 | Parse *pParse = pWInfo->pParse; |
| 5497 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5498 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5499 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5500 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 5501 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5502 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5503 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5504 | /* Generate loop termination code. |
| 5505 | */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 5506 | VdbeModuleComment((v, "End WHERE-core")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5507 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5508 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 5509 | int addr; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5510 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5511 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5512 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5513 | if( pLevel->op!=OP_Noop ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5514 | sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5515 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5516 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 5517 | VdbeCoverageIf(v, pLevel->op==OP_Next); |
| 5518 | VdbeCoverageIf(v, pLevel->op==OP_Prev); |
| 5519 | VdbeCoverageIf(v, pLevel->op==OP_VNext); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5520 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5521 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 5522 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5523 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5524 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5525 | 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] | 5526 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 5527 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5528 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 5529 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 5530 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5531 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5532 | } |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 5533 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5534 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 5535 | if( pLevel->addrSkip ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 5536 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 5537 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 5538 | sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 5539 | sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 5540 | } |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 5541 | if( pLevel->addrLikeRep ){ |
drh | b7c60ba | 2015-03-07 02:51:59 +0000 | [diff] [blame] | 5542 | int op; |
| 5543 | if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){ |
| 5544 | op = OP_DecrJumpZero; |
| 5545 | }else{ |
| 5546 | op = OP_JumpZeroIncr; |
| 5547 | } |
| 5548 | sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 5549 | VdbeCoverage(v); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 5550 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5551 | if( pLevel->iLeftJoin ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5552 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5553 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 5554 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 5555 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 5556 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 5557 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 5558 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5559 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 5560 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 5561 | if( pLevel->op==OP_Return ){ |
| 5562 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 5563 | }else{ |
| 5564 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 5565 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 5566 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5567 | } |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 5568 | VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5569 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5570 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5571 | |
| 5572 | /* The "break" point is here, just past the end of the outer loop. |
| 5573 | ** Set it. |
| 5574 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5575 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5576 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5577 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5578 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 5579 | int k, last; |
| 5580 | VdbeOp *pOp; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5581 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5582 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5583 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 5584 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5585 | pLoop = pLevel->pWLoop; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5586 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 5587 | /* For a co-routine, change all OP_Column references to the table of |
drh | 7b3aa08 | 2015-05-29 13:55:33 +0000 | [diff] [blame] | 5588 | ** the co-routine into OP_Copy of result contained in a register. |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 5589 | ** OP_Rowid becomes OP_Null. |
| 5590 | */ |
dan | fbf0f0e | 2014-03-03 14:20:30 +0000 | [diff] [blame] | 5591 | if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
drh | 7b3aa08 | 2015-05-29 13:55:33 +0000 | [diff] [blame] | 5592 | translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, |
| 5593 | pTabItem->regResult); |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 5594 | continue; |
| 5595 | } |
| 5596 | |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5597 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 5598 | ** Except, do not close cursors that will be reused by the OR optimization |
| 5599 | ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors |
| 5600 | ** created for the ONEPASS optimization. |
| 5601 | */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5602 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 5603 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5604 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5605 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5606 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5607 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5608 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 5609 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 5610 | if( (ws & WHERE_INDEXED)!=0 |
| 5611 | && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 |
| 5612 | && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] |
| 5613 | ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5614 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 5615 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 5618 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 5619 | ** from the index instead of from the table where possible. In some cases |
| 5620 | ** this optimization prevents the table from ever being read, which can |
| 5621 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5622 | ** |
| 5623 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 5624 | ** sqlite3WhereEnd will have created code that references the table |
| 5625 | ** directly. This loop scans all that code looking for opcodes |
| 5626 | ** that reference the table and converts them into opcodes that |
| 5627 | ** reference the index. |
| 5628 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5629 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 5630 | pIdx = pLoop->u.btree.pIndex; |
| 5631 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 5632 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5633 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5634 | if( pIdx && !db->mallocFailed ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5635 | last = sqlite3VdbeCurrentAddr(v); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 5636 | k = pLevel->addrBody; |
| 5637 | pOp = sqlite3VdbeGetOp(v, k); |
| 5638 | for(; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5639 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 5640 | if( pOp->opcode==OP_Column ){ |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 5641 | int x = pOp->p2; |
drh | 511717c | 2013-11-08 17:13:23 +0000 | [diff] [blame] | 5642 | assert( pIdx->pTable==pTab ); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 5643 | if( !HasRowid(pTab) ){ |
| 5644 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 5645 | x = pPk->aiColumn[x]; |
| 5646 | } |
| 5647 | x = sqlite3ColumnOfIndex(pIdx, x); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 5648 | if( x>=0 ){ |
| 5649 | pOp->p2 = x; |
| 5650 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5651 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 5652 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5653 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5654 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5655 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5658 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5659 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5660 | |
| 5661 | /* Final cleanup |
| 5662 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 5663 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5664 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5665 | return; |
| 5666 | } |